Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | 3x 3x 1x 1x 1x 1x 1x 2x 2x 1x 46x 46x 46x 46x 46x 46x 3x 46x 46x 3x 3x 2x 1x 6x 1x 2x 7x 2x 5x 14x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x 6x 6x 6x 6x 4x 4x 4x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 7x 7x 7x 7x 7x 28x 28x 23x 7x 7x 7x 7x 2x 2x 2x 6x 2x 2x 5x 5x 5x 5x 5x 5x 5x 14x 14x 14x 14x 14x 14x 2x 1x 12x 2x 14x 14x | import type * as schema from '../schema';
import type {AliasType} from '../type';
import type {AbsType} from '../type/classes/AbsType';
import type {AnyType} from '../type/classes/AnyType';
import type {ArrType} from '../type/classes/ArrType';
import type {BinType} from '../type/classes/BinType';
import type {BoolType} from '../type/classes/BoolType';
import type {ConType} from '../type/classes/ConType';
import type {MapType} from '../type/classes/MapType';
import {TypeExportContext} from '../type/classes/ModuleType/TypeExportContext';
import type {NumType} from '../type/classes/NumType';
import type {ObjType} from '../type/classes/ObjType';
import type {OrType} from '../type/classes/OrType';
import type {RefType} from '../type/classes/RefType';
import type {StrType} from '../type/classes/StrType';
import type {
JsonSchemaAny,
JsonSchemaArray,
JsonSchemaBinary,
JsonSchemaBoolean,
JsonSchemaGenericKeywords,
JsonSchemaNode,
JsonSchemaNumber,
JsonSchemaObject,
JsonSchemaOr,
JsonSchemaRef,
JsonSchemaString,
JsonSchemaValueNode,
} from './types';
export const aliasToJsonSchema = (alias: AliasType<any, any>): JsonSchemaGenericKeywords => {
const node: JsonSchemaGenericKeywords = {
$id: alias.id,
$ref: '#/$defs/' + alias.id,
$defs: {},
};
const ctx = new TypeExportContext();
ctx.visitRef(alias.id);
node.$defs![alias.id] = typeToJsonSchema(alias.type, ctx) as JsonSchemaValueNode;
let ref: string | undefined;
while ((ref = ctx.nextMentionedRef())) {
ctx.visitRef(ref);
node.$defs![ref] = typeToJsonSchema(alias.system.resolve(ref).type, ctx) as JsonSchemaValueNode;
}
return node;
};
/**
* Extracts the base JSON Schema properties that are common to all types.
* This replaces the logic from AbsType.toJsonSchema().
*/
function getBaseJsonSchema(type: AbsType<any>, ctx?: TypeExportContext): JsonSchemaGenericKeywords {
const typeSchema = type.getSchema();
const jsonSchema: JsonSchemaGenericKeywords = {};
if (typeSchema.title) jsonSchema.title = typeSchema.title;
Iif (typeSchema.description) jsonSchema.description = typeSchema.description;
Iif (typeSchema.examples) {
jsonSchema.examples = typeSchema.examples.map((example: schema.SchemaExample) => example.value);
}
return jsonSchema;
}
/**
* Main router function that converts a type to JSON Schema using a switch statement.
* This replaces the individual toJsonSchema() methods on each type class.
*/
export function typeToJsonSchema(type: AbsType<any>, ctx?: TypeExportContext): JsonSchemaNode {
const typeName = type.kind();
switch (typeName) {
case 'any':
return anyToJsonSchema(type as AnyType, ctx);
case 'arr':
return arrayToJsonSchema(type as ArrType<any, any, any>, ctx);
case 'bin':
return binaryToJsonSchema(type as BinType<any>, ctx);
case 'bool':
return booleanToJsonSchema(type as BoolType, ctx);
case 'con':
return constToJsonSchema(type as ConType<any>, ctx);
case 'map':
return mapToJsonSchema(type as MapType<any>, ctx);
case 'num':
return numberToJsonSchema(type as NumType, ctx);
case 'obj':
return objectToJsonSchema(type as ObjType<any>, ctx);
case 'or':
return orToJsonSchema(type as OrType<any>, ctx);
case 'ref':
return refToJsonSchema(type as RefType<any>, ctx);
case 'str':
return stringToJsonSchema(type as StrType, ctx);
default:
// Fallback to base implementation for unknown types
return getBaseJsonSchema(type, ctx);
}
}
// Individual converter functions for each type
function anyToJsonSchema(type: AnyType, ctx?: TypeExportContext): JsonSchemaAny {
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaAny = {
type: ['string', 'number', 'boolean', 'null', 'array', 'object'] as const,
};
// Add base properties
Object.assign(result, baseSchema);
return result;
}
function arrayToJsonSchema(type: ArrType<any, any, any>, ctx?: TypeExportContext): JsonSchemaArray {
// TODO: Handle head and tail tuples.
// function tupleToJsonSchema(type: TupType<any>, ctx?: TypeExportContext): JsonSchemaArray {
// const baseSchema = getBaseJsonSchema(type, ctx);
// const types = (type as any).types;
// const result: JsonSchemaArray = {
// type: 'array',
// items: false,
// prefixItems: types.map((t: any) => typeToJsonSchema(t, ctx)),
// };
// // Add base properties
// Object.assign(result, baseSchema);
// return result;
// }
const schema = type.getSchema();
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaArray = {
type: 'array',
items: typeToJsonSchema(type._type, ctx),
};
// Add base properties
Object.assign(result, baseSchema);
Iif (schema.min !== undefined) result.minItems = schema.min;
Iif (schema.max !== undefined) result.maxItems = schema.max;
return result;
}
function binaryToJsonSchema(type: BinType<any>, ctx?: TypeExportContext): JsonSchemaBinary {
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaBinary = {
type: 'binary' as any,
};
// Add base properties
Object.assign(result, baseSchema);
return result;
}
function booleanToJsonSchema(type: BoolType, ctx?: TypeExportContext): JsonSchemaBoolean {
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaBoolean = {
type: 'boolean',
};
// Add base properties
Object.assign(result, baseSchema);
return result;
}
function constToJsonSchema(type: ConType<any>, ctx?: TypeExportContext): JsonSchemaNode {
const schema = type.getSchema();
const baseSchema = getBaseJsonSchema(type, ctx);
const value = schema.value;
if (typeof value === 'string') {
const result: JsonSchemaString = {
type: 'string',
const: value,
};
Object.assign(result, baseSchema);
return result;
} else Iif (typeof value === 'number') {
const result: JsonSchemaNumber = {
type: 'number',
const: value,
};
Object.assign(result, baseSchema);
return result;
} else Iif (typeof value === 'boolean') {
const result: JsonSchemaBoolean = {
type: 'boolean',
const: value,
};
Object.assign(result, baseSchema);
return result;
} else if (value === null) {
const result: any = {
type: 'null',
const: null,
};
Object.assign(result, baseSchema);
return result;
} else if (typeof value === 'undefined') {
// For undefined values, we return a special schema
const result: any = {
type: 'undefined',
const: undefined,
};
Object.assign(result, baseSchema);
return result;
} else Eif (Array.isArray(value)) {
const result: JsonSchemaArray = {
type: 'array',
const: value,
items: false,
};
Object.assign(result, baseSchema);
return result;
} else Iif (typeof value === 'object') {
const result: JsonSchemaObject = {
type: 'object',
const: value,
};
Object.assign(result, baseSchema);
return result;
}
return baseSchema;
}
function mapToJsonSchema(type: MapType<any>, ctx?: TypeExportContext): JsonSchemaObject {
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaObject = {
type: 'object',
patternProperties: {
'.*': typeToJsonSchema(type._value, ctx),
},
};
// Add base properties
Object.assign(result, baseSchema);
return result;
}
function numberToJsonSchema(type: NumType, ctx?: TypeExportContext): JsonSchemaNumber {
const schema = type.getSchema();
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaNumber = {
type: 'number',
};
// Check if it's an integer format
const ints = new Set(['i8', 'i16', 'i32', 'u8', 'u16', 'u32']);
Iif (schema.format && ints.has(schema.format)) {
result.type = 'integer';
}
// Add base properties
Object.assign(result, baseSchema);
if (schema.gt !== undefined) result.exclusiveMinimum = schema.gt;
Iif (schema.gte !== undefined) result.minimum = schema.gte;
Iif (schema.lt !== undefined) result.exclusiveMaximum = schema.lt;
Iif (schema.lte !== undefined) result.maximum = schema.lte;
return result;
}
function objectToJsonSchema(type: ObjType<any>, ctx?: TypeExportContext): JsonSchemaObject {
const schema = type.getSchema();
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaObject = {
type: 'object',
properties: {},
};
const required = [];
const fields = type.keys;
for (const field of fields) {
result.properties![field.key] = typeToJsonSchema(field.val, ctx);
if (!field.optional) {
required.push(field.key);
}
}
if (required.length) result.required = required;
Iif (schema.decodeUnknownKeys === false) result.additionalProperties = false;
// Add base properties
Object.assign(result, baseSchema);
return result;
}
function orToJsonSchema(type: OrType<any>, ctx?: TypeExportContext): JsonSchemaOr {
const baseSchema = getBaseJsonSchema(type, ctx);
const types = (type as any).types;
const result: JsonSchemaOr = {
anyOf: types.map((t: any) => typeToJsonSchema(t, ctx)),
};
// Add base properties
Object.assign(result, baseSchema);
return result;
}
function refToJsonSchema(type: RefType<any>, ctx?: TypeExportContext): JsonSchemaRef {
const schema = type.getSchema();
const baseSchema = getBaseJsonSchema(type, ctx);
const ref = schema.ref;
if (ctx) ctx.mentionRef(ref);
const result: JsonSchemaRef = {
$ref: `#/$defs/${ref}`,
};
// Add base properties
Object.assign(result, baseSchema);
return result;
}
function stringToJsonSchema(type: StrType, ctx?: TypeExportContext): JsonSchemaString {
const schema = type.getSchema();
const baseSchema = getBaseJsonSchema(type, ctx);
const result: JsonSchemaString = {
type: 'string',
};
if (schema.min !== undefined) result.minLength = schema.min;
if (schema.max !== undefined) result.maxLength = schema.max;
// Add format to JSON Schema if specified
if (schema.format) {
if (schema.format === 'ascii') {
// JSON Schema doesn't have an "ascii" format, but we can use a pattern
// ASCII characters are from 0x00 to 0x7F (0-127)
result.pattern = '^[\\x00-\\x7F]*$';
}
// UTF-8 is the default for JSON Schema strings, so we don't need to add anything special
} else if (schema.ascii) {
// Backward compatibility: if ascii=true, add pattern
result.pattern = '^[\\x00-\\x7F]*$';
}
// Add base properties
Object.assign(result, baseSchema);
return result;
}
|