@atscript/typescript 0.1.100 → 0.1.102
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 +214 -22
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{json-schema-CtQ_rzJn.cjs → json-schema-B9xrfeoQ.cjs} +214 -22
- package/dist/{json-schema-BogNt-9w.mjs → json-schema-CW_50s1W.mjs} +214 -22
- package/dist/{plugin-DG3jlbSN.mjs → plugin-CECXHL6M.mjs} +1 -1
- package/dist/{plugin-Cu6Nez-H.cjs → plugin-DORafTJq.cjs} +1 -1
- package/dist/test-utils.cjs +1 -1
- package/dist/test-utils.mjs +1 -1
- package/dist/utils.cjs +34 -22
- package/dist/utils.d.ts +18 -0
- package/dist/utils.mjs +34 -22
- package/package.json +2 -2
|
@@ -1,3 +1,188 @@
|
|
|
1
|
+
//#region packages/typescript/src/runtime/validator-fast-check.ts
|
|
2
|
+
/**
|
|
3
|
+
* Allocation-free boolean pre-check used by {@link Validator.validate} before the
|
|
4
|
+
* error-collecting walk.
|
|
5
|
+
*
|
|
6
|
+
* It reads the type tree and metadata LIVE on every call (nothing derived from the
|
|
7
|
+
* type is cached), so `annotate()` / `metadata.set()` / prop mutations made after a
|
|
8
|
+
* validator was created are always honoured — the same as the full walk.
|
|
9
|
+
*
|
|
10
|
+
* Result contract (relative to the full walk over the same node and value):
|
|
11
|
+
* - `PASS` — the walk returns `true`, collects no errors and has no side effects.
|
|
12
|
+
* - `FAIL` — the walk returns `false`; every node the walk would visit was visited
|
|
13
|
+
* here too (so a walk that would throw makes this throw first) and the walk has
|
|
14
|
+
* no side effects for this node.
|
|
15
|
+
* - `BAIL` — not decidable here; the caller must run the full walk.
|
|
16
|
+
*
|
|
17
|
+
* Union branches are tried in declared order, continuing past `FAIL` and stopping
|
|
18
|
+
* at `BAIL`, which keeps union results identical to the walk.
|
|
19
|
+
*/ const FAIL = 0;
|
|
20
|
+
const PASS = 1;
|
|
21
|
+
const BAIL = 2;
|
|
22
|
+
/** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
|
|
23
|
+
/** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
|
|
24
|
+
/**
|
|
25
|
+
* `true` only when the full walk over `value` is guaranteed to pass with no errors
|
|
26
|
+
* and no side effects. Any other outcome (including a throw) means "run the walk".
|
|
27
|
+
*/ function fastCheckPasses(def, value, opts) {
|
|
28
|
+
try {
|
|
29
|
+
return fastCheck(def, value, TOP, opts) === PASS;
|
|
30
|
+
} catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
|
|
35
|
+
const regexCache = /* @__PURE__ */ new Map();
|
|
36
|
+
/** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
|
|
37
|
+
let byFlags = regexCache.get(pattern);
|
|
38
|
+
if (!byFlags) {
|
|
39
|
+
byFlags = /* @__PURE__ */ new Map();
|
|
40
|
+
regexCache.set(pattern, byFlags);
|
|
41
|
+
}
|
|
42
|
+
const flagsKey = flags || "";
|
|
43
|
+
let regex = byFlags.get(flagsKey);
|
|
44
|
+
if (!regex) {
|
|
45
|
+
regex = new RegExp(pattern, flags);
|
|
46
|
+
byFlags.set(flagsKey, regex);
|
|
47
|
+
}
|
|
48
|
+
return regex;
|
|
49
|
+
}
|
|
50
|
+
function fastCheck(def, value, flags, opts) {
|
|
51
|
+
if (def.optional && (value === void 0 || value === null)) return PASS;
|
|
52
|
+
const type = def.type;
|
|
53
|
+
switch (type.kind) {
|
|
54
|
+
case "": return fastCheckFinal(def, value);
|
|
55
|
+
case "object": return fastCheckObject(def, value, flags, opts);
|
|
56
|
+
case "array": return fastCheckArray(def, value, flags, opts);
|
|
57
|
+
case "union": {
|
|
58
|
+
const branchFlags = flags | IN_UNION;
|
|
59
|
+
for (const item of type.items) {
|
|
60
|
+
const result = fastCheck(item, value, branchFlags, opts);
|
|
61
|
+
if (result !== FAIL) return result;
|
|
62
|
+
}
|
|
63
|
+
return FAIL;
|
|
64
|
+
}
|
|
65
|
+
case "intersection":
|
|
66
|
+
for (const item of type.items) {
|
|
67
|
+
const result = fastCheck(item, value, flags, opts);
|
|
68
|
+
if (result !== PASS) return result;
|
|
69
|
+
}
|
|
70
|
+
return PASS;
|
|
71
|
+
case "tuple": {
|
|
72
|
+
const items = type.items;
|
|
73
|
+
if (!Array.isArray(value) || value.length !== items.length) return FAIL;
|
|
74
|
+
const itemFlags = flags & IN_UNION;
|
|
75
|
+
for (let i = 0; i < items.length; i++) {
|
|
76
|
+
const result = fastCheck(items[i], value[i], itemFlags, opts);
|
|
77
|
+
if (result !== PASS) return result;
|
|
78
|
+
}
|
|
79
|
+
return PASS;
|
|
80
|
+
}
|
|
81
|
+
default: return BAIL;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function fastCheckFinal(def, value) {
|
|
85
|
+
const type = def.type;
|
|
86
|
+
const designType = type.designType;
|
|
87
|
+
if (designType === "phantom") return PASS;
|
|
88
|
+
if (type.value !== void 0) return value === type.value ? PASS : FAIL;
|
|
89
|
+
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;
|
|
96
|
+
case "any": return PASS;
|
|
97
|
+
case "never": return FAIL;
|
|
98
|
+
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
99
|
+
case "null": return value === null ? PASS : FAIL;
|
|
100
|
+
case "decimal": return typeof value === "string" && DECIMAL_RE.test(value) ? PASS : FAIL;
|
|
101
|
+
default: return BAIL;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function fastCheckString(def, value) {
|
|
105
|
+
const metadata = def.metadata;
|
|
106
|
+
if (metadata.size === 0) return PASS;
|
|
107
|
+
if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
|
|
108
|
+
const minLength = metadata.get("expect.minLength");
|
|
109
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
110
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
111
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
112
|
+
const patterns = metadata.get("expect.pattern");
|
|
113
|
+
if (patterns) for (const { pattern, flags } of patterns) {
|
|
114
|
+
if (!pattern) continue;
|
|
115
|
+
const regex = getRegex(pattern, flags);
|
|
116
|
+
if (regex.global || regex.sticky) return BAIL;
|
|
117
|
+
if (!regex.test(value)) return FAIL;
|
|
118
|
+
}
|
|
119
|
+
return PASS;
|
|
120
|
+
}
|
|
121
|
+
function fastCheckNumber(def, value) {
|
|
122
|
+
const metadata = def.metadata;
|
|
123
|
+
if (metadata.size === 0) return PASS;
|
|
124
|
+
if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
|
|
125
|
+
const min = metadata.get("expect.min");
|
|
126
|
+
if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
|
|
127
|
+
const max = metadata.get("expect.max");
|
|
128
|
+
if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
|
|
129
|
+
return PASS;
|
|
130
|
+
}
|
|
131
|
+
function fastCheckObject(def, value, flags, opts) {
|
|
132
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
133
|
+
const type = def.type;
|
|
134
|
+
if (type.propsPatterns.length > 0) return BAIL;
|
|
135
|
+
const partial = opts.partial;
|
|
136
|
+
const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
|
|
137
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
138
|
+
const propFlags = flags & IN_UNION;
|
|
139
|
+
let result = PASS;
|
|
140
|
+
for (const [key, item] of type.props) {
|
|
141
|
+
if (isPhantomType(item)) continue;
|
|
142
|
+
const propValue = value[key];
|
|
143
|
+
if (propValue === void 0 && skipUndefined) continue;
|
|
144
|
+
const propResult = fastCheck(item, propValue, propFlags, opts);
|
|
145
|
+
if (propResult !== PASS) {
|
|
146
|
+
if (propResult === BAIL || !inUnion) return propResult;
|
|
147
|
+
result = FAIL;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (result !== PASS) return result;
|
|
151
|
+
const unknownProps = opts.unknownProps;
|
|
152
|
+
if (unknownProps === "error" || unknownProps === "strip") {
|
|
153
|
+
const props = type.props;
|
|
154
|
+
for (const key of Object.keys(value)) {
|
|
155
|
+
const known = props.get(key);
|
|
156
|
+
if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return PASS;
|
|
160
|
+
}
|
|
161
|
+
function fastCheckArray(def, value, flags, opts) {
|
|
162
|
+
if (!Array.isArray(value)) return FAIL;
|
|
163
|
+
const metadata = def.metadata;
|
|
164
|
+
if (metadata.size > 0) {
|
|
165
|
+
const minLength = metadata.get("expect.minLength");
|
|
166
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
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;
|
|
170
|
+
}
|
|
171
|
+
const of = def.type.of;
|
|
172
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
173
|
+
const itemFlags = flags & IN_UNION;
|
|
174
|
+
let result = PASS;
|
|
175
|
+
for (const item of value) {
|
|
176
|
+
const itemResult = fastCheck(of, item, itemFlags, opts);
|
|
177
|
+
if (itemResult !== PASS) {
|
|
178
|
+
if (itemResult === BAIL || !inUnion) return itemResult;
|
|
179
|
+
result = FAIL;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
//#endregion
|
|
1
186
|
//#region packages/typescript/src/runtime/validator.ts
|
|
2
187
|
function _define_property(obj, key, value) {
|
|
3
188
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
@@ -9,7 +194,6 @@ function _define_property(obj, key, value) {
|
|
|
9
194
|
else obj[key] = value;
|
|
10
195
|
return obj;
|
|
11
196
|
}
|
|
12
|
-
const regexCache = /* @__PURE__ */ new Map();
|
|
13
197
|
/**
|
|
14
198
|
* Validates values against an {@link TAtscriptAnnotatedType} definition.
|
|
15
199
|
*
|
|
@@ -30,7 +214,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
30
214
|
*
|
|
31
215
|
* @typeParam T - The annotated type definition.
|
|
32
216
|
* @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
|
|
33
|
-
*/ var Validator = class {
|
|
217
|
+
*/ var Validator = class Validator {
|
|
34
218
|
buildPath() {
|
|
35
219
|
if (this.depth <= 0) return "";
|
|
36
220
|
let path = this.pathSegments[0];
|
|
@@ -84,6 +268,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
84
268
|
this.stackErrors.length = 0;
|
|
85
269
|
this.depth = 0;
|
|
86
270
|
this.limitExceeded = false;
|
|
271
|
+
if (this.shortcuts && !this.opts.skipList && typeof this.opts.partial !== "function" && fastCheckPasses(this.def, value, this.opts)) return true;
|
|
87
272
|
this.context = context;
|
|
88
273
|
const passed = this.validateSafe(this.def, value);
|
|
89
274
|
this.context = void 0;
|
|
@@ -93,6 +278,14 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
93
278
|
}
|
|
94
279
|
return true;
|
|
95
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Plain `Validator` without plugins / `replace`: eligible for the allocation-free
|
|
283
|
+
* shortcuts. Subclasses may override the protected `validate*` hooks, so they
|
|
284
|
+
* always take the full walk. (A getter, not a constructor-set field: validators are
|
|
285
|
+
* often built per call, so construction stays as cheap as before.)
|
|
286
|
+
*/ get shortcuts() {
|
|
287
|
+
return this.constructor === Validator && !this.hasPlugins && !this.hasReplace;
|
|
288
|
+
}
|
|
96
289
|
validateSafe(def, value) {
|
|
97
290
|
if (this.limitExceeded) return false;
|
|
98
291
|
if (this.hasReplace) {
|
|
@@ -128,6 +321,11 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
128
321
|
}
|
|
129
322
|
validateUnion(def, value) {
|
|
130
323
|
const items = def.type.items;
|
|
324
|
+
if (this.shortcuts) for (const item of items) {
|
|
325
|
+
const type = item.type;
|
|
326
|
+
if (item.optional || type.kind !== "" || type.value === void 0 || type.designType === "phantom") break;
|
|
327
|
+
if (type.value === value) return true;
|
|
328
|
+
}
|
|
131
329
|
let details;
|
|
132
330
|
for (const item of items) {
|
|
133
331
|
this.stackErrors.push(null);
|
|
@@ -171,7 +369,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
171
369
|
return false;
|
|
172
370
|
}
|
|
173
371
|
const minLength = def.metadata.get("expect.minLength");
|
|
174
|
-
if (minLength) {
|
|
372
|
+
if (minLength !== void 0) {
|
|
175
373
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
176
374
|
if (value.length < length) {
|
|
177
375
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
|
|
@@ -180,7 +378,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
180
378
|
}
|
|
181
379
|
}
|
|
182
380
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
183
|
-
if (maxLength) {
|
|
381
|
+
if (maxLength !== void 0) {
|
|
184
382
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
185
383
|
if (value.length > length) {
|
|
186
384
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
|
|
@@ -352,7 +550,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
352
550
|
this.error(`Expected string (decimal), got ${typeOfValue}`);
|
|
353
551
|
return false;
|
|
354
552
|
}
|
|
355
|
-
if (
|
|
553
|
+
if (!DECIMAL_RE.test(value)) {
|
|
356
554
|
this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
|
|
357
555
|
return false;
|
|
358
556
|
}
|
|
@@ -371,7 +569,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
371
569
|
}
|
|
372
570
|
}
|
|
373
571
|
const minLength = def.metadata.get("expect.minLength");
|
|
374
|
-
if (minLength) {
|
|
572
|
+
if (minLength !== void 0) {
|
|
375
573
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
376
574
|
if (value.length < length) {
|
|
377
575
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -380,7 +578,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
380
578
|
}
|
|
381
579
|
}
|
|
382
580
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
383
|
-
if (maxLength) {
|
|
581
|
+
if (maxLength !== void 0) {
|
|
384
582
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
385
583
|
if (value.length > length) {
|
|
386
584
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -391,13 +589,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
391
589
|
const patterns = def.metadata.get("expect.pattern");
|
|
392
590
|
for (const { pattern, flags, message } of patterns || []) {
|
|
393
591
|
if (!pattern) continue;
|
|
394
|
-
|
|
395
|
-
let regex = regexCache.get(cacheKey);
|
|
396
|
-
if (!regex) {
|
|
397
|
-
regex = new RegExp(pattern, flags);
|
|
398
|
-
regexCache.set(cacheKey, regex);
|
|
399
|
-
}
|
|
400
|
-
if (!regex.test(value)) {
|
|
592
|
+
if (!getRegex(pattern, flags).test(value)) {
|
|
401
593
|
this.error(message || `Value is expected to match pattern "${pattern}"`);
|
|
402
594
|
return false;
|
|
403
595
|
}
|
|
@@ -413,7 +605,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
413
605
|
return false;
|
|
414
606
|
}
|
|
415
607
|
const min = def.metadata.get("expect.min");
|
|
416
|
-
if (min) {
|
|
608
|
+
if (min !== void 0) {
|
|
417
609
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
418
610
|
if (value < minValue) {
|
|
419
611
|
const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
|
|
@@ -422,7 +614,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
422
614
|
}
|
|
423
615
|
}
|
|
424
616
|
const max = def.metadata.get("expect.max");
|
|
425
|
-
if (max) {
|
|
617
|
+
if (max !== void 0) {
|
|
426
618
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
427
619
|
if (value > maxValue) {
|
|
428
620
|
const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
|
|
@@ -873,9 +1065,9 @@ function cloneTypeDef(type) {
|
|
|
873
1065
|
items: build(d.type.of)
|
|
874
1066
|
};
|
|
875
1067
|
const minLength = meta.get("expect.minLength");
|
|
876
|
-
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1068
|
+
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
877
1069
|
const maxLength = meta.get("expect.maxLength");
|
|
878
|
-
if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1070
|
+
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
879
1071
|
return schema;
|
|
880
1072
|
},
|
|
881
1073
|
union(d) {
|
|
@@ -918,18 +1110,18 @@ function cloneTypeDef(type) {
|
|
|
918
1110
|
if (schema.type === "string") {
|
|
919
1111
|
if (meta.get("meta.required")) schema.minLength = 1;
|
|
920
1112
|
const minLength = meta.get("expect.minLength");
|
|
921
|
-
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1113
|
+
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
922
1114
|
const maxLength = meta.get("expect.maxLength");
|
|
923
|
-
if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1115
|
+
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
924
1116
|
const patterns = meta.get("expect.pattern");
|
|
925
1117
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
926
1118
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
927
1119
|
}
|
|
928
1120
|
if (schema.type === "number" || schema.type === "integer") {
|
|
929
1121
|
const min = meta.get("expect.min");
|
|
930
|
-
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1122
|
+
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
931
1123
|
const max = meta.get("expect.max");
|
|
932
|
-
if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1124
|
+
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
933
1125
|
}
|
|
934
1126
|
return schema;
|
|
935
1127
|
}
|
|
@@ -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-CW_50s1W.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
|
|
|
@@ -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-B9xrfeoQ.cjs');
|
|
29
29
|
let path = require("path");
|
|
30
30
|
path = __toESM(path);
|
|
31
31
|
let _atscript_core = require("@atscript/core");
|
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-DORafTJq.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
package/dist/utils.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_json_schema = require('./json-schema-
|
|
2
|
+
const require_json_schema = require('./json-schema-B9xrfeoQ.cjs');
|
|
3
3
|
|
|
4
4
|
//#region packages/typescript/src/runtime/coerce.ts
|
|
5
5
|
/**
|
|
@@ -44,36 +44,40 @@ const require_json_schema = require('./json-schema-CtQ_rzJn.cjs');
|
|
|
44
44
|
}
|
|
45
45
|
/** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
|
|
46
46
|
if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
const type = def.type;
|
|
48
|
+
switch (type.kind) {
|
|
49
|
+
case "": {
|
|
50
|
+
const final = type;
|
|
51
|
+
if (final.designType === "phantom") return value;
|
|
52
|
+
const result = scalar(final.designType, value);
|
|
53
|
+
if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
|
|
51
54
|
return result;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
object: (d) => {
|
|
55
|
+
}
|
|
56
|
+
case "object":
|
|
55
57
|
if (!isPlainObject(value)) return NO_MATCH;
|
|
56
|
-
return coerceProps(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
return coerceProps(def, value);
|
|
59
|
+
case "array": return Array.isArray(value) ? coerceItems(value, type.of) : NO_MATCH;
|
|
60
|
+
case "tuple": {
|
|
61
|
+
const items = type.items;
|
|
62
|
+
return Array.isArray(value) && value.length === items.length ? coerceItems(value, void 0, items) : NO_MATCH;
|
|
63
|
+
}
|
|
64
|
+
case "union":
|
|
65
|
+
for (const item of type.items) {
|
|
62
66
|
const result = coerce(item, value);
|
|
63
67
|
if (result !== NO_MATCH) return result;
|
|
64
68
|
}
|
|
65
69
|
return NO_MATCH;
|
|
66
|
-
|
|
67
|
-
intersection: (d) => {
|
|
70
|
+
case "intersection": {
|
|
68
71
|
let current = value;
|
|
69
|
-
for (const item of
|
|
72
|
+
for (const item of type.items) {
|
|
70
73
|
const result = coerce(item, current);
|
|
71
74
|
if (result === NO_MATCH) return NO_MATCH;
|
|
72
75
|
current = result;
|
|
73
76
|
}
|
|
74
77
|
return current;
|
|
75
78
|
}
|
|
76
|
-
|
|
79
|
+
default: throw new Error(`Unknown type kind "${type.kind}"`);
|
|
80
|
+
}
|
|
77
81
|
}
|
|
78
82
|
function scalar(designType, value) {
|
|
79
83
|
switch (designType) {
|
|
@@ -527,7 +531,7 @@ function serializeRef(def, options, visited) {
|
|
|
527
531
|
}
|
|
528
532
|
function serializeNode(def, path, options, visited) {
|
|
529
533
|
const refDepth = options?.refDepth ?? 0;
|
|
530
|
-
const metadata = () => serializeMetadata(def
|
|
534
|
+
const metadata = () => serializeMetadata(def, {
|
|
531
535
|
path,
|
|
532
536
|
kind: def.type.kind
|
|
533
537
|
}, options, {
|
|
@@ -657,7 +661,7 @@ function serializeTypeDef(def, path, options, visited) {
|
|
|
657
661
|
*/ function shallowTarget(target, options, state, withMetadata) {
|
|
658
662
|
return {
|
|
659
663
|
id: target.id ?? "",
|
|
660
|
-
...withMetadata ? { metadata: serializeMetadata(target
|
|
664
|
+
...withMetadata ? { metadata: serializeMetadata(target, {
|
|
661
665
|
path: [],
|
|
662
666
|
kind: target.type.kind
|
|
663
667
|
}, options, state) } : {}
|
|
@@ -686,11 +690,19 @@ function serializeTypeDef(def, path, options, visited) {
|
|
|
686
690
|
ancestors.delete(value);
|
|
687
691
|
return out;
|
|
688
692
|
}
|
|
689
|
-
function serializeMetadata(
|
|
693
|
+
function serializeMetadata(owner, site, options, state) {
|
|
690
694
|
const { path, kind } = site;
|
|
691
695
|
const result = {};
|
|
692
696
|
const ignoreSet = options?.ignoreAnnotations ? new Set(options.ignoreAnnotations) : void 0;
|
|
693
|
-
|
|
697
|
+
const overrides = options?.annotationOverrides?.(owner);
|
|
698
|
+
let entries = owner.metadata.entries();
|
|
699
|
+
if (overrides) {
|
|
700
|
+
const merged = new Map(entries);
|
|
701
|
+
for (const [key, value] of Object.entries(overrides)) if (value === void 0) merged.delete(key);
|
|
702
|
+
else merged.set(key, value);
|
|
703
|
+
entries = merged;
|
|
704
|
+
}
|
|
705
|
+
for (const [key, value] of entries) {
|
|
694
706
|
if (ignoreSet?.has(key)) continue;
|
|
695
707
|
if (options?.processAnnotation) {
|
|
696
708
|
const processed = options.processAnnotation({
|
package/dist/utils.d.ts
CHANGED
|
@@ -80,6 +80,13 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
|
|
|
80
80
|
* @throws {ValidatorError} When validation fails and `safe` is not `true`.
|
|
81
81
|
*/
|
|
82
82
|
validate<TT = DataType>(value: any, safe?: boolean, context?: unknown): value is TT;
|
|
83
|
+
/**
|
|
84
|
+
* Plain `Validator` without plugins / `replace`: eligible for the allocation-free
|
|
85
|
+
* shortcuts. Subclasses may override the protected `validate*` hooks, so they
|
|
86
|
+
* always take the full walk. (A getter, not a constructor-set field: validators are
|
|
87
|
+
* often built per call, so construction stays as cheap as before.)
|
|
88
|
+
*/
|
|
89
|
+
private get shortcuts();
|
|
83
90
|
protected validateSafe(def: TAtscriptAnnotatedType, value: any): boolean;
|
|
84
91
|
protected get path(): string;
|
|
85
92
|
protected validateAnnotatedType(def: TAtscriptAnnotatedType, value: any): boolean;
|
|
@@ -629,6 +636,17 @@ interface TSerializeOptions {
|
|
|
629
636
|
* emits a shallow `{ id, metadata }` at the tail level instead of the full body.
|
|
630
637
|
*/
|
|
631
638
|
refDepth?: number;
|
|
639
|
+
/**
|
|
640
|
+
* Per-node annotation overrides, applied over the node's own metadata BEFORE
|
|
641
|
+
* `ignoreAnnotations` / `processAnnotation` (which still filter them). Called with the owning
|
|
642
|
+
* annotated type node for every serialized metadata block (type nodes and shallow ref /
|
|
643
|
+
* annotation-value targets; never for collapsed `$ref` uses). Keys absent from the node are
|
|
644
|
+
* added, existing keys are replaced, and a key whose value is `undefined` is removed. Must be a
|
|
645
|
+
* pure function of the node: the repeated-type detector compares the nodes' own metadata, and a
|
|
646
|
+
* collapsed use restores to the first node's (overridden) metadata. Runtime metadata is never
|
|
647
|
+
* mutated.
|
|
648
|
+
*/
|
|
649
|
+
annotationOverrides?: (type: TAtscriptAnnotatedType) => Readonly<Record<string, unknown>> | undefined;
|
|
632
650
|
}
|
|
633
651
|
/**
|
|
634
652
|
* Converts a runtime {@link TAtscriptAnnotatedType} into a plain JSON-safe object.
|
package/dist/utils.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTypeOfPrimitive, f as isPhantomType, i as mergeJsonSchemas, l as defineAnnotatedType, m as ValidatorError, n as detectDiscriminator, o as annotate, p as Validator, r as fromJsonSchema, s as cloneRefProp, t as buildJsonSchema, u as isAnnotatedType } from "./json-schema-
|
|
1
|
+
import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTypeOfPrimitive, f as isPhantomType, i as mergeJsonSchemas, l as defineAnnotatedType, m as ValidatorError, n as detectDiscriminator, o as annotate, p as Validator, r as fromJsonSchema, s as cloneRefProp, t as buildJsonSchema, u as isAnnotatedType } from "./json-schema-CW_50s1W.mjs";
|
|
2
2
|
|
|
3
3
|
//#region packages/typescript/src/runtime/coerce.ts
|
|
4
4
|
/**
|
|
@@ -43,36 +43,40 @@ import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTy
|
|
|
43
43
|
}
|
|
44
44
|
/** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
|
|
45
45
|
if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
const type = def.type;
|
|
47
|
+
switch (type.kind) {
|
|
48
|
+
case "": {
|
|
49
|
+
const final = type;
|
|
50
|
+
if (final.designType === "phantom") return value;
|
|
51
|
+
const result = scalar(final.designType, value);
|
|
52
|
+
if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
|
|
50
53
|
return result;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
object: (d) => {
|
|
54
|
+
}
|
|
55
|
+
case "object":
|
|
54
56
|
if (!isPlainObject(value)) return NO_MATCH;
|
|
55
|
-
return coerceProps(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
return coerceProps(def, value);
|
|
58
|
+
case "array": return Array.isArray(value) ? coerceItems(value, type.of) : NO_MATCH;
|
|
59
|
+
case "tuple": {
|
|
60
|
+
const items = type.items;
|
|
61
|
+
return Array.isArray(value) && value.length === items.length ? coerceItems(value, void 0, items) : NO_MATCH;
|
|
62
|
+
}
|
|
63
|
+
case "union":
|
|
64
|
+
for (const item of type.items) {
|
|
61
65
|
const result = coerce(item, value);
|
|
62
66
|
if (result !== NO_MATCH) return result;
|
|
63
67
|
}
|
|
64
68
|
return NO_MATCH;
|
|
65
|
-
|
|
66
|
-
intersection: (d) => {
|
|
69
|
+
case "intersection": {
|
|
67
70
|
let current = value;
|
|
68
|
-
for (const item of
|
|
71
|
+
for (const item of type.items) {
|
|
69
72
|
const result = coerce(item, current);
|
|
70
73
|
if (result === NO_MATCH) return NO_MATCH;
|
|
71
74
|
current = result;
|
|
72
75
|
}
|
|
73
76
|
return current;
|
|
74
77
|
}
|
|
75
|
-
|
|
78
|
+
default: throw new Error(`Unknown type kind "${type.kind}"`);
|
|
79
|
+
}
|
|
76
80
|
}
|
|
77
81
|
function scalar(designType, value) {
|
|
78
82
|
switch (designType) {
|
|
@@ -526,7 +530,7 @@ function serializeRef(def, options, visited) {
|
|
|
526
530
|
}
|
|
527
531
|
function serializeNode(def, path, options, visited) {
|
|
528
532
|
const refDepth = options?.refDepth ?? 0;
|
|
529
|
-
const metadata = () => serializeMetadata(def
|
|
533
|
+
const metadata = () => serializeMetadata(def, {
|
|
530
534
|
path,
|
|
531
535
|
kind: def.type.kind
|
|
532
536
|
}, options, {
|
|
@@ -656,7 +660,7 @@ function serializeTypeDef(def, path, options, visited) {
|
|
|
656
660
|
*/ function shallowTarget(target, options, state, withMetadata) {
|
|
657
661
|
return {
|
|
658
662
|
id: target.id ?? "",
|
|
659
|
-
...withMetadata ? { metadata: serializeMetadata(target
|
|
663
|
+
...withMetadata ? { metadata: serializeMetadata(target, {
|
|
660
664
|
path: [],
|
|
661
665
|
kind: target.type.kind
|
|
662
666
|
}, options, state) } : {}
|
|
@@ -685,11 +689,19 @@ function serializeTypeDef(def, path, options, visited) {
|
|
|
685
689
|
ancestors.delete(value);
|
|
686
690
|
return out;
|
|
687
691
|
}
|
|
688
|
-
function serializeMetadata(
|
|
692
|
+
function serializeMetadata(owner, site, options, state) {
|
|
689
693
|
const { path, kind } = site;
|
|
690
694
|
const result = {};
|
|
691
695
|
const ignoreSet = options?.ignoreAnnotations ? new Set(options.ignoreAnnotations) : void 0;
|
|
692
|
-
|
|
696
|
+
const overrides = options?.annotationOverrides?.(owner);
|
|
697
|
+
let entries = owner.metadata.entries();
|
|
698
|
+
if (overrides) {
|
|
699
|
+
const merged = new Map(entries);
|
|
700
|
+
for (const [key, value] of Object.entries(overrides)) if (value === void 0) merged.delete(key);
|
|
701
|
+
else merged.set(key, value);
|
|
702
|
+
entries = merged;
|
|
703
|
+
}
|
|
704
|
+
for (const [key, value] of entries) {
|
|
693
705
|
if (ignoreSet?.has(key)) continue;
|
|
694
706
|
if (options?.processAnnotation) {
|
|
695
707
|
const processed = options.processAnnotation({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/typescript",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.102",
|
|
4
4
|
"description": "Atscript: typescript-gen support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
75
|
"@atscript/db": "^0.1.97",
|
|
76
|
-
"@atscript/core": "^0.1.
|
|
76
|
+
"@atscript/core": "^0.1.102"
|
|
77
77
|
},
|
|
78
78
|
"peerDependenciesMeta": {
|
|
79
79
|
"@atscript/db": {
|