|
1 | 1 | import type {Display} from './common'; |
2 | | -import type {TExample, TType, WithValidator} from './schema'; |
| 2 | +import type {TExample, TType, WithValidator, Schema} from './schema'; |
3 | 3 |
|
4 | 4 | export const validateDisplay = ({title, description, intro}: Display): void => { |
5 | 5 | if (title !== undefined && typeof title !== 'string') throw new Error('INVALID_TITLE'); |
@@ -44,3 +44,212 @@ export const validateMinMax = (min: number | undefined, max: number | undefined) |
44 | 44 | } |
45 | 45 | if (min !== undefined && max !== undefined && min > max) throw new Error('MIN_MAX'); |
46 | 46 | }; |
| 47 | + |
| 48 | +// Individual schema validation functions for each type |
| 49 | + |
| 50 | +const validateAnySchema = (schema: any): void => { |
| 51 | + validateTType(schema, 'any'); |
| 52 | +}; |
| 53 | + |
| 54 | +const validateBooleanSchema = (schema: any): void => { |
| 55 | + validateTType(schema, 'bool'); |
| 56 | +}; |
| 57 | + |
| 58 | +const validateNumberSchema = (schema: any): void => { |
| 59 | + validateTType(schema, 'num'); |
| 60 | + validateWithValidator(schema); |
| 61 | + const {format, gt, gte, lt, lte} = schema; |
| 62 | + |
| 63 | + if (gt !== undefined && typeof gt !== 'number') throw new Error('GT_TYPE'); |
| 64 | + if (gte !== undefined && typeof gte !== 'number') throw new Error('GTE_TYPE'); |
| 65 | + if (lt !== undefined && typeof lt !== 'number') throw new Error('LT_TYPE'); |
| 66 | + if (lte !== undefined && typeof lte !== 'number') throw new Error('LTE_TYPE'); |
| 67 | + if (gt !== undefined && gte !== undefined) throw new Error('GT_GTE'); |
| 68 | + if (lt !== undefined && lte !== undefined) throw new Error('LT_LTE'); |
| 69 | + if ((gt !== undefined || gte !== undefined) && (lt !== undefined || lte !== undefined)) |
| 70 | + if ((gt ?? gte)! > (lt ?? lte)!) throw new Error('GT_LT'); |
| 71 | + |
| 72 | + if (format !== undefined) { |
| 73 | + if (typeof format !== 'string') throw new Error('FORMAT_TYPE'); |
| 74 | + if (!format) throw new Error('FORMAT_EMPTY'); |
| 75 | + switch (format) { |
| 76 | + case 'i': |
| 77 | + case 'u': |
| 78 | + case 'f': |
| 79 | + case 'i8': |
| 80 | + case 'i16': |
| 81 | + case 'i32': |
| 82 | + case 'i64': |
| 83 | + case 'u8': |
| 84 | + case 'u16': |
| 85 | + case 'u32': |
| 86 | + case 'u64': |
| 87 | + case 'f32': |
| 88 | + case 'f64': |
| 89 | + break; |
| 90 | + default: |
| 91 | + throw new Error('FORMAT_INVALID'); |
| 92 | + } |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +const validateStringSchema = (schema: any): void => { |
| 97 | + validateTType(schema, 'str'); |
| 98 | + validateWithValidator(schema); |
| 99 | + const {min, max, ascii, noJsonEscape, format} = schema; |
| 100 | + |
| 101 | + validateMinMax(min, max); |
| 102 | + |
| 103 | + if (ascii !== undefined) { |
| 104 | + if (typeof ascii !== 'boolean') throw new Error('ASCII'); |
| 105 | + } |
| 106 | + if (noJsonEscape !== undefined) { |
| 107 | + if (typeof noJsonEscape !== 'boolean') throw new Error('NO_JSON_ESCAPE_TYPE'); |
| 108 | + } |
| 109 | + if (format !== undefined) { |
| 110 | + if (format !== 'ascii' && format !== 'utf8') { |
| 111 | + throw new Error('INVALID_STRING_FORMAT'); |
| 112 | + } |
| 113 | + // If both format and ascii are specified, they should be consistent |
| 114 | + if (ascii !== undefined && format === 'ascii' && !ascii) { |
| 115 | + throw new Error('FORMAT_ASCII_MISMATCH'); |
| 116 | + } |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +const binaryFormats = new Set(['bencode', 'bson', 'cbor', 'ion', 'json', 'msgpack', 'resp3', 'ubjson']); |
| 121 | + |
| 122 | +const validateBinarySchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 123 | + validateTType(schema, 'bin'); |
| 124 | + const {min, max, format} = schema; |
| 125 | + validateMinMax(min, max); |
| 126 | + if (format !== undefined) { |
| 127 | + if (!binaryFormats.has(format)) throw new Error('FORMAT'); |
| 128 | + } |
| 129 | + validateChildSchema(schema.type); |
| 130 | +}; |
| 131 | + |
| 132 | +const validateArraySchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 133 | + validateTType(schema, 'arr'); |
| 134 | + const {min, max} = schema; |
| 135 | + validateMinMax(min, max); |
| 136 | + validateChildSchema(schema.type); |
| 137 | +}; |
| 138 | + |
| 139 | +const validateConstSchema = (schema: any): void => { |
| 140 | + validateTType(schema, 'const'); |
| 141 | +}; |
| 142 | + |
| 143 | +const validateTupleSchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 144 | + validateTType(schema, 'tup'); |
| 145 | + validateWithValidator(schema); |
| 146 | + const {types} = schema; |
| 147 | + if (!Array.isArray(types)) throw new Error('TYPES_TYPE'); |
| 148 | + for (const type of types) validateChildSchema(type); |
| 149 | +}; |
| 150 | + |
| 151 | +const validateObjectSchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 152 | + validateTType(schema, 'obj'); |
| 153 | + validateWithValidator(schema); |
| 154 | + const {fields, unknownFields} = schema; |
| 155 | + if (!Array.isArray(fields)) throw new Error('FIELDS_TYPE'); |
| 156 | + if (unknownFields !== undefined && typeof unknownFields !== 'boolean') throw new Error('UNKNOWN_FIELDS_TYPE'); |
| 157 | + for (const field of fields) validateChildSchema(field); |
| 158 | +}; |
| 159 | + |
| 160 | +const validateFieldSchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 161 | + validateTType(schema, 'field'); |
| 162 | + const {key, optional} = schema; |
| 163 | + if (typeof key !== 'string') throw new Error('KEY_TYPE'); |
| 164 | + if (optional !== undefined && typeof optional !== 'boolean') throw new Error('OPTIONAL_TYPE'); |
| 165 | + validateChildSchema(schema.type); |
| 166 | +}; |
| 167 | + |
| 168 | +const validateMapSchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 169 | + validateTType(schema, 'map'); |
| 170 | + validateChildSchema(schema.type); |
| 171 | +}; |
| 172 | + |
| 173 | +const validateRefSchema = (schema: any): void => { |
| 174 | + validateTType(schema, 'ref'); |
| 175 | + const {ref} = schema; |
| 176 | + if (typeof ref !== 'string') throw new Error('REF_TYPE'); |
| 177 | + if (!ref) throw new Error('REF_EMPTY'); |
| 178 | +}; |
| 179 | + |
| 180 | +const validateOrSchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 181 | + validateTType(schema, 'or'); |
| 182 | + const {types, discriminator} = schema; |
| 183 | + if (!discriminator || (discriminator[0] === 'num' && discriminator[1] === -1)) throw new Error('DISCRIMINATOR'); |
| 184 | + if (!Array.isArray(types)) throw new Error('TYPES_TYPE'); |
| 185 | + if (!types.length) throw new Error('TYPES_LENGTH'); |
| 186 | + for (const type of types) validateChildSchema(type); |
| 187 | +}; |
| 188 | + |
| 189 | +const validateFunctionSchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 190 | + validateTType(schema, 'fn'); |
| 191 | + validateChildSchema(schema.req); |
| 192 | + validateChildSchema(schema.res); |
| 193 | +}; |
| 194 | + |
| 195 | +const validateFunctionStreamingSchema = (schema: any, validateChildSchema: (schema: Schema) => void): void => { |
| 196 | + validateTType(schema, 'fn$'); |
| 197 | + validateChildSchema(schema.req); |
| 198 | + validateChildSchema(schema.res); |
| 199 | +}; |
| 200 | + |
| 201 | +/** |
| 202 | + * Main router function that validates a schema based on its kind. |
| 203 | + * This replaces the individual validateSchema() methods from type classes. |
| 204 | + */ |
| 205 | +export const validateSchema = (schema: Schema): void => { |
| 206 | + switch (schema.kind) { |
| 207 | + case 'any': |
| 208 | + validateAnySchema(schema); |
| 209 | + break; |
| 210 | + case 'bool': |
| 211 | + validateBooleanSchema(schema); |
| 212 | + break; |
| 213 | + case 'num': |
| 214 | + validateNumberSchema(schema); |
| 215 | + break; |
| 216 | + case 'str': |
| 217 | + validateStringSchema(schema); |
| 218 | + break; |
| 219 | + case 'bin': |
| 220 | + validateBinarySchema(schema, validateSchema); |
| 221 | + break; |
| 222 | + case 'arr': |
| 223 | + validateArraySchema(schema, validateSchema); |
| 224 | + break; |
| 225 | + case 'const': |
| 226 | + validateConstSchema(schema); |
| 227 | + break; |
| 228 | + case 'tup': |
| 229 | + validateTupleSchema(schema, validateSchema); |
| 230 | + break; |
| 231 | + case 'obj': |
| 232 | + validateObjectSchema(schema, validateSchema); |
| 233 | + break; |
| 234 | + case 'field': |
| 235 | + validateFieldSchema(schema, validateSchema); |
| 236 | + break; |
| 237 | + case 'map': |
| 238 | + validateMapSchema(schema, validateSchema); |
| 239 | + break; |
| 240 | + case 'ref': |
| 241 | + validateRefSchema(schema); |
| 242 | + break; |
| 243 | + case 'or': |
| 244 | + validateOrSchema(schema, validateSchema); |
| 245 | + break; |
| 246 | + case 'fn': |
| 247 | + validateFunctionSchema(schema, validateSchema); |
| 248 | + break; |
| 249 | + case 'fn$': |
| 250 | + validateFunctionStreamingSchema(schema, validateSchema); |
| 251 | + break; |
| 252 | + default: |
| 253 | + throw new Error(`Unknown schema kind: ${(schema as any).kind}`); |
| 254 | + } |
| 255 | +}; |
0 commit comments