@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
|
@@ -48,8 +48,25 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
48
48
|
}
|
|
49
49
|
return regex;
|
|
50
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
|
|
53
|
+
* type does not itself accept `null`. Shared by the walk and the fast pre-check.
|
|
54
|
+
*/ function rejectsNullAsRequired(def) {
|
|
55
|
+
return def.metadata.size > 0 && !!def.metadata.get("meta.required") && !acceptsNull(def);
|
|
56
|
+
}
|
|
57
|
+
function acceptsNull(def) {
|
|
58
|
+
const type = def.type;
|
|
59
|
+
if (type.kind === "") {
|
|
60
|
+
const designType = type.designType;
|
|
61
|
+
return designType === "null" || designType === "any";
|
|
62
|
+
}
|
|
63
|
+
if (type.kind === "union") {
|
|
64
|
+
for (const item of type.items) if (item.optional || acceptsNull(item)) return true;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
51
68
|
function fastCheck(def, value, flags, opts) {
|
|
52
|
-
if (def.optional && (value === void 0 || value === null)) return PASS;
|
|
69
|
+
if (def.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(def) ? FAIL : PASS;
|
|
53
70
|
const type = def.type;
|
|
54
71
|
switch (type.kind) {
|
|
55
72
|
case "": return fastCheckFinal(def, value);
|
|
@@ -59,7 +76,7 @@ function fastCheck(def, value, flags, opts) {
|
|
|
59
76
|
const branchFlags = flags | IN_UNION;
|
|
60
77
|
for (const item of type.items) {
|
|
61
78
|
const result = fastCheck(item, value, branchFlags, opts);
|
|
62
|
-
if (result !== FAIL) return result;
|
|
79
|
+
if (result !== FAIL) return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(def.metadata, value) : result;
|
|
63
80
|
}
|
|
64
81
|
return FAIL;
|
|
65
82
|
}
|
|
@@ -88,12 +105,9 @@ function fastCheckFinal(def, value) {
|
|
|
88
105
|
if (designType === "phantom") return PASS;
|
|
89
106
|
if (type.value !== void 0) return value === type.value ? PASS : FAIL;
|
|
90
107
|
switch (designType) {
|
|
91
|
-
case "string": return typeof value === "string" ? fastCheckString(def, value) : FAIL;
|
|
92
|
-
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def, value) : FAIL;
|
|
93
|
-
case "boolean":
|
|
94
|
-
if (typeof value !== "boolean") return FAIL;
|
|
95
|
-
if (def.metadata.size === 0) return PASS;
|
|
96
|
-
return def.metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
108
|
+
case "string": return typeof value === "string" ? fastCheckString(def.metadata, value) : FAIL;
|
|
109
|
+
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def.metadata, value) : FAIL;
|
|
110
|
+
case "boolean": return typeof value === "boolean" ? fastCheckBoolean(def.metadata, value) : FAIL;
|
|
97
111
|
case "any": return PASS;
|
|
98
112
|
case "never": return FAIL;
|
|
99
113
|
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
@@ -102,8 +116,24 @@ function fastCheckFinal(def, value) {
|
|
|
102
116
|
default: return BAIL;
|
|
103
117
|
}
|
|
104
118
|
}
|
|
105
|
-
|
|
106
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
|
|
121
|
+
* (`null` / objects pass; the caller skips literal branches).
|
|
122
|
+
*/ function fastCheckUnionConstraints(metadata, value) {
|
|
123
|
+
if (metadata.size === 0) return PASS;
|
|
124
|
+
switch (typeof value) {
|
|
125
|
+
case "string": return fastCheckString(metadata, value);
|
|
126
|
+
case "number": return fastCheckNumber(metadata, value);
|
|
127
|
+
case "boolean": return fastCheckBoolean(metadata, value);
|
|
128
|
+
case "object": return Array.isArray(value) ? fastCheckArrayConstraints(metadata, value) : PASS;
|
|
129
|
+
default: return PASS;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function fastCheckBoolean(metadata, value) {
|
|
133
|
+
if (metadata.size === 0) return PASS;
|
|
134
|
+
return metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
135
|
+
}
|
|
136
|
+
function fastCheckString(metadata, value) {
|
|
107
137
|
if (metadata.size === 0) return PASS;
|
|
108
138
|
if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
|
|
109
139
|
const minLength = metadata.get("expect.minLength");
|
|
@@ -119,8 +149,7 @@ function fastCheckString(def, value) {
|
|
|
119
149
|
}
|
|
120
150
|
return PASS;
|
|
121
151
|
}
|
|
122
|
-
function fastCheckNumber(
|
|
123
|
-
const metadata = def.metadata;
|
|
152
|
+
function fastCheckNumber(metadata, value) {
|
|
124
153
|
if (metadata.size === 0) return PASS;
|
|
125
154
|
if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
|
|
126
155
|
const min = metadata.get("expect.min");
|
|
@@ -163,11 +192,8 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
163
192
|
if (!Array.isArray(value)) return FAIL;
|
|
164
193
|
const metadata = def.metadata;
|
|
165
194
|
if (metadata.size > 0) {
|
|
166
|
-
const
|
|
167
|
-
if (
|
|
168
|
-
const maxLength = metadata.get("expect.maxLength");
|
|
169
|
-
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
170
|
-
if (metadata.get("expect.array.uniqueItems")) return BAIL;
|
|
195
|
+
const result = fastCheckArrayConstraints(metadata, value);
|
|
196
|
+
if (result !== PASS) return result;
|
|
171
197
|
}
|
|
172
198
|
const of = def.type.of;
|
|
173
199
|
const inUnion = (flags & IN_UNION) !== 0;
|
|
@@ -182,6 +208,13 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
182
208
|
}
|
|
183
209
|
return result;
|
|
184
210
|
}
|
|
211
|
+
function fastCheckArrayConstraints(metadata, value) {
|
|
212
|
+
const minLength = metadata.get("expect.minLength");
|
|
213
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
214
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
215
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
216
|
+
return metadata.get("expect.array.uniqueItems") ? BAIL : PASS;
|
|
217
|
+
}
|
|
185
218
|
|
|
186
219
|
//#endregion
|
|
187
220
|
//#region packages/typescript/src/runtime/validator.ts
|
|
@@ -297,7 +330,13 @@ function _define_property(obj, key, value) {
|
|
|
297
330
|
}
|
|
298
331
|
def = replaced;
|
|
299
332
|
}
|
|
300
|
-
if (def.optional && (value === void 0 || value === null))
|
|
333
|
+
if (def.optional && (value === void 0 || value === null)) {
|
|
334
|
+
if (value === null && rejectsNullAsRequired(def)) {
|
|
335
|
+
this.error(requiredMessage(def));
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
301
340
|
if (this.hasPlugins) for (const plugin of this.opts.plugins) {
|
|
302
341
|
const result = plugin(this, def, value);
|
|
303
342
|
if (result === false || result === true) return result;
|
|
@@ -332,7 +371,7 @@ function _define_property(obj, key, value) {
|
|
|
332
371
|
this.stackErrors.push(null);
|
|
333
372
|
if (this.validateSafe(item, value)) {
|
|
334
373
|
this.stackErrors.pop();
|
|
335
|
-
return
|
|
374
|
+
return this.validateUnionConstraints(def, item, value);
|
|
336
375
|
}
|
|
337
376
|
const branchErrors = this.stackErrors.pop();
|
|
338
377
|
if (this.limitExceeded) this.limitExceeded = false;
|
|
@@ -343,6 +382,23 @@ function _define_property(obj, key, value) {
|
|
|
343
382
|
this.error(`Value does not match any of the allowed types: ${expected}`, void 0, details);
|
|
344
383
|
return false;
|
|
345
384
|
}
|
|
385
|
+
/**
|
|
386
|
+
* Constraints annotated on the union itself (`@expect.min 0` on `number | null`)
|
|
387
|
+
* apply to the non-null value that matched `branch`, on top of the branch's own.
|
|
388
|
+
* A literal branch (an enum value) is exempt: its value is fixed.
|
|
389
|
+
*/ validateUnionConstraints(def, branch, value) {
|
|
390
|
+
const metadata = def.metadata;
|
|
391
|
+
if (metadata.size === 0 || branch.type.value !== void 0) return true;
|
|
392
|
+
switch (typeof value) {
|
|
393
|
+
case "string": return this.validateStringConstraints(metadata, value);
|
|
394
|
+
case "number": return this.validateNumberConstraints(metadata, value);
|
|
395
|
+
case "boolean": return this.validateBooleanConstraints(metadata, value);
|
|
396
|
+
case "object":
|
|
397
|
+
if (Array.isArray(value)) return this.validateArrayConstraints(metadata, value, arrayElementType(branch));
|
|
398
|
+
return true;
|
|
399
|
+
default: return true;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
346
402
|
validateIntersection(def, value) {
|
|
347
403
|
for (const item of def.type.items) if (!this.validateSafe(item, value)) return false;
|
|
348
404
|
return true;
|
|
@@ -369,7 +425,22 @@ function _define_property(obj, key, value) {
|
|
|
369
425
|
this.error("Expected array");
|
|
370
426
|
return false;
|
|
371
427
|
}
|
|
372
|
-
|
|
428
|
+
if (def.metadata.size > 0 && !this.validateArrayConstraints(def.metadata, value, def.type.of)) return false;
|
|
429
|
+
let i = 0;
|
|
430
|
+
let passed = true;
|
|
431
|
+
for (const item of value) {
|
|
432
|
+
this.push(String(i));
|
|
433
|
+
if (!this.validateSafe(def.type.of, item)) {
|
|
434
|
+
passed = false;
|
|
435
|
+
this.pop(true);
|
|
436
|
+
if (this.limitExceeded) return false;
|
|
437
|
+
} else this.pop(false);
|
|
438
|
+
i++;
|
|
439
|
+
}
|
|
440
|
+
return passed;
|
|
441
|
+
}
|
|
442
|
+
/** `@expect.minLength` / `maxLength` / `array.uniqueItems` checks for an array value. */ validateArrayConstraints(metadata, value, of) {
|
|
443
|
+
const minLength = metadata.get("expect.minLength");
|
|
373
444
|
if (minLength !== void 0) {
|
|
374
445
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
375
446
|
if (value.length < length) {
|
|
@@ -378,7 +449,7 @@ function _define_property(obj, key, value) {
|
|
|
378
449
|
return false;
|
|
379
450
|
}
|
|
380
451
|
}
|
|
381
|
-
const maxLength =
|
|
452
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
382
453
|
if (maxLength !== void 0) {
|
|
383
454
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
384
455
|
if (value.length > length) {
|
|
@@ -387,13 +458,13 @@ function _define_property(obj, key, value) {
|
|
|
387
458
|
return false;
|
|
388
459
|
}
|
|
389
460
|
}
|
|
390
|
-
const uniqueItems =
|
|
461
|
+
const uniqueItems = metadata.get("expect.array.uniqueItems");
|
|
391
462
|
if (uniqueItems) {
|
|
392
463
|
const separator = "▼↩";
|
|
393
464
|
const seen = /* @__PURE__ */ new Set();
|
|
394
465
|
const keyProps = /* @__PURE__ */ new Set();
|
|
395
|
-
if (
|
|
396
|
-
for (const [key, val] of
|
|
466
|
+
if (of?.type.kind === "object") {
|
|
467
|
+
for (const [key, val] of of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
|
|
397
468
|
}
|
|
398
469
|
for (let idx = 0; idx < value.length; idx++) {
|
|
399
470
|
const item = value[idx];
|
|
@@ -411,18 +482,7 @@ function _define_property(obj, key, value) {
|
|
|
411
482
|
seen.add(key);
|
|
412
483
|
}
|
|
413
484
|
}
|
|
414
|
-
|
|
415
|
-
let passed = true;
|
|
416
|
-
for (const item of value) {
|
|
417
|
-
this.push(String(i));
|
|
418
|
-
if (!this.validateSafe(def.type.of, item)) {
|
|
419
|
-
passed = false;
|
|
420
|
-
this.pop(true);
|
|
421
|
-
if (this.limitExceeded) return false;
|
|
422
|
-
} else this.pop(false);
|
|
423
|
-
i++;
|
|
424
|
-
}
|
|
425
|
-
return passed;
|
|
485
|
+
return true;
|
|
426
486
|
}
|
|
427
487
|
validateObject(def, value) {
|
|
428
488
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
@@ -560,8 +620,11 @@ function _define_property(obj, key, value) {
|
|
|
560
620
|
}
|
|
561
621
|
}
|
|
562
622
|
validateString(def, value) {
|
|
563
|
-
|
|
564
|
-
|
|
623
|
+
return this.validateStringConstraints(def.metadata, value);
|
|
624
|
+
}
|
|
625
|
+
/** `@meta.required` / `@expect.minLength` / `maxLength` / `pattern` checks for a string value. */ validateStringConstraints(metadata, value) {
|
|
626
|
+
if (metadata.size === 0) return true;
|
|
627
|
+
const filled = metadata.get("meta.required");
|
|
565
628
|
if (filled) {
|
|
566
629
|
if (value.trim().length === 0) {
|
|
567
630
|
const message = typeof filled === "object" && filled.message ? filled.message : `Must not be empty`;
|
|
@@ -569,7 +632,7 @@ function _define_property(obj, key, value) {
|
|
|
569
632
|
return false;
|
|
570
633
|
}
|
|
571
634
|
}
|
|
572
|
-
const minLength =
|
|
635
|
+
const minLength = metadata.get("expect.minLength");
|
|
573
636
|
if (minLength !== void 0) {
|
|
574
637
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
575
638
|
if (value.length < length) {
|
|
@@ -578,7 +641,7 @@ function _define_property(obj, key, value) {
|
|
|
578
641
|
return false;
|
|
579
642
|
}
|
|
580
643
|
}
|
|
581
|
-
const maxLength =
|
|
644
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
582
645
|
if (maxLength !== void 0) {
|
|
583
646
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
584
647
|
if (value.length > length) {
|
|
@@ -587,7 +650,7 @@ function _define_property(obj, key, value) {
|
|
|
587
650
|
return false;
|
|
588
651
|
}
|
|
589
652
|
}
|
|
590
|
-
const patterns =
|
|
653
|
+
const patterns = metadata.get("expect.pattern");
|
|
591
654
|
for (const { pattern, flags, message } of patterns || []) {
|
|
592
655
|
if (!pattern) continue;
|
|
593
656
|
if (!getRegex(pattern, flags).test(value)) {
|
|
@@ -598,14 +661,17 @@ function _define_property(obj, key, value) {
|
|
|
598
661
|
return true;
|
|
599
662
|
}
|
|
600
663
|
validateNumber(def, value) {
|
|
601
|
-
|
|
602
|
-
|
|
664
|
+
return this.validateNumberConstraints(def.metadata, value);
|
|
665
|
+
}
|
|
666
|
+
/** `@expect.int` / `min` / `max` checks for a number value. */ validateNumberConstraints(metadata, value) {
|
|
667
|
+
if (metadata.size === 0) return true;
|
|
668
|
+
const int = metadata.get("expect.int");
|
|
603
669
|
if (int && value % 1 !== 0) {
|
|
604
670
|
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
605
671
|
this.error(message);
|
|
606
672
|
return false;
|
|
607
673
|
}
|
|
608
|
-
const min =
|
|
674
|
+
const min = metadata.get("expect.min");
|
|
609
675
|
if (min !== void 0) {
|
|
610
676
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
611
677
|
if (value < minValue) {
|
|
@@ -614,7 +680,7 @@ function _define_property(obj, key, value) {
|
|
|
614
680
|
return false;
|
|
615
681
|
}
|
|
616
682
|
}
|
|
617
|
-
const max =
|
|
683
|
+
const max = metadata.get("expect.max");
|
|
618
684
|
if (max !== void 0) {
|
|
619
685
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
620
686
|
if (value > maxValue) {
|
|
@@ -626,8 +692,11 @@ function _define_property(obj, key, value) {
|
|
|
626
692
|
return true;
|
|
627
693
|
}
|
|
628
694
|
validateBoolean(def, value) {
|
|
629
|
-
|
|
630
|
-
|
|
695
|
+
return this.validateBooleanConstraints(def.metadata, value);
|
|
696
|
+
}
|
|
697
|
+
/** `@meta.required` check for a boolean value (must be `true`). */ validateBooleanConstraints(metadata, value) {
|
|
698
|
+
if (metadata.size === 0) return true;
|
|
699
|
+
const filled = metadata.get("meta.required");
|
|
631
700
|
if (filled) {
|
|
632
701
|
if (value !== true) {
|
|
633
702
|
const message = typeof filled === "object" && filled.message ? filled.message : `Must be checked`;
|
|
@@ -667,6 +736,23 @@ function _define_property(obj, key, value) {
|
|
|
667
736
|
if (this.hasReplace) this.replaceCache = /* @__PURE__ */ new WeakMap();
|
|
668
737
|
}
|
|
669
738
|
};
|
|
739
|
+
/** The `@meta.required` error message for `def` (custom message or the type's default). */ function requiredMessage(def) {
|
|
740
|
+
const filled = def.metadata.get("meta.required");
|
|
741
|
+
if (typeof filled === "object" && filled.message) return filled.message;
|
|
742
|
+
return def.type.kind === "" && def.type.designType === "boolean" ? "Must be checked" : "Must not be empty";
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Element type of the array a union branch holds — through nested unions too
|
|
746
|
+
* (`Items | undefined` with `type Items = Item[] | null`), so `@expect.array.key`
|
|
747
|
+
* fields of the elements are found.
|
|
748
|
+
*/ function arrayElementType(branch) {
|
|
749
|
+
const type = branch.type;
|
|
750
|
+
if (type.kind === "array") return type.of;
|
|
751
|
+
if (type.kind === "union") for (const item of type.items) {
|
|
752
|
+
const of = arrayElementType(item);
|
|
753
|
+
if (of) return of;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
670
756
|
/** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
|
|
671
757
|
constructor(errors) {
|
|
672
758
|
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";
|
|
@@ -1065,10 +1151,7 @@ function cloneTypeDef(type) {
|
|
|
1065
1151
|
type: "array",
|
|
1066
1152
|
items: build(d.type.of)
|
|
1067
1153
|
};
|
|
1068
|
-
|
|
1069
|
-
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1070
|
-
const maxLength = meta.get("expect.maxLength");
|
|
1071
|
-
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1154
|
+
applyArrayConstraints(schema, meta);
|
|
1072
1155
|
return schema;
|
|
1073
1156
|
},
|
|
1074
1157
|
union(d) {
|
|
@@ -1088,7 +1171,9 @@ function cloneTypeDef(type) {
|
|
|
1088
1171
|
}
|
|
1089
1172
|
};
|
|
1090
1173
|
}
|
|
1091
|
-
|
|
1174
|
+
const anyOf = d.type.items.map(build);
|
|
1175
|
+
if (meta.size > 0) applyUnionConstraints(anyOf, meta);
|
|
1176
|
+
return { anyOf };
|
|
1092
1177
|
},
|
|
1093
1178
|
intersection(d) {
|
|
1094
1179
|
return { allOf: d.type.items.map(build) };
|
|
@@ -1106,24 +1191,8 @@ function cloneTypeDef(type) {
|
|
|
1106
1191
|
if (d.type.designType && d.type.designType !== "any") {
|
|
1107
1192
|
const dt = d.type.designType;
|
|
1108
1193
|
schema.type = dt === "undefined" ? "null" : dt === "decimal" ? "string" : dt;
|
|
1109
|
-
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
1110
|
-
}
|
|
1111
|
-
if (schema.type === "string") {
|
|
1112
|
-
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1113
|
-
const minLength = meta.get("expect.minLength");
|
|
1114
|
-
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1115
|
-
const maxLength = meta.get("expect.maxLength");
|
|
1116
|
-
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1117
|
-
const patterns = meta.get("expect.pattern");
|
|
1118
|
-
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1119
|
-
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1120
|
-
}
|
|
1121
|
-
if (schema.type === "number" || schema.type === "integer") {
|
|
1122
|
-
const min = meta.get("expect.min");
|
|
1123
|
-
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1124
|
-
const max = meta.get("expect.max");
|
|
1125
|
-
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1126
1194
|
}
|
|
1195
|
+
applyScalarConstraints(schema, meta);
|
|
1127
1196
|
return schema;
|
|
1128
1197
|
}
|
|
1129
1198
|
});
|
|
@@ -1135,6 +1204,70 @@ function cloneTypeDef(type) {
|
|
|
1135
1204
|
};
|
|
1136
1205
|
return schema;
|
|
1137
1206
|
}
|
|
1207
|
+
/** `@expect.minLength` / `maxLength` → `minItems` / `maxItems` on an array schema. */ function applyArrayConstraints(schema, meta) {
|
|
1208
|
+
const minLength = meta.get("expect.minLength");
|
|
1209
|
+
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1210
|
+
const maxLength = meta.get("expect.maxLength");
|
|
1211
|
+
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1212
|
+
}
|
|
1213
|
+
/** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
|
|
1214
|
+
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
1215
|
+
if (schema.type === "string") {
|
|
1216
|
+
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1217
|
+
const minLength = meta.get("expect.minLength");
|
|
1218
|
+
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1219
|
+
const maxLength = meta.get("expect.maxLength");
|
|
1220
|
+
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1221
|
+
const patterns = meta.get("expect.pattern");
|
|
1222
|
+
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1223
|
+
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1224
|
+
}
|
|
1225
|
+
if (schema.type === "number" || schema.type === "integer") {
|
|
1226
|
+
const min = meta.get("expect.min");
|
|
1227
|
+
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1228
|
+
const max = meta.get("expect.max");
|
|
1229
|
+
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
const LOWER_BOUNDS = new Set([
|
|
1233
|
+
"minimum",
|
|
1234
|
+
"minLength",
|
|
1235
|
+
"minItems"
|
|
1236
|
+
]);
|
|
1237
|
+
const UPPER_BOUNDS = new Set([
|
|
1238
|
+
"maximum",
|
|
1239
|
+
"maxLength",
|
|
1240
|
+
"maxItems"
|
|
1241
|
+
]);
|
|
1242
|
+
/**
|
|
1243
|
+
* Puts union-level constraints onto the matching `anyOf` members (numbers, strings,
|
|
1244
|
+
* arrays; nested `anyOf` members recursively). When a member already has the same
|
|
1245
|
+
* bound, the stricter one wins; patterns accumulate.
|
|
1246
|
+
*/ function applyUnionConstraints(members, meta) {
|
|
1247
|
+
for (const member of members) {
|
|
1248
|
+
if (Array.isArray(member.anyOf)) {
|
|
1249
|
+
applyUnionConstraints(member.anyOf, meta);
|
|
1250
|
+
continue;
|
|
1251
|
+
}
|
|
1252
|
+
if (member.const !== void 0) continue;
|
|
1253
|
+
const type = member.type;
|
|
1254
|
+
const extra = { type };
|
|
1255
|
+
if (type === "array") applyArrayConstraints(extra, meta);
|
|
1256
|
+
else if (type === "string" || type === "number" || type === "integer") applyScalarConstraints(extra, meta);
|
|
1257
|
+
else continue;
|
|
1258
|
+
if (extra.type === "integer") member.type = "integer";
|
|
1259
|
+
for (const [key, value] of Object.entries(extra)) {
|
|
1260
|
+
if (key === "type") continue;
|
|
1261
|
+
if (key === "pattern" || key === "allOf") {
|
|
1262
|
+
const patterns = key === "pattern" ? [{ pattern: value }] : value;
|
|
1263
|
+
if (member.pattern === void 0 && key === "pattern" && !member.allOf) member.pattern = value;
|
|
1264
|
+
else member.allOf = (member.allOf || []).concat(patterns);
|
|
1265
|
+
} else if (member[key] === void 0) member[key] = value;
|
|
1266
|
+
else if (LOWER_BOUNDS.has(key)) member[key] = Math.max(member[key], value);
|
|
1267
|
+
else if (UPPER_BOUNDS.has(key)) member[key] = Math.min(member[key], value);
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1138
1271
|
/**
|
|
1139
1272
|
* Converts a JSON Schema object into a {@link TAtscriptAnnotatedType}.
|
|
1140
1273
|
*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { l as defineAnnotatedType, t as buildJsonSchema } from "./json-schema-
|
|
1
|
+
import { l as defineAnnotatedType, t as buildJsonSchema } from "./json-schema-BGYiqs-J.mjs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { DEFAULT_FORMAT, flattenInterfaceNode, getRelPath, hasNavPropAnnotation, isArray, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isInterface, isPrimitive, isProp, isQueryLogical, isRef, isStructure } from "@atscript/core";
|
|
4
4
|
|
|
@@ -1015,7 +1015,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1015
1015
|
const ref = node;
|
|
1016
1016
|
const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
1017
1017
|
const handle = this.toAnnotatedHandle(decl, true);
|
|
1018
|
-
|
|
1018
|
+
if (skipAnnotations) return handle;
|
|
1019
|
+
const own = this.doc.evalAnnotationsForNode(node);
|
|
1020
|
+
const referred = this.refTargetAnnotations(ref, decl);
|
|
1021
|
+
return this.applyExpectAnnotations(handle, referred ? this.doc.mergeNodesAnnotations(referred, own) : own);
|
|
1019
1022
|
}
|
|
1020
1023
|
case "primitive": {
|
|
1021
1024
|
const prim = node;
|
|
@@ -1097,6 +1100,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1097
1100
|
}, true);
|
|
1098
1101
|
break;
|
|
1099
1102
|
case "expect.int":
|
|
1103
|
+
case "meta.required":
|
|
1100
1104
|
handle.annotate(a.name, true);
|
|
1101
1105
|
break;
|
|
1102
1106
|
case "expect.array.uniqueItems":
|
|
@@ -1116,12 +1120,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1116
1120
|
case "ref": {
|
|
1117
1121
|
const ref = node;
|
|
1118
1122
|
const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
return this;
|
|
1124
|
-
}
|
|
1123
|
+
const primitive = this.inlinedPrimitive(ref, decl);
|
|
1124
|
+
if (primitive) {
|
|
1125
|
+
this.annotateType(primitive, name);
|
|
1126
|
+
return this;
|
|
1125
1127
|
}
|
|
1126
1128
|
if (this._adHocAnnotations && this.hasAdHocAnnotationsThroughRef()) {
|
|
1127
1129
|
let resolved = decl ? this.doc.mergeIntersection(decl) : void 0;
|
|
@@ -1141,7 +1143,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1141
1143
|
const refExpr = !!synth || (ownerDecl ? ownerDecl.doc !== this.doc : false) ? `() => ${refName}` : refName;
|
|
1142
1144
|
this.writeln(`$(${name ? `"", ${name}` : ""})`).indent().writeln(`.refTo(${refExpr}${chain})`);
|
|
1143
1145
|
if (!ref.hasChain) {
|
|
1144
|
-
if (ownerDecl?.node) ownerDecl.doc.
|
|
1146
|
+
if (ownerDecl?.node) ownerDecl.doc.evalReferredAnnotations(ownerDecl.node)?.forEach((an) => {
|
|
1145
1147
|
this.resolveAnnotationValue(ownerDecl.node, an);
|
|
1146
1148
|
});
|
|
1147
1149
|
}
|
|
@@ -1272,13 +1274,44 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1272
1274
|
}
|
|
1273
1275
|
defineGroup(node) {
|
|
1274
1276
|
const items = node.unwrap();
|
|
1275
|
-
for (const item of items) this.write(".item(").indent().annotateType(item).write(" .$type").writeln(`)`).unindent();
|
|
1277
|
+
for (const item of items) this.write(".item(").indent().annotateType(item).defineInlinePrimitiveMetadata(item).write(" .$type").writeln(`)`).unindent();
|
|
1276
1278
|
return this;
|
|
1277
1279
|
}
|
|
1278
1280
|
defineArray(node) {
|
|
1279
|
-
|
|
1281
|
+
const of = node.getDefinition();
|
|
1282
|
+
this.write(".of(").indent().annotateType(of).defineInlinePrimitiveMetadata(of).write(" .$type").writeln(`)`).unindent();
|
|
1283
|
+
return this;
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* A built-in primitive extension used directly as a union / tuple member or array
|
|
1287
|
+
* element (`number.int | null`, `string.email[]`) is inlined by `annotateType` without
|
|
1288
|
+
* its built-in annotations (`expect.int`, the email pattern, …) — a prop gets them
|
|
1289
|
+
* through `defineMetadata`, a member has no prop to carry them, so emit them here.
|
|
1290
|
+
*/ defineInlinePrimitiveMetadata(node) {
|
|
1291
|
+
const primitive = node && this.inlinedPrimitive(node);
|
|
1292
|
+
if (primitive?.annotations?.length) {
|
|
1293
|
+
this.indent();
|
|
1294
|
+
for (const an of primitive.annotations) this.resolveAnnotationValue(primitive, an);
|
|
1295
|
+
this.unindent();
|
|
1296
|
+
}
|
|
1280
1297
|
return this;
|
|
1281
1298
|
}
|
|
1299
|
+
/**
|
|
1300
|
+
* The built-in primitive a ref is inlined as by `annotateType` — not a named alias that
|
|
1301
|
+
* resolves to one (`type MyString = string`), which is emitted as a `refTo`.
|
|
1302
|
+
*/ inlinedPrimitive(node, decl = isRef(node) ? this.doc.unwindType(node.id, node.chain)?.def : void 0) {
|
|
1303
|
+
if (!isRef(node) || !isPrimitive(decl)) return;
|
|
1304
|
+
const ownerDecl = this.resolveOwner(this.doc, node.id);
|
|
1305
|
+
if (ownerDecl?.node && (ownerDecl.node.entity === "type" || ownerDecl.node.entity === "interface")) return;
|
|
1306
|
+
return decl;
|
|
1307
|
+
}
|
|
1308
|
+
/** Annotations a ref inherits from its target (see `annotateType`'s ref case). */ refTargetAnnotations(ref, decl) {
|
|
1309
|
+
const primitive = this.inlinedPrimitive(ref, decl);
|
|
1310
|
+
if (primitive) return primitive.annotations;
|
|
1311
|
+
if (ref.hasChain) return;
|
|
1312
|
+
const ownerDecl = this.resolveOwner(this.doc, ref.id);
|
|
1313
|
+
return ownerDecl?.node ? ownerDecl.doc.evalReferredAnnotations(ownerDecl.node) : void 0;
|
|
1314
|
+
}
|
|
1282
1315
|
defineMetadata(node) {
|
|
1283
1316
|
let annotations;
|
|
1284
1317
|
const nodeDef = node.getDefinition?.();
|
|
@@ -25,7 +25,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
25
25
|
}) : target, mod));
|
|
26
26
|
|
|
27
27
|
//#endregion
|
|
28
|
-
const require_json_schema = require('./json-schema-
|
|
28
|
+
const require_json_schema = require('./json-schema-L8xJQiLc.cjs');
|
|
29
29
|
let path = require("path");
|
|
30
30
|
path = __toESM(path);
|
|
31
31
|
let _atscript_core = require("@atscript/core");
|
|
@@ -1043,7 +1043,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1043
1043
|
const ref = node;
|
|
1044
1044
|
const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
1045
1045
|
const handle = this.toAnnotatedHandle(decl, true);
|
|
1046
|
-
|
|
1046
|
+
if (skipAnnotations) return handle;
|
|
1047
|
+
const own = this.doc.evalAnnotationsForNode(node);
|
|
1048
|
+
const referred = this.refTargetAnnotations(ref, decl);
|
|
1049
|
+
return this.applyExpectAnnotations(handle, referred ? this.doc.mergeNodesAnnotations(referred, own) : own);
|
|
1047
1050
|
}
|
|
1048
1051
|
case "primitive": {
|
|
1049
1052
|
const prim = node;
|
|
@@ -1125,6 +1128,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1125
1128
|
}, true);
|
|
1126
1129
|
break;
|
|
1127
1130
|
case "expect.int":
|
|
1131
|
+
case "meta.required":
|
|
1128
1132
|
handle.annotate(a.name, true);
|
|
1129
1133
|
break;
|
|
1130
1134
|
case "expect.array.uniqueItems":
|
|
@@ -1144,12 +1148,10 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1144
1148
|
case "ref": {
|
|
1145
1149
|
const ref = node;
|
|
1146
1150
|
const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
return this;
|
|
1152
|
-
}
|
|
1151
|
+
const primitive = this.inlinedPrimitive(ref, decl);
|
|
1152
|
+
if (primitive) {
|
|
1153
|
+
this.annotateType(primitive, name);
|
|
1154
|
+
return this;
|
|
1153
1155
|
}
|
|
1154
1156
|
if (this._adHocAnnotations && this.hasAdHocAnnotationsThroughRef()) {
|
|
1155
1157
|
let resolved = decl ? this.doc.mergeIntersection(decl) : void 0;
|
|
@@ -1169,7 +1171,7 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1169
1171
|
const refExpr = !!synth || (ownerDecl ? ownerDecl.doc !== this.doc : false) ? `() => ${refName}` : refName;
|
|
1170
1172
|
this.writeln(`$(${name ? `"", ${name}` : ""})`).indent().writeln(`.refTo(${refExpr}${chain})`);
|
|
1171
1173
|
if (!ref.hasChain) {
|
|
1172
|
-
if (ownerDecl?.node) ownerDecl.doc.
|
|
1174
|
+
if (ownerDecl?.node) ownerDecl.doc.evalReferredAnnotations(ownerDecl.node)?.forEach((an) => {
|
|
1173
1175
|
this.resolveAnnotationValue(ownerDecl.node, an);
|
|
1174
1176
|
});
|
|
1175
1177
|
}
|
|
@@ -1300,13 +1302,44 @@ var JsRenderer = class extends BaseRenderer {
|
|
|
1300
1302
|
}
|
|
1301
1303
|
defineGroup(node) {
|
|
1302
1304
|
const items = node.unwrap();
|
|
1303
|
-
for (const item of items) this.write(".item(").indent().annotateType(item).write(" .$type").writeln(`)`).unindent();
|
|
1305
|
+
for (const item of items) this.write(".item(").indent().annotateType(item).defineInlinePrimitiveMetadata(item).write(" .$type").writeln(`)`).unindent();
|
|
1304
1306
|
return this;
|
|
1305
1307
|
}
|
|
1306
1308
|
defineArray(node) {
|
|
1307
|
-
|
|
1309
|
+
const of = node.getDefinition();
|
|
1310
|
+
this.write(".of(").indent().annotateType(of).defineInlinePrimitiveMetadata(of).write(" .$type").writeln(`)`).unindent();
|
|
1311
|
+
return this;
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* A built-in primitive extension used directly as a union / tuple member or array
|
|
1315
|
+
* element (`number.int | null`, `string.email[]`) is inlined by `annotateType` without
|
|
1316
|
+
* its built-in annotations (`expect.int`, the email pattern, …) — a prop gets them
|
|
1317
|
+
* through `defineMetadata`, a member has no prop to carry them, so emit them here.
|
|
1318
|
+
*/ defineInlinePrimitiveMetadata(node) {
|
|
1319
|
+
const primitive = node && this.inlinedPrimitive(node);
|
|
1320
|
+
if (primitive?.annotations?.length) {
|
|
1321
|
+
this.indent();
|
|
1322
|
+
for (const an of primitive.annotations) this.resolveAnnotationValue(primitive, an);
|
|
1323
|
+
this.unindent();
|
|
1324
|
+
}
|
|
1308
1325
|
return this;
|
|
1309
1326
|
}
|
|
1327
|
+
/**
|
|
1328
|
+
* The built-in primitive a ref is inlined as by `annotateType` — not a named alias that
|
|
1329
|
+
* resolves to one (`type MyString = string`), which is emitted as a `refTo`.
|
|
1330
|
+
*/ inlinedPrimitive(node, decl = (0, _atscript_core.isRef)(node) ? this.doc.unwindType(node.id, node.chain)?.def : void 0) {
|
|
1331
|
+
if (!(0, _atscript_core.isRef)(node) || !(0, _atscript_core.isPrimitive)(decl)) return;
|
|
1332
|
+
const ownerDecl = this.resolveOwner(this.doc, node.id);
|
|
1333
|
+
if (ownerDecl?.node && (ownerDecl.node.entity === "type" || ownerDecl.node.entity === "interface")) return;
|
|
1334
|
+
return decl;
|
|
1335
|
+
}
|
|
1336
|
+
/** Annotations a ref inherits from its target (see `annotateType`'s ref case). */ refTargetAnnotations(ref, decl) {
|
|
1337
|
+
const primitive = this.inlinedPrimitive(ref, decl);
|
|
1338
|
+
if (primitive) return primitive.annotations;
|
|
1339
|
+
if (ref.hasChain) return;
|
|
1340
|
+
const ownerDecl = this.resolveOwner(this.doc, ref.id);
|
|
1341
|
+
return ownerDecl?.node ? ownerDecl.doc.evalReferredAnnotations(ownerDecl.node) : void 0;
|
|
1342
|
+
}
|
|
1310
1343
|
defineMetadata(node) {
|
|
1311
1344
|
let annotations;
|
|
1312
1345
|
const nodeDef = node.getDefinition?.();
|
package/dist/test-utils.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_plugin = require('./plugin-
|
|
2
|
+
const require_plugin = require('./plugin-gpq_ji-_.cjs');
|
|
3
3
|
let path = require("path");
|
|
4
4
|
path = require_plugin.__toESM(path);
|
|
5
5
|
let _atscript_core = require("@atscript/core");
|
package/dist/test-utils.mjs
CHANGED