|
| 1 | +import type { JsonSchemaType } from './types'; |
| 2 | + |
| 3 | +const DATA_VALUE_KEYWORDS = new Set(['const', 'default', 'enum', 'examples']); |
| 4 | +const SCHEMA_MAP_KEYWORDS = new Set(['$defs', 'definitions', 'dependencies', 'dependentSchemas', 'patternProperties', 'properties']); |
| 5 | +const SCHEMA_ARRAY_KEYWORDS = new Set(['allOf', 'anyOf', 'oneOf', 'prefixItems']); |
| 6 | +const SCHEMA_VALUE_KEYWORDS = new Set([ |
| 7 | + 'additionalProperties', |
| 8 | + 'contains', |
| 9 | + 'else', |
| 10 | + 'if', |
| 11 | + 'items', |
| 12 | + 'not', |
| 13 | + 'propertyNames', |
| 14 | + 'then', |
| 15 | + 'unevaluatedItems', |
| 16 | + 'unevaluatedProperties' |
| 17 | +]); |
| 18 | + |
| 19 | +function isJsonObject(value: unknown): value is Record<string, unknown> { |
| 20 | + return value !== null && typeof value === 'object' && !Array.isArray(value); |
| 21 | +} |
| 22 | + |
| 23 | +function normalizeSchemaObject(schema: Record<string, unknown>): Record<string, unknown> { |
| 24 | + const tupleItems = Array.isArray(schema.items) ? schema.items : undefined; |
| 25 | + const normalized: Record<string, unknown> = {}; |
| 26 | + |
| 27 | + for (const [key, value] of Object.entries(schema)) { |
| 28 | + if ((key === 'items' || key === 'additionalItems') && tupleItems !== undefined) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + if (DATA_VALUE_KEYWORDS.has(key)) { |
| 33 | + normalized[key] = value; |
| 34 | + } else if (SCHEMA_MAP_KEYWORDS.has(key) && isJsonObject(value)) { |
| 35 | + normalized[key] = Object.fromEntries( |
| 36 | + Object.entries(value).map(([childKey, childValue]) => [childKey, normalizeSchema(childValue)]) |
| 37 | + ); |
| 38 | + } else if (SCHEMA_ARRAY_KEYWORDS.has(key) && Array.isArray(value)) { |
| 39 | + normalized[key] = value.map(child => normalizeSchema(child)); |
| 40 | + } else if (SCHEMA_VALUE_KEYWORDS.has(key)) { |
| 41 | + normalized[key] = normalizeSchema(value); |
| 42 | + } else { |
| 43 | + normalized[key] = value; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (tupleItems !== undefined) { |
| 48 | + if (!('prefixItems' in normalized)) { |
| 49 | + normalized.prefixItems = tupleItems.map(item => normalizeSchema(item)); |
| 50 | + } |
| 51 | + |
| 52 | + if ('additionalItems' in schema && schema.additionalItems !== true) { |
| 53 | + normalized.items = normalizeSchema(schema.additionalItems); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return normalized; |
| 58 | +} |
| 59 | + |
| 60 | +function normalizeSchema(schema: unknown): unknown { |
| 61 | + if (schema === true || schema === false) { |
| 62 | + return schema; |
| 63 | + } |
| 64 | + |
| 65 | + if (Array.isArray(schema)) { |
| 66 | + return schema.map(item => normalizeSchema(item)); |
| 67 | + } |
| 68 | + |
| 69 | + if (!isJsonObject(schema)) { |
| 70 | + return schema; |
| 71 | + } |
| 72 | + |
| 73 | + return normalizeSchemaObject(schema); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * JSON Schema 2020-12 replaced draft-07 tuple syntax (`items: [...]` plus |
| 78 | + * `additionalItems`) with `prefixItems` plus `items`. Normalize the legacy |
| 79 | + * tuple form before handing schemas to 2020-12 validators so older advertised |
| 80 | + * tool schemas remain callable. |
| 81 | + */ |
| 82 | +export function normalizeLegacyTupleSchema(schema: JsonSchemaType): JsonSchemaType { |
| 83 | + return normalizeSchema(schema) as JsonSchemaType; |
| 84 | +} |
0 commit comments