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