|
| 1 | +import type { JSONSchema7 } from 'json-schema' |
| 2 | + |
| 3 | +import { SimpleSchema } from './SimpleSchema.js' |
| 4 | +import { StandardSchemaKeyDefinition } from './types.js' |
| 5 | + |
| 6 | +const jsonSchemaVersion = 'https://json-schema.org/draft/2020-12/schema' |
| 7 | + |
| 8 | +function toJSArray (ss: SimpleSchema, key: string, fieldDef: StandardSchemaKeyDefinition): JSONSchema7 | null { |
| 9 | + const itemSchema = fieldDefToJsonSchema(ss, `${key}.$`) |
| 10 | + if (itemSchema == null) return null |
| 11 | + |
| 12 | + const arrayDef: JSONSchema7 = { |
| 13 | + type: 'array', |
| 14 | + items: [itemSchema], |
| 15 | + additionalItems: false |
| 16 | + } |
| 17 | + |
| 18 | + if (fieldDef.minCount !== undefined) { |
| 19 | + arrayDef.minItems = fieldDef.minCount |
| 20 | + } |
| 21 | + |
| 22 | + if (fieldDef.maxCount !== undefined) { |
| 23 | + arrayDef.maxItems = fieldDef.maxCount |
| 24 | + } |
| 25 | + |
| 26 | + return arrayDef |
| 27 | +} |
| 28 | + |
| 29 | +function toJsProperties (ss: SimpleSchema): { |
| 30 | + properties: Record<string, JSONSchema7> |
| 31 | + required: string[] |
| 32 | +} { |
| 33 | + const properties: Record<string, JSONSchema7> = {} |
| 34 | + const required: string[] = [] |
| 35 | + |
| 36 | + for (const key of ss.objectKeys()) { |
| 37 | + const fieldDef = ss.schema(key) |
| 38 | + if (fieldDef == null) continue |
| 39 | + if (fieldDef.optional !== true) required.push(key) |
| 40 | + const schema = fieldDefToJsonSchema(ss, key) |
| 41 | + if (schema != null) properties[key] = schema |
| 42 | + } |
| 43 | + |
| 44 | + return { properties, required } |
| 45 | +} |
| 46 | + |
| 47 | +function toJSObj (simpleSchema: SimpleSchema, additionalProperties: boolean = false): JSONSchema7 | null { |
| 48 | + return { |
| 49 | + type: 'object', |
| 50 | + ...toJsProperties(simpleSchema), |
| 51 | + additionalProperties |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function fieldDefToJsonSchema (ss: SimpleSchema, key: string): JSONSchema7 | null { |
| 56 | + const fieldDef = ss.schema(key) |
| 57 | + if (fieldDef == null) return null |
| 58 | + |
| 59 | + const itemSchemas = [] |
| 60 | + |
| 61 | + for (const fieldTypeDef of fieldDef.type.definitions) { |
| 62 | + let itemSchema: JSONSchema7 | null = null |
| 63 | + |
| 64 | + switch (fieldTypeDef.type) { |
| 65 | + case String: |
| 66 | + itemSchema = { type: 'string' } |
| 67 | + if (fieldTypeDef.allowedValues !== undefined && typeof fieldTypeDef.allowedValues !== 'function') { |
| 68 | + itemSchema.enum = [...fieldTypeDef.allowedValues] |
| 69 | + } |
| 70 | + if (fieldTypeDef.max !== undefined && typeof fieldTypeDef.max !== 'function') { |
| 71 | + itemSchema.maxLength = fieldTypeDef.max as number |
| 72 | + } |
| 73 | + if (fieldTypeDef.min !== undefined && typeof fieldTypeDef.min !== 'function') { |
| 74 | + itemSchema.minLength = fieldTypeDef.min as number |
| 75 | + } |
| 76 | + if (fieldTypeDef.regEx instanceof RegExp) { |
| 77 | + itemSchema.pattern = String(fieldTypeDef.regEx) |
| 78 | + } |
| 79 | + break |
| 80 | + |
| 81 | + case Number: |
| 82 | + case SimpleSchema.Integer: |
| 83 | + itemSchema = { type: fieldTypeDef.type === Number ? 'number' : 'integer' } |
| 84 | + if (fieldTypeDef.max !== undefined && typeof fieldTypeDef.max !== 'function') { |
| 85 | + if (fieldTypeDef.exclusiveMax === true) { |
| 86 | + itemSchema.exclusiveMaximum = fieldTypeDef.max as number |
| 87 | + } else { |
| 88 | + itemSchema.maximum = fieldTypeDef.max as number |
| 89 | + } |
| 90 | + } |
| 91 | + if (fieldTypeDef.min !== undefined && typeof fieldTypeDef.min !== 'function') { |
| 92 | + if (fieldTypeDef.exclusiveMin === true) { |
| 93 | + itemSchema.exclusiveMinimum = fieldTypeDef.min as number |
| 94 | + } else { |
| 95 | + itemSchema.minimum = fieldTypeDef.min as number |
| 96 | + } |
| 97 | + } |
| 98 | + break |
| 99 | + |
| 100 | + case Boolean: |
| 101 | + itemSchema = { type: 'boolean' } |
| 102 | + break |
| 103 | + |
| 104 | + case Date: |
| 105 | + itemSchema = { |
| 106 | + type: 'string', |
| 107 | + format: 'date-time' |
| 108 | + } |
| 109 | + break |
| 110 | + |
| 111 | + case Array: |
| 112 | + itemSchema = toJSArray(ss, key, fieldDef) |
| 113 | + break |
| 114 | + |
| 115 | + case Object: |
| 116 | + itemSchema = toJSObj(ss.getObjectSchema(key), fieldTypeDef.blackbox) |
| 117 | + break |
| 118 | + |
| 119 | + case SimpleSchema.Any: |
| 120 | + // In JSONSchema an empty object means any type |
| 121 | + itemSchema = {} |
| 122 | + break |
| 123 | + |
| 124 | + default: |
| 125 | + if (SimpleSchema.isSimpleSchema(fieldTypeDef.type)) { |
| 126 | + itemSchema = toJSObj(fieldTypeDef.type as SimpleSchema, fieldTypeDef.blackbox) |
| 127 | + } else if ( |
| 128 | + // support custom objects |
| 129 | + fieldTypeDef.type instanceof Function |
| 130 | + ) { |
| 131 | + itemSchema = toJSObj(ss.getObjectSchema(key), fieldTypeDef.blackbox) |
| 132 | + } |
| 133 | + break |
| 134 | + } |
| 135 | + |
| 136 | + if (itemSchema != null && fieldTypeDef.defaultValue !== undefined) { |
| 137 | + itemSchema.default = fieldTypeDef.defaultValue |
| 138 | + } |
| 139 | + |
| 140 | + if (itemSchema != null) itemSchemas.push(itemSchema) |
| 141 | + } |
| 142 | + |
| 143 | + if (itemSchemas.length > 1) { |
| 144 | + return { anyOf: itemSchemas } |
| 145 | + } |
| 146 | + |
| 147 | + return itemSchemas[0] ?? null |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Convert a SimpleSchema to a JSONSchema Document. |
| 152 | + * |
| 153 | + * Notes: |
| 154 | + * - Date fields will become string fields with built-in 'date-time' format. |
| 155 | + * - JSONSchema does not support minimum or maximum values for date fields |
| 156 | + * - Custom validators are ignored |
| 157 | + * - Field definition properties that are a function are ignored |
| 158 | + * - Custom objects are treated as regular objects |
| 159 | + * |
| 160 | + * @param simpleSchema SimpleSchema instance to convert |
| 161 | + * @param id Optional ID to use for the `$id` field |
| 162 | + * @returns JSONSchema Document |
| 163 | + */ |
| 164 | +export function toJsonSchema (simpleSchema: SimpleSchema, id?: string): JSONSchema7 { |
| 165 | + return { |
| 166 | + ...(id != null ? { $id: id } : {}), |
| 167 | + $schema: jsonSchemaVersion, |
| 168 | + ...toJSObj(simpleSchema) |
| 169 | + } |
| 170 | +} |
0 commit comments