@atscript/typescript 0.2.5 → 0.2.7
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 +35 -23
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +2 -2
- package/dist/{plugin-Bx7j-vWf.mjs → plugin-DUInOy5v.mjs} +1 -1
- package/dist/{plugin-JJ9MJ6AF.cjs → plugin-lDfE9qm8.cjs} +1 -1
- package/dist/realm-runtime.js +351 -181
- package/dist/test-utils.cjs +2 -2
- package/dist/test-utils.mjs +2 -2
- package/dist/{user-text-Dl8FNKLL.cjs → user-text-BvyErcKK.cjs} +29 -35
- package/dist/{user-text-BOf56N4d.mjs → user-text-DZR5hfLa.mjs} +24 -24
- package/dist/utils.cjs +329 -159
- package/dist/utils.d.ts +33 -13
- package/dist/utils.mjs +329 -160
- package/package.json +2 -2
package/dist/utils.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ as
|
|
1
|
+
import { $ as VALIDATOR_KEYS, A as propAtChain, B as registerCompiledCheck, C as createAnnotatedTypeNode, D as isAnnotatedType, E as defineAnnotatedType, F as VALIDATOR_MESSAGES, G as $nr, H as jsonSchemaPattern, I as formatCodeOf, J as watchLayer, K as LayerMap, L as formatValidatorMessage, M as ValidatorError, N as unionDiscriminator, O as isAnnotatedTypeOfPrimitive, P as VALIDATOR_ERROR_CODES, Q as DECIMAL_RE, R as localizeErrors, S as annotate, T as createObjectTypeDef, U as statelessRegExp, V as detectDiscriminator, W as $ak, X as touched, Y as invalidateCompiledValidators, Z as typeWriteCount, _ as pendingStatements, a as userTextOf, b as cutKey, c as $blk, d as $fc, et as arrayKeyProps, f as $ft, g as appliedBlockModules, h as $sh, i as userTextKeys, j as Validator, k as isPhantomType, l as $end, m as $q, o as uniqueName, p as $ps, q as metaEmpty, r as registerUserText, s as $alias, t as $ut, tt as boundOf, u as $f, w as createMetadataMap, x as enterCut, y as cutAs, z as messagesTranslator } from "./user-text-DZR5hfLa.mjs";
|
|
2
2
|
|
|
3
3
|
//#region packages/typescript/src/runtime/copy-on-change.ts
|
|
4
4
|
/**
|
|
@@ -84,122 +84,49 @@ import { $ as DECIMAL_RE, A as propAtChain, B as registerCompiledCheck, C as cre
|
|
|
84
84
|
else out[key] = value;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
//#endregion
|
|
88
|
-
//#region packages/typescript/src/runtime/coerce.ts
|
|
89
|
-
/**
|
|
90
|
-
* Coerces string-transport input (route params, query strings) toward the
|
|
91
|
-
* scalar shapes an annotated type expects.
|
|
92
|
-
*
|
|
93
|
-
* Pure and non-throwing: when a value can't be coerced it is returned
|
|
94
|
-
* untouched, leaving the {@link Validator} to report the proper error.
|
|
95
|
-
* Coercion converts representation only — constraint checks (`@expect.int`,
|
|
96
|
-
* `@expect.min`, patterns) remain the validator's job.
|
|
97
|
-
*
|
|
98
|
-
* Rules:
|
|
99
|
-
* - Strings targeting `number` are trimmed and parsed via `Number()`; only
|
|
100
|
-
* finite results are accepted.
|
|
101
|
-
* - Strings targeting `boolean` accept `"true"/"1"` and `"false"/"0"`.
|
|
102
|
-
* - Unions try each branch in declared order; the first successful parse wins.
|
|
103
|
-
* - Objects recurse into props when the input is a plain object (covers
|
|
104
|
-
* `@Query()` DTOs where every field arrives as a string).
|
|
105
|
-
* - Arrays and tuples coerce items; intersections apply each member in order.
|
|
106
|
-
* - `string` / `decimal` targets never change string input.
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* ```ts
|
|
110
|
-
* coerceForType(KafkaOffset, '42') // → 42
|
|
111
|
-
* coerceForType(KafkaOffset, 'abc') // → 'abc' (validator reports the error)
|
|
112
|
-
* ```
|
|
113
|
-
*/ function coerceForType(def, value) {
|
|
114
|
-
const result = coerce(def, value);
|
|
115
|
-
return result === NO_MATCH ? value : result;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Coerces a single scalar value toward a design type, using the same rules
|
|
119
|
-
* as {@link coerceForType}. Returns the input untouched when it can't coerce.
|
|
120
|
-
* Useful when only a design type is known (e.g. `design:paramtypes` fallbacks).
|
|
121
|
-
*/ function coerceScalar(designType, value) {
|
|
122
|
-
const result = scalar(designType, value);
|
|
123
|
-
return result === NO_MATCH ? value : result;
|
|
124
|
-
}
|
|
125
|
-
/** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
|
|
126
|
-
if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
|
|
127
|
-
const type = def.type;
|
|
128
|
-
switch (type.kind) {
|
|
129
|
-
case "": {
|
|
130
|
-
const final = type;
|
|
131
|
-
if (final.designType === "phantom") return value;
|
|
132
|
-
const result = scalar(final.designType, value);
|
|
133
|
-
if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
|
|
134
|
-
return result;
|
|
135
|
-
}
|
|
136
|
-
case "object":
|
|
137
|
-
if (!isPlainObject$1(value)) return NO_MATCH;
|
|
138
|
-
return mapProps(type, value, coerceChild, void 0);
|
|
139
|
-
case "array": return Array.isArray(value) ? mapItems(value, type.of, coerceChild, void 0) : NO_MATCH;
|
|
140
|
-
case "tuple": {
|
|
141
|
-
const items = type.items;
|
|
142
|
-
return Array.isArray(value) && value.length === items.length ? mapItems(value, items, coerceChild, void 0) : NO_MATCH;
|
|
143
|
-
}
|
|
144
|
-
case "union":
|
|
145
|
-
for (const item of type.items) {
|
|
146
|
-
const result = coerce(item, value);
|
|
147
|
-
if (result !== NO_MATCH) return result;
|
|
148
|
-
}
|
|
149
|
-
return NO_MATCH;
|
|
150
|
-
case "intersection": {
|
|
151
|
-
let current = value;
|
|
152
|
-
for (const item of type.items) {
|
|
153
|
-
const result = coerce(item, current);
|
|
154
|
-
if (result === NO_MATCH) return NO_MATCH;
|
|
155
|
-
current = result;
|
|
156
|
-
}
|
|
157
|
-
return current;
|
|
158
|
-
}
|
|
159
|
-
default: throw new Error(`Unknown type kind "${type.kind}"`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
function scalar(designType, value) {
|
|
163
|
-
switch (designType) {
|
|
164
|
-
case "string":
|
|
165
|
-
case "decimal": return typeof value === "string" ? value : NO_MATCH;
|
|
166
|
-
case "number":
|
|
167
|
-
if (typeof value === "number") return value;
|
|
168
|
-
if (typeof value === "string") {
|
|
169
|
-
const trimmed = value.trim();
|
|
170
|
-
if (trimmed.length > 0) {
|
|
171
|
-
const parsed = Number(trimmed);
|
|
172
|
-
if (Number.isFinite(parsed)) return parsed;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return NO_MATCH;
|
|
176
|
-
case "boolean":
|
|
177
|
-
if (typeof value === "boolean") return value;
|
|
178
|
-
if (value === "true" || value === "1") return true;
|
|
179
|
-
if (value === "false" || value === "0") return false;
|
|
180
|
-
return NO_MATCH;
|
|
181
|
-
case "object": return typeof value === "object" ? value : NO_MATCH;
|
|
182
|
-
case "any":
|
|
183
|
-
case "phantom": return value;
|
|
184
|
-
default: return NO_MATCH;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
/** A prop / item: its coerced value, else the value as it is (copy-on-change, see `mapProps`). */ function coerceChild(def, input) {
|
|
188
|
-
const result = coerce(def, input);
|
|
189
|
-
return result === NO_MATCH ? input : result;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
87
|
//#endregion
|
|
193
88
|
//#region packages/typescript/src/runtime/normalize.ts
|
|
194
89
|
const TRIM = 1;
|
|
195
90
|
const LOWERCASE = 2;
|
|
196
91
|
const UPPERCASE = 4;
|
|
197
92
|
/**
|
|
198
|
-
* Whether each node's subtree has normalizers: valid until the next
|
|
199
|
-
* `invalidateCompiledValidators()` (checked by {@link normalizeForType}).
|
|
93
|
+
* Whether each node's subtree has normalizers: valid until the next type tree write (metadata or
|
|
94
|
+
* shape) or `invalidateCompiledValidators()` (checked by {@link normalizeForType}).
|
|
200
95
|
*/ let reach = /* @__PURE__ */ new WeakMap();
|
|
201
96
|
let stampWrites = -1;
|
|
202
|
-
|
|
97
|
+
function newJudges(opts) {
|
|
98
|
+
const root = {
|
|
99
|
+
...opts,
|
|
100
|
+
errorLimit: 1
|
|
101
|
+
};
|
|
102
|
+
delete root.translate;
|
|
103
|
+
if (root.unknownProps === "strip") root.unknownProps = "ignore";
|
|
104
|
+
const judge = {
|
|
105
|
+
opts: root,
|
|
106
|
+
validators: /* @__PURE__ */ new WeakMap()
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
root: judge,
|
|
110
|
+
nested: root.partial === true ? {
|
|
111
|
+
opts: {
|
|
112
|
+
...root,
|
|
113
|
+
partial: false
|
|
114
|
+
},
|
|
115
|
+
validators: /* @__PURE__ */ new WeakMap()
|
|
116
|
+
} : judge
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const DEFAULT_JUDGES = newJudges({});
|
|
120
|
+
/** The judges by the options object passed (the same options: the same validators). */ const judgesByOpts = /* @__PURE__ */ new WeakMap();
|
|
121
|
+
function judgesFor(opts) {
|
|
122
|
+
if (!opts) return DEFAULT_JUDGES;
|
|
123
|
+
let found = judgesByOpts.get(opts);
|
|
124
|
+
if (!found) {
|
|
125
|
+
found = newJudges(opts);
|
|
126
|
+
judgesByOpts.set(opts, found);
|
|
127
|
+
}
|
|
128
|
+
return found;
|
|
129
|
+
}
|
|
203
130
|
/**
|
|
204
131
|
* Normalizes input toward the string forms an annotated type declares with `@normalize.trim`,
|
|
205
132
|
* `@normalize.lowercase` and `@normalize.uppercase`.
|
|
@@ -220,9 +147,11 @@ let stampEpoch = -1;
|
|
|
220
147
|
* `detectDiscriminator`) uses the branch the value's discriminator names;
|
|
221
148
|
* - otherwise every branch that can take the value is tried (a literal branch can take a string
|
|
222
149
|
* only when the normalized string equals its literal): when they all give the same result it
|
|
223
|
-
* is used; when they differ, the branches whose result their own validator accepts
|
|
224
|
-
*
|
|
225
|
-
*
|
|
150
|
+
* is used; when they differ, the branches whose result their own validator accepts are kept,
|
|
151
|
+
* and the result is used when they agree; else the value is returned untouched (ambiguous).
|
|
152
|
+
* The branch validators take `opts` — pass the options the result will be validated with
|
|
153
|
+
* (`partial`, `unknownProps`, …) so a branch is judged as the validator will judge it; default
|
|
154
|
+
* options when omitted. (`unknownProps: 'strip'` judges as `'ignore'`: nothing is stripped.)
|
|
226
155
|
*
|
|
227
156
|
* The intended flow is `coerceForType` → `normalizeForType` → `validate`.
|
|
228
157
|
*
|
|
@@ -230,17 +159,16 @@ let stampEpoch = -1;
|
|
|
230
159
|
* ```ts
|
|
231
160
|
* // @normalize.trim @normalize.lowercase email: string.email
|
|
232
161
|
* normalizeForType(User, { email: ' Ann@Example.COM ' }) // → { email: 'ann@example.com' }
|
|
162
|
+
* // judged as a partial update will be validated
|
|
163
|
+
* normalizeForType(Patch, body, { partial: true })
|
|
233
164
|
* ```
|
|
234
|
-
*/ function normalizeForType(def, value) {
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
if (!hasNormalizers(def)) return value;
|
|
243
|
-
const result = normalize(def, value, 0, /* @__PURE__ */ new Map());
|
|
165
|
+
*/ function normalizeForType(def, value, opts) {
|
|
166
|
+
if (!typeHasNormalizers(def)) return value;
|
|
167
|
+
const result = normalize(def, value, 0, {
|
|
168
|
+
memo: /* @__PURE__ */ new Map(),
|
|
169
|
+
root: value,
|
|
170
|
+
opts
|
|
171
|
+
});
|
|
244
172
|
return result === NO_MATCH ? value : result;
|
|
245
173
|
}
|
|
246
174
|
/** The nodes `def`'s value walk can enter (props, pattern props, items, elements). */ function childrenOf(def) {
|
|
@@ -260,6 +188,18 @@ let stampEpoch = -1;
|
|
|
260
188
|
}
|
|
261
189
|
}
|
|
262
190
|
/**
|
|
191
|
+
* Whether {@link normalizeForType} can change a value of type `def`: `def` or a type its walk
|
|
192
|
+
* reaches (props, pattern props, items, union and intersection members) carries `@normalize.*`.
|
|
193
|
+
* Cached per type and kept current across type writes, so callers can skip their own walks.
|
|
194
|
+
*/ function typeHasNormalizers(def) {
|
|
195
|
+
const writes = typeWriteCount();
|
|
196
|
+
if (writes !== stampWrites) {
|
|
197
|
+
stampWrites = writes;
|
|
198
|
+
reach = /* @__PURE__ */ new WeakMap();
|
|
199
|
+
}
|
|
200
|
+
return hasNormalizers(def);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
263
203
|
* Whether `def` or a node its walk reaches has normalizers. Answers (and caches) every node
|
|
264
204
|
* reached, recursive types included: collects the reached nodes with their parents, then marks
|
|
265
205
|
* the nodes with normalizers and, back along the parents, every node that reaches one.
|
|
@@ -305,7 +245,7 @@ let stampEpoch = -1;
|
|
|
305
245
|
if (meta.get("normalize.uppercase")) flags |= UPPERCASE;
|
|
306
246
|
return flags;
|
|
307
247
|
}
|
|
308
|
-
function applyFlags(value, flags) {
|
|
248
|
+
/** `value` with normalizer `flags` applied, in their fixed order (see {@link flagsOf}). */ function applyFlags(value, flags) {
|
|
309
249
|
let out = value;
|
|
310
250
|
if (flags & TRIM) out = out.trim();
|
|
311
251
|
if (flags & LOWERCASE) out = out.toLowerCase();
|
|
@@ -319,37 +259,37 @@ function applyFlags(value, flags) {
|
|
|
319
259
|
* Objects and unions are memoized per call by input object: a recursive union without a
|
|
320
260
|
* discriminator is tried for every branch at every level, which without it is exponential in
|
|
321
261
|
* the depth of the value.
|
|
322
|
-
*/ function normalize(def, value, inherited,
|
|
262
|
+
*/ function normalize(def, value, inherited, walk) {
|
|
323
263
|
if (value === void 0 || value === null) return value;
|
|
324
|
-
if (inherited === 0 && !hasNormalizers(def)) return takes(def, value) ? value : NO_MATCH;
|
|
264
|
+
if (inherited === 0 && !hasNormalizers(def)) return takes$1(def, value) ? value : NO_MATCH;
|
|
325
265
|
const type = def.type;
|
|
326
266
|
switch (type.kind) {
|
|
327
267
|
case "": {
|
|
328
268
|
const out = typeof value === "string" && type.designType === "string" ? applyFlags(value, flagsOf(def, inherited)) : value;
|
|
329
|
-
return takes(def, out) ? out : NO_MATCH;
|
|
269
|
+
return takes$1(def, out) ? out : NO_MATCH;
|
|
330
270
|
}
|
|
331
271
|
case "object": {
|
|
332
272
|
if (!isPlainObject$1(value)) return NO_MATCH;
|
|
333
|
-
const entries = entriesOf(memo, value);
|
|
334
|
-
return recall(entries, type, 0) ?? remember(entries, type, 0, mapProps(type, value, normalizeProp,
|
|
273
|
+
const entries = entriesOf(walk.memo, value);
|
|
274
|
+
return recall(entries, type, 0) ?? remember(entries, type, 0, mapProps(type, value, normalizeProp, walk));
|
|
335
275
|
}
|
|
336
|
-
case "array": return Array.isArray(value) ? mapItems(value, type.of, normalizeItem,
|
|
276
|
+
case "array": return Array.isArray(value) ? mapItems(value, type.of, normalizeItem, walk) : NO_MATCH;
|
|
337
277
|
case "tuple": {
|
|
338
278
|
const items = type.items;
|
|
339
|
-
return Array.isArray(value) && value.length === items.length ? mapItems(value, items, normalizeItem,
|
|
279
|
+
return Array.isArray(value) && value.length === items.length ? mapItems(value, items, normalizeItem, walk) : NO_MATCH;
|
|
340
280
|
}
|
|
341
281
|
case "union": {
|
|
342
282
|
const flags = flagsOf(def, inherited);
|
|
343
283
|
const items = type.items;
|
|
344
|
-
if (typeof value !== "object") return normalizeUnion(items, value, flags,
|
|
345
|
-
const entries = entriesOf(memo, value);
|
|
346
|
-
return recall(entries, type, flags) ?? remember(entries, type, flags, normalizeUnion(items, value, flags,
|
|
284
|
+
if (typeof value !== "object") return normalizeUnion(items, value, flags, walk);
|
|
285
|
+
const entries = entriesOf(walk.memo, value);
|
|
286
|
+
return recall(entries, type, flags) ?? remember(entries, type, flags, normalizeUnion(items, value, flags, walk));
|
|
347
287
|
}
|
|
348
288
|
case "intersection": {
|
|
349
289
|
const flags = flagsOf(def, inherited);
|
|
350
290
|
let current = value;
|
|
351
291
|
for (const item of type.items) {
|
|
352
|
-
const result = normalize(item, current, flags,
|
|
292
|
+
const result = normalize(item, current, flags, walk);
|
|
353
293
|
if (result === NO_MATCH) return NO_MATCH;
|
|
354
294
|
current = result;
|
|
355
295
|
}
|
|
@@ -377,20 +317,47 @@ function applyFlags(value, flags) {
|
|
|
377
317
|
]);
|
|
378
318
|
return result;
|
|
379
319
|
}
|
|
380
|
-
/** An array / tuple item: its normalized value, else the value as it is. */ function normalizeItem(def, input,
|
|
320
|
+
/** An array / tuple item: its normalized value, else the value as it is. */ function normalizeItem(def, input, walk) {
|
|
381
321
|
if (!hasNormalizers(def)) return input;
|
|
382
|
-
const result = normalize(def, input, 0,
|
|
322
|
+
const result = normalize(def, input, 0, walk);
|
|
383
323
|
return result === NO_MATCH ? input : result;
|
|
384
324
|
}
|
|
385
325
|
/**
|
|
386
326
|
* A prop: as {@link normalizeItem}, but NO_MATCH (the object is not this type) when a literal prop
|
|
387
|
-
* (`kind: 'cat'`) holds another value.
|
|
388
|
-
*/ function normalizeProp(def, input,
|
|
389
|
-
const out = normalizeItem(def, input,
|
|
390
|
-
if (out === input &&
|
|
327
|
+
* (`kind: 'cat'`, `kind: 'cat' | null`, see {@link literalNodes}) holds another value.
|
|
328
|
+
*/ function normalizeProp(def, input, walk) {
|
|
329
|
+
const out = normalizeItem(def, input, walk);
|
|
330
|
+
if (out === input && input !== void 0 && input !== null) {
|
|
331
|
+
const literals = literalNodes(def);
|
|
332
|
+
if (literals && !literals.some((l) => l.node.type.value === input)) return NO_MATCH;
|
|
333
|
+
}
|
|
391
334
|
return out;
|
|
392
335
|
}
|
|
393
|
-
/**
|
|
336
|
+
/**
|
|
337
|
+
* The literals a literal prop can hold: `def` itself when it is a literal (`kind: 'cat'`), the
|
|
338
|
+
* literals of a union of literals, `null` and `undefined` (`kind: 'cat' | null`, nested unions and
|
|
339
|
+
* aliases too); `undefined` for any other type. Each with the normalizers of the nodes on its way
|
|
340
|
+
* (a union's apply to its members). A literal prop holding another value tells that an object is
|
|
341
|
+
* not of its type, for normalization and coercion alike (see `coerce.ts`).
|
|
342
|
+
*/ function literalNodes(def, inherited = 0) {
|
|
343
|
+
const type = def.type;
|
|
344
|
+
if (type.kind === "") return type.value === void 0 ? void 0 : [{
|
|
345
|
+
node: def,
|
|
346
|
+
flags: flagsOf(def, inherited)
|
|
347
|
+
}];
|
|
348
|
+
if (type.kind !== "union") return;
|
|
349
|
+
const flags = flagsOf(def, inherited);
|
|
350
|
+
const out = [];
|
|
351
|
+
for (const item of type.items) {
|
|
352
|
+
const designType = item.type.designType;
|
|
353
|
+
if (item.type.kind === "" && (designType === "null" || designType === "undefined")) continue;
|
|
354
|
+
const inner = literalNodes(item, flags);
|
|
355
|
+
if (!inner) return;
|
|
356
|
+
out.push(...inner);
|
|
357
|
+
}
|
|
358
|
+
return out.length > 0 ? out : void 0;
|
|
359
|
+
}
|
|
360
|
+
/** Whether `def` can take `value` by its kind alone (see {@link normalize}). */ function takes$1(def, value) {
|
|
394
361
|
const type = def.type;
|
|
395
362
|
switch (type.kind) {
|
|
396
363
|
case "": {
|
|
@@ -409,8 +376,8 @@ function applyFlags(value, flags) {
|
|
|
409
376
|
case "object": return isPlainObject$1(value);
|
|
410
377
|
case "array": return Array.isArray(value);
|
|
411
378
|
case "tuple": return Array.isArray(value) && value.length === type.items.length;
|
|
412
|
-
case "union": return type.items.some((item) => takes(item, value));
|
|
413
|
-
case "intersection": return type.items.every((item) => takes(item, value));
|
|
379
|
+
case "union": return type.items.some((item) => takes$1(item, value));
|
|
380
|
+
case "intersection": return type.items.every((item) => takes$1(item, value));
|
|
414
381
|
default: return false;
|
|
415
382
|
}
|
|
416
383
|
}
|
|
@@ -418,19 +385,19 @@ function applyFlags(value, flags) {
|
|
|
418
385
|
* A union: the branch a discriminator names, else the one result every branch that can take
|
|
419
386
|
* the value agrees on — or, when they differ, the one result of the branches whose validator
|
|
420
387
|
* accepts their result (NO_MATCH when none can take the value or it stays ambiguous).
|
|
421
|
-
*/ function normalizeUnion(items, value, flags,
|
|
388
|
+
*/ function normalizeUnion(items, value, flags, walk) {
|
|
422
389
|
if (isPlainObject$1(value)) {
|
|
423
390
|
const disc = unionDiscriminator(items);
|
|
424
391
|
if (disc) {
|
|
425
392
|
const tag = value[disc.property];
|
|
426
|
-
for (const [index, literal] of disc.branches) if (literal === tag) return normalize(items[index], value, flags,
|
|
393
|
+
for (const [index, literal] of disc.branches) if (literal === tag) return normalize(items[index], value, flags, walk);
|
|
427
394
|
}
|
|
428
395
|
}
|
|
429
396
|
let first = NO_MATCH;
|
|
430
397
|
let firstItem;
|
|
431
398
|
let rest;
|
|
432
399
|
for (const item of items) {
|
|
433
|
-
const out = normalize(item, value, flags,
|
|
400
|
+
const out = normalize(item, value, flags, walk);
|
|
434
401
|
if (out === NO_MATCH) continue;
|
|
435
402
|
if (first === NO_MATCH) {
|
|
436
403
|
first = out;
|
|
@@ -439,23 +406,21 @@ function applyFlags(value, flags) {
|
|
|
439
406
|
}
|
|
440
407
|
if (!rest || rest.every(([, out]) => sameValue(out, first))) return first;
|
|
441
408
|
rest.unshift([firstItem, first]);
|
|
409
|
+
const judges = judgesFor(walk.opts);
|
|
410
|
+
const judge = value === walk.root ? judges.root : judges.nested;
|
|
442
411
|
let accepted = NO_MATCH;
|
|
443
412
|
for (const [item, out] of rest) {
|
|
444
|
-
if (!branchValidator(item).validate(out, true)) continue;
|
|
413
|
+
if (!branchValidator(judge, item).validate(out, true)) continue;
|
|
445
414
|
if (accepted === NO_MATCH) accepted = out;
|
|
446
415
|
else if (!sameValue(accepted, out)) return NO_MATCH;
|
|
447
416
|
}
|
|
448
417
|
return accepted;
|
|
449
418
|
}
|
|
450
|
-
/**
|
|
451
|
-
|
|
452
|
-
* judging its own normalized result.
|
|
453
|
-
*/ const branchValidators = /* @__PURE__ */ new WeakMap();
|
|
454
|
-
function branchValidator(def) {
|
|
455
|
-
let validator = branchValidators.get(def);
|
|
419
|
+
/** Union branch `def`'s validator, judging its own normalized result (see {@link TJudges}). */ function branchValidator(judge, def) {
|
|
420
|
+
let validator = judge.validators.get(def);
|
|
456
421
|
if (!validator) {
|
|
457
|
-
validator = new Validator(def,
|
|
458
|
-
|
|
422
|
+
validator = new Validator(def, judge.opts);
|
|
423
|
+
judge.validators.set(def, validator);
|
|
459
424
|
}
|
|
460
425
|
return validator;
|
|
461
426
|
}
|
|
@@ -469,6 +434,210 @@ function branchValidator(def) {
|
|
|
469
434
|
return false;
|
|
470
435
|
}
|
|
471
436
|
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region packages/typescript/src/runtime/coerce.ts
|
|
439
|
+
/**
|
|
440
|
+
* Coerces string-transport input (route params, query strings) toward the
|
|
441
|
+
* scalar shapes an annotated type expects.
|
|
442
|
+
*
|
|
443
|
+
* Pure and non-throwing: when a value can't be coerced it is returned
|
|
444
|
+
* untouched, leaving the {@link Validator} to report the proper error.
|
|
445
|
+
* Coercion converts representation only — constraint checks (`@expect.int`,
|
|
446
|
+
* `@expect.min`, patterns) remain the validator's job.
|
|
447
|
+
*
|
|
448
|
+
* Rules:
|
|
449
|
+
* - Strings targeting `number` are trimmed and parsed via `Number()`; only
|
|
450
|
+
* finite results are accepted.
|
|
451
|
+
* - Strings targeting `boolean` accept `"true"/"1"` and `"false"/"0"`.
|
|
452
|
+
* - Unions pick one branch and coerce the value against it only (linear, no
|
|
453
|
+
* backtracking). A scalar branch takes what it parses; an object (tuple)
|
|
454
|
+
* branch takes a plain object (an array of its length) unless a literal
|
|
455
|
+
* property (item) — `kind: 'cat'`, `kind: 'cat' | null` — holds a value that
|
|
456
|
+
* does not coerce to it, a string compared after the normalizers of the
|
|
457
|
+
* literal and of the unions it is in. An object takes the branch its
|
|
458
|
+
* discriminator names (as the validator does: the literal itself first, else
|
|
459
|
+
* `"2"` names a `2` literal), else the first whose shape it has (required
|
|
460
|
+
* props present, no undeclared key), else the first that takes it. When no
|
|
461
|
+
* branch takes the value, it is returned untouched.
|
|
462
|
+
* - Objects recurse into props when the input is a plain object (covers
|
|
463
|
+
* `@Query()` DTOs where every field arrives as a string), whatever their
|
|
464
|
+
* literal props hold unless tried as a union branch.
|
|
465
|
+
* - Arrays and tuples coerce items; intersections apply each member in order.
|
|
466
|
+
* - `string` / `decimal` targets never change string input.
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* ```ts
|
|
470
|
+
* coerceForType(KafkaOffset, '42') // → 42
|
|
471
|
+
* coerceForType(KafkaOffset, 'abc') // → 'abc' (validator reports the error)
|
|
472
|
+
* ```
|
|
473
|
+
*/ function coerceForType(def, value) {
|
|
474
|
+
const result = coerce(def, value);
|
|
475
|
+
return result === NO_MATCH ? value : result;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Coerces a single scalar value toward a design type, using the same rules
|
|
479
|
+
* as {@link coerceForType}. Returns the input untouched when it can't coerce.
|
|
480
|
+
* Useful when only a design type is known (e.g. `design:paramtypes` fallbacks).
|
|
481
|
+
*/ function coerceScalar(designType, value) {
|
|
482
|
+
const result = scalar(designType, value);
|
|
483
|
+
return result === NO_MATCH ? value : result;
|
|
484
|
+
}
|
|
485
|
+
/** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
|
|
486
|
+
if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
|
|
487
|
+
const type = def.type;
|
|
488
|
+
switch (type.kind) {
|
|
489
|
+
case "": {
|
|
490
|
+
const final = type;
|
|
491
|
+
if (final.designType === "phantom") return value;
|
|
492
|
+
const result = scalar(final.designType, value);
|
|
493
|
+
if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
|
|
494
|
+
return result;
|
|
495
|
+
}
|
|
496
|
+
case "object":
|
|
497
|
+
if (!isPlainObject$1(value)) return NO_MATCH;
|
|
498
|
+
return mapProps(type, value, coerceChild, void 0);
|
|
499
|
+
case "array": return Array.isArray(value) ? mapItems(value, type.of, coerceChild, void 0) : NO_MATCH;
|
|
500
|
+
case "tuple": {
|
|
501
|
+
const items = type.items;
|
|
502
|
+
return Array.isArray(value) && value.length === items.length ? mapItems(value, items, coerceChild, void 0) : NO_MATCH;
|
|
503
|
+
}
|
|
504
|
+
case "union": {
|
|
505
|
+
const branch = branchOf(type.items, value);
|
|
506
|
+
return branch ? coerce(branch, value) : NO_MATCH;
|
|
507
|
+
}
|
|
508
|
+
case "intersection": {
|
|
509
|
+
let current = value;
|
|
510
|
+
for (const item of type.items) {
|
|
511
|
+
const result = coerce(item, current);
|
|
512
|
+
if (result === NO_MATCH) return NO_MATCH;
|
|
513
|
+
current = result;
|
|
514
|
+
}
|
|
515
|
+
return current;
|
|
516
|
+
}
|
|
517
|
+
default: throw new Error(`Unknown type kind "${type.kind}"`);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
function scalar(designType, value) {
|
|
521
|
+
switch (designType) {
|
|
522
|
+
case "string":
|
|
523
|
+
case "decimal": return typeof value === "string" ? value : NO_MATCH;
|
|
524
|
+
case "number":
|
|
525
|
+
if (typeof value === "number") return value;
|
|
526
|
+
if (typeof value === "string") {
|
|
527
|
+
const trimmed = value.trim();
|
|
528
|
+
if (trimmed.length > 0) {
|
|
529
|
+
const parsed = Number(trimmed);
|
|
530
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return NO_MATCH;
|
|
534
|
+
case "boolean":
|
|
535
|
+
if (typeof value === "boolean") return value;
|
|
536
|
+
if (value === "true" || value === "1") return true;
|
|
537
|
+
if (value === "false" || value === "0") return false;
|
|
538
|
+
return NO_MATCH;
|
|
539
|
+
case "object": return typeof value === "object" ? value : NO_MATCH;
|
|
540
|
+
case "any":
|
|
541
|
+
case "phantom": return value;
|
|
542
|
+
default: return NO_MATCH;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* The branch of union `items` that takes `value` (see {@link takes}): the one an object's
|
|
547
|
+
* discriminator names; else, for an object, the first that takes it and whose shape it has (see
|
|
548
|
+
* {@link fits}); else the first that takes it, in declared order (a partial patch); `undefined`
|
|
549
|
+
* when none does.
|
|
550
|
+
*/ function branchOf(items, value) {
|
|
551
|
+
let first;
|
|
552
|
+
if (isPlainObject$1(value)) {
|
|
553
|
+
const named = discriminatedBranch(items, value);
|
|
554
|
+
if (named && takes(named, value)) return named;
|
|
555
|
+
for (const item of items) if (takes(item, value)) {
|
|
556
|
+
if (fits(item, value)) return item;
|
|
557
|
+
first ?? (first = item);
|
|
558
|
+
}
|
|
559
|
+
return first;
|
|
560
|
+
}
|
|
561
|
+
return items.find((item) => takes(item, value));
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Whether object `value` has the shape of branch `def`, by its own keys alone: every required
|
|
565
|
+
* prop is one of them, and each of them is a prop (or matches a prop pattern) of the branch. A
|
|
566
|
+
* union: one of its branches; a branch that is not an object (nor an intersection of objects)
|
|
567
|
+
* has any shape.
|
|
568
|
+
*/ function fits(def, value) {
|
|
569
|
+
if (def.type.kind === "union") return def.type.items.some((item) => fits(item, value));
|
|
570
|
+
const objects = [];
|
|
571
|
+
if (!objectsOf(def, objects)) return true;
|
|
572
|
+
for (const o of objects) for (const [key, prop] of o.props) if (!prop.optional && !Object.hasOwn(value, key)) return false;
|
|
573
|
+
return Object.keys(value).every((key) => objects.some((o) => o.props.has(key) || o.propsPatterns.some((p) => p.pattern.test(key))));
|
|
574
|
+
}
|
|
575
|
+
/** Collects the objects `def` is (an object, an intersection of objects); `false` for others. */ function objectsOf(def, out) {
|
|
576
|
+
const type = def.type;
|
|
577
|
+
if (type.kind === "object") {
|
|
578
|
+
out.push(type);
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
return type.kind === "intersection" && type.items.every((item) => objectsOf(item, out));
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Whether union branch `def` takes `value`: exactly when coercing `value` against it does not
|
|
585
|
+
* fail (a branch taken never fails after it), decided from `value` itself — its kind, its length,
|
|
586
|
+
* its direct literal props and items — never from what it holds deeper. A union takes one branch
|
|
587
|
+
* once, so coercion is linear in the value.
|
|
588
|
+
*/ function takes(def, value) {
|
|
589
|
+
if (value === void 0 || value === null) return def.optional === true;
|
|
590
|
+
const type = def.type;
|
|
591
|
+
switch (type.kind) {
|
|
592
|
+
case "": return coerce(def, value) !== NO_MATCH;
|
|
593
|
+
case "object":
|
|
594
|
+
if (!isPlainObject$1(value)) return false;
|
|
595
|
+
for (const [key, prop] of type.props) if (Object.hasOwn(value, key) && !holdsLiteral(prop, value[key])) return false;
|
|
596
|
+
return true;
|
|
597
|
+
case "array": return Array.isArray(value);
|
|
598
|
+
case "tuple": {
|
|
599
|
+
const items = type.items;
|
|
600
|
+
return Array.isArray(value) && value.length === items.length && items.every((item, i) => holdsLiteral(item, value[i]));
|
|
601
|
+
}
|
|
602
|
+
case "union": return type.items.some((item) => takes(item, value));
|
|
603
|
+
case "intersection": return type.items.every((item) => takes(item, value));
|
|
604
|
+
default: return false;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Whether prop (item) `def` of a union branch can hold `input`: anything, but a literal prop
|
|
609
|
+
* (see `literalNodes`: `kind: 'cat'`, `kind: 'cat' | null`) only a value that coerces to one of
|
|
610
|
+
* its literals — a string after the normalizers on the literal's way (`' Cat '` to `'cat'` with
|
|
611
|
+
* `@normalize.trim @normalize.lowercase`). `null` / `undefined` are left to the validator.
|
|
612
|
+
*/ function holdsLiteral(def, input) {
|
|
613
|
+
if (input === void 0 || input === null) return true;
|
|
614
|
+
const literals = literalNodes(def);
|
|
615
|
+
return !literals || literals.some((literal) => coercesTo(literal, input));
|
|
616
|
+
}
|
|
617
|
+
/** Whether `input` coerces to the value of `literal` (see `holdsLiteral`). */ function coercesTo(literal, input) {
|
|
618
|
+
const value = typeof input === "string" ? applyFlags(input, literal.flags) : input;
|
|
619
|
+
return coerce(literal.node, value) !== NO_MATCH;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* The branch of union `items` the discriminator of object `value` names: its literal itself,
|
|
623
|
+
* else the one its value coerces to (`"2"` names `2`, see {@link coercesTo}); `undefined` when the
|
|
624
|
+
* union has no discriminator or the value names no branch.
|
|
625
|
+
*/ function discriminatedBranch(items, value) {
|
|
626
|
+
const disc = unionDiscriminator(items);
|
|
627
|
+
if (disc === void 0) return;
|
|
628
|
+
const tag = Object.hasOwn(value, disc.property) ? value[disc.property] : void 0;
|
|
629
|
+
if (tag === void 0 || tag === null) return;
|
|
630
|
+
for (const [index, literal] of disc.branches) if (literal === tag) return items[index];
|
|
631
|
+
for (const [index] of disc.branches) {
|
|
632
|
+
const prop = items[index].type.props.get(disc.property);
|
|
633
|
+
if (prop && holdsLiteral(prop, tag)) return items[index];
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/** A prop / item: its coerced value, else the value as it is (copy-on-change, see `mapProps`). */ function coerceChild(def, input) {
|
|
637
|
+
const result = coerce(def, input);
|
|
638
|
+
return result === NO_MATCH ? input : result;
|
|
639
|
+
}
|
|
640
|
+
|
|
472
641
|
//#endregion
|
|
473
642
|
//#region packages/typescript/src/runtime/traverse.ts
|
|
474
643
|
/**
|
|
@@ -1575,12 +1744,12 @@ function isPlainObject(value) {
|
|
|
1575
1744
|
/** The lazy JSON schema of each type, with the metadata write count it was built at. */ const lazy = /* @__PURE__ */ new WeakMap();
|
|
1576
1745
|
/**
|
|
1577
1746
|
* The JSON schema of `type` built by the runtime (`jsonSchema: 'lazy'`): built once, again after
|
|
1578
|
-
* a
|
|
1579
|
-
* (`static toJsonSchema()`), with the `jsonSchemaOptions` of the build (the same
|
|
1580
|
-
* of a type).
|
|
1747
|
+
* a type tree write (a block, a runtime edit of metadata or shape: see `typeWrites`). Called by
|
|
1748
|
+
* generated code (`static toJsonSchema()`), with the `jsonSchemaOptions` of the build (the same
|
|
1749
|
+
* for every call of a type).
|
|
1581
1750
|
*/ function lazyJsonSchema(type, opts) {
|
|
1582
1751
|
const known = lazy.get(type);
|
|
1583
|
-
const writes =
|
|
1752
|
+
const writes = typeWriteCount();
|
|
1584
1753
|
if (known?.writes === writes) return known.schema;
|
|
1585
1754
|
const schema = buildJsonSchema(type, opts);
|
|
1586
1755
|
lazy.set(type, {
|
|
@@ -2514,4 +2683,4 @@ let LiteralScanner = class LiteralScanner {
|
|
|
2514
2683
|
}
|
|
2515
2684
|
|
|
2516
2685
|
//#endregion
|
|
2517
|
-
export { $ak, $alias, $blk, $end, $f, $fc, $ft, $nr, $ps, $q, $sh, $ut, LayerMap, SERIALIZE_VERSION, VALIDATOR_ERROR_CODES, VALIDATOR_MESSAGES, Validator, ValidatorError, annotate, appliedBlockModules, buildJsonSchema, bundledSchemaOf, coerceForType, coerceScalar, createAnnotatedTypeNode, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, detectDiscriminator, flattenAnnotatedType, forAnnotatedType, formatValidatorMessage, fromJsonSchema, invalidateCompiledValidators, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, lazyJsonSchema, localizeErrors, mergeJsonSchemas, messagesTranslator, metaEmpty, normalizeForType, pendingStatements, registerCompiledCheck, registerUserText, serializeAnnotatedType, throwFeatureDisabled, translateServed, unionDiscriminator, userTextKeys, userTextOf, watchLayer };
|
|
2686
|
+
export { $ak, $alias, $blk, $end, $f, $fc, $ft, $nr, $ps, $q, $sh, $ut, LayerMap, SERIALIZE_VERSION, VALIDATOR_ERROR_CODES, VALIDATOR_MESSAGES, Validator, ValidatorError, annotate, appliedBlockModules, buildJsonSchema, bundledSchemaOf, coerceForType, coerceScalar, createAnnotatedTypeNode, createDataFromAnnotatedType, defineAnnotatedType, deserializeAnnotatedType, detectDiscriminator, flattenAnnotatedType, forAnnotatedType, formatValidatorMessage, fromJsonSchema, invalidateCompiledValidators, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType, lazyJsonSchema, localizeErrors, mergeJsonSchemas, messagesTranslator, metaEmpty, normalizeForType, pendingStatements, registerCompiledCheck, registerUserText, serializeAnnotatedType, throwFeatureDisabled, translateServed, typeHasNormalizers, unionDiscriminator, userTextKeys, userTextOf, watchLayer };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/typescript",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Atscript: typescript-gen support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"@atscript/db": "^0.1.159",
|
|
77
|
-
"@atscript/core": "^0.2.
|
|
77
|
+
"@atscript/core": "^0.2.7"
|
|
78
78
|
},
|
|
79
79
|
"peerDependenciesMeta": {
|
|
80
80
|
"@atscript/db": {
|