@atscript/typescript 0.1.102 → 0.1.104
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/dist/cli.cjs +245 -79
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{json-schema-CW_50s1W.mjs → json-schema-BGYiqs-J.mjs} +202 -69
- package/dist/{json-schema-B9xrfeoQ.cjs → json-schema-L8xJQiLc.cjs} +202 -69
- package/dist/{plugin-CECXHL6M.mjs → plugin-Crcxc9aq.mjs} +44 -11
- package/dist/{plugin-DORafTJq.cjs → plugin-gpq_ji-_.cjs} +44 -11
- package/dist/test-utils.cjs +1 -1
- package/dist/test-utils.mjs +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.ts +14 -0
- package/dist/utils.mjs +1 -1
- package/package.json +2 -2
|
@@ -47,8 +47,25 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
47
47
|
}
|
|
48
48
|
return regex;
|
|
49
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
|
|
52
|
+
* type does not itself accept `null`. Shared by the walk and the fast pre-check.
|
|
53
|
+
*/ function rejectsNullAsRequired(def) {
|
|
54
|
+
return def.metadata.size > 0 && !!def.metadata.get("meta.required") && !acceptsNull(def);
|
|
55
|
+
}
|
|
56
|
+
function acceptsNull(def) {
|
|
57
|
+
const type = def.type;
|
|
58
|
+
if (type.kind === "") {
|
|
59
|
+
const designType = type.designType;
|
|
60
|
+
return designType === "null" || designType === "any";
|
|
61
|
+
}
|
|
62
|
+
if (type.kind === "union") {
|
|
63
|
+
for (const item of type.items) if (item.optional || acceptsNull(item)) return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
50
67
|
function fastCheck(def, value, flags, opts) {
|
|
51
|
-
if (def.optional && (value === void 0 || value === null)) return PASS;
|
|
68
|
+
if (def.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(def) ? FAIL : PASS;
|
|
52
69
|
const type = def.type;
|
|
53
70
|
switch (type.kind) {
|
|
54
71
|
case "": return fastCheckFinal(def, value);
|
|
@@ -58,7 +75,7 @@ function fastCheck(def, value, flags, opts) {
|
|
|
58
75
|
const branchFlags = flags | IN_UNION;
|
|
59
76
|
for (const item of type.items) {
|
|
60
77
|
const result = fastCheck(item, value, branchFlags, opts);
|
|
61
|
-
if (result !== FAIL) return result;
|
|
78
|
+
if (result !== FAIL) return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(def.metadata, value) : result;
|
|
62
79
|
}
|
|
63
80
|
return FAIL;
|
|
64
81
|
}
|
|
@@ -87,12 +104,9 @@ function fastCheckFinal(def, value) {
|
|
|
87
104
|
if (designType === "phantom") return PASS;
|
|
88
105
|
if (type.value !== void 0) return value === type.value ? PASS : FAIL;
|
|
89
106
|
switch (designType) {
|
|
90
|
-
case "string": return typeof value === "string" ? fastCheckString(def, value) : FAIL;
|
|
91
|
-
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def, value) : FAIL;
|
|
92
|
-
case "boolean":
|
|
93
|
-
if (typeof value !== "boolean") return FAIL;
|
|
94
|
-
if (def.metadata.size === 0) return PASS;
|
|
95
|
-
return def.metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
107
|
+
case "string": return typeof value === "string" ? fastCheckString(def.metadata, value) : FAIL;
|
|
108
|
+
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def.metadata, value) : FAIL;
|
|
109
|
+
case "boolean": return typeof value === "boolean" ? fastCheckBoolean(def.metadata, value) : FAIL;
|
|
96
110
|
case "any": return PASS;
|
|
97
111
|
case "never": return FAIL;
|
|
98
112
|
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
@@ -101,8 +115,24 @@ function fastCheckFinal(def, value) {
|
|
|
101
115
|
default: return BAIL;
|
|
102
116
|
}
|
|
103
117
|
}
|
|
104
|
-
|
|
105
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
|
|
120
|
+
* (`null` / objects pass; the caller skips literal branches).
|
|
121
|
+
*/ function fastCheckUnionConstraints(metadata, value) {
|
|
122
|
+
if (metadata.size === 0) return PASS;
|
|
123
|
+
switch (typeof value) {
|
|
124
|
+
case "string": return fastCheckString(metadata, value);
|
|
125
|
+
case "number": return fastCheckNumber(metadata, value);
|
|
126
|
+
case "boolean": return fastCheckBoolean(metadata, value);
|
|
127
|
+
case "object": return Array.isArray(value) ? fastCheckArrayConstraints(metadata, value) : PASS;
|
|
128
|
+
default: return PASS;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function fastCheckBoolean(metadata, value) {
|
|
132
|
+
if (metadata.size === 0) return PASS;
|
|
133
|
+
return metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
134
|
+
}
|
|
135
|
+
function fastCheckString(metadata, value) {
|
|
106
136
|
if (metadata.size === 0) return PASS;
|
|
107
137
|
if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
|
|
108
138
|
const minLength = metadata.get("expect.minLength");
|
|
@@ -118,8 +148,7 @@ function fastCheckString(def, value) {
|
|
|
118
148
|
}
|
|
119
149
|
return PASS;
|
|
120
150
|
}
|
|
121
|
-
function fastCheckNumber(
|
|
122
|
-
const metadata = def.metadata;
|
|
151
|
+
function fastCheckNumber(metadata, value) {
|
|
123
152
|
if (metadata.size === 0) return PASS;
|
|
124
153
|
if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
|
|
125
154
|
const min = metadata.get("expect.min");
|
|
@@ -162,11 +191,8 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
162
191
|
if (!Array.isArray(value)) return FAIL;
|
|
163
192
|
const metadata = def.metadata;
|
|
164
193
|
if (metadata.size > 0) {
|
|
165
|
-
const
|
|
166
|
-
if (
|
|
167
|
-
const maxLength = metadata.get("expect.maxLength");
|
|
168
|
-
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
169
|
-
if (metadata.get("expect.array.uniqueItems")) return BAIL;
|
|
194
|
+
const result = fastCheckArrayConstraints(metadata, value);
|
|
195
|
+
if (result !== PASS) return result;
|
|
170
196
|
}
|
|
171
197
|
const of = def.type.of;
|
|
172
198
|
const inUnion = (flags & IN_UNION) !== 0;
|
|
@@ -181,6 +207,13 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
181
207
|
}
|
|
182
208
|
return result;
|
|
183
209
|
}
|
|
210
|
+
function fastCheckArrayConstraints(metadata, value) {
|
|
211
|
+
const minLength = metadata.get("expect.minLength");
|
|
212
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
213
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
214
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
215
|
+
return metadata.get("expect.array.uniqueItems") ? BAIL : PASS;
|
|
216
|
+
}
|
|
184
217
|
|
|
185
218
|
//#endregion
|
|
186
219
|
//#region packages/typescript/src/runtime/validator.ts
|
|
@@ -296,7 +329,13 @@ function _define_property(obj, key, value) {
|
|
|
296
329
|
}
|
|
297
330
|
def = replaced;
|
|
298
331
|
}
|
|
299
|
-
if (def.optional && (value === void 0 || value === null))
|
|
332
|
+
if (def.optional && (value === void 0 || value === null)) {
|
|
333
|
+
if (value === null && rejectsNullAsRequired(def)) {
|
|
334
|
+
this.error(requiredMessage(def));
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
300
339
|
if (this.hasPlugins) for (const plugin of this.opts.plugins) {
|
|
301
340
|
const result = plugin(this, def, value);
|
|
302
341
|
if (result === false || result === true) return result;
|
|
@@ -331,7 +370,7 @@ function _define_property(obj, key, value) {
|
|
|
331
370
|
this.stackErrors.push(null);
|
|
332
371
|
if (this.validateSafe(item, value)) {
|
|
333
372
|
this.stackErrors.pop();
|
|
334
|
-
return
|
|
373
|
+
return this.validateUnionConstraints(def, item, value);
|
|
335
374
|
}
|
|
336
375
|
const branchErrors = this.stackErrors.pop();
|
|
337
376
|
if (this.limitExceeded) this.limitExceeded = false;
|
|
@@ -342,6 +381,23 @@ function _define_property(obj, key, value) {
|
|
|
342
381
|
this.error(`Value does not match any of the allowed types: ${expected}`, void 0, details);
|
|
343
382
|
return false;
|
|
344
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* Constraints annotated on the union itself (`@expect.min 0` on `number | null`)
|
|
386
|
+
* apply to the non-null value that matched `branch`, on top of the branch's own.
|
|
387
|
+
* A literal branch (an enum value) is exempt: its value is fixed.
|
|
388
|
+
*/ validateUnionConstraints(def, branch, value) {
|
|
389
|
+
const metadata = def.metadata;
|
|
390
|
+
if (metadata.size === 0 || branch.type.value !== void 0) return true;
|
|
391
|
+
switch (typeof value) {
|
|
392
|
+
case "string": return this.validateStringConstraints(metadata, value);
|
|
393
|
+
case "number": return this.validateNumberConstraints(metadata, value);
|
|
394
|
+
case "boolean": return this.validateBooleanConstraints(metadata, value);
|
|
395
|
+
case "object":
|
|
396
|
+
if (Array.isArray(value)) return this.validateArrayConstraints(metadata, value, arrayElementType(branch));
|
|
397
|
+
return true;
|
|
398
|
+
default: return true;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
345
401
|
validateIntersection(def, value) {
|
|
346
402
|
for (const item of def.type.items) if (!this.validateSafe(item, value)) return false;
|
|
347
403
|
return true;
|
|
@@ -368,7 +424,22 @@ function _define_property(obj, key, value) {
|
|
|
368
424
|
this.error("Expected array");
|
|
369
425
|
return false;
|
|
370
426
|
}
|
|
371
|
-
|
|
427
|
+
if (def.metadata.size > 0 && !this.validateArrayConstraints(def.metadata, value, def.type.of)) return false;
|
|
428
|
+
let i = 0;
|
|
429
|
+
let passed = true;
|
|
430
|
+
for (const item of value) {
|
|
431
|
+
this.push(String(i));
|
|
432
|
+
if (!this.validateSafe(def.type.of, item)) {
|
|
433
|
+
passed = false;
|
|
434
|
+
this.pop(true);
|
|
435
|
+
if (this.limitExceeded) return false;
|
|
436
|
+
} else this.pop(false);
|
|
437
|
+
i++;
|
|
438
|
+
}
|
|
439
|
+
return passed;
|
|
440
|
+
}
|
|
441
|
+
/** `@expect.minLength` / `maxLength` / `array.uniqueItems` checks for an array value. */ validateArrayConstraints(metadata, value, of) {
|
|
442
|
+
const minLength = metadata.get("expect.minLength");
|
|
372
443
|
if (minLength !== void 0) {
|
|
373
444
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
374
445
|
if (value.length < length) {
|
|
@@ -377,7 +448,7 @@ function _define_property(obj, key, value) {
|
|
|
377
448
|
return false;
|
|
378
449
|
}
|
|
379
450
|
}
|
|
380
|
-
const maxLength =
|
|
451
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
381
452
|
if (maxLength !== void 0) {
|
|
382
453
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
383
454
|
if (value.length > length) {
|
|
@@ -386,13 +457,13 @@ function _define_property(obj, key, value) {
|
|
|
386
457
|
return false;
|
|
387
458
|
}
|
|
388
459
|
}
|
|
389
|
-
const uniqueItems =
|
|
460
|
+
const uniqueItems = metadata.get("expect.array.uniqueItems");
|
|
390
461
|
if (uniqueItems) {
|
|
391
462
|
const separator = "▼↩";
|
|
392
463
|
const seen = /* @__PURE__ */ new Set();
|
|
393
464
|
const keyProps = /* @__PURE__ */ new Set();
|
|
394
|
-
if (
|
|
395
|
-
for (const [key, val] of
|
|
465
|
+
if (of?.type.kind === "object") {
|
|
466
|
+
for (const [key, val] of of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
|
|
396
467
|
}
|
|
397
468
|
for (let idx = 0; idx < value.length; idx++) {
|
|
398
469
|
const item = value[idx];
|
|
@@ -410,18 +481,7 @@ function _define_property(obj, key, value) {
|
|
|
410
481
|
seen.add(key);
|
|
411
482
|
}
|
|
412
483
|
}
|
|
413
|
-
|
|
414
|
-
let passed = true;
|
|
415
|
-
for (const item of value) {
|
|
416
|
-
this.push(String(i));
|
|
417
|
-
if (!this.validateSafe(def.type.of, item)) {
|
|
418
|
-
passed = false;
|
|
419
|
-
this.pop(true);
|
|
420
|
-
if (this.limitExceeded) return false;
|
|
421
|
-
} else this.pop(false);
|
|
422
|
-
i++;
|
|
423
|
-
}
|
|
424
|
-
return passed;
|
|
484
|
+
return true;
|
|
425
485
|
}
|
|
426
486
|
validateObject(def, value) {
|
|
427
487
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
@@ -559,8 +619,11 @@ function _define_property(obj, key, value) {
|
|
|
559
619
|
}
|
|
560
620
|
}
|
|
561
621
|
validateString(def, value) {
|
|
562
|
-
|
|
563
|
-
|
|
622
|
+
return this.validateStringConstraints(def.metadata, value);
|
|
623
|
+
}
|
|
624
|
+
/** `@meta.required` / `@expect.minLength` / `maxLength` / `pattern` checks for a string value. */ validateStringConstraints(metadata, value) {
|
|
625
|
+
if (metadata.size === 0) return true;
|
|
626
|
+
const filled = metadata.get("meta.required");
|
|
564
627
|
if (filled) {
|
|
565
628
|
if (value.trim().length === 0) {
|
|
566
629
|
const message = typeof filled === "object" && filled.message ? filled.message : `Must not be empty`;
|
|
@@ -568,7 +631,7 @@ function _define_property(obj, key, value) {
|
|
|
568
631
|
return false;
|
|
569
632
|
}
|
|
570
633
|
}
|
|
571
|
-
const minLength =
|
|
634
|
+
const minLength = metadata.get("expect.minLength");
|
|
572
635
|
if (minLength !== void 0) {
|
|
573
636
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
574
637
|
if (value.length < length) {
|
|
@@ -577,7 +640,7 @@ function _define_property(obj, key, value) {
|
|
|
577
640
|
return false;
|
|
578
641
|
}
|
|
579
642
|
}
|
|
580
|
-
const maxLength =
|
|
643
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
581
644
|
if (maxLength !== void 0) {
|
|
582
645
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
583
646
|
if (value.length > length) {
|
|
@@ -586,7 +649,7 @@ function _define_property(obj, key, value) {
|
|
|
586
649
|
return false;
|
|
587
650
|
}
|
|
588
651
|
}
|
|
589
|
-
const patterns =
|
|
652
|
+
const patterns = metadata.get("expect.pattern");
|
|
590
653
|
for (const { pattern, flags, message } of patterns || []) {
|
|
591
654
|
if (!pattern) continue;
|
|
592
655
|
if (!getRegex(pattern, flags).test(value)) {
|
|
@@ -597,14 +660,17 @@ function _define_property(obj, key, value) {
|
|
|
597
660
|
return true;
|
|
598
661
|
}
|
|
599
662
|
validateNumber(def, value) {
|
|
600
|
-
|
|
601
|
-
|
|
663
|
+
return this.validateNumberConstraints(def.metadata, value);
|
|
664
|
+
}
|
|
665
|
+
/** `@expect.int` / `min` / `max` checks for a number value. */ validateNumberConstraints(metadata, value) {
|
|
666
|
+
if (metadata.size === 0) return true;
|
|
667
|
+
const int = metadata.get("expect.int");
|
|
602
668
|
if (int && value % 1 !== 0) {
|
|
603
669
|
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
604
670
|
this.error(message);
|
|
605
671
|
return false;
|
|
606
672
|
}
|
|
607
|
-
const min =
|
|
673
|
+
const min = metadata.get("expect.min");
|
|
608
674
|
if (min !== void 0) {
|
|
609
675
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
610
676
|
if (value < minValue) {
|
|
@@ -613,7 +679,7 @@ function _define_property(obj, key, value) {
|
|
|
613
679
|
return false;
|
|
614
680
|
}
|
|
615
681
|
}
|
|
616
|
-
const max =
|
|
682
|
+
const max = metadata.get("expect.max");
|
|
617
683
|
if (max !== void 0) {
|
|
618
684
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
619
685
|
if (value > maxValue) {
|
|
@@ -625,8 +691,11 @@ function _define_property(obj, key, value) {
|
|
|
625
691
|
return true;
|
|
626
692
|
}
|
|
627
693
|
validateBoolean(def, value) {
|
|
628
|
-
|
|
629
|
-
|
|
694
|
+
return this.validateBooleanConstraints(def.metadata, value);
|
|
695
|
+
}
|
|
696
|
+
/** `@meta.required` check for a boolean value (must be `true`). */ validateBooleanConstraints(metadata, value) {
|
|
697
|
+
if (metadata.size === 0) return true;
|
|
698
|
+
const filled = metadata.get("meta.required");
|
|
630
699
|
if (filled) {
|
|
631
700
|
if (value !== true) {
|
|
632
701
|
const message = typeof filled === "object" && filled.message ? filled.message : `Must be checked`;
|
|
@@ -666,6 +735,23 @@ function _define_property(obj, key, value) {
|
|
|
666
735
|
if (this.hasReplace) this.replaceCache = /* @__PURE__ */ new WeakMap();
|
|
667
736
|
}
|
|
668
737
|
};
|
|
738
|
+
/** The `@meta.required` error message for `def` (custom message or the type's default). */ function requiredMessage(def) {
|
|
739
|
+
const filled = def.metadata.get("meta.required");
|
|
740
|
+
if (typeof filled === "object" && filled.message) return filled.message;
|
|
741
|
+
return def.type.kind === "" && def.type.designType === "boolean" ? "Must be checked" : "Must not be empty";
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Element type of the array a union branch holds — through nested unions too
|
|
745
|
+
* (`Items | undefined` with `type Items = Item[] | null`), so `@expect.array.key`
|
|
746
|
+
* fields of the elements are found.
|
|
747
|
+
*/ function arrayElementType(branch) {
|
|
748
|
+
const type = branch.type;
|
|
749
|
+
if (type.kind === "array") return type.of;
|
|
750
|
+
if (type.kind === "union") for (const item of type.items) {
|
|
751
|
+
const of = arrayElementType(item);
|
|
752
|
+
if (of) return of;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
669
755
|
/** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
|
|
670
756
|
constructor(errors) {
|
|
671
757
|
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";
|
|
@@ -1064,10 +1150,7 @@ function cloneTypeDef(type) {
|
|
|
1064
1150
|
type: "array",
|
|
1065
1151
|
items: build(d.type.of)
|
|
1066
1152
|
};
|
|
1067
|
-
|
|
1068
|
-
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1069
|
-
const maxLength = meta.get("expect.maxLength");
|
|
1070
|
-
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1153
|
+
applyArrayConstraints(schema, meta);
|
|
1071
1154
|
return schema;
|
|
1072
1155
|
},
|
|
1073
1156
|
union(d) {
|
|
@@ -1087,7 +1170,9 @@ function cloneTypeDef(type) {
|
|
|
1087
1170
|
}
|
|
1088
1171
|
};
|
|
1089
1172
|
}
|
|
1090
|
-
|
|
1173
|
+
const anyOf = d.type.items.map(build);
|
|
1174
|
+
if (meta.size > 0) applyUnionConstraints(anyOf, meta);
|
|
1175
|
+
return { anyOf };
|
|
1091
1176
|
},
|
|
1092
1177
|
intersection(d) {
|
|
1093
1178
|
return { allOf: d.type.items.map(build) };
|
|
@@ -1105,24 +1190,8 @@ function cloneTypeDef(type) {
|
|
|
1105
1190
|
if (d.type.designType && d.type.designType !== "any") {
|
|
1106
1191
|
const dt = d.type.designType;
|
|
1107
1192
|
schema.type = dt === "undefined" ? "null" : dt === "decimal" ? "string" : dt;
|
|
1108
|
-
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
1109
|
-
}
|
|
1110
|
-
if (schema.type === "string") {
|
|
1111
|
-
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1112
|
-
const minLength = meta.get("expect.minLength");
|
|
1113
|
-
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1114
|
-
const maxLength = meta.get("expect.maxLength");
|
|
1115
|
-
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1116
|
-
const patterns = meta.get("expect.pattern");
|
|
1117
|
-
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1118
|
-
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1119
|
-
}
|
|
1120
|
-
if (schema.type === "number" || schema.type === "integer") {
|
|
1121
|
-
const min = meta.get("expect.min");
|
|
1122
|
-
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1123
|
-
const max = meta.get("expect.max");
|
|
1124
|
-
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1125
1193
|
}
|
|
1194
|
+
applyScalarConstraints(schema, meta);
|
|
1126
1195
|
return schema;
|
|
1127
1196
|
}
|
|
1128
1197
|
});
|
|
@@ -1134,6 +1203,70 @@ function cloneTypeDef(type) {
|
|
|
1134
1203
|
};
|
|
1135
1204
|
return schema;
|
|
1136
1205
|
}
|
|
1206
|
+
/** `@expect.minLength` / `maxLength` → `minItems` / `maxItems` on an array schema. */ function applyArrayConstraints(schema, meta) {
|
|
1207
|
+
const minLength = meta.get("expect.minLength");
|
|
1208
|
+
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1209
|
+
const maxLength = meta.get("expect.maxLength");
|
|
1210
|
+
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1211
|
+
}
|
|
1212
|
+
/** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
|
|
1213
|
+
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
1214
|
+
if (schema.type === "string") {
|
|
1215
|
+
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1216
|
+
const minLength = meta.get("expect.minLength");
|
|
1217
|
+
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1218
|
+
const maxLength = meta.get("expect.maxLength");
|
|
1219
|
+
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1220
|
+
const patterns = meta.get("expect.pattern");
|
|
1221
|
+
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1222
|
+
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1223
|
+
}
|
|
1224
|
+
if (schema.type === "number" || schema.type === "integer") {
|
|
1225
|
+
const min = meta.get("expect.min");
|
|
1226
|
+
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1227
|
+
const max = meta.get("expect.max");
|
|
1228
|
+
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
const LOWER_BOUNDS = new Set([
|
|
1232
|
+
"minimum",
|
|
1233
|
+
"minLength",
|
|
1234
|
+
"minItems"
|
|
1235
|
+
]);
|
|
1236
|
+
const UPPER_BOUNDS = new Set([
|
|
1237
|
+
"maximum",
|
|
1238
|
+
"maxLength",
|
|
1239
|
+
"maxItems"
|
|
1240
|
+
]);
|
|
1241
|
+
/**
|
|
1242
|
+
* Puts union-level constraints onto the matching `anyOf` members (numbers, strings,
|
|
1243
|
+
* arrays; nested `anyOf` members recursively). When a member already has the same
|
|
1244
|
+
* bound, the stricter one wins; patterns accumulate.
|
|
1245
|
+
*/ function applyUnionConstraints(members, meta) {
|
|
1246
|
+
for (const member of members) {
|
|
1247
|
+
if (Array.isArray(member.anyOf)) {
|
|
1248
|
+
applyUnionConstraints(member.anyOf, meta);
|
|
1249
|
+
continue;
|
|
1250
|
+
}
|
|
1251
|
+
if (member.const !== void 0) continue;
|
|
1252
|
+
const type = member.type;
|
|
1253
|
+
const extra = { type };
|
|
1254
|
+
if (type === "array") applyArrayConstraints(extra, meta);
|
|
1255
|
+
else if (type === "string" || type === "number" || type === "integer") applyScalarConstraints(extra, meta);
|
|
1256
|
+
else continue;
|
|
1257
|
+
if (extra.type === "integer") member.type = "integer";
|
|
1258
|
+
for (const [key, value] of Object.entries(extra)) {
|
|
1259
|
+
if (key === "type") continue;
|
|
1260
|
+
if (key === "pattern" || key === "allOf") {
|
|
1261
|
+
const patterns = key === "pattern" ? [{ pattern: value }] : value;
|
|
1262
|
+
if (member.pattern === void 0 && key === "pattern" && !member.allOf) member.pattern = value;
|
|
1263
|
+
else member.allOf = (member.allOf || []).concat(patterns);
|
|
1264
|
+
} else if (member[key] === void 0) member[key] = value;
|
|
1265
|
+
else if (LOWER_BOUNDS.has(key)) member[key] = Math.max(member[key], value);
|
|
1266
|
+
else if (UPPER_BOUNDS.has(key)) member[key] = Math.min(member[key], value);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1137
1270
|
/**
|
|
1138
1271
|
* Converts a JSON Schema object into a {@link TAtscriptAnnotatedType}.
|
|
1139
1272
|
*
|