Skip to content

Commit

Permalink
Use schema's title when available in {one,any,all}Of applicators
Browse files Browse the repository at this point in the history
  Applicators can be a little hard to navigate, especially on deeply nested schema. While the uid provides a nice middleground solution, it doesn't actually work when schemas are defined outside of the main specification and more importantly, it isn't necessarily meant to be human-readable. The 'title' property however is meant just for that.
  • Loading branch information
KtorZ committed Sep 28, 2023
1 parent 5d26352 commit 1b1a754
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
21 changes: 18 additions & 3 deletions library/src/components/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ export const Schema: React.FunctionComponent<Props> = ({
<Schema
key={idx}
schema={s}
schemaName={idx === 0 ? 'Adheres to:' : 'Or to:'}
schemaName={SchemaHelpers.applicatorSchemaName(
idx,
'Adheres to',
'Or to',
s.title(),
)}
/>
))}
{schema.anyOf() &&
Expand All @@ -290,7 +295,12 @@ export const Schema: React.FunctionComponent<Props> = ({
<Schema
key={idx}
schema={s}
schemaName={idx === 0 ? 'Can adhere to:' : 'Or to:'}
schemaName={SchemaHelpers.applicatorSchemaName(
idx,
'Can adhere to',
'Or to',
s.title(),
)}
/>
))}
{schema.allOf() &&
Expand All @@ -300,7 +310,12 @@ export const Schema: React.FunctionComponent<Props> = ({
<Schema
key={idx}
schema={s}
schemaName={idx === 0 ? 'Consists of:' : 'And with:'}
schemaName={SchemaHelpers.applicatorSchemaName(
idx,
'Consists of',
'And of',
s.title(),
)}
/>
))}
{schema.not() && (
Expand Down
14 changes: 14 additions & 0 deletions library/src/helpers/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ export class SchemaHelpers {
return type;
}

static applicatorSchemaName(
idx: number,
firstCase: string,
otherCases: string,
title?: string,
) {
const suffix = (title != null && ` ${title}:`) || `:`;
if (idx === 0) {
return `${firstCase}${suffix}`;
} else {
return `${otherCases}${suffix}`;
}
}

static prettifyValue(value: any, strict = true): string {
const typeOf = typeof value;
if (typeOf === 'string') {
Expand Down
1 change: 1 addition & 0 deletions playground/src/specs/streetlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ components:
union:
type: [string, number]
objectWithKey:
title: objectWithKey
type: object
propertyNames:
format: email
Expand Down

0 comments on commit 1b1a754

Please sign in to comment.