|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
| 2 | + |
| 3 | +import { Ajv2019 } from 'ajv/dist/2019.js' |
| 4 | +import { Ajv2020 } from 'ajv/dist/2020.js' |
| 5 | +import type AjvCore from 'ajv/dist/core.js' |
| 6 | +import type { |
| 7 | + AnySchema, |
| 8 | + AnySchemaObject, |
| 9 | + DataValidationCxt, |
| 10 | +} from 'ajv/dist/types/index.js' |
| 11 | + |
| 12 | +import { constantResultSchema, metaSchema } from './common.js' |
| 13 | +import type { SchemaVersion } from './types.js' |
| 14 | + |
| 15 | +const ajvCache: Partial<Record<'default' | 'draft2020', AjvCore.default>> = {} |
| 16 | + |
| 17 | +/** |
| 18 | + * Retrieves an Ajv validator instance for a specified JSON Schema draft version. |
| 19 | + * |
| 20 | + * This function returns a cached Ajv instance if available, or creates a new one configured for either |
| 21 | + * 'draft2019' (default) or 'draft2020'. The created instance is enhanced with a custom "migrateSchema" keyword |
| 22 | + * that transforms JSON Schemas to be compatible with different specification drafts by adjusting properties |
| 23 | + * such as "id", "$schema", "constant", "enum", and various numeric constraints. |
| 24 | + * |
| 25 | + * @param version - The target JSON Schema version (defaults to 'draft2019'). |
| 26 | + * @returns An Ajv instance configured for JSON Schema validation and migration. |
| 27 | + * |
| 28 | + * @remark The custom "migrateSchema" keyword may throw a TypeError if a schema's "id" is not a string, or an Error |
| 29 | + * if the "id" format is invalid for the given version during the migration process. |
| 30 | + */ |
| 31 | +export function getAjv(version: SchemaVersion = 'draft2019') { |
| 32 | + const isDraft2020 = version === 'draft2020' |
| 33 | + |
| 34 | + const cacheKey = isDraft2020 ? 'draft2020' : 'default' |
| 35 | + |
| 36 | + let ajv = ajvCache[cacheKey] |
| 37 | + |
| 38 | + if (ajv) { |
| 39 | + return ajv |
| 40 | + } |
| 41 | + |
| 42 | + ajv = new (isDraft2020 ? Ajv2020 : Ajv2019)({ allErrors: true }) |
| 43 | + |
| 44 | + ajv.addKeyword({ |
| 45 | + keyword: 'migrateSchema', |
| 46 | + schemaType: 'string', |
| 47 | + modifying: true, |
| 48 | + metaSchema: { enum: ['draft7', 'draft2019', 'draft2020'] }, |
| 49 | + // eslint-disable-next-line sonarjs/cognitive-complexity |
| 50 | + validate( |
| 51 | + version: SchemaVersion, |
| 52 | + schema: AnySchema, |
| 53 | + _parentSchema?: AnySchemaObject, |
| 54 | + dataCxt?: DataValidationCxt, |
| 55 | + ) { |
| 56 | + if (typeof schema != 'object') { |
| 57 | + return true |
| 58 | + } |
| 59 | + |
| 60 | + if (dataCxt) { |
| 61 | + const { parentData, parentDataProperty } = dataCxt |
| 62 | + const valid = constantResultSchema(schema) |
| 63 | + if (typeof valid == 'boolean') { |
| 64 | + parentData[parentDataProperty] = valid |
| 65 | + return true |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const dsCopy = { ...schema } |
| 70 | + |
| 71 | + for (const key in dsCopy) { |
| 72 | + delete schema[key] |
| 73 | + switch (key) { |
| 74 | + case 'id': { |
| 75 | + const { id } = dsCopy |
| 76 | + if (typeof id !== 'string') { |
| 77 | + throw new TypeError( |
| 78 | + `json-schema-migrate: schema id must be string`, |
| 79 | + ) |
| 80 | + } |
| 81 | + if ((version === 'draft2019' || isDraft2020) && id.includes('#')) { |
| 82 | + const [$id, $anchor, ...rest] = id.split('#') |
| 83 | + if (rest.length > 0) { |
| 84 | + throw new Error(`json-schema-migrate: invalid schema id ${id}`) |
| 85 | + } |
| 86 | + if ($id) { |
| 87 | + schema.$id = $id |
| 88 | + } |
| 89 | + if ($anchor && $anchor !== '/') { |
| 90 | + schema.$anchor = $anchor |
| 91 | + } |
| 92 | + } else { |
| 93 | + schema.$id = id |
| 94 | + } |
| 95 | + break |
| 96 | + } |
| 97 | + case '$schema': { |
| 98 | + schema.$schema = metaSchema(version) |
| 99 | + break |
| 100 | + } |
| 101 | + case 'constant': { |
| 102 | + schema.const = dsCopy.constant |
| 103 | + break |
| 104 | + } |
| 105 | + case 'enum': { |
| 106 | + if ( |
| 107 | + Array.isArray(dsCopy.enum) && |
| 108 | + dsCopy.enum.length === 1 && |
| 109 | + dsCopy.constant === undefined && |
| 110 | + dsCopy.const === undefined |
| 111 | + ) { |
| 112 | + schema.const = dsCopy.enum[0] |
| 113 | + } else { |
| 114 | + schema.enum = dsCopy.enum |
| 115 | + } |
| 116 | + break |
| 117 | + } |
| 118 | + case 'exclusiveMaximum': { |
| 119 | + migrateExclusive(schema, key, 'maximum') |
| 120 | + break |
| 121 | + } |
| 122 | + case 'exclusiveMinimum': { |
| 123 | + migrateExclusive(schema, key, 'minimum') |
| 124 | + break |
| 125 | + } |
| 126 | + case 'maximum': { |
| 127 | + if (dsCopy.exclusiveMaximum !== true) { |
| 128 | + schema.maximum = dsCopy.maximum |
| 129 | + } |
| 130 | + break |
| 131 | + } |
| 132 | + case 'minimum': { |
| 133 | + if (dsCopy.exclusiveMinimum !== true) { |
| 134 | + schema.minimum = dsCopy.minimum |
| 135 | + } |
| 136 | + break |
| 137 | + } |
| 138 | + case 'dependencies': { |
| 139 | + const deps = dsCopy.dependencies as Record<string, unknown> |
| 140 | + if (version === 'draft7') { |
| 141 | + schema.dependencies = deps |
| 142 | + } else { |
| 143 | + for (const prop in deps) { |
| 144 | + const kwd = Array.isArray(deps[prop]) |
| 145 | + ? 'dependentRequired' |
| 146 | + : 'dependentSchemas' |
| 147 | + schema[kwd] ||= {} |
| 148 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 149 | + schema[kwd][prop] = deps[prop] |
| 150 | + } |
| 151 | + } |
| 152 | + break |
| 153 | + } |
| 154 | + case 'items': { |
| 155 | + if (isDraft2020 && Array.isArray(dsCopy.items)) { |
| 156 | + schema.prefixItems = dsCopy.items |
| 157 | + if (dsCopy.additionalItems !== undefined) { |
| 158 | + schema.items = dsCopy.additionalItems |
| 159 | + } |
| 160 | + } else { |
| 161 | + schema.items = dsCopy.items |
| 162 | + } |
| 163 | + break |
| 164 | + } |
| 165 | + case 'additionalItems': { |
| 166 | + if (!isDraft2020) { |
| 167 | + schema.additionalItems = dsCopy.additionalItems |
| 168 | + } |
| 169 | + break |
| 170 | + } |
| 171 | + case '$recursiveAnchor': { |
| 172 | + if (isDraft2020) { |
| 173 | + schema.$dynamicAnchor = 'meta' |
| 174 | + } else { |
| 175 | + schema.$recursiveAnchor = dsCopy.$recursiveAnchor |
| 176 | + } |
| 177 | + break |
| 178 | + } |
| 179 | + default: { |
| 180 | + schema[key] = dsCopy[key] |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + return true |
| 185 | + |
| 186 | + function migrateExclusive( |
| 187 | + schema: AnySchemaObject, |
| 188 | + key: string, |
| 189 | + limit: string, |
| 190 | + ): void { |
| 191 | + if (dsCopy[key] === true) { |
| 192 | + schema[key] = dsCopy[limit] |
| 193 | + } else if (dsCopy[key] !== false && dsCopy[key] !== undefined) { |
| 194 | + ajv!.logger.warn(`${key} is not boolean`) |
| 195 | + } |
| 196 | + } |
| 197 | + }, |
| 198 | + }) |
| 199 | + |
| 200 | + ajvCache[cacheKey] = ajv |
| 201 | + |
| 202 | + return ajv |
| 203 | +} |
0 commit comments