From 94ad7d56fc63f62e354895fa3ffebfc271070078 Mon Sep 17 00:00:00 2001 From: Claire Saba Date: Wed, 14 Feb 2024 11:44:00 +0100 Subject: [PATCH] refactor(description): merge renderJsonSchema and renderSimpleJsonSchema --- src/description.ts | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/description.ts b/src/description.ts index 5f9d6cb..64e470f 100644 --- a/src/description.ts +++ b/src/description.ts @@ -3,21 +3,13 @@ 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 { @@ -25,11 +17,21 @@ function renderObjectOfJsonSchema (locale: string, object?: { [key: string]: Loc ? 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, @@ -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 }