@atscript/typescript 0.1.34 → 0.1.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/cli.cjs +552 -113
- package/dist/index.cjs +165 -814
- package/dist/index.mjs +157 -806
- package/dist/json-schema-0UUPoHud.mjs +952 -0
- package/dist/json-schema-S5-XAOrR.cjs +1030 -0
- package/dist/utils.cjs +46 -976
- package/dist/utils.d.ts +47 -3
- package/dist/utils.mjs +24 -954
- package/package.json +11 -6
|
@@ -0,0 +1,952 @@
|
|
|
1
|
+
|
|
2
|
+
//#region packages/typescript/src/validator.ts
|
|
3
|
+
function _define_property(obj, key, value) {
|
|
4
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
5
|
+
value,
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true
|
|
9
|
+
});
|
|
10
|
+
else obj[key] = value;
|
|
11
|
+
return obj;
|
|
12
|
+
}
|
|
13
|
+
const regexCache = new Map();
|
|
14
|
+
var Validator = class {
|
|
15
|
+
isLimitExceeded() {
|
|
16
|
+
if (this.stackErrors.length > 0) {
|
|
17
|
+
const top = this.stackErrors[this.stackErrors.length - 1];
|
|
18
|
+
return top !== null && top.length >= this.opts.errorLimit;
|
|
19
|
+
}
|
|
20
|
+
return this.errors.length >= this.opts.errorLimit;
|
|
21
|
+
}
|
|
22
|
+
push(name) {
|
|
23
|
+
this.stackPath.push(name);
|
|
24
|
+
this.stackErrors.push(null);
|
|
25
|
+
this.cachedPath = this.stackPath.length <= 1 ? "" : this.stackPath[1] + (this.stackPath.length > 2 ? "." + this.stackPath.slice(2).join(".") : "");
|
|
26
|
+
}
|
|
27
|
+
pop(saveErrors) {
|
|
28
|
+
this.stackPath.pop();
|
|
29
|
+
const popped = this.stackErrors.pop();
|
|
30
|
+
if (saveErrors && popped !== null && popped !== undefined && popped.length > 0) for (const err of popped) this.error(err.message, err.path, err.details);
|
|
31
|
+
this.cachedPath = this.stackPath.length <= 1 ? "" : this.stackPath[1] + (this.stackPath.length > 2 ? "." + this.stackPath.slice(2).join(".") : "");
|
|
32
|
+
return popped;
|
|
33
|
+
}
|
|
34
|
+
clear() {
|
|
35
|
+
this.stackErrors[this.stackErrors.length - 1] = null;
|
|
36
|
+
}
|
|
37
|
+
error(message, path, details) {
|
|
38
|
+
let errors = this.stackErrors[this.stackErrors.length - 1];
|
|
39
|
+
if (!errors) if (this.stackErrors.length > 0) {
|
|
40
|
+
errors = [];
|
|
41
|
+
this.stackErrors[this.stackErrors.length - 1] = errors;
|
|
42
|
+
} else errors = this.errors;
|
|
43
|
+
const error = {
|
|
44
|
+
path: path || this.cachedPath,
|
|
45
|
+
message
|
|
46
|
+
};
|
|
47
|
+
if (details?.length) error.details = details;
|
|
48
|
+
errors.push(error);
|
|
49
|
+
}
|
|
50
|
+
throw() {
|
|
51
|
+
throw new ValidatorError(this.errors);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validates a value against the type definition.
|
|
55
|
+
*
|
|
56
|
+
* Acts as a TypeScript type guard — when it returns `true`, the value
|
|
57
|
+
* is narrowed to `DataType`.
|
|
58
|
+
*
|
|
59
|
+
* @param value - The value to validate.
|
|
60
|
+
* @param safe - If `true`, returns `false` on failure instead of throwing.
|
|
61
|
+
* @returns `true` if the value matches the type definition.
|
|
62
|
+
* @throws {ValidatorError} When validation fails and `safe` is not `true`.
|
|
63
|
+
*/ validate(value, safe, context) {
|
|
64
|
+
this.errors = [];
|
|
65
|
+
this.stackErrors = [];
|
|
66
|
+
this.stackPath = [""];
|
|
67
|
+
this.cachedPath = "";
|
|
68
|
+
this.context = context;
|
|
69
|
+
const passed = this.validateSafe(this.def, value);
|
|
70
|
+
this.pop(!passed);
|
|
71
|
+
this.context = undefined;
|
|
72
|
+
if (!passed) {
|
|
73
|
+
if (safe) return false;
|
|
74
|
+
this.throw();
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
validateSafe(def, value) {
|
|
79
|
+
if (this.isLimitExceeded()) return false;
|
|
80
|
+
if (!isAnnotatedType(def)) throw new Error("Can not validate not-annotated type");
|
|
81
|
+
if (typeof this.opts.replace === "function") def = this.opts.replace(def, this.cachedPath);
|
|
82
|
+
if (def.optional && (value === undefined || value === null)) return true;
|
|
83
|
+
for (const plugin of this.opts.plugins) {
|
|
84
|
+
const result = plugin(this, def, value);
|
|
85
|
+
if (result === false || result === true) return result;
|
|
86
|
+
}
|
|
87
|
+
return this.validateAnnotatedType(def, value);
|
|
88
|
+
}
|
|
89
|
+
get path() {
|
|
90
|
+
return this.cachedPath;
|
|
91
|
+
}
|
|
92
|
+
validateAnnotatedType(def, value) {
|
|
93
|
+
switch (def.type.kind) {
|
|
94
|
+
case "": {
|
|
95
|
+
if (def.type.designType === "phantom") return true;
|
|
96
|
+
return this.validatePrimitive(def, value);
|
|
97
|
+
}
|
|
98
|
+
case "object": return this.validateObject(def, value);
|
|
99
|
+
case "array": return this.validateArray(def, value);
|
|
100
|
+
case "union": return this.validateUnion(def, value);
|
|
101
|
+
case "intersection": return this.validateIntersection(def, value);
|
|
102
|
+
case "tuple": return this.validateTuple(def, value);
|
|
103
|
+
default: throw new Error(`Unknown type kind "${def.type.kind}"`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
validateUnion(def, value) {
|
|
107
|
+
let i = 0;
|
|
108
|
+
const popped = [];
|
|
109
|
+
for (const item of def.type.items) {
|
|
110
|
+
this.push(`[${item.type.kind || item.type.designType}(${i})]`);
|
|
111
|
+
if (this.validateSafe(item, value)) {
|
|
112
|
+
this.pop(false);
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
const errors = this.pop(false);
|
|
116
|
+
if (errors) popped.push(...errors);
|
|
117
|
+
i++;
|
|
118
|
+
}
|
|
119
|
+
this.clear();
|
|
120
|
+
const expected = def.type.items.map((item, i$1) => `[${item.type.kind || item.type.designType}(${i$1})]`).join(", ");
|
|
121
|
+
this.error(`Value does not match any of the allowed types: ${expected}`, undefined, popped);
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
validateIntersection(def, value) {
|
|
125
|
+
for (const item of def.type.items) if (!this.validateSafe(item, value)) return false;
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
validateTuple(def, value) {
|
|
129
|
+
if (!Array.isArray(value) || value.length !== def.type.items.length) {
|
|
130
|
+
this.error(`Expected array of length ${def.type.items.length}`);
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
let i = 0;
|
|
134
|
+
for (const item of def.type.items) {
|
|
135
|
+
this.push(String(i));
|
|
136
|
+
if (!this.validateSafe(item, value[i])) {
|
|
137
|
+
this.pop(true);
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
this.pop(false);
|
|
141
|
+
i++;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
validateArray(def, value) {
|
|
146
|
+
if (!Array.isArray(value)) {
|
|
147
|
+
this.error("Expected array");
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
const minLength = def.metadata.get("expect.minLength");
|
|
151
|
+
if (minLength) {
|
|
152
|
+
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
153
|
+
if (value.length < length) {
|
|
154
|
+
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
|
|
155
|
+
this.error(message);
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const maxLength = def.metadata.get("expect.maxLength");
|
|
160
|
+
if (maxLength) {
|
|
161
|
+
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
162
|
+
if (value.length > length) {
|
|
163
|
+
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
|
|
164
|
+
this.error(message);
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const uniqueItems = def.metadata.get("expect.array.uniqueItems");
|
|
169
|
+
if (uniqueItems) {
|
|
170
|
+
const separator = "▼↩";
|
|
171
|
+
const seen = new Set();
|
|
172
|
+
const keyProps = new Set();
|
|
173
|
+
if (def.type.of.type.kind === "object") {
|
|
174
|
+
for (const [key, val] of def.type.of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
|
|
175
|
+
}
|
|
176
|
+
for (let idx = 0; idx < value.length; idx++) {
|
|
177
|
+
const item = value[idx];
|
|
178
|
+
let key;
|
|
179
|
+
if (keyProps.size > 0) {
|
|
180
|
+
key = "";
|
|
181
|
+
for (const prop of keyProps) key += JSON.stringify(item[prop]) + separator;
|
|
182
|
+
} else key = JSON.stringify(item);
|
|
183
|
+
if (seen.has(key)) {
|
|
184
|
+
this.push(String(idx));
|
|
185
|
+
this.error(uniqueItems.message || "Duplicate items are not allowed");
|
|
186
|
+
this.pop(true);
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
seen.add(key);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
let i = 0;
|
|
193
|
+
let passed = true;
|
|
194
|
+
for (const item of value) {
|
|
195
|
+
this.push(String(i));
|
|
196
|
+
if (!this.validateSafe(def.type.of, item)) {
|
|
197
|
+
passed = false;
|
|
198
|
+
this.pop(true);
|
|
199
|
+
if (this.isLimitExceeded()) return false;
|
|
200
|
+
} else this.pop(false);
|
|
201
|
+
i++;
|
|
202
|
+
}
|
|
203
|
+
return passed;
|
|
204
|
+
}
|
|
205
|
+
validateObject(def, value) {
|
|
206
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
207
|
+
this.error("Expected object");
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
let passed = true;
|
|
211
|
+
const valueKeys = new Set(Object.keys(value));
|
|
212
|
+
const typeKeys = new Set();
|
|
213
|
+
let skipList;
|
|
214
|
+
if (this.opts.skipList) {
|
|
215
|
+
const path = this.stackPath.length > 1 ? `${this.cachedPath}.` : "";
|
|
216
|
+
for (const item of this.opts.skipList) if (item.startsWith(path)) {
|
|
217
|
+
const key = item.slice(path.length);
|
|
218
|
+
if (!skipList) skipList = new Set();
|
|
219
|
+
skipList.add(key);
|
|
220
|
+
valueKeys.delete(key);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
let partialFunctionMatched = false;
|
|
224
|
+
if (typeof this.opts.partial === "function") partialFunctionMatched = this.opts.partial(def, this.cachedPath);
|
|
225
|
+
for (const [key, item] of def.type.props.entries()) {
|
|
226
|
+
if (skipList && skipList.has(key) || isPhantomType(item)) continue;
|
|
227
|
+
typeKeys.add(key);
|
|
228
|
+
if (value[key] === undefined) {
|
|
229
|
+
if (partialFunctionMatched || this.opts.partial === "deep" || this.opts.partial === true && this.stackPath.length <= 1) continue;
|
|
230
|
+
}
|
|
231
|
+
this.push(key);
|
|
232
|
+
if (this.validateSafe(item, value[key])) this.pop(false);
|
|
233
|
+
else {
|
|
234
|
+
passed = false;
|
|
235
|
+
this.pop(true);
|
|
236
|
+
if (this.isLimitExceeded()) return false;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
for (const key of valueKeys)
|
|
240
|
+
/** matched patterns for unknown keys */ if (!typeKeys.has(key)) {
|
|
241
|
+
const matched = [];
|
|
242
|
+
for (const { pattern, def: propDef } of def.type.propsPatterns) if (pattern.test(key)) matched.push({
|
|
243
|
+
pattern,
|
|
244
|
+
def: propDef
|
|
245
|
+
});
|
|
246
|
+
if (matched.length > 0) {
|
|
247
|
+
this.push(key);
|
|
248
|
+
let keyPassed = false;
|
|
249
|
+
for (const { def: propDef } of matched) {
|
|
250
|
+
if (this.validateSafe(propDef, value[key])) {
|
|
251
|
+
keyPassed = true;
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
this.clear();
|
|
255
|
+
}
|
|
256
|
+
if (!keyPassed) {
|
|
257
|
+
this.validateSafe(matched[0].def, value[key]);
|
|
258
|
+
this.pop(true);
|
|
259
|
+
passed = false;
|
|
260
|
+
if (this.isLimitExceeded()) return false;
|
|
261
|
+
} else this.pop(false);
|
|
262
|
+
} else if (this.opts.unknownProps !== "ignore") {
|
|
263
|
+
if (this.opts.unknownProps === "error") {
|
|
264
|
+
this.push(key);
|
|
265
|
+
this.error(`Unexpected property`);
|
|
266
|
+
this.pop(true);
|
|
267
|
+
if (this.isLimitExceeded()) return false;
|
|
268
|
+
passed = false;
|
|
269
|
+
} else if (this.opts.unknownProps === "strip") delete value[key];
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return passed;
|
|
273
|
+
}
|
|
274
|
+
validatePrimitive(def, value) {
|
|
275
|
+
if (def.type.value !== undefined) {
|
|
276
|
+
if (value !== def.type.value) {
|
|
277
|
+
this.error(`Expected ${def.type.value}, got ${value}`);
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
const typeOfValue = Array.isArray(value) ? "array" : typeof value;
|
|
283
|
+
switch (def.type.designType) {
|
|
284
|
+
case "never": {
|
|
285
|
+
this.error(`This type is impossible, must be an internal problem`);
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
case "any": return true;
|
|
289
|
+
case "string": {
|
|
290
|
+
if (typeOfValue !== def.type.designType) {
|
|
291
|
+
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
return this.validateString(def, value);
|
|
295
|
+
}
|
|
296
|
+
case "number": {
|
|
297
|
+
if (typeOfValue !== def.type.designType) {
|
|
298
|
+
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
return this.validateNumber(def, value);
|
|
302
|
+
}
|
|
303
|
+
case "boolean": {
|
|
304
|
+
if (typeOfValue !== def.type.designType) {
|
|
305
|
+
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
return this.validateBoolean(def, value);
|
|
309
|
+
}
|
|
310
|
+
case "undefined": {
|
|
311
|
+
if (value !== undefined) {
|
|
312
|
+
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
case "null": {
|
|
318
|
+
if (value !== null) {
|
|
319
|
+
this.error(`Expected ${def.type.designType}, got ${typeOfValue}`);
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
default: throw new Error(`Unknown type "${def.type.designType}"`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
validateString(def, value) {
|
|
328
|
+
const filled = def.metadata.get("meta.required");
|
|
329
|
+
if (filled) {
|
|
330
|
+
if (value.trim().length === 0) {
|
|
331
|
+
const message = typeof filled === "object" && filled.message ? filled.message : `Must not be empty`;
|
|
332
|
+
this.error(message);
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const minLength = def.metadata.get("expect.minLength");
|
|
337
|
+
if (minLength) {
|
|
338
|
+
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
339
|
+
if (value.length < length) {
|
|
340
|
+
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
|
|
341
|
+
this.error(message);
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const maxLength = def.metadata.get("expect.maxLength");
|
|
346
|
+
if (maxLength) {
|
|
347
|
+
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
348
|
+
if (value.length > length) {
|
|
349
|
+
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
|
|
350
|
+
this.error(message);
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const patterns = def.metadata.get("expect.pattern");
|
|
355
|
+
for (const { pattern, flags, message } of patterns || []) {
|
|
356
|
+
if (!pattern) continue;
|
|
357
|
+
const cacheKey = `${pattern}//${flags || ""}`;
|
|
358
|
+
let regex = regexCache.get(cacheKey);
|
|
359
|
+
if (!regex) {
|
|
360
|
+
regex = new RegExp(pattern, flags);
|
|
361
|
+
regexCache.set(cacheKey, regex);
|
|
362
|
+
}
|
|
363
|
+
if (!regex.test(value)) {
|
|
364
|
+
this.error(message || `Value is expected to match pattern "${pattern}"`);
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
validateNumber(def, value) {
|
|
371
|
+
const int = def.metadata.get("expect.int");
|
|
372
|
+
if (int && value % 1 !== 0) {
|
|
373
|
+
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
374
|
+
this.error(message);
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
const min = def.metadata.get("expect.min");
|
|
378
|
+
if (min) {
|
|
379
|
+
const minValue = typeof min === "number" ? min : min.minValue;
|
|
380
|
+
if (value < minValue) {
|
|
381
|
+
const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
|
|
382
|
+
this.error(message);
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
const max = def.metadata.get("expect.max");
|
|
387
|
+
if (max) {
|
|
388
|
+
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
389
|
+
if (value > maxValue) {
|
|
390
|
+
const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
|
|
391
|
+
this.error(message);
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return true;
|
|
396
|
+
}
|
|
397
|
+
validateBoolean(def, value) {
|
|
398
|
+
const filled = def.metadata.get("meta.required");
|
|
399
|
+
if (filled) {
|
|
400
|
+
if (value !== true) {
|
|
401
|
+
const message = typeof filled === "object" && filled.message ? filled.message : `Must be checked`;
|
|
402
|
+
this.error(message);
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return true;
|
|
407
|
+
}
|
|
408
|
+
constructor(def, opts) {
|
|
409
|
+
_define_property(this, "def", void 0);
|
|
410
|
+
_define_property(this, "opts", void 0);
|
|
411
|
+
/** Validation errors collected during the last {@link validate} call. */ _define_property(this, "errors", void 0);
|
|
412
|
+
_define_property(this, "stackErrors", void 0);
|
|
413
|
+
_define_property(this, "stackPath", void 0);
|
|
414
|
+
_define_property(this, "cachedPath", void 0);
|
|
415
|
+
_define_property(this, "context", void 0);
|
|
416
|
+
this.def = def;
|
|
417
|
+
this.errors = [];
|
|
418
|
+
this.stackErrors = [];
|
|
419
|
+
this.stackPath = [];
|
|
420
|
+
this.cachedPath = "";
|
|
421
|
+
this.opts = {
|
|
422
|
+
partial: false,
|
|
423
|
+
unknownProps: "error",
|
|
424
|
+
errorLimit: 10,
|
|
425
|
+
...opts,
|
|
426
|
+
plugins: opts?.plugins || []
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
var ValidatorError = class extends Error {
|
|
431
|
+
constructor(errors) {
|
|
432
|
+
super(`${errors[0].path ? errors[0].path + ": " : ""}${errors[0].message}`), _define_property(this, "errors", void 0), _define_property(this, "name", void 0), this.errors = errors, this.name = "Validation Error";
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
//#endregion
|
|
437
|
+
//#region packages/typescript/src/annotated-type.ts
|
|
438
|
+
const COMPLEX_KINDS = new Set([
|
|
439
|
+
"union",
|
|
440
|
+
"intersection",
|
|
441
|
+
"tuple"
|
|
442
|
+
]);
|
|
443
|
+
const NON_PRIMITIVE_KINDS = new Set(["array", "object"]);
|
|
444
|
+
/** Shared validator method reused by all annotated type nodes. */ function validatorMethod(opts) {
|
|
445
|
+
return new Validator(this, opts);
|
|
446
|
+
}
|
|
447
|
+
function createAnnotatedTypeNode(type, metadata, opts) {
|
|
448
|
+
return {
|
|
449
|
+
__is_atscript_annotated_type: true,
|
|
450
|
+
type,
|
|
451
|
+
metadata,
|
|
452
|
+
validator: validatorMethod,
|
|
453
|
+
id: opts?.id,
|
|
454
|
+
optional: opts?.optional,
|
|
455
|
+
ref: opts?.ref
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
function isAnnotatedType(type) {
|
|
459
|
+
return type && type.__is_atscript_annotated_type;
|
|
460
|
+
}
|
|
461
|
+
function annotate(metadata, key, value, asArray) {
|
|
462
|
+
if (!metadata) return;
|
|
463
|
+
if (asArray) if (metadata.has(key)) {
|
|
464
|
+
const a = metadata.get(key);
|
|
465
|
+
if (Array.isArray(a)) a.push(value);
|
|
466
|
+
else metadata.set(key, [a, value]);
|
|
467
|
+
} else metadata.set(key, [value]);
|
|
468
|
+
else metadata.set(key, value);
|
|
469
|
+
}
|
|
470
|
+
function cloneRefProp(parentType, propName) {
|
|
471
|
+
if (parentType.kind !== "object") return;
|
|
472
|
+
const objType = parentType;
|
|
473
|
+
const existing = objType.props.get(propName);
|
|
474
|
+
if (!existing) return;
|
|
475
|
+
const clonedType = cloneTypeDef(existing.type);
|
|
476
|
+
objType.props.set(propName, createAnnotatedTypeNode(clonedType, new Map(existing.metadata), {
|
|
477
|
+
id: existing.id,
|
|
478
|
+
optional: existing.optional
|
|
479
|
+
}));
|
|
480
|
+
}
|
|
481
|
+
function cloneTypeDef(type) {
|
|
482
|
+
if (type.kind === "object") {
|
|
483
|
+
const obj = type;
|
|
484
|
+
const props = new Map();
|
|
485
|
+
for (const [k, v] of obj.props) props.set(k, createAnnotatedTypeNode(v.type, new Map(v.metadata), {
|
|
486
|
+
id: v.id,
|
|
487
|
+
optional: v.optional
|
|
488
|
+
}));
|
|
489
|
+
return {
|
|
490
|
+
kind: "object",
|
|
491
|
+
props,
|
|
492
|
+
propsPatterns: [...obj.propsPatterns],
|
|
493
|
+
tags: new Set(obj.tags)
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
if (type.kind === "array") {
|
|
497
|
+
const arr = type;
|
|
498
|
+
return {
|
|
499
|
+
kind: "array",
|
|
500
|
+
of: arr.of,
|
|
501
|
+
tags: new Set(arr.tags)
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
if (type.kind === "union" || type.kind === "intersection" || type.kind === "tuple") {
|
|
505
|
+
const complex = type;
|
|
506
|
+
return {
|
|
507
|
+
kind: type.kind,
|
|
508
|
+
items: [...complex.items],
|
|
509
|
+
tags: new Set(complex.tags)
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
return {
|
|
513
|
+
...type,
|
|
514
|
+
tags: new Set(type.tags)
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
function defineAnnotatedType(_kind, base) {
|
|
518
|
+
const kind = _kind || "";
|
|
519
|
+
const type = base?.type || {};
|
|
520
|
+
type.kind = kind;
|
|
521
|
+
if (COMPLEX_KINDS.has(kind)) type.items = [];
|
|
522
|
+
if (kind === "object") {
|
|
523
|
+
type.props = new Map();
|
|
524
|
+
type.propsPatterns = [];
|
|
525
|
+
}
|
|
526
|
+
type.tags = new Set();
|
|
527
|
+
const metadata = base?.metadata || new Map();
|
|
528
|
+
const payload = {
|
|
529
|
+
__is_atscript_annotated_type: true,
|
|
530
|
+
metadata,
|
|
531
|
+
type,
|
|
532
|
+
validator: validatorMethod
|
|
533
|
+
};
|
|
534
|
+
base = base ? Object.assign(base, payload) : payload;
|
|
535
|
+
const handle = {
|
|
536
|
+
$type: base,
|
|
537
|
+
$def: type,
|
|
538
|
+
$metadata: metadata,
|
|
539
|
+
tags(...tags) {
|
|
540
|
+
for (const tag of tags) this.$def.tags.add(tag);
|
|
541
|
+
return this;
|
|
542
|
+
},
|
|
543
|
+
designType(value) {
|
|
544
|
+
this.$def.designType = value;
|
|
545
|
+
return this;
|
|
546
|
+
},
|
|
547
|
+
value(value) {
|
|
548
|
+
this.$def.value = value;
|
|
549
|
+
return this;
|
|
550
|
+
},
|
|
551
|
+
of(value) {
|
|
552
|
+
this.$def.of = value;
|
|
553
|
+
return this;
|
|
554
|
+
},
|
|
555
|
+
item(value) {
|
|
556
|
+
this.$def.items.push(value);
|
|
557
|
+
return this;
|
|
558
|
+
},
|
|
559
|
+
prop(name, value) {
|
|
560
|
+
this.$def.props.set(name, value);
|
|
561
|
+
return this;
|
|
562
|
+
},
|
|
563
|
+
propPattern(pattern, def) {
|
|
564
|
+
this.$def.propsPatterns.push({
|
|
565
|
+
pattern,
|
|
566
|
+
def
|
|
567
|
+
});
|
|
568
|
+
return this;
|
|
569
|
+
},
|
|
570
|
+
optional(value = true) {
|
|
571
|
+
this.$type.optional = value;
|
|
572
|
+
return this;
|
|
573
|
+
},
|
|
574
|
+
copyMetadata(fromMetadata, ignore) {
|
|
575
|
+
for (const [key, value] of fromMetadata.entries()) if (!ignore || !ignore.has(key)) this.$metadata.set(key, value);
|
|
576
|
+
return this;
|
|
577
|
+
},
|
|
578
|
+
refTo(type$1, chain) {
|
|
579
|
+
if (isAnnotatedType(type$1)) {
|
|
580
|
+
let newBase = type$1;
|
|
581
|
+
const typeName = type$1.name || "Unknown";
|
|
582
|
+
if (chain) for (let i = 0; i < chain.length; i++) {
|
|
583
|
+
const c = chain[i];
|
|
584
|
+
if (newBase.type.kind === "object" && newBase.type.props.has(c)) newBase = newBase.type.props.get(c);
|
|
585
|
+
else {
|
|
586
|
+
const keys = chain.slice(0, i + 1).map((k) => `["${k}"]`).join("");
|
|
587
|
+
throw new Error(`Can't find prop ${typeName}${keys}`);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
this.$type = createAnnotatedTypeNode(newBase.type, metadata, {
|
|
591
|
+
id: newBase.id,
|
|
592
|
+
ref: chain && chain.length > 0 ? {
|
|
593
|
+
type: () => type$1,
|
|
594
|
+
field: chain.join(".")
|
|
595
|
+
} : undefined
|
|
596
|
+
});
|
|
597
|
+
} else if (typeof type$1 === "function") {
|
|
598
|
+
const lazyType = type$1;
|
|
599
|
+
this.$type = createAnnotatedTypeNode({ kind: "" }, metadata, { ref: {
|
|
600
|
+
type: lazyType,
|
|
601
|
+
field: chain ? chain.join(".") : ""
|
|
602
|
+
} });
|
|
603
|
+
const node = this.$type;
|
|
604
|
+
const placeholder = node.type;
|
|
605
|
+
Object.defineProperty(node, "type", {
|
|
606
|
+
get() {
|
|
607
|
+
const t = lazyType();
|
|
608
|
+
if (!isAnnotatedType(t)) {
|
|
609
|
+
Object.defineProperty(node, "type", {
|
|
610
|
+
value: placeholder,
|
|
611
|
+
writable: false,
|
|
612
|
+
configurable: true
|
|
613
|
+
});
|
|
614
|
+
return placeholder;
|
|
615
|
+
}
|
|
616
|
+
let target = t;
|
|
617
|
+
if (chain) for (const c of chain) if (target.type.kind === "object" && target.type.props.has(c)) target = target.type.props.get(c);
|
|
618
|
+
else return t.type;
|
|
619
|
+
node.id = chain ? target.id : target.id || t.id;
|
|
620
|
+
Object.defineProperty(node, "type", {
|
|
621
|
+
value: target.type,
|
|
622
|
+
writable: false,
|
|
623
|
+
configurable: true
|
|
624
|
+
});
|
|
625
|
+
return target.type;
|
|
626
|
+
},
|
|
627
|
+
configurable: true
|
|
628
|
+
});
|
|
629
|
+
} else throw new TypeError(`${type$1} is not annotated type`);
|
|
630
|
+
return this;
|
|
631
|
+
},
|
|
632
|
+
annotate(key, value, asArray) {
|
|
633
|
+
annotate(this.$metadata, key, value, asArray);
|
|
634
|
+
return this;
|
|
635
|
+
},
|
|
636
|
+
id(value) {
|
|
637
|
+
this.$type.id = value;
|
|
638
|
+
return this;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
return handle;
|
|
642
|
+
}
|
|
643
|
+
function isPhantomType(def) {
|
|
644
|
+
return def.type.kind === "" && def.type.designType === "phantom";
|
|
645
|
+
}
|
|
646
|
+
function isAnnotatedTypeOfPrimitive(t) {
|
|
647
|
+
if (NON_PRIMITIVE_KINDS.has(t.type.kind)) return false;
|
|
648
|
+
if (!t.type.kind) return true;
|
|
649
|
+
if (COMPLEX_KINDS.has(t.type.kind)) {
|
|
650
|
+
for (const item of t.type.items) if (!isAnnotatedTypeOfPrimitive(item)) return false;
|
|
651
|
+
return true;
|
|
652
|
+
}
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
//#endregion
|
|
657
|
+
//#region packages/typescript/src/traverse.ts
|
|
658
|
+
function forAnnotatedType(def, handlers) {
|
|
659
|
+
switch (def.type.kind) {
|
|
660
|
+
case "": {
|
|
661
|
+
const typed = def;
|
|
662
|
+
if (handlers.phantom && typed.type.designType === "phantom") return handlers.phantom(typed);
|
|
663
|
+
return handlers.final(typed);
|
|
664
|
+
}
|
|
665
|
+
case "object": return handlers.object(def);
|
|
666
|
+
case "array": return handlers.array(def);
|
|
667
|
+
case "union": return handlers.union(def);
|
|
668
|
+
case "intersection": return handlers.intersection(def);
|
|
669
|
+
case "tuple": return handlers.tuple(def);
|
|
670
|
+
default: throw new Error(`Unknown type kind "${def.type.kind}"`);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
//#endregion
|
|
675
|
+
//#region packages/typescript/src/json-schema.ts
|
|
676
|
+
/**
|
|
677
|
+
* Detects a discriminator property across union items.
|
|
678
|
+
*
|
|
679
|
+
* Scans all items for object-typed members that share a common property
|
|
680
|
+
* with distinct const/literal values. If exactly one such property exists,
|
|
681
|
+
* it is returned as the discriminator.
|
|
682
|
+
*/ function detectDiscriminator(items) {
|
|
683
|
+
if (items.length < 2) return null;
|
|
684
|
+
for (const item of items) if (item.type.kind !== "object") return null;
|
|
685
|
+
const firstObj = items[0].type;
|
|
686
|
+
const candidates = [];
|
|
687
|
+
for (const [propName, propType] of firstObj.props.entries()) if (propType.type.kind === "" && propType.type.value !== undefined) candidates.push(propName);
|
|
688
|
+
let result = null;
|
|
689
|
+
for (const candidate of candidates) {
|
|
690
|
+
const values = new Set();
|
|
691
|
+
const indexMapping = {};
|
|
692
|
+
let valid = true;
|
|
693
|
+
for (let i = 0; i < items.length; i++) {
|
|
694
|
+
const obj = items[i].type;
|
|
695
|
+
const prop = obj.props.get(candidate);
|
|
696
|
+
if (!prop || prop.type.kind !== "" || prop.type.value === undefined) {
|
|
697
|
+
valid = false;
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
const val = prop.type.value;
|
|
701
|
+
if (values.has(val)) {
|
|
702
|
+
valid = false;
|
|
703
|
+
break;
|
|
704
|
+
}
|
|
705
|
+
values.add(val);
|
|
706
|
+
indexMapping[String(val)] = i;
|
|
707
|
+
}
|
|
708
|
+
if (valid) {
|
|
709
|
+
if (result) return null;
|
|
710
|
+
result = {
|
|
711
|
+
propertyName: candidate,
|
|
712
|
+
indexMapping
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return result;
|
|
717
|
+
}
|
|
718
|
+
function buildJsonSchema(type) {
|
|
719
|
+
const defs = {};
|
|
720
|
+
let hasDefs = false;
|
|
721
|
+
const buildObject = (d) => {
|
|
722
|
+
const properties = {};
|
|
723
|
+
const required = [];
|
|
724
|
+
for (const [key, val] of d.type.props.entries()) {
|
|
725
|
+
if (isPhantomType(val)) continue;
|
|
726
|
+
properties[key] = build(val);
|
|
727
|
+
if (!val.optional) required.push(key);
|
|
728
|
+
}
|
|
729
|
+
const schema$1 = {
|
|
730
|
+
type: "object",
|
|
731
|
+
properties
|
|
732
|
+
};
|
|
733
|
+
if (required.length > 0) schema$1.required = required;
|
|
734
|
+
return schema$1;
|
|
735
|
+
};
|
|
736
|
+
const build = (def) => {
|
|
737
|
+
if (def.id && def.type.kind === "object" && def !== type) {
|
|
738
|
+
const name = def.id;
|
|
739
|
+
if (!defs[name]) {
|
|
740
|
+
hasDefs = true;
|
|
741
|
+
defs[name] = {};
|
|
742
|
+
defs[name] = buildObject(def);
|
|
743
|
+
}
|
|
744
|
+
return { $ref: `#/$defs/${name}` };
|
|
745
|
+
}
|
|
746
|
+
const meta = def.metadata;
|
|
747
|
+
return forAnnotatedType(def, {
|
|
748
|
+
phantom() {
|
|
749
|
+
return {};
|
|
750
|
+
},
|
|
751
|
+
object(d) {
|
|
752
|
+
return buildObject(d);
|
|
753
|
+
},
|
|
754
|
+
array(d) {
|
|
755
|
+
const schema$1 = {
|
|
756
|
+
type: "array",
|
|
757
|
+
items: build(d.type.of)
|
|
758
|
+
};
|
|
759
|
+
const minLength = meta.get("expect.minLength");
|
|
760
|
+
if (minLength) schema$1.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
761
|
+
const maxLength = meta.get("expect.maxLength");
|
|
762
|
+
if (maxLength) schema$1.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
763
|
+
return schema$1;
|
|
764
|
+
},
|
|
765
|
+
union(d) {
|
|
766
|
+
const disc = detectDiscriminator(d.type.items);
|
|
767
|
+
if (disc) {
|
|
768
|
+
const oneOf = d.type.items.map(build);
|
|
769
|
+
const mapping = {};
|
|
770
|
+
for (const [val, idx] of Object.entries(disc.indexMapping)) {
|
|
771
|
+
const item = d.type.items[idx];
|
|
772
|
+
mapping[val] = item.id && defs[item.id] ? `#/$defs/${item.id}` : `#/oneOf/${idx}`;
|
|
773
|
+
}
|
|
774
|
+
return {
|
|
775
|
+
oneOf,
|
|
776
|
+
discriminator: {
|
|
777
|
+
propertyName: disc.propertyName,
|
|
778
|
+
mapping
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
return { anyOf: d.type.items.map(build) };
|
|
783
|
+
},
|
|
784
|
+
intersection(d) {
|
|
785
|
+
return { allOf: d.type.items.map(build) };
|
|
786
|
+
},
|
|
787
|
+
tuple(d) {
|
|
788
|
+
return {
|
|
789
|
+
type: "array",
|
|
790
|
+
items: d.type.items.map(build),
|
|
791
|
+
additionalItems: false
|
|
792
|
+
};
|
|
793
|
+
},
|
|
794
|
+
final(d) {
|
|
795
|
+
const schema$1 = {};
|
|
796
|
+
if (d.type.value !== undefined) schema$1.const = d.type.value;
|
|
797
|
+
if (d.type.designType && d.type.designType !== "any") {
|
|
798
|
+
schema$1.type = d.type.designType === "undefined" ? "null" : d.type.designType;
|
|
799
|
+
if (schema$1.type === "number" && meta.get("expect.int")) schema$1.type = "integer";
|
|
800
|
+
}
|
|
801
|
+
if (schema$1.type === "string") {
|
|
802
|
+
if (meta.get("meta.required")) schema$1.minLength = 1;
|
|
803
|
+
const minLength = meta.get("expect.minLength");
|
|
804
|
+
if (minLength) schema$1.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
805
|
+
const maxLength = meta.get("expect.maxLength");
|
|
806
|
+
if (maxLength) schema$1.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
807
|
+
const patterns = meta.get("expect.pattern");
|
|
808
|
+
if (patterns?.length) if (patterns.length === 1) schema$1.pattern = patterns[0].pattern;
|
|
809
|
+
else schema$1.allOf = (schema$1.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
810
|
+
}
|
|
811
|
+
if (schema$1.type === "number" || schema$1.type === "integer") {
|
|
812
|
+
const min = meta.get("expect.min");
|
|
813
|
+
if (min) schema$1.minimum = typeof min === "number" ? min : min.minValue;
|
|
814
|
+
const max = meta.get("expect.max");
|
|
815
|
+
if (max) schema$1.maximum = typeof max === "number" ? max : max.maxValue;
|
|
816
|
+
}
|
|
817
|
+
return schema$1;
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
};
|
|
821
|
+
const schema = build(type);
|
|
822
|
+
if (hasDefs) return {
|
|
823
|
+
...schema,
|
|
824
|
+
$defs: defs
|
|
825
|
+
};
|
|
826
|
+
return schema;
|
|
827
|
+
}
|
|
828
|
+
function fromJsonSchema(schema) {
|
|
829
|
+
const defsSource = schema.$defs || schema.definitions || {};
|
|
830
|
+
const resolved = new Map();
|
|
831
|
+
const convert = (s) => {
|
|
832
|
+
if (!s || Object.keys(s).length === 0) return defineAnnotatedType().designType("any").$type;
|
|
833
|
+
if (s.$ref) {
|
|
834
|
+
const refName = s.$ref.replace(/^#\/(\$defs|definitions)\//, "");
|
|
835
|
+
if (resolved.has(refName)) return resolved.get(refName);
|
|
836
|
+
if (defsSource[refName]) {
|
|
837
|
+
const placeholder = defineAnnotatedType().designType("any").$type;
|
|
838
|
+
resolved.set(refName, placeholder);
|
|
839
|
+
const type = convert(defsSource[refName]);
|
|
840
|
+
resolved.set(refName, type);
|
|
841
|
+
return type;
|
|
842
|
+
}
|
|
843
|
+
throw new Error(`Unresolvable $ref: ${s.$ref}`);
|
|
844
|
+
}
|
|
845
|
+
if ("const" in s) {
|
|
846
|
+
const val = s.const;
|
|
847
|
+
const dt = val === null ? "null" : typeof val;
|
|
848
|
+
return defineAnnotatedType().designType(dt).value(val).$type;
|
|
849
|
+
}
|
|
850
|
+
if (s.enum) {
|
|
851
|
+
const handle = defineAnnotatedType("union");
|
|
852
|
+
for (const val of s.enum) {
|
|
853
|
+
const dt = val === null ? "null" : typeof val;
|
|
854
|
+
handle.item(defineAnnotatedType().designType(dt).value(val).$type);
|
|
855
|
+
}
|
|
856
|
+
return handle.$type;
|
|
857
|
+
}
|
|
858
|
+
if (s.anyOf) {
|
|
859
|
+
const handle = defineAnnotatedType("union");
|
|
860
|
+
for (const item of s.anyOf) handle.item(convert(item));
|
|
861
|
+
return handle.$type;
|
|
862
|
+
}
|
|
863
|
+
if (s.oneOf) {
|
|
864
|
+
const handle = defineAnnotatedType("union");
|
|
865
|
+
for (const item of s.oneOf) handle.item(convert(item));
|
|
866
|
+
return handle.$type;
|
|
867
|
+
}
|
|
868
|
+
if (s.allOf && !s.type) {
|
|
869
|
+
const handle = defineAnnotatedType("intersection");
|
|
870
|
+
for (const item of s.allOf) handle.item(convert(item));
|
|
871
|
+
return handle.$type;
|
|
872
|
+
}
|
|
873
|
+
if (Array.isArray(s.type)) {
|
|
874
|
+
const handle = defineAnnotatedType("union");
|
|
875
|
+
for (const t of s.type) handle.item(convert({
|
|
876
|
+
...s,
|
|
877
|
+
type: t
|
|
878
|
+
}));
|
|
879
|
+
return handle.$type;
|
|
880
|
+
}
|
|
881
|
+
if (s.type === "object") {
|
|
882
|
+
const handle = defineAnnotatedType("object");
|
|
883
|
+
const required = new Set(s.required || []);
|
|
884
|
+
if (s.properties) for (const [key, propSchema] of Object.entries(s.properties)) {
|
|
885
|
+
const propType = convert(propSchema);
|
|
886
|
+
if (!required.has(key)) propType.optional = true;
|
|
887
|
+
handle.prop(key, propType);
|
|
888
|
+
}
|
|
889
|
+
return handle.$type;
|
|
890
|
+
}
|
|
891
|
+
if (s.type === "array") {
|
|
892
|
+
if (Array.isArray(s.items)) {
|
|
893
|
+
const handle$1 = defineAnnotatedType("tuple");
|
|
894
|
+
for (const item of s.items) handle$1.item(convert(item));
|
|
895
|
+
return handle$1.$type;
|
|
896
|
+
}
|
|
897
|
+
const itemType = s.items ? convert(s.items) : defineAnnotatedType().designType("any").$type;
|
|
898
|
+
const handle = defineAnnotatedType("array").of(itemType);
|
|
899
|
+
if (typeof s.minItems === "number") handle.annotate("expect.minLength", { length: s.minItems });
|
|
900
|
+
if (typeof s.maxItems === "number") handle.annotate("expect.maxLength", { length: s.maxItems });
|
|
901
|
+
return handle.$type;
|
|
902
|
+
}
|
|
903
|
+
if (s.type === "string") {
|
|
904
|
+
const handle = defineAnnotatedType().designType("string").tags("string");
|
|
905
|
+
if (typeof s.minLength === "number") handle.annotate("expect.minLength", { length: s.minLength });
|
|
906
|
+
if (typeof s.maxLength === "number") handle.annotate("expect.maxLength", { length: s.maxLength });
|
|
907
|
+
if (s.pattern) handle.annotate("expect.pattern", { pattern: s.pattern }, true);
|
|
908
|
+
if (s.allOf) {
|
|
909
|
+
for (const item of s.allOf) if (item.pattern) handle.annotate("expect.pattern", { pattern: item.pattern }, true);
|
|
910
|
+
}
|
|
911
|
+
return handle.$type;
|
|
912
|
+
}
|
|
913
|
+
if (s.type === "integer") {
|
|
914
|
+
const handle = defineAnnotatedType().designType("number").tags("number");
|
|
915
|
+
handle.annotate("expect.int", true);
|
|
916
|
+
if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
|
|
917
|
+
if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
|
|
918
|
+
return handle.$type;
|
|
919
|
+
}
|
|
920
|
+
if (s.type === "number") {
|
|
921
|
+
const handle = defineAnnotatedType().designType("number").tags("number");
|
|
922
|
+
if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
|
|
923
|
+
if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
|
|
924
|
+
return handle.$type;
|
|
925
|
+
}
|
|
926
|
+
if (s.type === "boolean") return defineAnnotatedType().designType("boolean").tags("boolean").$type;
|
|
927
|
+
if (s.type === "null") return defineAnnotatedType().designType("null").tags("null").$type;
|
|
928
|
+
return defineAnnotatedType().designType("any").$type;
|
|
929
|
+
};
|
|
930
|
+
return convert(schema);
|
|
931
|
+
}
|
|
932
|
+
function mergeJsonSchemas(types) {
|
|
933
|
+
const mergedDefs = {};
|
|
934
|
+
const schemas = {};
|
|
935
|
+
for (const type of types) {
|
|
936
|
+
const name = type.id;
|
|
937
|
+
if (!name) throw new Error("mergeJsonSchemas: all types must have an id");
|
|
938
|
+
const schema = buildJsonSchema(type);
|
|
939
|
+
if (schema.$defs) {
|
|
940
|
+
for (const [defName, defSchema] of Object.entries(schema.$defs)) if (!mergedDefs[defName]) mergedDefs[defName] = defSchema;
|
|
941
|
+
const { $defs: _,...rest } = schema;
|
|
942
|
+
schemas[name] = rest;
|
|
943
|
+
} else schemas[name] = schema;
|
|
944
|
+
}
|
|
945
|
+
return {
|
|
946
|
+
schemas,
|
|
947
|
+
$defs: mergedDefs
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
//#endregion
|
|
952
|
+
export { Validator, ValidatorError, annotate, buildJsonSchema, cloneRefProp, createAnnotatedTypeNode, defineAnnotatedType, forAnnotatedType, fromJsonSchema, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, mergeJsonSchemas };
|