Skip to content

Commit

Permalink
feat: use schema's title when available in {one,any,all}Of applicat…
Browse files Browse the repository at this point in the history
…ors (#744)
  • Loading branch information
KtorZ committed Nov 14, 2023
1 parent c228526 commit 8782f4a
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 8782f4a

Please sign in to comment.