import DxComponentInputSchema from './manifest/v1/DxComponentInputSchema.json'; import DxContentMetaSchema from './manifest/v1/DxContentMetaSchema.json'; import FormattedText from './formatted-text/v1/formattedText.json'; import { SchemaValidationError } from './errors/SchemaValidationError'; import { Draft07, JSONSchema, Draft } from '@squiz/json-schema-library'; import { draft07Config } from '@squiz/json-schema-library'; import { FORMATTED_TEXT_SCHEMA_ID } from './formatted-text/v1/formattedTextConstants'; import * as MANIFEST_MODELS from './manifest/v1/manifestModels'; import { BaseFormattedTextType, ComponentInputFormattedTextType } from './primitiveTypes/FormattedText'; import { SquizImageType } from './primitiveTypes/SquizImage'; import { SquizLinkType } from './primitiveTypes/SquizLink'; import { TypeResolverBuilder } from './jsonTypeResolution/TypeResolverBuilder'; import { ComponentInputMetaSchema, JobV1MetaSchema, JSONSchemaService, ManifestV1MetaSchema, } from './JsonSchemaService'; import { defaultConfig } from './defaultDraftConfig'; export { defaultConfig }; const FTSchema = new Draft07(FormattedText, defaultConfig); /** * @deprecated Only used for FormattedText resolution */ export const ComponentInputSchema = new Draft( { ...defaultConfig, resolveRef(schema, rootSchema) { const resolvedSchema = draft07Config.resolveRef(schema, rootSchema) as MANIFEST_MODELS.v1.CoreSchemaMetaSchema; if (!resolvedSchema) { return resolvedSchema; } if (resolvedSchema.type === 'FormattedText') { return FTSchema.rootSchema; } else if (Array.isArray(resolvedSchema.type) && resolvedSchema.type.includes('FormattedText')) { return { ...schema, ...FTSchema.rootSchema, type: resolvedSchema.type.filter((t) => t !== 'FormattedText').concat(FormattedText.type as 'array'), }; } else { return resolvedSchema; } }, each: (core, data, callback, schema, pointer = '#') => { // Stop iterating when FormattedText if (schema?.type === 'FormattedText' || schema?.$id === FORMATTED_TEXT_SCHEMA_ID) { schema = core.resolveRef(schema); callback(schema, data, pointer); } else { defaultConfig.each(core, data, callback, schema, pointer); } }, }, DxComponentInputSchema, ); ComponentInputSchema.addRemoteSchema('DxComponentInputSchema.json/DxContentMetaSchema.json', DxContentMetaSchema); /** * Deprecated JSON Validation Service that wraps the JSONSchemaService * @deprecated Use JSONSchemaService instead */ export class JsonValidationService { private jobSchemaService = new JSONSchemaService(TypeResolverBuilder.new().build(), JobV1MetaSchema); private componentInputSchemaService = new JSONSchemaService( TypeResolverBuilder.new() .addPrimitive(BaseFormattedTextType) .addPrimitive(SquizImageType) .addPrimitive(SquizLinkType) .build(), ComponentInputMetaSchema, ); private renderInputSchemaService = new JSONSchemaService( TypeResolverBuilder.new() .addPrimitive(ComponentInputFormattedTextType) .addPrimitive(SquizImageType) .addPrimitive(SquizLinkType) .build(), ComponentInputMetaSchema, ); private componentManifestSchemaService = new JSONSchemaService( TypeResolverBuilder.new().build(), ManifestV1MetaSchema, ); validateManifest(manifest: unknown, version: 'v1' | 'JobV1') { switch (version) { case 'v1': { return this.componentManifestSchemaService.validateInput(manifest); } case 'JobV1': { return this.jobSchemaService.validateInput(manifest); } default: throw new SchemaValidationError('Invalid manifest version'); } } validateContentSchema(contentSchema: JSONSchema) { return this.componentInputSchemaService.validateInput(contentSchema); } validateComponentInput(functionInputSchema: JSONSchema, inputValue: unknown) { return this.componentInputSchemaService.validateInput(inputValue, functionInputSchema); } validateRenderInput(functionInputSchema: JSONSchema, inputValue: unknown) { return this.renderInputSchemaService.validateInput(inputValue, functionInputSchema); } }