@atscript/typescript 0.2.5 → 0.2.6

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,21 @@ 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) {
4161
+ */ function normalizeForType(def, value, opts) {
4162
+ const writes = typeWriteCount();
4163
+ if (writes !== stampWrites) {
4234
4164
  stampWrites = writes;
4235
- stampEpoch = epoch;
4236
4165
  reach = /* @__PURE__ */ new WeakMap();
4237
4166
  }
4238
4167
  if (!hasNormalizers(def)) return value;
4239
- const result = normalize(def, value, 0, /* @__PURE__ */ new Map());
4168
+ const result = normalize(def, value, 0, {
4169
+ memo: /* @__PURE__ */ new Map(),
4170
+ root: value,
4171
+ opts
4172
+ });
4240
4173
  return result === NO_MATCH ? value : result;
4241
4174
  }
4242
4175
  /** The nodes `def`'s value walk can enter (props, pattern props, items, elements). */ function childrenOf(def) {
@@ -4301,7 +4234,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4301
4234
  if (meta.get("normalize.uppercase")) flags |= UPPERCASE;
4302
4235
  return flags;
4303
4236
  }
4304
- function applyFlags(value, flags) {
4237
+ /** `value` with normalizer `flags` applied, in their fixed order (see {@link flagsOf}). */ function applyFlags(value, flags) {
4305
4238
  let out = value;
4306
4239
  if (flags & TRIM) out = out.trim();
4307
4240
  if (flags & LOWERCASE) out = out.toLowerCase();
@@ -4315,37 +4248,37 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4315
4248
  * Objects and unions are memoized per call by input object: a recursive union without a
4316
4249
  * discriminator is tried for every branch at every level, which without it is exponential in
4317
4250
  * the depth of the value.
4318
- */ function normalize(def, value, inherited, memo) {
4251
+ */ function normalize(def, value, inherited, walk) {
4319
4252
  if (value === void 0 || value === null) return value;
4320
- if (inherited === 0 && !hasNormalizers(def)) return takes(def, value) ? value : NO_MATCH;
4253
+ if (inherited === 0 && !hasNormalizers(def)) return takes$1(def, value) ? value : NO_MATCH;
4321
4254
  const type = def.type;
4322
4255
  switch (type.kind) {
4323
4256
  case "": {
4324
4257
  const out = typeof value === "string" && type.designType === "string" ? applyFlags(value, flagsOf(def, inherited)) : value;
4325
- return takes(def, out) ? out : NO_MATCH;
4258
+ return takes$1(def, out) ? out : NO_MATCH;
4326
4259
  }
4327
4260
  case "object": {
4328
4261
  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));
4262
+ const entries = entriesOf(walk.memo, value);
4263
+ return recall(entries, type, 0) ?? remember(entries, type, 0, mapProps(type, value, normalizeProp, walk));
4331
4264
  }
4332
- case "array": return Array.isArray(value) ? mapItems(value, type.of, normalizeItem, memo) : NO_MATCH;
4265
+ case "array": return Array.isArray(value) ? mapItems(value, type.of, normalizeItem, walk) : NO_MATCH;
4333
4266
  case "tuple": {
4334
4267
  const items = type.items;
4335
- return Array.isArray(value) && value.length === items.length ? mapItems(value, items, normalizeItem, memo) : NO_MATCH;
4268
+ return Array.isArray(value) && value.length === items.length ? mapItems(value, items, normalizeItem, walk) : NO_MATCH;
4336
4269
  }
4337
4270
  case "union": {
4338
4271
  const flags = flagsOf(def, inherited);
4339
4272
  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));
4273
+ if (typeof value !== "object") return normalizeUnion(items, value, flags, walk);
4274
+ const entries = entriesOf(walk.memo, value);
4275
+ return recall(entries, type, flags) ?? remember(entries, type, flags, normalizeUnion(items, value, flags, walk));
4343
4276
  }
4344
4277
  case "intersection": {
4345
4278
  const flags = flagsOf(def, inherited);
4346
4279
  let current = value;
4347
4280
  for (const item of type.items) {
4348
- const result = normalize(item, current, flags, memo);
4281
+ const result = normalize(item, current, flags, walk);
4349
4282
  if (result === NO_MATCH) return NO_MATCH;
4350
4283
  current = result;
4351
4284
  }
@@ -4373,20 +4306,47 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4373
4306
  ]);
4374
4307
  return result;
4375
4308
  }
4376
- /** An array / tuple item: its normalized value, else the value as it is. */ function normalizeItem(def, input, memo) {
4309
+ /** An array / tuple item: its normalized value, else the value as it is. */ function normalizeItem(def, input, walk) {
4377
4310
  if (!hasNormalizers(def)) return input;
4378
- const result = normalize(def, input, 0, memo);
4311
+ const result = normalize(def, input, 0, walk);
4379
4312
  return result === NO_MATCH ? input : result;
4380
4313
  }
4381
4314
  /**
4382
4315
  * 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;
4316
+ * (`kind: 'cat'`, `kind: 'cat' | null`, see {@link literalNodes}) holds another value.
4317
+ */ function normalizeProp(def, input, walk) {
4318
+ const out = normalizeItem(def, input, walk);
4319
+ if (out === input && input !== void 0 && input !== null) {
4320
+ const literals = literalNodes(def);
4321
+ if (literals && !literals.some((l) => l.node.type.value === input)) return NO_MATCH;
4322
+ }
4387
4323
  return out;
4388
4324
  }
4389
- /** Whether `def` can take `value` by its kind alone (see {@link normalize}). */ function takes(def, value) {
4325
+ /**
4326
+ * The literals a literal prop can hold: `def` itself when it is a literal (`kind: 'cat'`), the
4327
+ * literals of a union of literals, `null` and `undefined` (`kind: 'cat' | null`, nested unions and
4328
+ * aliases too); `undefined` for any other type. Each with the normalizers of the nodes on its way
4329
+ * (a union's apply to its members). A literal prop holding another value tells that an object is
4330
+ * not of its type, for normalization and coercion alike (see `coerce.ts`).
4331
+ */ function literalNodes(def, inherited = 0) {
4332
+ const type = def.type;
4333
+ if (type.kind === "") return type.value === void 0 ? void 0 : [{
4334
+ node: def,
4335
+ flags: flagsOf(def, inherited)
4336
+ }];
4337
+ if (type.kind !== "union") return;
4338
+ const flags = flagsOf(def, inherited);
4339
+ const out = [];
4340
+ for (const item of type.items) {
4341
+ const designType = item.type.designType;
4342
+ if (item.type.kind === "" && (designType === "null" || designType === "undefined")) continue;
4343
+ const inner = literalNodes(item, flags);
4344
+ if (!inner) return;
4345
+ out.push(...inner);
4346
+ }
4347
+ return out.length > 0 ? out : void 0;
4348
+ }
4349
+ /** Whether `def` can take `value` by its kind alone (see {@link normalize}). */ function takes$1(def, value) {
4390
4350
  const type = def.type;
4391
4351
  switch (type.kind) {
4392
4352
  case "": {
@@ -4405,8 +4365,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4405
4365
  case "object": return isPlainObject$1(value);
4406
4366
  case "array": return Array.isArray(value);
4407
4367
  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));
4368
+ case "union": return type.items.some((item) => takes$1(item, value));
4369
+ case "intersection": return type.items.every((item) => takes$1(item, value));
4410
4370
  default: return false;
4411
4371
  }
4412
4372
  }
@@ -4414,19 +4374,19 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4414
4374
  * A union: the branch a discriminator names, else the one result every branch that can take
4415
4375
  * the value agrees on — or, when they differ, the one result of the branches whose validator
4416
4376
  * accepts their result (NO_MATCH when none can take the value or it stays ambiguous).
4417
- */ function normalizeUnion(items, value, flags, memo) {
4377
+ */ function normalizeUnion(items, value, flags, walk) {
4418
4378
  if (isPlainObject$1(value)) {
4419
4379
  const disc = unionDiscriminator(items);
4420
4380
  if (disc) {
4421
4381
  const tag = value[disc.property];
4422
- for (const [index, literal] of disc.branches) if (literal === tag) return normalize(items[index], value, flags, memo);
4382
+ for (const [index, literal] of disc.branches) if (literal === tag) return normalize(items[index], value, flags, walk);
4423
4383
  }
4424
4384
  }
4425
4385
  let first = NO_MATCH;
4426
4386
  let firstItem;
4427
4387
  let rest;
4428
4388
  for (const item of items) {
4429
- const out = normalize(item, value, flags, memo);
4389
+ const out = normalize(item, value, flags, walk);
4430
4390
  if (out === NO_MATCH) continue;
4431
4391
  if (first === NO_MATCH) {
4432
4392
  first = out;
@@ -4435,23 +4395,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4435
4395
  }
4436
4396
  if (!rest || rest.every(([, out]) => sameValue(out, first))) return first;
4437
4397
  rest.unshift([firstItem, first]);
4398
+ const judges = judgesFor(walk.opts);
4399
+ const judge = value === walk.root ? judges.root : judges.nested;
4438
4400
  let accepted = NO_MATCH;
4439
4401
  for (const [item, out] of rest) {
4440
- if (!branchValidator(item).validate(out, true)) continue;
4402
+ if (!branchValidator(judge, item).validate(out, true)) continue;
4441
4403
  if (accepted === NO_MATCH) accepted = out;
4442
4404
  else if (!sameValue(accepted, out)) return NO_MATCH;
4443
4405
  }
4444
4406
  return accepted;
4445
4407
  }
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);
4408
+ /** Union branch `def`'s validator, judging its own normalized result (see {@link TJudges}). */ function branchValidator(judge, def) {
4409
+ let validator = judge.validators.get(def);
4452
4410
  if (!validator) {
4453
- validator = new Validator(def, { errorLimit: 1 });
4454
- branchValidators.set(def, validator);
4411
+ validator = new Validator(def, judge.opts);
4412
+ judge.validators.set(def, validator);
4455
4413
  }
4456
4414
  return validator;
4457
4415
  }
@@ -4465,6 +4423,210 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4465
4423
  return false;
4466
4424
  }
4467
4425
 
4426
+ //#endregion
4427
+ //#region packages/typescript/src/runtime/coerce.ts
4428
+ /**
4429
+ * Coerces string-transport input (route params, query strings) toward the
4430
+ * scalar shapes an annotated type expects.
4431
+ *
4432
+ * Pure and non-throwing: when a value can't be coerced it is returned
4433
+ * untouched, leaving the {@link Validator} to report the proper error.
4434
+ * Coercion converts representation only — constraint checks (`@expect.int`,
4435
+ * `@expect.min`, patterns) remain the validator's job.
4436
+ *
4437
+ * Rules:
4438
+ * - Strings targeting `number` are trimmed and parsed via `Number()`; only
4439
+ * finite results are accepted.
4440
+ * - Strings targeting `boolean` accept `"true"/"1"` and `"false"/"0"`.
4441
+ * - Unions pick one branch and coerce the value against it only (linear, no
4442
+ * backtracking). A scalar branch takes what it parses; an object (tuple)
4443
+ * branch takes a plain object (an array of its length) unless a literal
4444
+ * property (item) — `kind: 'cat'`, `kind: 'cat' | null` — holds a value that
4445
+ * does not coerce to it, a string compared after the normalizers of the
4446
+ * literal and of the unions it is in. An object takes the branch its
4447
+ * discriminator names (as the validator does: the literal itself first, else
4448
+ * `"2"` names a `2` literal), else the first whose shape it has (required
4449
+ * props present, no undeclared key), else the first that takes it. When no
4450
+ * branch takes the value, it is returned untouched.
4451
+ * - Objects recurse into props when the input is a plain object (covers
4452
+ * `@Query()` DTOs where every field arrives as a string), whatever their
4453
+ * literal props hold unless tried as a union branch.
4454
+ * - Arrays and tuples coerce items; intersections apply each member in order.
4455
+ * - `string` / `decimal` targets never change string input.
4456
+ *
4457
+ * @example
4458
+ * ```ts
4459
+ * coerceForType(KafkaOffset, '42') // → 42
4460
+ * coerceForType(KafkaOffset, 'abc') // → 'abc' (validator reports the error)
4461
+ * ```
4462
+ */ function coerceForType(def, value) {
4463
+ const result = coerce(def, value);
4464
+ return result === NO_MATCH ? value : result;
4465
+ }
4466
+ /**
4467
+ * Coerces a single scalar value toward a design type, using the same rules
4468
+ * as {@link coerceForType}. Returns the input untouched when it can't coerce.
4469
+ * Useful when only a design type is known (e.g. `design:paramtypes` fallbacks).
4470
+ */ function coerceScalar(designType, value) {
4471
+ const result = scalar(designType, value);
4472
+ return result === NO_MATCH ? value : result;
4473
+ }
4474
+ /** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
4475
+ if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
4476
+ const type = def.type;
4477
+ switch (type.kind) {
4478
+ case "": {
4479
+ const final = type;
4480
+ if (final.designType === "phantom") return value;
4481
+ const result = scalar(final.designType, value);
4482
+ if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
4483
+ return result;
4484
+ }
4485
+ case "object":
4486
+ if (!isPlainObject$1(value)) return NO_MATCH;
4487
+ return mapProps(type, value, coerceChild, void 0);
4488
+ case "array": return Array.isArray(value) ? mapItems(value, type.of, coerceChild, void 0) : NO_MATCH;
4489
+ case "tuple": {
4490
+ const items = type.items;
4491
+ return Array.isArray(value) && value.length === items.length ? mapItems(value, items, coerceChild, void 0) : NO_MATCH;
4492
+ }
4493
+ case "union": {
4494
+ const branch = branchOf(type.items, value);
4495
+ return branch ? coerce(branch, value) : NO_MATCH;
4496
+ }
4497
+ case "intersection": {
4498
+ let current = value;
4499
+ for (const item of type.items) {
4500
+ const result = coerce(item, current);
4501
+ if (result === NO_MATCH) return NO_MATCH;
4502
+ current = result;
4503
+ }
4504
+ return current;
4505
+ }
4506
+ default: throw new Error(`Unknown type kind "${type.kind}"`);
4507
+ }
4508
+ }
4509
+ function scalar(designType, value) {
4510
+ switch (designType) {
4511
+ case "string":
4512
+ case "decimal": return typeof value === "string" ? value : NO_MATCH;
4513
+ case "number":
4514
+ if (typeof value === "number") return value;
4515
+ if (typeof value === "string") {
4516
+ const trimmed = value.trim();
4517
+ if (trimmed.length > 0) {
4518
+ const parsed = Number(trimmed);
4519
+ if (Number.isFinite(parsed)) return parsed;
4520
+ }
4521
+ }
4522
+ return NO_MATCH;
4523
+ case "boolean":
4524
+ if (typeof value === "boolean") return value;
4525
+ if (value === "true" || value === "1") return true;
4526
+ if (value === "false" || value === "0") return false;
4527
+ return NO_MATCH;
4528
+ case "object": return typeof value === "object" ? value : NO_MATCH;
4529
+ case "any":
4530
+ case "phantom": return value;
4531
+ default: return NO_MATCH;
4532
+ }
4533
+ }
4534
+ /**
4535
+ * The branch of union `items` that takes `value` (see {@link takes}): the one an object's
4536
+ * discriminator names; else, for an object, the first that takes it and whose shape it has (see
4537
+ * {@link fits}); else the first that takes it, in declared order (a partial patch); `undefined`
4538
+ * when none does.
4539
+ */ function branchOf(items, value) {
4540
+ let first;
4541
+ if (isPlainObject$1(value)) {
4542
+ const named = discriminatedBranch(items, value);
4543
+ if (named && takes(named, value)) return named;
4544
+ for (const item of items) if (takes(item, value)) {
4545
+ if (fits(item, value)) return item;
4546
+ first ?? (first = item);
4547
+ }
4548
+ return first;
4549
+ }
4550
+ return items.find((item) => takes(item, value));
4551
+ }
4552
+ /**
4553
+ * Whether object `value` has the shape of branch `def`, by its own keys alone: every required
4554
+ * prop is one of them, and each of them is a prop (or matches a prop pattern) of the branch. A
4555
+ * union: one of its branches; a branch that is not an object (nor an intersection of objects)
4556
+ * has any shape.
4557
+ */ function fits(def, value) {
4558
+ if (def.type.kind === "union") return def.type.items.some((item) => fits(item, value));
4559
+ const objects = [];
4560
+ if (!objectsOf(def, objects)) return true;
4561
+ for (const o of objects) for (const [key, prop] of o.props) if (!prop.optional && !Object.hasOwn(value, key)) return false;
4562
+ return Object.keys(value).every((key) => objects.some((o) => o.props.has(key) || o.propsPatterns.some((p) => p.pattern.test(key))));
4563
+ }
4564
+ /** Collects the objects `def` is (an object, an intersection of objects); `false` for others. */ function objectsOf(def, out) {
4565
+ const type = def.type;
4566
+ if (type.kind === "object") {
4567
+ out.push(type);
4568
+ return true;
4569
+ }
4570
+ return type.kind === "intersection" && type.items.every((item) => objectsOf(item, out));
4571
+ }
4572
+ /**
4573
+ * Whether union branch `def` takes `value`: exactly when coercing `value` against it does not
4574
+ * fail (a branch taken never fails after it), decided from `value` itself — its kind, its length,
4575
+ * its direct literal props and items — never from what it holds deeper. A union takes one branch
4576
+ * once, so coercion is linear in the value.
4577
+ */ function takes(def, value) {
4578
+ if (value === void 0 || value === null) return def.optional === true;
4579
+ const type = def.type;
4580
+ switch (type.kind) {
4581
+ case "": return coerce(def, value) !== NO_MATCH;
4582
+ case "object":
4583
+ if (!isPlainObject$1(value)) return false;
4584
+ for (const [key, prop] of type.props) if (Object.hasOwn(value, key) && !holdsLiteral(prop, value[key])) return false;
4585
+ return true;
4586
+ case "array": return Array.isArray(value);
4587
+ case "tuple": {
4588
+ const items = type.items;
4589
+ return Array.isArray(value) && value.length === items.length && items.every((item, i) => holdsLiteral(item, value[i]));
4590
+ }
4591
+ case "union": return type.items.some((item) => takes(item, value));
4592
+ case "intersection": return type.items.every((item) => takes(item, value));
4593
+ default: return false;
4594
+ }
4595
+ }
4596
+ /**
4597
+ * Whether prop (item) `def` of a union branch can hold `input`: anything, but a literal prop
4598
+ * (see `literalNodes`: `kind: 'cat'`, `kind: 'cat' | null`) only a value that coerces to one of
4599
+ * its literals — a string after the normalizers on the literal's way (`' Cat '` to `'cat'` with
4600
+ * `@normalize.trim @normalize.lowercase`). `null` / `undefined` are left to the validator.
4601
+ */ function holdsLiteral(def, input) {
4602
+ if (input === void 0 || input === null) return true;
4603
+ const literals = literalNodes(def);
4604
+ return !literals || literals.some((literal) => coercesTo(literal, input));
4605
+ }
4606
+ /** Whether `input` coerces to the value of `literal` (see `holdsLiteral`). */ function coercesTo(literal, input) {
4607
+ const value = typeof input === "string" ? applyFlags(input, literal.flags) : input;
4608
+ return coerce(literal.node, value) !== NO_MATCH;
4609
+ }
4610
+ /**
4611
+ * The branch of union `items` the discriminator of object `value` names: its literal itself,
4612
+ * else the one its value coerces to (`"2"` names `2`, see {@link coercesTo}); `undefined` when the
4613
+ * union has no discriminator or the value names no branch.
4614
+ */ function discriminatedBranch(items, value) {
4615
+ const disc = unionDiscriminator(items);
4616
+ if (disc === void 0) return;
4617
+ const tag = Object.hasOwn(value, disc.property) ? value[disc.property] : void 0;
4618
+ if (tag === void 0 || tag === null) return;
4619
+ for (const [index, literal] of disc.branches) if (literal === tag) return items[index];
4620
+ for (const [index] of disc.branches) {
4621
+ const prop = items[index].type.props.get(disc.property);
4622
+ if (prop && holdsLiteral(prop, tag)) return items[index];
4623
+ }
4624
+ }
4625
+ /** A prop / item: its coerced value, else the value as it is (copy-on-change, see `mapProps`). */ function coerceChild(def, input) {
4626
+ const result = coerce(def, input);
4627
+ return result === NO_MATCH ? input : result;
4628
+ }
4629
+
4468
4630
  //#endregion
4469
4631
  //#region packages/typescript/src/runtime/traverse.ts
4470
4632
  /**
@@ -5582,12 +5744,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5582
5744
  /** The lazy JSON schema of each type, with the metadata write count it was built at. */ const lazy = /* @__PURE__ */ new WeakMap();
5583
5745
  /**
5584
5746
  * 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).
5747
+ * a type tree write (a block, a runtime edit of metadata or shape: see `typeWrites`). Called by
5748
+ * generated code (`static toJsonSchema()`), with the `jsonSchemaOptions` of the build (the same
5749
+ * for every call of a type).
5588
5750
  */ function lazyJsonSchema(type, opts) {
5589
5751
  const known = lazy.get(type);
5590
- const writes = metadataWriteCount();
5752
+ const writes = typeWriteCount();
5591
5753
  if (known?.writes === writes) return known.schema;
5592
5754
  const schema = buildJsonSchema(type, opts);
5593
5755
  lazy.set(type, {