Skip to content

Commit

Permalink
refactor(description): merge renderJsonSchema and renderSimpleJsonSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSaba committed Feb 14, 2024
1 parent bfa793c commit 94ad7d5
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ import { DEFAULT_DESCRIPTION_LOCALE } from './constants'
import { LocalizedJSONSchema, LocalizedJSONSchemaDefinition } from './types'
import { isNotEmpty } from './utils'

export function getDescription (schema: LocalizedJSONSchema, locale: string = DEFAULT_DESCRIPTION_LOCALE): string | undefined {
function getDescription (schema: LocalizedJSONSchema, locale: string = DEFAULT_DESCRIPTION_LOCALE): string | undefined {
const description = isNotEmpty(schema.description) ? schema.description : undefined
return schema.descriptions?.[locale] ?? description
}

function renderSimpleJsonSchema (locale: string, schema?: LocalizedJSONSchemaDefinition): JSONSchema6Definition | undefined {
return schema == null
? undefined
: typeof schema === 'boolean'
? schema
: renderJsonSchema(schema, locale)
}

function renderArrayOfJsonSchema (locale: string, array?: LocalizedJSONSchemaDefinition[]): JSONSchema6Definition[] | undefined {
return array?.map(schema => renderSimpleJsonSchema(locale, schema) as JSONSchema6Definition)
return array?.map(schema => renderJsonSchema(locale, schema)).filter((schema): schema is JSONSchema6Definition => schema != null)
}

function renderObjectOfJsonSchema (locale: string, object?: { [key: string]: LocalizedJSONSchemaDefinition }): { [key: string]: JSONSchema6Definition } | undefined {
return object == null
? undefined
: Object.entries(object).reduce((newSchema, [key, schema]) => ({
...newSchema,
[key]: renderSimpleJsonSchema(locale, schema)
[key]: renderJsonSchema(locale, schema)
}), {})
}

export function renderJsonSchema (schema: LocalizedJSONSchema, locale: string): JSONSchema6 {
function renderJsonSchema (locale: string, schema: LocalizedJSONSchema): JSONSchema6
function renderJsonSchema (locale: string, schema: boolean): boolean
function renderJsonSchema (locale: string, schema: undefined): undefined
function renderJsonSchema (locale: string, schema?: LocalizedJSONSchemaDefinition): JSONSchema6Definition | undefined
function renderJsonSchema (locale: string, schema?: LocalizedJSONSchemaDefinition): JSONSchema6Definition | undefined {
if (schema == null) {
return undefined
} else if (typeof schema === 'boolean') {
return schema
}

const description = getDescription(schema, locale)
const {
descriptions,
Expand All @@ -53,26 +55,28 @@ export function renderJsonSchema (schema: LocalizedJSONSchema, locale: string):
? undefined
: Object.entries(dependencies).reduce((newSchema, [key, schema]) => ({
...newSchema,
[key]: Array.isArray(schema) ? schema : renderSimpleJsonSchema(locale, schema)
[key]: Array.isArray(schema) ? schema : renderJsonSchema(locale, schema)
}), {})

return {
...jsonSchema,
items: Array.isArray(items)
? renderArrayOfJsonSchema(locale, items)
: renderSimpleJsonSchema(locale, items),
additionalItems: renderSimpleJsonSchema(locale, additionalItems),
contains: renderSimpleJsonSchema(locale, contains),
: renderJsonSchema(locale, items),
additionalItems: renderJsonSchema(locale, additionalItems),
contains: renderJsonSchema(locale, contains),
properties: renderObjectOfJsonSchema(locale, properties),
patternProperties: renderObjectOfJsonSchema(locale, patternProperties),
additionalProperties: renderSimpleJsonSchema(locale, additionalProperties),
additionalProperties: renderJsonSchema(locale, additionalProperties),
dependencies: newDependencies,
propertyNames: renderSimpleJsonSchema(locale, propertyNames),
propertyNames: renderJsonSchema(locale, propertyNames),
allOf: renderArrayOfJsonSchema(locale, allOf),
anyOf: renderArrayOfJsonSchema(locale, anyOf),
oneOf: renderArrayOfJsonSchema(locale, oneOf),
not: renderSimpleJsonSchema(locale, not),
not: renderJsonSchema(locale, not),
definitions: renderObjectOfJsonSchema(locale, definitions),
description: description ?? jsonSchema.description
}
}

export { getDescription, renderJsonSchema }

0 comments on commit 94ad7d5

Please sign in to comment.