@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.
@@ -76,10 +76,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
76
76
  const mapSet = Map.prototype.set;
77
77
  /** Global mutation epoch: compiled checks bound in an older epoch re-bind before use. */ let epoch = 0;
78
78
  /**
79
- * Writes of metadata maps so far (any map, any key: `annotate()`, the `Map` methods, a node's map
80
- * replaced, a layer flag or intrinsic mark registered): a lazy JSON schema or a layered map's
81
- * flattened view built before the last one is built again (see `lazyJsonSchema`, `LayerMap`).
82
- */ let metadataWrites = 0;
79
+ * Writes of type trees so far, watched or not: of metadata (any map, any key: `annotate()`, the
80
+ * `Map` methods, a node's map replaced, a layer flag or intrinsic mark registered) and of shape
81
+ * (every {@link touched} mutation, `invalidateCompiledValidators()`). What is derived from a type
82
+ * tree before the last write is derived again (`lazyJsonSchema`, `LayerMap`, `normalizeForType`).
83
+ */ let typeWrites = 0;
83
84
  /** Some bind has watched an object (until then no mutation can affect a compiled check). */ let tracking = false;
84
85
  /** Objects (nodes, type defs, metadata / props maps) read by some compiled-check bind. */ const watched = /* @__PURE__ */ new WeakSet();
85
86
  /** The current mutation epoch. */ function currentEpoch() {
@@ -89,7 +90,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
89
90
  tracking = true;
90
91
  watched.add(obj);
91
92
  }
92
- /** Records a tracked mutation of `obj` (a no-op unless some bind has read it). */ function touched(obj) {
93
+ /**
94
+ * Records a tracked mutation of `obj`: a type tree write (see `typeWrites`); compiled checks
95
+ * re-bind when some bind has read it.
96
+ */ function touched(obj) {
97
+ typeWrites++;
93
98
  if (tracking && watched.has(obj)) epoch++;
94
99
  }
95
100
  /**
@@ -115,13 +120,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
115
120
  * avoid mutating validated types per request.
116
121
  */ function invalidateCompiledValidators() {
117
122
  epoch++;
118
- metadataWrites++;
123
+ typeWrites++;
119
124
  }
120
- /** The number of metadata writes so far (see `metadataWrites`). */ function metadataWriteCount() {
121
- return metadataWrites;
125
+ /** The number of type tree writes so far (see `typeWrites`). */ function typeWriteCount() {
126
+ return typeWrites;
122
127
  }
123
128
  /** Records a write of a node's metadata the maps do not see: the node's map replaced. */ function metadataReplaced() {
124
- metadataWrites++;
129
+ typeWrites++;
125
130
  }
126
131
  /**
127
132
  * `annotate(m, key, value, true)` where map `m` holds `own` as its own value of `key`: appended in
@@ -130,7 +135,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
130
135
  if (Array.isArray(own)) {
131
136
  own.push(value);
132
137
  if (VALIDATOR_KEY_SET.has(key)) touched(m);
133
- metadataWrites++;
138
+ else typeWrites++;
134
139
  } else m.set(key, [own, value]);
135
140
  }
136
141
  /**
@@ -140,21 +145,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
140
145
  set(key, value) {
141
146
  super.set(key, value);
142
147
  if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
143
- metadataWrites++;
148
+ else typeWrites++;
144
149
  return this;
145
150
  }
146
151
  delete(key) {
147
152
  const deleted = super.delete(key);
148
- if (deleted) {
149
- if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
150
- metadataWrites++;
151
- }
153
+ if (deleted) if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
154
+ else typeWrites++;
152
155
  return deleted;
153
156
  }
154
157
  clear() {
155
158
  super.clear();
156
159
  touched(this);
157
- metadataWrites++;
158
160
  }
159
161
  /** A new map holding `entries`: no write (a new map is in no compiled check nor schema). */ constructor(entries) {
160
162
  super();
@@ -222,7 +224,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
222
224
  * a base shows through every layer over it.
223
225
  *
224
226
  * Reads go through a flattened view of the effective entries, rebuilt on the first read after any
225
- * metadata write (one global write counter: `metadataWriteCount`). The view is used only while
227
+ * type tree write (one global write counter: `typeWriteCount`). The view is used only while
226
228
  * every map below is tracked (an `AnnotationMap` or a `LayerMap`, whose writes the counter
227
229
  * sees); otherwise the reads are live. A raw write the counter does not see (a node's map swapped,
228
230
  * `node.metadata = X`, or edited in place) must be followed by `invalidateCompiledValidators()`,
@@ -292,7 +294,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
292
294
  s.add(key);
293
295
  hiding = true;
294
296
  touched(m);
295
- metadataReplaced();
296
297
  }
297
298
  /** The keys map `m` hides from its bases (see {@link hideKey}), for a map replacing it. */ function moveHidden(from, to) {
298
299
  const s = HIDDEN.get(from);
@@ -313,7 +314,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
313
314
  * The flattened view (built again after a metadata write), `undefined`: read live. Internal:
314
315
  * this module's readers and the validators read it (`readable`, `filledMeta`), never write.
315
316
  */ view() {
316
- return this.w === metadataWriteCount() ? this.f : this.flatten();
317
+ return this.w === typeWriteCount() ? this.f : this.flatten();
317
318
  }
318
319
  flatten() {
319
320
  this.w = -1;
@@ -323,7 +324,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
323
324
  f = keys.length === 0 ? NO_ENTRIES : new AnnotationMap(keys.map((k) => [k, this.liveGet(k)]));
324
325
  }
325
326
  this.f = f;
326
- this.w = metadataWriteCount();
327
+ this.w = typeWriteCount();
327
328
  return f;
328
329
  }
329
330
  /** The value of `key` (not concatenated), or `MISS`; `nr`: a `passedWhenReferred:false` key. */ lookup(key, nr) {
@@ -351,7 +352,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
351
352
  return MISS;
352
353
  }
353
354
  get(key) {
354
- const view = this.w === metadataWriteCount() ? this.f : this.flatten();
355
+ const view = this.w === typeWriteCount() ? this.f : this.flatten();
355
356
  return view === void 0 ? this.liveGet(key) : mapGet.call(view, key);
356
357
  }
357
358
  /** `get` read through the layers (see the file comment). */ liveGet(key) {
@@ -412,7 +413,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
412
413
  const deleted = super.delete(key);
413
414
  if ((hiding ? HIDDEN.get(this) : void 0)?.delete(key)) {
414
415
  touched(this);
415
- metadataReplaced();
416
416
  return true;
417
417
  }
418
418
  return deleted;
@@ -454,7 +454,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
454
454
  });
455
455
  /**
456
456
  * Every map of `m`'s layers is tracked (an `AnnotationMap` or a `LayerMap`, whose writes count in
457
- * `metadataWrites`), so a flattened view of `m` stays right until the next write.
457
+ * `typeWrites`), so a flattened view of `m` stays right until the next write.
458
458
  */ function tracked(m, seen) {
459
459
  const p = Object.getPrototypeOf(m);
460
460
  if (p !== AnnotationMap.prototype && p !== LayerMap.prototype) return false;
@@ -4080,122 +4080,49 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4080
4080
  else out[key] = value;
4081
4081
  }
4082
4082
 
4083
- //#endregion
4084
- //#region packages/typescript/src/runtime/coerce.ts
4085
- /**
4086
- * Coerces string-transport input (route params, query strings) toward the
4087
- * scalar shapes an annotated type expects.
4088
- *
4089
- * Pure and non-throwing: when a value can't be coerced it is returned
4090
- * untouched, leaving the {@link Validator} to report the proper error.
4091
- * Coercion converts representation only — constraint checks (`@expect.int`,
4092
- * `@expect.min`, patterns) remain the validator's job.
4093
- *
4094
- * Rules:
4095
- * - Strings targeting `number` are trimmed and parsed via `Number()`; only
4096
- * finite results are accepted.
4097
- * - Strings targeting `boolean` accept `"true"/"1"` and `"false"/"0"`.
4098
- * - Unions try each branch in declared order; the first successful parse wins.
4099
- * - Objects recurse into props when the input is a plain object (covers
4100
- * `@Query()` DTOs where every field arrives as a string).
4101
- * - Arrays and tuples coerce items; intersections apply each member in order.
4102
- * - `string` / `decimal` targets never change string input.
4103
- *
4104
- * @example
4105
- * ```ts
4106
- * coerceForType(KafkaOffset, '42') // → 42
4107
- * coerceForType(KafkaOffset, 'abc') // → 'abc' (validator reports the error)
4108
- * ```
4109
- */ function coerceForType(def, value) {
4110
- const result = coerce(def, value);
4111
- return result === NO_MATCH ? value : result;
4112
- }
4113
- /**
4114
- * Coerces a single scalar value toward a design type, using the same rules
4115
- * as {@link coerceForType}. Returns the input untouched when it can't coerce.
4116
- * Useful when only a design type is known (e.g. `design:paramtypes` fallbacks).
4117
- */ function coerceScalar(designType, value) {
4118
- const result = scalar(designType, value);
4119
- return result === NO_MATCH ? value : result;
4120
- }
4121
- /** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
4122
- if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
4123
- const type = def.type;
4124
- switch (type.kind) {
4125
- case "": {
4126
- const final = type;
4127
- if (final.designType === "phantom") return value;
4128
- const result = scalar(final.designType, value);
4129
- if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
4130
- return result;
4131
- }
4132
- case "object":
4133
- if (!isPlainObject$1(value)) return NO_MATCH;
4134
- return mapProps(type, value, coerceChild, void 0);
4135
- case "array": return Array.isArray(value) ? mapItems(value, type.of, coerceChild, void 0) : NO_MATCH;
4136
- case "tuple": {
4137
- const items = type.items;
4138
- return Array.isArray(value) && value.length === items.length ? mapItems(value, items, coerceChild, void 0) : NO_MATCH;
4139
- }
4140
- case "union":
4141
- for (const item of type.items) {
4142
- const result = coerce(item, value);
4143
- if (result !== NO_MATCH) return result;
4144
- }
4145
- return NO_MATCH;
4146
- case "intersection": {
4147
- let current = value;
4148
- for (const item of type.items) {
4149
- const result = coerce(item, current);
4150
- if (result === NO_MATCH) return NO_MATCH;
4151
- current = result;
4152
- }
4153
- return current;
4154
- }
4155
- default: throw new Error(`Unknown type kind "${type.kind}"`);
4156
- }
4157
- }
4158
- function scalar(designType, value) {
4159
- switch (designType) {
4160
- case "string":
4161
- case "decimal": return typeof value === "string" ? value : NO_MATCH;
4162
- case "number":
4163
- if (typeof value === "number") return value;
4164
- if (typeof value === "string") {
4165
- const trimmed = value.trim();
4166
- if (trimmed.length > 0) {
4167
- const parsed = Number(trimmed);
4168
- if (Number.isFinite(parsed)) return parsed;
4169
- }
4170
- }
4171
- return NO_MATCH;
4172
- case "boolean":
4173
- if (typeof value === "boolean") return value;
4174
- if (value === "true" || value === "1") return true;
4175
- if (value === "false" || value === "0") return false;
4176
- return NO_MATCH;
4177
- case "object": return typeof value === "object" ? value : NO_MATCH;
4178
- case "any":
4179
- case "phantom": return value;
4180
- default: return NO_MATCH;
4181
- }
4182
- }
4183
- /** A prop / item: its coerced value, else the value as it is (copy-on-change, see `mapProps`). */ function coerceChild(def, input) {
4184
- const result = coerce(def, input);
4185
- return result === NO_MATCH ? input : result;
4186
- }
4187
-
4188
4083
  //#endregion
4189
4084
  //#region packages/typescript/src/runtime/normalize.ts
4190
4085
  const TRIM = 1;
4191
4086
  const LOWERCASE = 2;
4192
4087
  const UPPERCASE = 4;
4193
4088
  /**
4194
- * Whether each node's subtree has normalizers: valid until the next metadata write or
4195
- * `invalidateCompiledValidators()` (checked by {@link normalizeForType}).
4089
+ * Whether each node's subtree has normalizers: valid until the next type tree write (metadata or
4090
+ * shape) or `invalidateCompiledValidators()` (checked by {@link normalizeForType}).
4196
4091
  */ let reach = /* @__PURE__ */ new WeakMap();
4197
4092
  let stampWrites = -1;
4198
- let stampEpoch = -1;
4093
+ function newJudges(opts) {
4094
+ const root = {
4095
+ ...opts,
4096
+ errorLimit: 1
4097
+ };
4098
+ delete root.translate;
4099
+ if (root.unknownProps === "strip") root.unknownProps = "ignore";
4100
+ const judge = {
4101
+ opts: root,
4102
+ validators: /* @__PURE__ */ new WeakMap()
4103
+ };
4104
+ return {
4105
+ root: judge,
4106
+ nested: root.partial === true ? {
4107
+ opts: {
4108
+ ...root,
4109
+ partial: false
4110
+ },
4111
+ validators: /* @__PURE__ */ new WeakMap()
4112
+ } : judge
4113
+ };
4114
+ }
4115
+ const DEFAULT_JUDGES = newJudges({});
4116
+ /** The judges by the options object passed (the same options: the same validators). */ const judgesByOpts = /* @__PURE__ */ new WeakMap();
4117
+ function judgesFor(opts) {
4118
+ if (!opts) return DEFAULT_JUDGES;
4119
+ let found = judgesByOpts.get(opts);
4120
+ if (!found) {
4121
+ found = newJudges(opts);
4122
+ judgesByOpts.set(opts, found);
4123
+ }
4124
+ return found;
4125
+ }
4199
4126
  /**
4200
4127
  * Normalizes input toward the string forms an annotated type declares with `@normalize.trim`,
4201
4128
  * `@normalize.lowercase` and `@normalize.uppercase`.
@@ -4216,9 +4143,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4216
4143
  * `detectDiscriminator`) uses the branch the value's discriminator names;
4217
4144
  * - otherwise every branch that can take the value is tried (a literal branch can take a string
4218
4145
  * only when the normalized string equals its literal): when they all give the same result it
4219
- * is used; when they differ, the branches whose result their own validator accepts (default
4220
- * options) are kept, and the result is used when they agree; else the value is returned
4221
- * untouched (ambiguous).
4146
+ * is used; when they differ, the branches whose result their own validator accepts are kept,
4147
+ * and the result is used when they agree; else the value is returned untouched (ambiguous).
4148
+ * The branch validators take `opts` — pass the options the result will be validated with
4149
+ * (`partial`, `unknownProps`, …) so a branch is judged as the validator will judge it; default
4150
+ * options when omitted. (`unknownProps: 'strip'` judges as `'ignore'`: nothing is stripped.)
4222
4151
  *
4223
4152
  * The intended flow is `coerceForType` → `normalizeForType` → `validate`.
4224
4153
  *
@@ -4226,17 +4155,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4226
4155
  * ```ts
4227
4156
  * // @normalize.trim @normalize.lowercase email: string.email
4228
4157
  * normalizeForType(User, { email: ' Ann@Example.COM ' }) // → { email: 'ann@example.com' }
4158
+ * // judged as a partial update will be validated
4159
+ * normalizeForType(Patch, body, { partial: true })
4229
4160
  * ```
4230
- */ function normalizeForType(def, value) {
4231
- const writes = metadataWriteCount();
4232
- const epoch = currentEpoch();
4233
- if (writes !== stampWrites || epoch !== stampEpoch) {
4234
- stampWrites = writes;
4235
- stampEpoch = epoch;
4236
- reach = /* @__PURE__ */ new WeakMap();
4237
- }
4238
- if (!hasNormalizers(def)) return value;
4239
- const result = normalize(def, value, 0, /* @__PURE__ */ new Map());
4161
+ */ function normalizeForType(def, value, opts) {
4162
+ if (!typeHasNormalizers(def)) return value;
4163
+ const result = normalize(def, value, 0, {
4164
+ memo: /* @__PURE__ */ new Map(),
4165
+ root: value,
4166
+ opts
4167
+ });
4240
4168
  return result === NO_MATCH ? value : result;
4241
4169
  }
4242
4170
  /** The nodes `def`'s value walk can enter (props, pattern props, items, elements). */ function childrenOf(def) {
@@ -4256,6 +4184,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4256
4184
  }
4257
4185
  }
4258
4186
  /**
4187
+ * Whether {@link normalizeForType} can change a value of type `def`: `def` or a type its walk
4188
+ * reaches (props, pattern props, items, union and intersection members) carries `@normalize.*`.
4189
+ * Cached per type and kept current across type writes, so callers can skip their own walks.
4190
+ */ function typeHasNormalizers(def) {
4191
+ const writes = typeWriteCount();
4192
+ if (writes !== stampWrites) {
4193
+ stampWrites = writes;
4194
+ reach = /* @__PURE__ */ new WeakMap();
4195
+ }
4196
+ return hasNormalizers(def);
4197
+ }
4198
+ /**
4259
4199
  * Whether `def` or a node its walk reaches has normalizers. Answers (and caches) every node
4260
4200
  * reached, recursive types included: collects the reached nodes with their parents, then marks
4261
4201
  * the nodes with normalizers and, back along the parents, every node that reaches one.
@@ -4301,7 +4241,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4301
4241
  if (meta.get("normalize.uppercase")) flags |= UPPERCASE;
4302
4242
  return flags;
4303
4243
  }
4304
- function applyFlags(value, flags) {
4244
+ /** `value` with normalizer `flags` applied, in their fixed order (see {@link flagsOf}). */ function applyFlags(value, flags) {
4305
4245
  let out = value;
4306
4246
  if (flags & TRIM) out = out.trim();
4307
4247
  if (flags & LOWERCASE) out = out.toLowerCase();
@@ -4315,37 +4255,37 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4315
4255
  * Objects and unions are memoized per call by input object: a recursive union without a
4316
4256
  * discriminator is tried for every branch at every level, which without it is exponential in
4317
4257
  * the depth of the value.
4318
- */ function normalize(def, value, inherited, memo) {
4258
+ */ function normalize(def, value, inherited, walk) {
4319
4259
  if (value === void 0 || value === null) return value;
4320
- if (inherited === 0 && !hasNormalizers(def)) return takes(def, value) ? value : NO_MATCH;
4260
+ if (inherited === 0 && !hasNormalizers(def)) return takes$1(def, value) ? value : NO_MATCH;
4321
4261
  const type = def.type;
4322
4262
  switch (type.kind) {
4323
4263
  case "": {
4324
4264
  const out = typeof value === "string" && type.designType === "string" ? applyFlags(value, flagsOf(def, inherited)) : value;
4325
- return takes(def, out) ? out : NO_MATCH;
4265
+ return takes$1(def, out) ? out : NO_MATCH;
4326
4266
  }
4327
4267
  case "object": {
4328
4268
  if (!isPlainObject$1(value)) return NO_MATCH;
4329
- const entries = entriesOf(memo, value);
4330
- return recall(entries, type, 0) ?? remember(entries, type, 0, mapProps(type, value, normalizeProp, memo));
4269
+ const entries = entriesOf(walk.memo, value);
4270
+ return recall(entries, type, 0) ?? remember(entries, type, 0, mapProps(type, value, normalizeProp, walk));
4331
4271
  }
4332
- case "array": return Array.isArray(value) ? mapItems(value, type.of, normalizeItem, memo) : NO_MATCH;
4272
+ case "array": return Array.isArray(value) ? mapItems(value, type.of, normalizeItem, walk) : NO_MATCH;
4333
4273
  case "tuple": {
4334
4274
  const items = type.items;
4335
- return Array.isArray(value) && value.length === items.length ? mapItems(value, items, normalizeItem, memo) : NO_MATCH;
4275
+ return Array.isArray(value) && value.length === items.length ? mapItems(value, items, normalizeItem, walk) : NO_MATCH;
4336
4276
  }
4337
4277
  case "union": {
4338
4278
  const flags = flagsOf(def, inherited);
4339
4279
  const items = type.items;
4340
- if (typeof value !== "object") return normalizeUnion(items, value, flags, memo);
4341
- const entries = entriesOf(memo, value);
4342
- return recall(entries, type, flags) ?? remember(entries, type, flags, normalizeUnion(items, value, flags, memo));
4280
+ if (typeof value !== "object") return normalizeUnion(items, value, flags, walk);
4281
+ const entries = entriesOf(walk.memo, value);
4282
+ return recall(entries, type, flags) ?? remember(entries, type, flags, normalizeUnion(items, value, flags, walk));
4343
4283
  }
4344
4284
  case "intersection": {
4345
4285
  const flags = flagsOf(def, inherited);
4346
4286
  let current = value;
4347
4287
  for (const item of type.items) {
4348
- const result = normalize(item, current, flags, memo);
4288
+ const result = normalize(item, current, flags, walk);
4349
4289
  if (result === NO_MATCH) return NO_MATCH;
4350
4290
  current = result;
4351
4291
  }
@@ -4373,20 +4313,47 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4373
4313
  ]);
4374
4314
  return result;
4375
4315
  }
4376
- /** An array / tuple item: its normalized value, else the value as it is. */ function normalizeItem(def, input, memo) {
4316
+ /** An array / tuple item: its normalized value, else the value as it is. */ function normalizeItem(def, input, walk) {
4377
4317
  if (!hasNormalizers(def)) return input;
4378
- const result = normalize(def, input, 0, memo);
4318
+ const result = normalize(def, input, 0, walk);
4379
4319
  return result === NO_MATCH ? input : result;
4380
4320
  }
4381
4321
  /**
4382
4322
  * A prop: as {@link normalizeItem}, but NO_MATCH (the object is not this type) when a literal prop
4383
- * (`kind: 'cat'`) holds another value.
4384
- */ function normalizeProp(def, input, memo) {
4385
- const out = normalizeItem(def, input, memo);
4386
- if (out === input && def.type.kind === "" && def.type.value !== void 0 && input !== def.type.value && input !== void 0 && input !== null) return NO_MATCH;
4323
+ * (`kind: 'cat'`, `kind: 'cat' | null`, see {@link literalNodes}) holds another value.
4324
+ */ function normalizeProp(def, input, walk) {
4325
+ const out = normalizeItem(def, input, walk);
4326
+ if (out === input && input !== void 0 && input !== null) {
4327
+ const literals = literalNodes(def);
4328
+ if (literals && !literals.some((l) => l.node.type.value === input)) return NO_MATCH;
4329
+ }
4387
4330
  return out;
4388
4331
  }
4389
- /** Whether `def` can take `value` by its kind alone (see {@link normalize}). */ function takes(def, value) {
4332
+ /**
4333
+ * The literals a literal prop can hold: `def` itself when it is a literal (`kind: 'cat'`), the
4334
+ * literals of a union of literals, `null` and `undefined` (`kind: 'cat' | null`, nested unions and
4335
+ * aliases too); `undefined` for any other type. Each with the normalizers of the nodes on its way
4336
+ * (a union's apply to its members). A literal prop holding another value tells that an object is
4337
+ * not of its type, for normalization and coercion alike (see `coerce.ts`).
4338
+ */ function literalNodes(def, inherited = 0) {
4339
+ const type = def.type;
4340
+ if (type.kind === "") return type.value === void 0 ? void 0 : [{
4341
+ node: def,
4342
+ flags: flagsOf(def, inherited)
4343
+ }];
4344
+ if (type.kind !== "union") return;
4345
+ const flags = flagsOf(def, inherited);
4346
+ const out = [];
4347
+ for (const item of type.items) {
4348
+ const designType = item.type.designType;
4349
+ if (item.type.kind === "" && (designType === "null" || designType === "undefined")) continue;
4350
+ const inner = literalNodes(item, flags);
4351
+ if (!inner) return;
4352
+ out.push(...inner);
4353
+ }
4354
+ return out.length > 0 ? out : void 0;
4355
+ }
4356
+ /** Whether `def` can take `value` by its kind alone (see {@link normalize}). */ function takes$1(def, value) {
4390
4357
  const type = def.type;
4391
4358
  switch (type.kind) {
4392
4359
  case "": {
@@ -4405,8 +4372,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4405
4372
  case "object": return isPlainObject$1(value);
4406
4373
  case "array": return Array.isArray(value);
4407
4374
  case "tuple": return Array.isArray(value) && value.length === type.items.length;
4408
- case "union": return type.items.some((item) => takes(item, value));
4409
- case "intersection": return type.items.every((item) => takes(item, value));
4375
+ case "union": return type.items.some((item) => takes$1(item, value));
4376
+ case "intersection": return type.items.every((item) => takes$1(item, value));
4410
4377
  default: return false;
4411
4378
  }
4412
4379
  }
@@ -4414,19 +4381,19 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4414
4381
  * A union: the branch a discriminator names, else the one result every branch that can take
4415
4382
  * the value agrees on — or, when they differ, the one result of the branches whose validator
4416
4383
  * accepts their result (NO_MATCH when none can take the value or it stays ambiguous).
4417
- */ function normalizeUnion(items, value, flags, memo) {
4384
+ */ function normalizeUnion(items, value, flags, walk) {
4418
4385
  if (isPlainObject$1(value)) {
4419
4386
  const disc = unionDiscriminator(items);
4420
4387
  if (disc) {
4421
4388
  const tag = value[disc.property];
4422
- for (const [index, literal] of disc.branches) if (literal === tag) return normalize(items[index], value, flags, memo);
4389
+ for (const [index, literal] of disc.branches) if (literal === tag) return normalize(items[index], value, flags, walk);
4423
4390
  }
4424
4391
  }
4425
4392
  let first = NO_MATCH;
4426
4393
  let firstItem;
4427
4394
  let rest;
4428
4395
  for (const item of items) {
4429
- const out = normalize(item, value, flags, memo);
4396
+ const out = normalize(item, value, flags, walk);
4430
4397
  if (out === NO_MATCH) continue;
4431
4398
  if (first === NO_MATCH) {
4432
4399
  first = out;
@@ -4435,23 +4402,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4435
4402
  }
4436
4403
  if (!rest || rest.every(([, out]) => sameValue(out, first))) return first;
4437
4404
  rest.unshift([firstItem, first]);
4405
+ const judges = judgesFor(walk.opts);
4406
+ const judge = value === walk.root ? judges.root : judges.nested;
4438
4407
  let accepted = NO_MATCH;
4439
4408
  for (const [item, out] of rest) {
4440
- if (!branchValidator(item).validate(out, true)) continue;
4409
+ if (!branchValidator(judge, item).validate(out, true)) continue;
4441
4410
  if (accepted === NO_MATCH) accepted = out;
4442
4411
  else if (!sameValue(accepted, out)) return NO_MATCH;
4443
4412
  }
4444
4413
  return accepted;
4445
4414
  }
4446
- /**
4447
- * Each union branch's validator (default options; one error is enough to reject a result),
4448
- * judging its own normalized result.
4449
- */ const branchValidators = /* @__PURE__ */ new WeakMap();
4450
- function branchValidator(def) {
4451
- let validator = branchValidators.get(def);
4415
+ /** Union branch `def`'s validator, judging its own normalized result (see {@link TJudges}). */ function branchValidator(judge, def) {
4416
+ let validator = judge.validators.get(def);
4452
4417
  if (!validator) {
4453
- validator = new Validator(def, { errorLimit: 1 });
4454
- branchValidators.set(def, validator);
4418
+ validator = new Validator(def, judge.opts);
4419
+ judge.validators.set(def, validator);
4455
4420
  }
4456
4421
  return validator;
4457
4422
  }
@@ -4465,6 +4430,210 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4465
4430
  return false;
4466
4431
  }
4467
4432
 
4433
+ //#endregion
4434
+ //#region packages/typescript/src/runtime/coerce.ts
4435
+ /**
4436
+ * Coerces string-transport input (route params, query strings) toward the
4437
+ * scalar shapes an annotated type expects.
4438
+ *
4439
+ * Pure and non-throwing: when a value can't be coerced it is returned
4440
+ * untouched, leaving the {@link Validator} to report the proper error.
4441
+ * Coercion converts representation only — constraint checks (`@expect.int`,
4442
+ * `@expect.min`, patterns) remain the validator's job.
4443
+ *
4444
+ * Rules:
4445
+ * - Strings targeting `number` are trimmed and parsed via `Number()`; only
4446
+ * finite results are accepted.
4447
+ * - Strings targeting `boolean` accept `"true"/"1"` and `"false"/"0"`.
4448
+ * - Unions pick one branch and coerce the value against it only (linear, no
4449
+ * backtracking). A scalar branch takes what it parses; an object (tuple)
4450
+ * branch takes a plain object (an array of its length) unless a literal
4451
+ * property (item) — `kind: 'cat'`, `kind: 'cat' | null` — holds a value that
4452
+ * does not coerce to it, a string compared after the normalizers of the
4453
+ * literal and of the unions it is in. An object takes the branch its
4454
+ * discriminator names (as the validator does: the literal itself first, else
4455
+ * `"2"` names a `2` literal), else the first whose shape it has (required
4456
+ * props present, no undeclared key), else the first that takes it. When no
4457
+ * branch takes the value, it is returned untouched.
4458
+ * - Objects recurse into props when the input is a plain object (covers
4459
+ * `@Query()` DTOs where every field arrives as a string), whatever their
4460
+ * literal props hold unless tried as a union branch.
4461
+ * - Arrays and tuples coerce items; intersections apply each member in order.
4462
+ * - `string` / `decimal` targets never change string input.
4463
+ *
4464
+ * @example
4465
+ * ```ts
4466
+ * coerceForType(KafkaOffset, '42') // → 42
4467
+ * coerceForType(KafkaOffset, 'abc') // → 'abc' (validator reports the error)
4468
+ * ```
4469
+ */ function coerceForType(def, value) {
4470
+ const result = coerce(def, value);
4471
+ return result === NO_MATCH ? value : result;
4472
+ }
4473
+ /**
4474
+ * Coerces a single scalar value toward a design type, using the same rules
4475
+ * as {@link coerceForType}. Returns the input untouched when it can't coerce.
4476
+ * Useful when only a design type is known (e.g. `design:paramtypes` fallbacks).
4477
+ */ function coerceScalar(designType, value) {
4478
+ const result = scalar(designType, value);
4479
+ return result === NO_MATCH ? value : result;
4480
+ }
4481
+ /** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
4482
+ if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
4483
+ const type = def.type;
4484
+ switch (type.kind) {
4485
+ case "": {
4486
+ const final = type;
4487
+ if (final.designType === "phantom") return value;
4488
+ const result = scalar(final.designType, value);
4489
+ if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
4490
+ return result;
4491
+ }
4492
+ case "object":
4493
+ if (!isPlainObject$1(value)) return NO_MATCH;
4494
+ return mapProps(type, value, coerceChild, void 0);
4495
+ case "array": return Array.isArray(value) ? mapItems(value, type.of, coerceChild, void 0) : NO_MATCH;
4496
+ case "tuple": {
4497
+ const items = type.items;
4498
+ return Array.isArray(value) && value.length === items.length ? mapItems(value, items, coerceChild, void 0) : NO_MATCH;
4499
+ }
4500
+ case "union": {
4501
+ const branch = branchOf(type.items, value);
4502
+ return branch ? coerce(branch, value) : NO_MATCH;
4503
+ }
4504
+ case "intersection": {
4505
+ let current = value;
4506
+ for (const item of type.items) {
4507
+ const result = coerce(item, current);
4508
+ if (result === NO_MATCH) return NO_MATCH;
4509
+ current = result;
4510
+ }
4511
+ return current;
4512
+ }
4513
+ default: throw new Error(`Unknown type kind "${type.kind}"`);
4514
+ }
4515
+ }
4516
+ function scalar(designType, value) {
4517
+ switch (designType) {
4518
+ case "string":
4519
+ case "decimal": return typeof value === "string" ? value : NO_MATCH;
4520
+ case "number":
4521
+ if (typeof value === "number") return value;
4522
+ if (typeof value === "string") {
4523
+ const trimmed = value.trim();
4524
+ if (trimmed.length > 0) {
4525
+ const parsed = Number(trimmed);
4526
+ if (Number.isFinite(parsed)) return parsed;
4527
+ }
4528
+ }
4529
+ return NO_MATCH;
4530
+ case "boolean":
4531
+ if (typeof value === "boolean") return value;
4532
+ if (value === "true" || value === "1") return true;
4533
+ if (value === "false" || value === "0") return false;
4534
+ return NO_MATCH;
4535
+ case "object": return typeof value === "object" ? value : NO_MATCH;
4536
+ case "any":
4537
+ case "phantom": return value;
4538
+ default: return NO_MATCH;
4539
+ }
4540
+ }
4541
+ /**
4542
+ * The branch of union `items` that takes `value` (see {@link takes}): the one an object's
4543
+ * discriminator names; else, for an object, the first that takes it and whose shape it has (see
4544
+ * {@link fits}); else the first that takes it, in declared order (a partial patch); `undefined`
4545
+ * when none does.
4546
+ */ function branchOf(items, value) {
4547
+ let first;
4548
+ if (isPlainObject$1(value)) {
4549
+ const named = discriminatedBranch(items, value);
4550
+ if (named && takes(named, value)) return named;
4551
+ for (const item of items) if (takes(item, value)) {
4552
+ if (fits(item, value)) return item;
4553
+ first ?? (first = item);
4554
+ }
4555
+ return first;
4556
+ }
4557
+ return items.find((item) => takes(item, value));
4558
+ }
4559
+ /**
4560
+ * Whether object `value` has the shape of branch `def`, by its own keys alone: every required
4561
+ * prop is one of them, and each of them is a prop (or matches a prop pattern) of the branch. A
4562
+ * union: one of its branches; a branch that is not an object (nor an intersection of objects)
4563
+ * has any shape.
4564
+ */ function fits(def, value) {
4565
+ if (def.type.kind === "union") return def.type.items.some((item) => fits(item, value));
4566
+ const objects = [];
4567
+ if (!objectsOf(def, objects)) return true;
4568
+ for (const o of objects) for (const [key, prop] of o.props) if (!prop.optional && !Object.hasOwn(value, key)) return false;
4569
+ return Object.keys(value).every((key) => objects.some((o) => o.props.has(key) || o.propsPatterns.some((p) => p.pattern.test(key))));
4570
+ }
4571
+ /** Collects the objects `def` is (an object, an intersection of objects); `false` for others. */ function objectsOf(def, out) {
4572
+ const type = def.type;
4573
+ if (type.kind === "object") {
4574
+ out.push(type);
4575
+ return true;
4576
+ }
4577
+ return type.kind === "intersection" && type.items.every((item) => objectsOf(item, out));
4578
+ }
4579
+ /**
4580
+ * Whether union branch `def` takes `value`: exactly when coercing `value` against it does not
4581
+ * fail (a branch taken never fails after it), decided from `value` itself — its kind, its length,
4582
+ * its direct literal props and items — never from what it holds deeper. A union takes one branch
4583
+ * once, so coercion is linear in the value.
4584
+ */ function takes(def, value) {
4585
+ if (value === void 0 || value === null) return def.optional === true;
4586
+ const type = def.type;
4587
+ switch (type.kind) {
4588
+ case "": return coerce(def, value) !== NO_MATCH;
4589
+ case "object":
4590
+ if (!isPlainObject$1(value)) return false;
4591
+ for (const [key, prop] of type.props) if (Object.hasOwn(value, key) && !holdsLiteral(prop, value[key])) return false;
4592
+ return true;
4593
+ case "array": return Array.isArray(value);
4594
+ case "tuple": {
4595
+ const items = type.items;
4596
+ return Array.isArray(value) && value.length === items.length && items.every((item, i) => holdsLiteral(item, value[i]));
4597
+ }
4598
+ case "union": return type.items.some((item) => takes(item, value));
4599
+ case "intersection": return type.items.every((item) => takes(item, value));
4600
+ default: return false;
4601
+ }
4602
+ }
4603
+ /**
4604
+ * Whether prop (item) `def` of a union branch can hold `input`: anything, but a literal prop
4605
+ * (see `literalNodes`: `kind: 'cat'`, `kind: 'cat' | null`) only a value that coerces to one of
4606
+ * its literals — a string after the normalizers on the literal's way (`' Cat '` to `'cat'` with
4607
+ * `@normalize.trim @normalize.lowercase`). `null` / `undefined` are left to the validator.
4608
+ */ function holdsLiteral(def, input) {
4609
+ if (input === void 0 || input === null) return true;
4610
+ const literals = literalNodes(def);
4611
+ return !literals || literals.some((literal) => coercesTo(literal, input));
4612
+ }
4613
+ /** Whether `input` coerces to the value of `literal` (see `holdsLiteral`). */ function coercesTo(literal, input) {
4614
+ const value = typeof input === "string" ? applyFlags(input, literal.flags) : input;
4615
+ return coerce(literal.node, value) !== NO_MATCH;
4616
+ }
4617
+ /**
4618
+ * The branch of union `items` the discriminator of object `value` names: its literal itself,
4619
+ * else the one its value coerces to (`"2"` names `2`, see {@link coercesTo}); `undefined` when the
4620
+ * union has no discriminator or the value names no branch.
4621
+ */ function discriminatedBranch(items, value) {
4622
+ const disc = unionDiscriminator(items);
4623
+ if (disc === void 0) return;
4624
+ const tag = Object.hasOwn(value, disc.property) ? value[disc.property] : void 0;
4625
+ if (tag === void 0 || tag === null) return;
4626
+ for (const [index, literal] of disc.branches) if (literal === tag) return items[index];
4627
+ for (const [index] of disc.branches) {
4628
+ const prop = items[index].type.props.get(disc.property);
4629
+ if (prop && holdsLiteral(prop, tag)) return items[index];
4630
+ }
4631
+ }
4632
+ /** A prop / item: its coerced value, else the value as it is (copy-on-change, see `mapProps`). */ function coerceChild(def, input) {
4633
+ const result = coerce(def, input);
4634
+ return result === NO_MATCH ? input : result;
4635
+ }
4636
+
4468
4637
  //#endregion
4469
4638
  //#region packages/typescript/src/runtime/traverse.ts
4470
4639
  /**
@@ -5582,12 +5751,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5582
5751
  /** The lazy JSON schema of each type, with the metadata write count it was built at. */ const lazy = /* @__PURE__ */ new WeakMap();
5583
5752
  /**
5584
5753
  * The JSON schema of `type` built by the runtime (`jsonSchema: 'lazy'`): built once, again after
5585
- * a metadata write (a block, a runtime edit: see `metadataWrites`). Called by generated code
5586
- * (`static toJsonSchema()`), with the `jsonSchemaOptions` of the build (the same for every call
5587
- * of a type).
5754
+ * a type tree write (a block, a runtime edit of metadata or shape: see `typeWrites`). Called by
5755
+ * generated code (`static toJsonSchema()`), with the `jsonSchemaOptions` of the build (the same
5756
+ * for every call of a type).
5588
5757
  */ function lazyJsonSchema(type, opts) {
5589
5758
  const known = lazy.get(type);
5590
- const writes = metadataWriteCount();
5759
+ const writes = typeWriteCount();
5591
5760
  if (known?.writes === writes) return known.schema;
5592
5761
  const schema = buildJsonSchema(type, opts);
5593
5762
  lazy.set(type, {
@@ -7564,6 +7733,7 @@ exports.registerUserText = registerUserText;
7564
7733
  exports.serializeAnnotatedType = serializeAnnotatedType;
7565
7734
  exports.throwFeatureDisabled = throwFeatureDisabled;
7566
7735
  exports.translateServed = translateServed;
7736
+ exports.typeHasNormalizers = typeHasNormalizers;
7567
7737
  exports.unionDiscriminator = unionDiscriminator;
7568
7738
  exports.userTextKeys = userTextKeys;
7569
7739
  exports.userTextOf = userTextOf;