@sorbetai/tools-node 0.1.0-alpha.17 → 0.1.0-alpha.19

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.
@@ -1,4 +1,4 @@
1
- //#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.5.0_patch_hash=3a50e14587df64dfdea0adbae45b34d11585bb58009be0694fa2cf4a7986fbdf/node_modules/@earendil-works/pi-schema/dist/builders.js
1
+ //#region ../../node_modules/.pnpm/@earendil-works+pi-schema@1.6.0/node_modules/@earendil-works/pi-schema/dist/builders.js
2
2
  /** A string, with optional length bounds (in code points), pattern and format. */
3
3
  function str(options = {}) {
4
4
  return {
@@ -99,15 +99,474 @@ function obj(properties, options = {}) {
99
99
 
100
100
  //#endregion
101
101
  //#region ../schema/dist/compiler.js
102
+ const ALWAYS = () => true;
103
+ const NEVER = () => false;
104
+ const IS_STRING = (v) => typeof v === "string";
105
+ const IS_NUMBER = (v) => Number.isFinite(v);
106
+ const IS_INTEGER = (v) => Number.isInteger(v);
107
+ const IS_BOOLEAN = (v) => typeof v === "boolean";
108
+ const IS_NULL = (v) => v === null;
109
+ const IS_ARRAY = (v) => Array.isArray(v);
110
+ const IS_BYTES = (v) => v instanceof Uint8Array;
111
+ const IS_BIGINT = (v) => typeof v === "bigint";
112
+ const IS_UNDEFINED = (v) => v === void 0;
113
+ const IS_OBJECT = (v) => isObject(v);
114
+ /** Whether `v` is a JSON object: not null, an array or bytes. */
115
+ function isObject(v) {
116
+ return typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Uint8Array);
117
+ }
118
+ /**
119
+ * The check of a `type` name. `bytes` (a Uint8Array), `bigint` (TypeBox's `Type.BigInt`) and `undefined` (TypeBox's
120
+ * `Type.Undefined`) extend JSON Schema for values that never pass through JSON. A `number` is finite, as in TypeBox:
121
+ * JSON has no NaN or Infinity, so only a value built in code can be one.
122
+ */
123
+ const TYPE_CHECKS = new Map([
124
+ ["string", IS_STRING],
125
+ ["number", IS_NUMBER],
126
+ ["integer", IS_INTEGER],
127
+ ["boolean", IS_BOOLEAN],
128
+ ["null", IS_NULL],
129
+ ["array", IS_ARRAY],
130
+ ["object", IS_OBJECT],
131
+ ["bytes", IS_BYTES],
132
+ ["bigint", IS_BIGINT],
133
+ ["undefined", IS_UNDEFINED]
134
+ ]);
135
+ const leaves = new WeakMap([
136
+ [IS_STRING, [1, void 0]],
137
+ [IS_NUMBER, [2, void 0]],
138
+ [IS_INTEGER, [3, void 0]],
139
+ [IS_BOOLEAN, [4, void 0]],
140
+ [IS_NULL, [5, void 0]],
141
+ [ALWAYS, [6, void 0]]
142
+ ]);
143
+ /** The leaf form of `check`, undefined when it is not a leaf. */
144
+ function leafOf(check$1) {
145
+ return leaves.get(check$1);
146
+ }
147
+ function leaf(code, arg, check$1) {
148
+ leaves.set(check$1, [code, arg]);
149
+ return check$1;
150
+ }
151
+ /** Bounds of a number or a string length leaf. */
152
+ var Bounds = class {
153
+ min;
154
+ max;
155
+ xmin;
156
+ xmax;
157
+ constructor(min, max, xmin, xmax) {
158
+ this.min = min;
159
+ this.max = max;
160
+ this.xmin = xmin;
161
+ this.xmax = xmax;
162
+ }
163
+ };
164
+ function leafOk(code, x, arg) {
165
+ switch (code) {
166
+ case 1: return typeof x === "string";
167
+ case 2: return Number.isFinite(x);
168
+ case 3: return Number.isInteger(x);
169
+ case 4: return typeof x === "boolean";
170
+ case 5: return x === null;
171
+ case 7: return x === arg;
172
+ case 8: {
173
+ const list = arg;
174
+ for (let i = 0; i < list.length; i++) if (list[i] === x) return true;
175
+ return false;
176
+ }
177
+ case 9: return (typeBits(x) & arg) !== 0;
178
+ case 10: return Number.isInteger(x) && inBounds(x, arg);
179
+ case 11: return Number.isFinite(x) && inBounds(x, arg);
180
+ case 12: return typeof x === "string" && lengthOk(x, arg.min, arg.max);
181
+ case 13: return typeof x === "string" && arg.test(x);
182
+ default: return true;
183
+ }
184
+ }
185
+ function inBounds(x, b) {
186
+ return x >= b.min && x <= b.max && x > b.xmin && x < b.xmax;
187
+ }
188
+ /** Whether a combined object or array check also checks the type: not at all, strictly, or passing other types. */
189
+ const GUARD_NONE = 0;
190
+ const GUARD_STRICT = 1;
191
+ const GUARD_LOOSE = 2;
192
+ /** Flags of a declared property in an object check's entries: its leaf code and two bits. */
193
+ const CODE = 15;
194
+ const REQUIRED = 16;
195
+ const INHERITED = 32;
196
+ /** All of `parts`; two and three parts get closures without a loop. */
197
+ function and(parts) {
198
+ switch (parts.length) {
199
+ case 0: return ALWAYS;
200
+ case 1: return parts[0];
201
+ case 2: {
202
+ const [a, b] = parts;
203
+ return (v) => a(v) && b(v);
204
+ }
205
+ case 3: {
206
+ const [a, b, c] = parts;
207
+ return (v) => a(v) && b(v) && c(v);
208
+ }
209
+ default: return (v) => {
210
+ for (let i = 0; i < parts.length; i++) if (!parts[i](v)) return false;
211
+ return true;
212
+ };
213
+ }
214
+ }
215
+ /** A check whose implementation is set later: the indirection a recursive reference goes through. */
216
+ function indirect(slot) {
217
+ return (v) => slot.check(v);
218
+ }
219
+ /** `check` with `uri` pushed onto the dynamic scope while it runs (only for schemas with `$dynamicRef`). */
220
+ function scopedCheck(scope, uri, check$1) {
221
+ return (v) => {
222
+ scope.push(uri);
223
+ const ok = check$1(v);
224
+ scope.pop();
225
+ return ok;
226
+ };
227
+ }
228
+ /** Any of the named types. */
229
+ function typeCheck(types) {
230
+ if (types.length === 1) return (typeof types[0] === "string" ? TYPE_CHECKS.get(types[0]) : void 0) ?? NEVER;
231
+ return maskCheck(typeMask(types));
232
+ }
233
+ const TYPE_BITS = new Map([
234
+ ["string", 1],
235
+ ["number", 2],
236
+ ["integer", 4],
237
+ ["boolean", 8],
238
+ ["null", 16],
239
+ ["array", 32],
240
+ ["object", 64],
241
+ ["bytes", 128],
242
+ ["bigint", 256],
243
+ ["undefined", 512]
244
+ ]);
245
+ /** The bits of the named types; unknown names add none. */
246
+ function typeMask(types) {
247
+ let mask = 0;
248
+ for (const t of types) if (typeof t === "string") mask |= TYPE_BITS.get(t) ?? 0;
249
+ return mask;
250
+ }
251
+ /** The type bits of `v`: an integer has the bits of `number` and `integer`; NaN and Infinity have neither. */
252
+ function typeBits(v) {
253
+ switch (typeof v) {
254
+ case "string": return 1;
255
+ case "number": return Number.isInteger(v) ? 6 : Number.isFinite(v) ? 2 : 0;
256
+ case "boolean": return 8;
257
+ case "object": return v === null ? 16 : Array.isArray(v) ? 32 : v instanceof Uint8Array ? 128 : 64;
258
+ case "bigint": return 256;
259
+ case "undefined": return 512;
260
+ default: return 0;
261
+ }
262
+ }
263
+ function maskCheck(mask) {
264
+ return leaf(9, mask, (v) => (typeBits(v) & mask) !== 0);
265
+ }
266
+ /** `anyOf` whose bare-type branches have the types of `mask`: those by one type test, the others by their checks. */
267
+ function maskedAnyCheck(mask, checks) {
268
+ if (checks.length === 0) return maskCheck(mask);
269
+ const rest = checks.length === 1 ? checks[0] : anyCheck(checks);
270
+ return (v) => (typeBits(v) & mask) !== 0 || rest(v);
271
+ }
272
+ /** A Uint8Array within the length bounds. */
273
+ function bytesCheck(min, max) {
274
+ return (v) => v instanceof Uint8Array && v.length >= min && v.length <= max;
275
+ }
276
+ /** The number of Unicode code points of `s`: JSON Schema string lengths count code points, not UTF-16 units. */
277
+ function codePoints(s) {
278
+ let n = 0;
279
+ for (let i = 0; i < s.length; i++, n++) {
280
+ const c = s.charCodeAt(i);
281
+ if (c >= 55296 && c <= 56319 && i + 1 < s.length) {
282
+ const next = s.charCodeAt(i + 1);
283
+ if (next >= 56320 && next <= 57343) i++;
284
+ }
285
+ }
286
+ return n;
287
+ }
288
+ function lengthOk(s, min, max) {
289
+ const units = s.length;
290
+ if (units < min) return false;
291
+ if (units < 2 * min && codePoints(s) < min) return false;
292
+ return units <= max || codePoints(s) <= max;
293
+ }
294
+ /** The string keywords: length bounds, pattern and format. */
295
+ function stringCheck(strict, min, max, re, format) {
296
+ if (re !== void 0 && min === 0 && max === Number.POSITIVE_INFINITY && format === void 0) return strict ? leaf(13, re, (v) => typeof v === "string" && re.test(v)) : (v) => typeof v !== "string" || re.test(v);
297
+ if (re === void 0 && format === void 0) return strict ? leaf(12, new Bounds(min, max, 0, 0), (v) => typeof v === "string" && lengthOk(v, min, max)) : (v) => typeof v !== "string" || lengthOk(v, min, max);
298
+ return (v) => {
299
+ if (typeof v !== "string") return !strict;
300
+ return lengthOk(v, min, max) && (re === void 0 || re.test(v)) && (format === void 0 || format(v));
301
+ };
302
+ }
303
+ /**
304
+ * The number keywords, for numbers and bigints alike (as TypeBox applies them): bounds of either kind compare with
305
+ * values of either kind. `mode`: 0 passes other types, 1 requires a number, 2 an integer, 3 a bigint.
306
+ */
307
+ function numberCheck(mode, min, max, xmin, xmax, multipleOf) {
308
+ const check$1 = (v) => {
309
+ if (typeof v !== "number" && typeof v !== "bigint") return mode === 0;
310
+ if (mode === 3 ? typeof v !== "bigint" : mode !== 0 && !Number.isFinite(v)) return false;
311
+ if (mode === 2 && !Number.isInteger(v)) return false;
312
+ return v >= min && v <= max && v > xmin && v < xmax && (multipleOf === void 0 || isMultiple(v, multipleOf));
313
+ };
314
+ if (mode === 0 || mode === 3 || multipleOf !== void 0 || !(typeof min === "number" && typeof max === "number" && typeof xmin === "number" && typeof xmax === "number")) return check$1;
315
+ return leaf(mode === 2 ? 10 : 11, new Bounds(min, max, xmin, xmax), check$1);
316
+ }
317
+ /**
318
+ * Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. With
319
+ * a bigint on either side, exact integer arithmetic; a bigint is no multiple of 0 or of a fraction.
320
+ *
321
+ * Deliberately unlike TypeBox, which throws there (`BigInt(0.5)`, division by zero): a check answers for every value
322
+ * and never throws, and no real schema pairs a bigint with a fractional or zero `multipleOf`.
323
+ */
324
+ function isMultiple(v, d) {
325
+ if (typeof v === "bigint" || typeof d === "bigint") {
326
+ if (typeof v === "number" && !Number.isInteger(v) || typeof d === "number" && !Number.isInteger(d)) return false;
327
+ const divisor = BigInt(d);
328
+ return divisor !== 0n && BigInt(v) % divisor === 0n;
329
+ }
330
+ const q = v / d;
331
+ if (Number.isInteger(q)) return true;
332
+ if (!Number.isFinite(q)) return v % d === 0;
333
+ const [vc, ve] = decimal(v);
334
+ const [dc, de] = decimal(d);
335
+ return ve >= de ? vc * 10n ** BigInt(ve - de) % dc === 0n : vc % (dc * 10n ** BigInt(de - ve)) === 0n;
336
+ }
337
+ function decimal(x) {
338
+ const [mantissa, exponent = "0"] = Math.abs(x).toString().split("e");
339
+ const point = mantissa.indexOf(".");
340
+ const fraction = point < 0 ? 0 : mantissa.length - point - 1;
341
+ return [BigInt(mantissa.replace(".", "")), Number(exponent) - fraction];
342
+ }
343
+ /** `inner` for objects; other values pass unless `strict`. */
344
+ function guardObject(inner, strict) {
345
+ return strict ? (v) => isObject(v) && inner(v) : (v) => !isObject(v) || inner(v);
346
+ }
347
+ /** `inner` for arrays; other values pass unless `strict`. */
348
+ function guardArray(inner, strict) {
349
+ return strict ? (v) => Array.isArray(v) && inner(v) : (v) => !Array.isArray(v) || inner(v);
350
+ }
351
+ /**
352
+ * An object with declared properties: `entries` is `[key, check, flags]` per property. The value's own keys are
353
+ * walked with for...in (whose `o[k]` V8 reads from the enum cache, unlike a load by a name from a list), each found
354
+ * among the declared names; `closed` rejects any other key. A property whose value is `undefined` counts as absent.
355
+ */
356
+ function declaredObject(entries$1, requiredCount, closed, guard) {
357
+ const n = entries$1.length;
358
+ return (v) => {
359
+ if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
360
+ const o = v;
361
+ let required = 0;
362
+ let next = 0;
363
+ for (const k in o) {
364
+ let i = next;
365
+ if (entries$1[i] !== k) {
366
+ i = 0;
367
+ while (i < n && entries$1[i] !== k) i += 3;
368
+ if (i === n) {
369
+ if (closed) return false;
370
+ continue;
371
+ }
372
+ }
373
+ next = i + 3;
374
+ const x = o[k];
375
+ const f = entries$1[i + 2];
376
+ if (x === void 0 && !(f & REQUIRED)) continue;
377
+ const c = f & CODE;
378
+ if (c !== 0 ? !leafOk(c, x, entries$1[i + 1]) : !entries$1[i + 1](x)) return false;
379
+ if (f & REQUIRED) required++;
380
+ }
381
+ return required === requiredCount;
382
+ };
383
+ }
384
+ /** An object with declared properties, required keys not declared, pattern properties and additional properties. */
385
+ function openObject(entries$1, keys, extraRequired, patterns, additional, guard) {
386
+ const known = patterns.length > 0 || additional !== void 0 ? new Set(keys) : void 0;
387
+ return (v) => {
388
+ if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
389
+ const o = v;
390
+ for (let i = 0; i < entries$1.length; i += 3) {
391
+ const k = entries$1[i];
392
+ const x = o[k];
393
+ const f = entries$1[i + 2];
394
+ if (x === void 0 && !(f & REQUIRED && Object.hasOwn(o, k)) || f & INHERITED && !Object.hasOwn(o, k)) {
395
+ if (f & REQUIRED) return false;
396
+ continue;
397
+ }
398
+ const c = f & CODE;
399
+ if (c !== 0 ? !leafOk(c, x, entries$1[i + 1]) : !entries$1[i + 1](x)) return false;
400
+ }
401
+ for (let i = 0; i < extraRequired.length; i++) if (!Object.hasOwn(o, extraRequired[i])) return false;
402
+ if (known === void 0) return true;
403
+ for (const k of Object.keys(o)) {
404
+ let matched = known.has(k);
405
+ for (let i = 0; i < patterns.length; i++) if (patterns[i][0].test(k)) {
406
+ matched = true;
407
+ if (!patterns[i][1](o[k])) return false;
408
+ }
409
+ if (!matched && additional !== void 0 && !additional(o[k])) return false;
410
+ }
411
+ return true;
412
+ };
413
+ }
414
+ /** Every property name passes `check` (`v` is an object). */
415
+ function propertyNamesCheck(check$1) {
416
+ return (v) => {
417
+ for (const k in v) if (!check$1(k)) return false;
418
+ return true;
419
+ };
420
+ }
421
+ /** `minProperties` and `maxProperties` (`v` is an object). */
422
+ function propertyCountCheck(min, max) {
423
+ return (v) => {
424
+ let n = 0;
425
+ for (const _ in v) n++;
426
+ return n >= min && n <= max;
427
+ };
428
+ }
429
+ /** `dependentRequired` (`v` is an object). */
430
+ function dependentRequiredCheck(entries$1) {
431
+ return (v) => {
432
+ const o = v;
433
+ for (const [k, list] of entries$1) if (Object.hasOwn(o, k)) {
434
+ for (const r of list) if (!Object.hasOwn(o, r)) return false;
435
+ }
436
+ return true;
437
+ };
438
+ }
439
+ /** `dependentSchemas` (`v` is an object). */
440
+ function dependentSchemasCheck(entries$1) {
441
+ return (v) => {
442
+ for (const [k, check$1] of entries$1) if (Object.hasOwn(v, k) && !check$1(v)) return false;
443
+ return true;
444
+ };
445
+ }
446
+ const NO_LEAF = [0, void 0];
447
+ /** The common array: guard, length bounds, prefix items and the items after them, in one closure. */
448
+ function listCheck(guard, min, max, prefix, rest) {
449
+ const n = prefix.length;
450
+ const [code, arg] = (rest === void 0 ? void 0 : leafOf(rest)) ?? NO_LEAF;
451
+ return (v) => {
452
+ if (!Array.isArray(v)) return guard === GUARD_LOOSE;
453
+ const length = v.length;
454
+ if (length < min || length > max) return false;
455
+ const m = length < n ? length : n;
456
+ for (let i = 0; i < m; i++) if (!prefix[i](v[i])) return false;
457
+ if (rest === void 0) return true;
458
+ if (code !== 0) {
459
+ for (let i = n; i < length; i++) if (!leafOk(code, v[i], arg)) return false;
460
+ return true;
461
+ }
462
+ for (let i = n; i < length; i++) if (!rest(v[i])) return false;
463
+ return true;
464
+ };
465
+ }
466
+ /** Prefix items and the items after them (`v` is an array). */
467
+ function itemsCheck(prefix, rest) {
468
+ return listCheck(GUARD_NONE, 0, Number.POSITIVE_INFINITY, prefix, rest);
469
+ }
470
+ /** `minItems` and `maxItems` (`v` is an array). */
471
+ function lengthCheck(min, max) {
472
+ return (v) => v.length >= min && v.length <= max;
473
+ }
474
+ /** `contains` with its bounds (`v` is an array). */
475
+ function containsCheck(check$1, min, max) {
476
+ return (v) => {
477
+ const list = v;
478
+ let n = 0;
479
+ for (let i = 0; i < list.length; i++) if (check$1(list[i]) && ++n > max) return false;
480
+ return n >= min;
481
+ };
482
+ }
483
+ /** `uniqueItems` (`v` is an array). */
484
+ function uniqueCheck(v) {
485
+ const list = v;
486
+ for (let i = 1; i < list.length; i++) for (let j = 0; j < i; j++) if (deepEqual(list[i], list[j])) return false;
487
+ return true;
488
+ }
489
+ /** `enum`: up to 8 primitives by a scan, more by a Set, objects and arrays by deep equality. */
490
+ function enumCheck(values) {
491
+ const primitives = values.filter((x) => typeof x !== "object" || x === null);
492
+ if (primitives.length === values.length && primitives.length <= 8) return leaf(8, primitives, (v) => {
493
+ for (let i = 0; i < primitives.length; i++) if (primitives[i] === v) return true;
494
+ return false;
495
+ });
496
+ const set = new Set(primitives);
497
+ const objects = values.filter((x) => typeof x === "object" && x !== null);
498
+ return (v) => set.has(v) || typeof v === "object" && objects.some((o) => deepEqual(v, o));
499
+ }
500
+ /** `const`. */
501
+ function constCheck(value) {
502
+ if (typeof value !== "object" || value === null) return leaf(7, value, (v) => v === value);
503
+ return (v) => deepEqual(v, value);
504
+ }
505
+ /** `anyOf`. */
506
+ function anyCheck(checks) {
507
+ return (v) => {
508
+ for (let i = 0; i < checks.length; i++) if (checks[i](v)) return true;
509
+ return false;
510
+ };
511
+ }
512
+ /** `oneOf`. */
513
+ function oneCheck(checks) {
514
+ return (v) => {
515
+ let n = 0;
516
+ for (let i = 0; i < checks.length; i++) if (checks[i](v) && ++n > 1) return false;
517
+ return n === 1;
518
+ };
519
+ }
520
+ /** `not`. */
521
+ function notCheck(check$1) {
522
+ return (v) => !check$1(v);
523
+ }
524
+ /** `if`, `then` and `else`. */
525
+ function ifCheck(cond, then, otherwise) {
526
+ return (v) => cond(v) ? then(v) : otherwise(v);
527
+ }
528
+ /** A union of objects tagged by a const on `key`: the tag selects the one branch that can match. */
529
+ function taggedCheck(key, tags, checks) {
530
+ return (v) => {
531
+ if (!isObject(v)) return false;
532
+ const tag = v[key];
533
+ for (let i = 0; i < tags.length; i++) if (tags[i] === tag) return checks[i](v);
534
+ return false;
535
+ };
536
+ }
537
+ /**
538
+ * JSON equality, as `const`, `enum` and `uniqueItems` compare: the same primitives, arrays with equal items in order,
539
+ * objects with the same keys and equal values (key order does not matter).
540
+ */
541
+ function deepEqual(a, b) {
542
+ if (a === b) return true;
543
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
544
+ const array = Array.isArray(a);
545
+ if (array !== Array.isArray(b)) return false;
546
+ if (array) {
547
+ const x$1 = a;
548
+ const y$1 = b;
549
+ if (x$1.length !== y$1.length) return false;
550
+ for (let i = 0; i < x$1.length; i++) if (!deepEqual(x$1[i], y$1[i])) return false;
551
+ return true;
552
+ }
553
+ const x = a;
554
+ const y = b;
555
+ const keys = Object.keys(x);
556
+ if (keys.length !== Object.keys(y).length) return false;
557
+ for (const k of keys) if (!Object.hasOwn(y, k) || !deepEqual(x[k], y[k])) return false;
558
+ return true;
559
+ }
102
560
  /** The base URI of a root schema without `$id`; relative references resolve against it. */
103
561
  const DEFAULT_BASE = "https://json-schema.invalid/root.json";
104
562
  /**
105
563
  * Resolves references in the root schema and the registered documents. The schema is indexed on the first
106
- * reference, so a schema without references is never walked for identifiers.
564
+ * reference or the first `$id` a walker meets, so a schema with neither is never walked for identifiers.
107
565
  */
108
566
  var Resolver = class {
109
567
  draft;
110
- rootBase;
568
+ /** The base URI the root schema is walked from; walkers apply the root's own `$id` through `baseFor`. */
569
+ rootBase = DEFAULT_BASE;
111
570
  /** Schema object to the base URI it was indexed under. */
112
571
  baseOf = /* @__PURE__ */ new Map();
113
572
  /** URI (with a `#name` fragment) of every `$dynamicAnchor`, to its schema. */
@@ -126,17 +585,21 @@ var Resolver = class {
126
585
  this.root = root;
127
586
  this.options = options;
128
587
  this.rejectUnresolved = options.unresolvedRefs === "reject";
129
- this.rootBase = isSchemaObject(root) ? this.baseFor(root, DEFAULT_BASE) : DEFAULT_BASE;
130
588
  }
131
589
  /**
132
590
  * The base URI of `node`, whose parent has `base`: its `$id` changes it, unless draft 7 makes the `$id` inert. Once
133
591
  * the schema is indexed, the indexed base (a resolved reference target already has its own).
134
592
  */
135
593
  baseFor(node, base) {
136
- const indexed = this.baseOf.get(node);
594
+ let indexed = this.baseOf.get(node);
137
595
  if (indexed !== void 0) return indexed;
138
596
  const id = node.$id;
139
597
  if (typeof id !== "string") return base;
598
+ if (!this.indexed) {
599
+ this.ensureIndexed();
600
+ indexed = this.baseOf.get(node);
601
+ if (indexed !== void 0) return indexed;
602
+ }
140
603
  if (this.draft === "7" && (typeof node.$ref === "string" || id.startsWith("#"))) return base;
141
604
  return documentOf(resolveUri(id, base));
142
605
  }
@@ -365,712 +828,380 @@ var Coercer = class {
365
828
  try {
366
829
  result = this.convert(node, base, value);
367
830
  } finally {
368
- entered.length -= 2;
369
- }
370
- if (memoize && this.cuts === cuts) {
371
- this.memo ??= /* @__PURE__ */ new Map();
372
- let values = this.memo.get(node);
373
- if (values === void 0) {
374
- values = /* @__PURE__ */ new Map();
375
- this.memo.set(node, values);
376
- }
377
- values.set(value, result);
378
- }
379
- return result;
380
- }
381
- /** Whether `value` passes the schema `node`; undefined when `node` cannot be compiled. */
382
- passes(node, base, value) {
383
- try {
384
- return this.c.check(node, this.baseOf(node, base))(value);
385
- } catch {
386
- return;
387
- }
388
- }
389
- baseOf(node, base) {
390
- return isSchemaObject(node) ? this.c.r.baseOf.get(node) ?? base : base;
391
- }
392
- dropOptionalNulls(value, schema, base) {
393
- if (!isSchemaObject(schema)) return value;
394
- const b = this.baseOf(schema, base);
395
- if (Array.isArray(value)) return mapItems(value, (item, i) => {
396
- const itemSchema = itemSchemaAt(schema, i);
397
- return itemSchema === void 0 ? item : this.dropOptionalNulls(item, itemSchema, b);
398
- });
399
- if (!isPlainObject(value)) return value;
400
- const properties = isSchemaObject(schema.properties) ? schema.properties : {};
401
- const patterns = isSchemaObject(schema.patternProperties) ? Object.entries(schema.patternProperties).map(([pattern, item]) => [sharedRegex(pattern), item]) : [];
402
- const additional = isSchemaObject(schema.additionalProperties) ? schema.additionalProperties : void 0;
403
- if (patterns.length === 0 && additional === void 0 && !isSchemaObject(schema.properties)) return value;
404
- const required = new Set(Array.isArray(schema.required) ? schema.required : []);
405
- let out;
406
- const set = (key, next) => {
407
- if (next === (out ?? value)[key]) return;
408
- out ??= { ...value };
409
- out[key] = next;
410
- };
411
- for (const [key, property] of Object.entries(properties)) {
412
- if (!Object.hasOwn(value, key)) continue;
413
- const isRef = isSchemaObject(property) && typeof property.$ref === "string";
414
- if (value[key] === null && !required.has(key) && !isRef && this.passes(property, b, null) === false) {
415
- out ??= { ...value };
416
- delete out[key];
417
- continue;
418
- }
419
- set(key, this.dropOptionalNulls(value[key], property, b));
420
- }
421
- if (patterns.length > 0 || additional !== void 0) for (const key of Object.keys(value)) {
422
- let matched = Object.hasOwn(properties, key);
423
- for (const [pattern, item] of patterns) {
424
- if (!pattern.test(key)) continue;
425
- matched = true;
426
- set(key, this.dropOptionalNulls((out ?? value)[key], item, b));
427
- }
428
- if (!matched && additional !== void 0) set(key, this.dropOptionalNulls(value[key], additional, b));
429
- }
430
- return out ?? value;
431
- }
432
- convert(node, base, value) {
433
- if (!isSchemaObject(node)) return value;
434
- const b = this.baseOf(node, base);
435
- if (typeof node.$ref === "string") {
436
- let target;
437
- try {
438
- target = this.c.r.resolve(node.$ref, b);
439
- } catch {
440
- return value;
441
- }
442
- return isSchemaObject(target.node) ? this.reenter(target.node, target.base, value, true) : value;
443
- }
444
- return this.convertNode(node, b, value);
445
- }
446
- /** One schema node's conversion: its first keyword among `const`, `enum`, the unions, `allOf` and `type`. */
447
- convertNode(node, b, value) {
448
- if (Object.hasOwn(node, "const")) return convertLiteral(node.const, value);
449
- if (Array.isArray(node.enum)) return this.convertEnum(node, node.enum, b, value);
450
- if (Array.isArray(node.anyOf)) return this.convertUnion(node, node.anyOf, b, value);
451
- if (Array.isArray(node.oneOf)) return this.convertUnion(node, node.oneOf, b, value);
452
- if (Array.isArray(node.allOf)) {
453
- let next = value;
454
- for (const member of node.allOf) next = this.convert(member, b, next);
455
- return this.convertMembers(node, b, next);
456
- }
457
- switch (node.type) {
458
- case "string": return orValue(tryString(value), value);
459
- case "number": return orValue(tryNumber(value), value);
460
- case "integer": {
461
- const result = tryNumber(value);
462
- return result === void 0 ? value : Math.trunc(result.value);
463
- }
464
- case "boolean": return orValue(tryBoolean(value), value);
465
- case "null": return orValue(tryNull(value), value);
466
- case "array": return this.convertArray(node, b, value);
467
- case "object": return this.convertObject(node, b, value);
468
- case void 0:
469
- if (node.properties !== void 0 || node.patternProperties !== void 0) return this.convertObject(node, b, value);
470
- if (node.items !== void 0 || node.prefixItems !== void 0) return this.convertArray(node, b, value);
471
- return value;
472
- default: return value;
473
- }
474
- }
475
- /** An enum is a union of its values: a member stays, else the first value the input converts to. */
476
- convertEnum(node, values, base, value) {
477
- if (values.some((member) => member === value)) return value;
478
- for (const member of values) {
479
- const candidate = convertLiteral(member, value);
480
- if (candidate !== value && this.passes(node, base, candidate) === true) return candidate;
481
- }
482
- return value;
483
- }
484
- /**
485
- * A value one branch accepts stays; else the first branch's conversion that the whole union accepts. Branches are
486
- * converted in order and only until one is accepted, so a later recursive branch is not entered needlessly. A node
487
- * that also declares members (plain JSON Schema; TypeBox's never do) converts them into each candidate before it is
488
- * checked, so a branch is not discarded for a sibling property that is converted after it.
489
- */
490
- convertUnion(node, branches, base, value) {
491
- if (branches.some((branch) => this.passes(branch, base, value) === true)) return this.convertMembers(node, base, value);
492
- for (const branch of branches) {
493
- const candidate = this.convertMembers(node, base, this.convert(branch, base, value));
494
- if (this.passes(node, base, candidate) === true) return candidate;
495
- }
496
- return this.convertMembers(node, base, value);
497
- }
498
- /** The object members of a union or intersection node, when it declares any. */
499
- convertMembers(node, base, value) {
500
- return node.properties !== void 0 || node.patternProperties !== void 0 || node.additionalProperties !== void 0 ? this.convertObject(node, base, value) : value;
501
- }
502
- convertArray(node, base, value) {
503
- const prefix = Array.isArray(node.items) ? node.items : Array.isArray(node.prefixItems) ? node.prefixItems : void 0;
504
- if (prefix !== void 0) {
505
- if (!Array.isArray(value)) return value;
506
- const rest = Array.isArray(node.items) ? void 0 : node.items;
507
- return mapItems(value, (item$1, i) => {
508
- const schema = i < prefix.length ? prefix[i] : rest;
509
- return schema === void 0 ? item$1 : this.convert(schema, base, item$1);
510
- });
511
- }
512
- if (Array.isArray(value)) return node.items === void 0 ? value : mapItems(value, (item$1) => this.convert(node.items, base, item$1));
513
- const item = node.items;
514
- return [isSchemaObject(item) ? this.reenter(item, base, value, false) : value];
515
- }
516
- convertObject(node, base, value) {
517
- if (!isPlainObject(value)) return value;
518
- const properties = isSchemaObject(node.properties) ? node.properties : {};
519
- const patterns = isSchemaObject(node.patternProperties) ? Object.entries(node.patternProperties).map(([pattern, schema]) => [sharedRegex(pattern), schema]) : [];
520
- const additional = isSchemaObject(node.additionalProperties) ? node.additionalProperties : void 0;
521
- let out;
522
- for (const key of Object.keys(value)) {
523
- if (value[key] === void 0) continue;
524
- let next = value[key];
525
- let matched = Object.hasOwn(properties, key);
526
- if (matched) next = this.convert(properties[key], base, next);
527
- for (const [pattern, schema] of patterns) {
528
- if (!pattern.test(key)) continue;
529
- matched = true;
530
- next = this.convert(schema, base, next);
531
- }
532
- if (!matched && additional !== void 0) next = this.convert(additional, base, next);
533
- if (next !== value[key]) {
534
- out ??= { ...value };
535
- out[key] = next;
536
- }
831
+ entered.length -= 2;
537
832
  }
538
- return out ?? value;
539
- }
540
- coerceTypes(value, schema, base) {
541
- if (!isSchemaObject(schema)) return value;
542
- const b = this.baseOf(schema, base);
543
- let next = value;
544
- if (Array.isArray(schema.allOf)) for (const member of schema.allOf) next = this.coerceTypes(next, member, b);
545
- if (Array.isArray(schema.anyOf)) next = this.coerceUnion(next, schema.anyOf, b);
546
- if (Array.isArray(schema.oneOf)) next = this.coerceUnion(next, schema.oneOf, b);
547
- const types = typesOf(schema);
548
- const matchesUnionMember = types.length > 1 && types.some((type) => matchesType(next, type));
549
- if (types.length > 0 && !matchesUnionMember) for (const type of types) {
550
- const candidate = coercePrimitive(next, type);
551
- if (candidate !== next) {
552
- next = candidate;
553
- break;
833
+ if (memoize && this.cuts === cuts) {
834
+ this.memo ??= /* @__PURE__ */ new Map();
835
+ let values = this.memo.get(node);
836
+ if (values === void 0) {
837
+ values = /* @__PURE__ */ new Map();
838
+ this.memo.set(node, values);
554
839
  }
840
+ values.set(value, result);
555
841
  }
556
- if (types.includes("object") && isPlainObject(next)) next = this.coerceProperties(next, schema, b);
557
- if (types.includes("array") && Array.isArray(next)) next = this.coerceItems(next, schema, b);
558
- return next;
842
+ return result;
559
843
  }
560
- coerceUnion(value, branches, base) {
561
- for (const branch of branches) if (this.passes(branch, base, value) === true) return value;
562
- for (const branch of branches) {
563
- const coerced = this.coerceTypes(value, branch, base);
564
- if (this.passes(branch, base, coerced) === true) return coerced;
844
+ /** Whether `value` passes the schema `node`; undefined when `node` cannot be compiled. */
845
+ passes(node, base, value) {
846
+ try {
847
+ return this.c.check(node, this.baseOf(node, base))(value);
848
+ } catch {
849
+ return;
565
850
  }
566
- return value;
567
851
  }
568
- coerceProperties(value, schema, base) {
852
+ baseOf(node, base) {
853
+ return isSchemaObject(node) ? this.c.r.baseFor(node, base) : base;
854
+ }
855
+ dropOptionalNulls(value, schema, base) {
856
+ if (!isSchemaObject(schema)) return value;
857
+ const b = this.baseOf(schema, base);
858
+ if (Array.isArray(value)) return mapItems(value, (item, i) => {
859
+ const itemSchema = itemSchemaAt(schema, i);
860
+ return itemSchema === void 0 ? item : this.dropOptionalNulls(item, itemSchema, b);
861
+ });
862
+ if (!isPlainObject(value)) return value;
569
863
  const properties = isSchemaObject(schema.properties) ? schema.properties : {};
570
864
  const patterns = isSchemaObject(schema.patternProperties) ? Object.entries(schema.patternProperties).map(([pattern, item]) => [sharedRegex(pattern), item]) : [];
571
865
  const additional = isSchemaObject(schema.additionalProperties) ? schema.additionalProperties : void 0;
866
+ if (patterns.length === 0 && additional === void 0 && !isSchemaObject(schema.properties)) return value;
867
+ const required = new Set(Array.isArray(schema.required) ? schema.required : []);
572
868
  let out;
573
869
  const set = (key, next) => {
574
870
  if (next === (out ?? value)[key]) return;
575
871
  out ??= { ...value };
576
872
  out[key] = next;
577
873
  };
578
- for (const [key, property] of Object.entries(properties)) if (key in value) set(key, this.coerceTypes(value[key], property, base));
579
- for (const key of Object.keys(value)) {
874
+ for (const [key, property] of Object.entries(properties)) {
875
+ if (!Object.hasOwn(value, key)) continue;
876
+ const isRef = isSchemaObject(property) && typeof property.$ref === "string";
877
+ if (value[key] === null && !required.has(key) && !isRef && this.passes(property, b, null) === false) {
878
+ out ??= { ...value };
879
+ delete out[key];
880
+ continue;
881
+ }
882
+ set(key, this.dropOptionalNulls(value[key], property, b));
883
+ }
884
+ if (patterns.length > 0 || additional !== void 0) for (const key of Object.keys(value)) {
580
885
  let matched = Object.hasOwn(properties, key);
581
886
  for (const [pattern, item] of patterns) {
582
887
  if (!pattern.test(key)) continue;
583
888
  matched = true;
584
- set(key, this.coerceTypes((out ?? value)[key], item, base));
889
+ set(key, this.dropOptionalNulls((out ?? value)[key], item, b));
585
890
  }
586
- if (!matched && additional !== void 0) set(key, this.coerceTypes(value[key], additional, base));
891
+ if (!matched && additional !== void 0) set(key, this.dropOptionalNulls(value[key], additional, b));
587
892
  }
588
893
  return out ?? value;
589
894
  }
590
- coerceItems(value, schema, base) {
591
- return mapItems(value, (item, i) => {
592
- const itemSchema = itemSchemaAt(schema, i);
593
- return isSchemaObject(itemSchema) ? this.coerceTypes(item, itemSchema, base) : item;
594
- });
595
- }
596
- };
597
- /**
598
- * The schema of an array's item at `index`: a tuple's (`prefixItems`, or a draft-7 `items` array) for its prefix,
599
- * `items` after `prefixItems`, or `items` for every item. Undefined when nothing applies.
600
- */
601
- function itemSchemaAt(schema, index) {
602
- if (Array.isArray(schema.items)) return schema.items[index];
603
- if (Array.isArray(schema.prefixItems) && index < schema.prefixItems.length) return schema.prefixItems[index];
604
- return schema.items;
605
- }
606
- /** `items` mapped by `map`; the same array when every item maps to itself. */
607
- function mapItems(items, map$1) {
608
- let out;
609
- for (let i = 0; i < items.length; i++) {
610
- const next = map$1(items[i], i);
611
- if (next === items[i]) continue;
612
- out ??= items.slice();
613
- out[i] = next;
614
- }
615
- return out ?? items;
616
- }
617
- function orValue(result, value) {
618
- return result === void 0 ? value : result.value;
619
- }
620
- /** A literal (`const`, an enum value) taken from a value of another type that converts to exactly it. */
621
- function convertLiteral(literal, value) {
622
- if (literal === value) return value;
623
- const result = typeof literal === "string" ? tryString(value) : typeof literal === "number" ? tryNumber(value) : typeof literal === "boolean" ? tryBoolean(value) : void 0;
624
- return result !== void 0 && result.value === literal ? result.value : value;
625
- }
626
- function tryString(value) {
627
- if (typeof value === "bigint" || typeof value === "boolean" || isFiniteNumber(value)) return { value: String(value) };
628
- if (value === null) return { value: "null" };
629
- if (typeof value === "string") return { value };
630
- if (value === void 0) return { value: "" };
631
- }
632
- const MAX_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
633
- const MIN_BIGINT = BigInt(Number.MIN_SAFE_INTEGER);
634
- function tryNumber(value) {
635
- if (typeof value === "bigint") return value <= MAX_BIGINT && value >= MIN_BIGINT ? { value: Number(value) } : void 0;
636
- if (typeof value === "boolean") return { value: value ? 1 : 0 };
637
- if (isFiniteNumber(value)) return { value };
638
- if (value === null || value === void 0) return { value: 0 };
639
- if (typeof value !== "string") return void 0;
640
- const coerced = +value;
641
- if (Number.isFinite(coerced)) return { value: coerced };
642
- const lowercase = value.toLowerCase();
643
- if (lowercase === "false") return { value: 0 };
644
- if (lowercase === "true") return { value: 1 };
645
- const big = bigIntOfString(value);
646
- return big !== void 0 && big <= MAX_BIGINT && big >= MIN_BIGINT ? { value: Number(big) } : void 0;
647
- }
648
- /** TypeBox's TryBigInt of a string: `12n`, `12.5` (truncated) or `12`; true and false are not reached from tryNumber. */
649
- function bigIntOfString(value) {
650
- if (/^-?(0|[1-9]\d*)n$/.test(value)) return BigInt(value.slice(0, -1));
651
- if (/^-?(0|[1-9]\d*)\.\d+$/.test(value)) return BigInt(value.split(".")[0]);
652
- if (/^-?(0|[1-9]\d*)$/.test(value)) return BigInt(value);
653
- }
654
- function tryBoolean(value) {
655
- if (typeof value === "bigint") return value === 0n ? { value: false } : value === 1n ? { value: true } : void 0;
656
- if (typeof value === "boolean") return { value };
657
- if (isFiniteNumber(value)) return value === 0 ? { value: false } : value === 1 ? { value: true } : void 0;
658
- if (value === null || value === void 0) return { value: false };
659
- if (typeof value !== "string") return void 0;
660
- const lowercase = value.toLowerCase();
661
- if (lowercase === "false" || value === "0") return { value: false };
662
- if (lowercase === "true" || value === "1") return { value: true };
663
- }
664
- function tryNull(value) {
665
- if (typeof value === "bigint") return value === 0n ? { value: null } : void 0;
666
- if (typeof value === "boolean") return value === false ? { value: null } : void 0;
667
- if (isFiniteNumber(value)) return value === 0 ? { value: null } : void 0;
668
- if (value === null || value === void 0) return { value: null };
669
- if (typeof value !== "string") return void 0;
670
- const lowercase = value.toLowerCase();
671
- return lowercase === "undefined" || lowercase === "null" || value === "" || value === "0" ? { value: null } : void 0;
672
- }
673
- function isFiniteNumber(value) {
674
- return typeof value === "number" && Number.isFinite(value);
675
- }
676
- function typesOf(schema) {
677
- if (typeof schema.type === "string") return [schema.type];
678
- if (Array.isArray(schema.type)) return schema.type.filter((type) => typeof type === "string");
679
- return [];
680
- }
681
- function matchesType(value, type) {
682
- switch (type) {
683
- case "number": return typeof value === "number";
684
- case "integer": return typeof value === "number" && Number.isInteger(value);
685
- case "boolean": return typeof value === "boolean";
686
- case "string": return typeof value === "string";
687
- case "null": return value === null;
688
- case "array": return Array.isArray(value);
689
- case "object": return isPlainObject(value);
690
- default: return false;
691
- }
692
- }
693
- function coercePrimitive(value, type) {
694
- switch (type) {
695
- case "number":
696
- case "integer":
697
- if (value === null) return 0;
698
- if (typeof value === "string" && value.trim() !== "") {
699
- const parsed = Number(value);
700
- if (type === "number" ? Number.isFinite(parsed) : Number.isInteger(parsed)) return parsed;
895
+ convert(node, base, value) {
896
+ if (!isSchemaObject(node)) return value;
897
+ const b = this.baseOf(node, base);
898
+ if (typeof node.$ref === "string") {
899
+ let target;
900
+ try {
901
+ target = this.c.r.resolve(node.$ref, b);
902
+ } catch {
903
+ return value;
701
904
  }
702
- if (typeof value === "boolean") return value ? 1 : 0;
703
- return value;
704
- case "boolean":
705
- if (value === null) return false;
706
- if (value === "true" || value === 1) return true;
707
- if (value === "false" || value === 0) return false;
708
- return value;
709
- case "string":
710
- if (value === null) return "";
711
- if (typeof value === "number" || typeof value === "boolean") return String(value);
712
- return value;
713
- case "null": return value === "" || value === 0 || value === false ? null : value;
714
- default: return value;
905
+ return isSchemaObject(target.node) ? this.reenter(target.node, target.base, value, true) : value;
906
+ }
907
+ return this.convertNode(node, b, value);
715
908
  }
716
- }
717
- function isPlainObject(value) {
718
- return typeof value === "object" && value !== null && !Array.isArray(value);
719
- }
720
- const ALWAYS = () => true;
721
- const NEVER = () => false;
722
- const IS_STRING = (v) => typeof v === "string";
723
- const IS_NUMBER = (v) => Number.isFinite(v);
724
- const IS_INTEGER = (v) => Number.isInteger(v);
725
- const IS_BOOLEAN = (v) => typeof v === "boolean";
726
- const IS_NULL = (v) => v === null;
727
- const IS_ARRAY = (v) => Array.isArray(v);
728
- const IS_BYTES = (v) => v instanceof Uint8Array;
729
- const IS_OBJECT = (v) => isObject(v);
730
- /** Whether `v` is a JSON object: not null, an array or bytes. */
731
- function isObject(v) {
732
- return typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Uint8Array);
733
- }
734
- /** The check of a `type` name; `bytes` (a Uint8Array) extends JSON Schema for values that never pass through JSON. */
735
- const TYPE_CHECKS = new Map([
736
- ["string", IS_STRING],
737
- ["number", IS_NUMBER],
738
- ["integer", IS_INTEGER],
739
- ["boolean", IS_BOOLEAN],
740
- ["null", IS_NULL],
741
- ["array", IS_ARRAY],
742
- ["object", IS_OBJECT],
743
- ["bytes", IS_BYTES]
744
- ]);
745
- /** Leaf checks that object and array loops run inline by code instead of calling them. */
746
- const LEAF_CODES = new Map([
747
- [IS_STRING, 1],
748
- [IS_NUMBER, 2],
749
- [IS_INTEGER, 3],
750
- [IS_BOOLEAN, 4],
751
- [IS_NULL, 5],
752
- [ALWAYS, 6]
753
- ]);
754
- function leafOk(code, x) {
755
- switch (code) {
756
- case 1: return typeof x === "string";
757
- case 2: return Number.isFinite(x);
758
- case 3: return Number.isInteger(x);
759
- case 4: return typeof x === "boolean";
760
- case 5: return x === null;
761
- default: return true;
909
+ /** One schema node's conversion: its first keyword among `const`, `enum`, the unions, `allOf` and `type`. */
910
+ convertNode(node, b, value) {
911
+ if (Object.hasOwn(node, "const")) return convertLiteral(node.const, value);
912
+ if (Array.isArray(node.enum)) return this.convertEnum(node, node.enum, b, value);
913
+ if (Array.isArray(node.anyOf)) return this.convertUnion(node, node.anyOf, b, value);
914
+ if (Array.isArray(node.oneOf)) return this.convertUnion(node, node.oneOf, b, value);
915
+ if (Array.isArray(node.allOf)) {
916
+ let next = value;
917
+ for (const member of node.allOf) next = this.convert(member, b, next);
918
+ return this.convertMembers(node, b, next);
919
+ }
920
+ switch (node.type) {
921
+ case "string": return orValue(tryString(value), value);
922
+ case "number": return orValue(tryNumber(value), value);
923
+ case "integer": {
924
+ const result = tryNumber(value);
925
+ return result === void 0 ? value : Math.trunc(result.value);
926
+ }
927
+ case "boolean": return orValue(tryBoolean(value), value);
928
+ case "null": return orValue(tryNull(value), value);
929
+ case "bigint": return orValue(tryBigInt(value), value);
930
+ case "undefined": return orValue(tryUndefined(value), value);
931
+ case "array": return this.convertArray(node, b, value);
932
+ case "object": return this.convertObject(node, b, value);
933
+ case void 0:
934
+ if (node.properties !== void 0 || node.patternProperties !== void 0) return this.convertObject(node, b, value);
935
+ if (node.items !== void 0 || node.prefixItems !== void 0) return this.convertArray(node, b, value);
936
+ return value;
937
+ default: return value;
938
+ }
762
939
  }
763
- }
764
- /** Whether a combined object or array check also checks the type: not at all, strictly, or passing other types. */
765
- const GUARD_NONE = 0;
766
- const GUARD_STRICT = 1;
767
- const GUARD_LOOSE = 2;
768
- /** Flags of a declared property in an object check's entries: its leaf code and two bits. */
769
- const CODE = 7;
770
- const REQUIRED = 8;
771
- const INHERITED = 16;
772
- /** All of `parts`; two and three parts get closures without a loop. */
773
- function and(parts) {
774
- switch (parts.length) {
775
- case 0: return ALWAYS;
776
- case 1: return parts[0];
777
- case 2: {
778
- const [a, b] = parts;
779
- return (v) => a(v) && b(v);
940
+ /** An enum is a union of its values: a member stays, else the first value the input converts to. */
941
+ convertEnum(node, values, base, value) {
942
+ if (values.some((member) => member === value)) return value;
943
+ for (const member of values) {
944
+ const candidate = convertLiteral(member, value);
945
+ if (candidate !== value && this.passes(node, base, candidate) === true) return candidate;
780
946
  }
781
- case 3: {
782
- const [a, b, c] = parts;
783
- return (v) => a(v) && b(v) && c(v);
947
+ return value;
948
+ }
949
+ /**
950
+ * A value one branch accepts stays; else the first branch's conversion that the whole union accepts. Branches are
951
+ * converted in order and only until one is accepted, so a later recursive branch is not entered needlessly. A node
952
+ * that also declares members (plain JSON Schema; TypeBox's never do) converts them into each candidate before it is
953
+ * checked, so a branch is not discarded for a sibling property that is converted after it.
954
+ */
955
+ convertUnion(node, branches, base, value) {
956
+ if (branches.some((branch) => this.passes(branch, base, value) === true)) return this.convertMembers(node, base, value);
957
+ for (const branch of branches) {
958
+ const candidate = this.convertMembers(node, base, this.convert(branch, base, value));
959
+ if (this.passes(node, base, candidate) === true) return candidate;
784
960
  }
785
- default: return (v) => {
786
- for (let i = 0; i < parts.length; i++) if (!parts[i](v)) return false;
787
- return true;
788
- };
961
+ return this.convertMembers(node, base, value);
789
962
  }
790
- }
791
- /** A check whose implementation is set later: the indirection a recursive reference goes through. */
792
- function indirect(slot) {
793
- return (v) => slot.check(v);
794
- }
795
- /** `check` with `uri` pushed onto the dynamic scope while it runs (only for schemas with `$dynamicRef`). */
796
- function scopedCheck(scope, uri, check$1) {
797
- return (v) => {
798
- scope.push(uri);
799
- const ok = check$1(v);
800
- scope.pop();
801
- return ok;
802
- };
803
- }
804
- /** Any of the named types. */
805
- function typeCheck(types) {
806
- const checks = types.map((t) => (typeof t === "string" ? TYPE_CHECKS.get(t) : void 0) ?? NEVER);
807
- if (checks.length === 1) return checks[0];
808
- return (v) => {
809
- for (let i = 0; i < checks.length; i++) if (checks[i](v)) return true;
810
- return false;
811
- };
812
- }
813
- /** A Uint8Array within the length bounds. */
814
- function bytesCheck(min, max) {
815
- return (v) => v instanceof Uint8Array && v.length >= min && v.length <= max;
816
- }
817
- /** The number of Unicode code points of `s`: JSON Schema string lengths count code points, not UTF-16 units. */
818
- function codePoints(s) {
819
- let n = 0;
820
- for (let i = 0; i < s.length; i++, n++) {
821
- const c = s.charCodeAt(i);
822
- if (c >= 55296 && c <= 56319 && i + 1 < s.length) {
823
- const next = s.charCodeAt(i + 1);
824
- if (next >= 56320 && next <= 57343) i++;
963
+ /** The object members of a union or intersection node, when it declares any. */
964
+ convertMembers(node, base, value) {
965
+ return node.properties !== void 0 || node.patternProperties !== void 0 || node.additionalProperties !== void 0 ? this.convertObject(node, base, value) : value;
966
+ }
967
+ convertArray(node, base, value) {
968
+ const prefix = Array.isArray(node.items) ? node.items : Array.isArray(node.prefixItems) ? node.prefixItems : void 0;
969
+ if (prefix !== void 0) {
970
+ if (!Array.isArray(value)) return value;
971
+ const rest = Array.isArray(node.items) ? void 0 : node.items;
972
+ return mapItems(value, (item$1, i) => {
973
+ const schema = i < prefix.length ? prefix[i] : rest;
974
+ return schema === void 0 ? item$1 : this.convert(schema, base, item$1);
975
+ });
825
976
  }
977
+ if (Array.isArray(value)) return node.items === void 0 ? value : mapItems(value, (item$1) => this.convert(node.items, base, item$1));
978
+ const item = node.items;
979
+ return [isSchemaObject(item) ? this.reenter(item, base, value, false) : value];
826
980
  }
827
- return n;
828
- }
829
- function lengthOk(s, min, max) {
830
- const units = s.length;
831
- if (units < min) return false;
832
- if (units < 2 * min && codePoints(s) < min) return false;
833
- return units <= max || codePoints(s) <= max;
834
- }
835
- /** The string keywords: length bounds, pattern and format. */
836
- function stringCheck(strict, min, max, re, format) {
837
- if (re !== void 0 && min === 0 && max === Number.POSITIVE_INFINITY && format === void 0) return strict ? (v) => typeof v === "string" && re.test(v) : (v) => typeof v !== "string" || re.test(v);
838
- if (re === void 0 && format === void 0) return strict ? (v) => typeof v === "string" && lengthOk(v, min, max) : (v) => typeof v !== "string" || lengthOk(v, min, max);
839
- return (v) => {
840
- if (typeof v !== "string") return !strict;
841
- return lengthOk(v, min, max) && (re === void 0 || re.test(v)) && (format === void 0 || format(v));
842
- };
843
- }
844
- /** The number keywords. `mode`: 0 passes other types, 1 requires a number, 2 an integer. */
845
- function numberCheck(mode, min, max, xmin, xmax, multipleOf) {
846
- return (v) => {
847
- if (typeof v !== "number") return mode === 0;
848
- if (mode === 2 && !Number.isInteger(v)) return false;
849
- return v >= min && v <= max && v > xmin && v < xmax && (multipleOf === void 0 || isMultiple(v, multipleOf));
850
- };
851
- }
852
- /** Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. */
853
- function isMultiple(v, d) {
854
- const q = v / d;
855
- if (Number.isInteger(q)) return true;
856
- if (!Number.isFinite(q)) return v % d === 0;
857
- const [vc, ve] = decimal(v);
858
- const [dc, de] = decimal(d);
859
- return ve >= de ? vc * 10n ** BigInt(ve - de) % dc === 0n : vc % (dc * 10n ** BigInt(de - ve)) === 0n;
860
- }
861
- function decimal(x) {
862
- const [mantissa, exponent = "0"] = Math.abs(x).toString().split("e");
863
- const point = mantissa.indexOf(".");
864
- const fraction = point < 0 ? 0 : mantissa.length - point - 1;
865
- return [BigInt(mantissa.replace(".", "")), Number(exponent) - fraction];
866
- }
867
- /** `inner` for objects; other values pass unless `strict`. */
868
- function guardObject(inner, strict) {
869
- return strict ? (v) => isObject(v) && inner(v) : (v) => !isObject(v) || inner(v);
870
- }
871
- /** `inner` for arrays; other values pass unless `strict`. */
872
- function guardArray(inner, strict) {
873
- return strict ? (v) => Array.isArray(v) && inner(v) : (v) => !Array.isArray(v) || inner(v);
874
- }
875
- /**
876
- * An object with declared properties: `entries` is `[key, check, flags]` per property. The value's own keys are
877
- * walked with for...in (whose `o[k]` V8 reads from the enum cache, unlike a load by a name from a list), each found
878
- * among the declared names; `closed` rejects any other key. A property whose value is `undefined` counts as absent.
879
- */
880
- function declaredObject(entries$1, requiredCount, closed, guard) {
881
- const n = entries$1.length;
882
- return (v) => {
883
- if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
884
- const o = v;
885
- let required = 0;
886
- for (const k in o) {
887
- let i = 0;
888
- while (i < n && entries$1[i] !== k) i += 3;
889
- if (i === n) {
890
- if (closed) return false;
891
- continue;
981
+ convertObject(node, base, value) {
982
+ if (!isPlainObject(value)) return value;
983
+ const properties = isSchemaObject(node.properties) ? node.properties : {};
984
+ const patterns = isSchemaObject(node.patternProperties) ? Object.entries(node.patternProperties).map(([pattern, schema]) => [sharedRegex(pattern), schema]) : [];
985
+ const additional = isSchemaObject(node.additionalProperties) ? node.additionalProperties : void 0;
986
+ let out;
987
+ for (const key of Object.keys(value)) {
988
+ if (value[key] === void 0) continue;
989
+ let next = value[key];
990
+ let matched = Object.hasOwn(properties, key);
991
+ if (matched) next = this.convert(properties[key], base, next);
992
+ for (const [pattern, schema] of patterns) {
993
+ if (!pattern.test(key)) continue;
994
+ matched = true;
995
+ next = this.convert(schema, base, next);
892
996
  }
893
- const x = o[k];
894
- if (x === void 0) continue;
895
- const f = entries$1[i + 2];
896
- const c = f & CODE;
897
- if (c !== 0 ? !leafOk(c, x) : !entries$1[i + 1](x)) return false;
898
- if (f & REQUIRED) required++;
899
- }
900
- return required === requiredCount;
901
- };
902
- }
903
- /** An object with declared properties, required keys not declared, pattern properties and additional properties. */
904
- function openObject(entries$1, keys, extraRequired, patterns, additional, guard) {
905
- const known = patterns.length > 0 || additional !== void 0 ? new Set(keys) : void 0;
906
- return (v) => {
907
- if (guard !== GUARD_NONE && !isObject(v)) return guard === GUARD_LOOSE;
908
- const o = v;
909
- for (let i = 0; i < entries$1.length; i += 3) {
910
- const k = entries$1[i];
911
- const x = o[k];
912
- const f = entries$1[i + 2];
913
- if (x === void 0 || f & INHERITED && !Object.hasOwn(o, k)) {
914
- if (f & REQUIRED) return false;
915
- continue;
997
+ if (!matched && additional !== void 0) next = this.convert(additional, base, next);
998
+ if (next !== value[key]) {
999
+ out ??= { ...value };
1000
+ out[key] = next;
916
1001
  }
917
- const c = f & CODE;
918
- if (c !== 0 ? !leafOk(c, x) : !entries$1[i + 1](x)) return false;
919
1002
  }
920
- for (let i = 0; i < extraRequired.length; i++) if (!Object.hasOwn(o, extraRequired[i])) return false;
921
- if (known === void 0) return true;
922
- for (const k of Object.keys(o)) {
923
- let matched = known.has(k);
924
- for (let i = 0; i < patterns.length; i++) if (patterns[i][0].test(k)) {
925
- matched = true;
926
- if (!patterns[i][1](o[k])) return false;
1003
+ return out ?? value;
1004
+ }
1005
+ coerceTypes(value, schema, base) {
1006
+ if (!isSchemaObject(schema)) return value;
1007
+ const b = this.baseOf(schema, base);
1008
+ let next = value;
1009
+ if (Array.isArray(schema.allOf)) for (const member of schema.allOf) next = this.coerceTypes(next, member, b);
1010
+ if (Array.isArray(schema.anyOf)) next = this.coerceUnion(next, schema.anyOf, b);
1011
+ if (Array.isArray(schema.oneOf)) next = this.coerceUnion(next, schema.oneOf, b);
1012
+ const types = typesOf(schema);
1013
+ const matchesUnionMember = types.length > 1 && types.some((type) => matchesType(next, type));
1014
+ if (types.length > 0 && !matchesUnionMember) for (const type of types) {
1015
+ const candidate = coercePrimitive(next, type);
1016
+ if (candidate !== next) {
1017
+ next = candidate;
1018
+ break;
927
1019
  }
928
- if (!matched && additional !== void 0 && !additional(o[k])) return false;
929
1020
  }
930
- return true;
931
- };
932
- }
933
- /** Every property name passes `check` (`v` is an object). */
934
- function propertyNamesCheck(check$1) {
935
- return (v) => {
936
- for (const k in v) if (!check$1(k)) return false;
937
- return true;
938
- };
939
- }
940
- /** `minProperties` and `maxProperties` (`v` is an object). */
941
- function propertyCountCheck(min, max) {
942
- return (v) => {
943
- let n = 0;
944
- for (const _ in v) n++;
945
- return n >= min && n <= max;
946
- };
947
- }
948
- /** `dependentRequired` (`v` is an object). */
949
- function dependentRequiredCheck(entries$1) {
950
- return (v) => {
951
- const o = v;
952
- for (const [k, list] of entries$1) if (Object.hasOwn(o, k)) {
953
- for (const r of list) if (!Object.hasOwn(o, r)) return false;
1021
+ if (types.includes("object") && isPlainObject(next)) next = this.coerceProperties(next, schema, b);
1022
+ if (types.includes("array") && Array.isArray(next)) next = this.coerceItems(next, schema, b);
1023
+ return next;
1024
+ }
1025
+ coerceUnion(value, branches, base) {
1026
+ for (const branch of branches) if (this.passes(branch, base, value) === true) return value;
1027
+ for (const branch of branches) {
1028
+ const coerced = this.coerceTypes(value, branch, base);
1029
+ if (this.passes(branch, base, coerced) === true) return coerced;
1030
+ }
1031
+ return value;
1032
+ }
1033
+ coerceProperties(value, schema, base) {
1034
+ const properties = isSchemaObject(schema.properties) ? schema.properties : {};
1035
+ const patterns = isSchemaObject(schema.patternProperties) ? Object.entries(schema.patternProperties).map(([pattern, item]) => [sharedRegex(pattern), item]) : [];
1036
+ const additional = isSchemaObject(schema.additionalProperties) ? schema.additionalProperties : void 0;
1037
+ let out;
1038
+ const set = (key, next) => {
1039
+ if (next === (out ?? value)[key]) return;
1040
+ out ??= { ...value };
1041
+ out[key] = next;
1042
+ };
1043
+ for (const [key, property] of Object.entries(properties)) if (key in value) set(key, this.coerceTypes(value[key], property, base));
1044
+ for (const key of Object.keys(value)) {
1045
+ let matched = Object.hasOwn(properties, key);
1046
+ for (const [pattern, item] of patterns) {
1047
+ if (!pattern.test(key)) continue;
1048
+ matched = true;
1049
+ set(key, this.coerceTypes((out ?? value)[key], item, base));
1050
+ }
1051
+ if (!matched && additional !== void 0) set(key, this.coerceTypes(value[key], additional, base));
954
1052
  }
955
- return true;
956
- };
1053
+ return out ?? value;
1054
+ }
1055
+ coerceItems(value, schema, base) {
1056
+ return mapItems(value, (item, i) => {
1057
+ const itemSchema = itemSchemaAt(schema, i);
1058
+ return isSchemaObject(itemSchema) ? this.coerceTypes(item, itemSchema, base) : item;
1059
+ });
1060
+ }
1061
+ };
1062
+ /**
1063
+ * The schema of an array's item at `index`: a tuple's (`prefixItems`, or a draft-7 `items` array) for its prefix,
1064
+ * `items` after `prefixItems`, or `items` for every item. Undefined when nothing applies.
1065
+ */
1066
+ function itemSchemaAt(schema, index) {
1067
+ if (Array.isArray(schema.items)) return schema.items[index];
1068
+ if (Array.isArray(schema.prefixItems) && index < schema.prefixItems.length) return schema.prefixItems[index];
1069
+ return schema.items;
957
1070
  }
958
- /** `dependentSchemas` (`v` is an object). */
959
- function dependentSchemasCheck(entries$1) {
960
- return (v) => {
961
- for (const [k, check$1] of entries$1) if (Object.hasOwn(v, k) && !check$1(v)) return false;
962
- return true;
963
- };
1071
+ /** `items` mapped by `map`; the same array when every item maps to itself. */
1072
+ function mapItems(items, map$1) {
1073
+ let out;
1074
+ for (let i = 0; i < items.length; i++) {
1075
+ const next = map$1(items[i], i);
1076
+ if (next === items[i]) continue;
1077
+ out ??= items.slice();
1078
+ out[i] = next;
1079
+ }
1080
+ return out ?? items;
964
1081
  }
965
- /** The common array: guard, length bounds, prefix items and the items after them, in one closure. */
966
- function listCheck(guard, min, max, prefix, rest) {
967
- const n = prefix.length;
968
- const code = rest === void 0 ? 0 : LEAF_CODES.get(rest) ?? 0;
969
- return (v) => {
970
- if (!Array.isArray(v)) return guard === GUARD_LOOSE;
971
- const length = v.length;
972
- if (length < min || length > max) return false;
973
- const m = length < n ? length : n;
974
- for (let i = 0; i < m; i++) if (!prefix[i](v[i])) return false;
975
- if (rest === void 0) return true;
976
- if (code !== 0) {
977
- for (let i = n; i < length; i++) if (!leafOk(code, v[i])) return false;
978
- return true;
979
- }
980
- for (let i = n; i < length; i++) if (!rest(v[i])) return false;
981
- return true;
982
- };
1082
+ function orValue(result, value) {
1083
+ return result === void 0 ? value : result.value;
983
1084
  }
984
- /** Prefix items and the items after them (`v` is an array). */
985
- function itemsCheck(prefix, rest) {
986
- return listCheck(GUARD_NONE, 0, Number.POSITIVE_INFINITY, prefix, rest);
1085
+ /** A literal (`const`, an enum value) taken from a value of another type that converts to exactly it. */
1086
+ function convertLiteral(literal, value) {
1087
+ if (literal === value) return value;
1088
+ const result = typeof literal === "string" ? tryString(value) : typeof literal === "number" ? tryNumber(value) : typeof literal === "boolean" ? tryBoolean(value) : typeof literal === "bigint" ? tryBigInt(value) : void 0;
1089
+ return result !== void 0 && result.value === literal ? result.value : value;
987
1090
  }
988
- /** `minItems` and `maxItems` (`v` is an array). */
989
- function lengthCheck(min, max) {
990
- return (v) => v.length >= min && v.length <= max;
1091
+ function tryString(value) {
1092
+ if (typeof value === "bigint" || typeof value === "boolean" || isFiniteNumber(value)) return { value: String(value) };
1093
+ if (value === null) return { value: "null" };
1094
+ if (typeof value === "string") return { value };
1095
+ if (value === void 0) return { value: "" };
991
1096
  }
992
- /** `contains` with its bounds (`v` is an array). */
993
- function containsCheck(check$1, min, max) {
994
- return (v) => {
995
- const list = v;
996
- let n = 0;
997
- for (let i = 0; i < list.length; i++) if (check$1(list[i]) && ++n > max) return false;
998
- return n >= min;
999
- };
1097
+ const MAX_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
1098
+ const MIN_BIGINT = BigInt(Number.MIN_SAFE_INTEGER);
1099
+ function tryNumber(value) {
1100
+ if (typeof value === "bigint") return value <= MAX_BIGINT && value >= MIN_BIGINT ? { value: Number(value) } : void 0;
1101
+ if (typeof value === "boolean") return { value: value ? 1 : 0 };
1102
+ if (isFiniteNumber(value)) return { value };
1103
+ if (value === null || value === void 0) return { value: 0 };
1104
+ if (typeof value !== "string") return void 0;
1105
+ const coerced = +value;
1106
+ if (Number.isFinite(coerced)) return { value: coerced };
1107
+ const lowercase = value.toLowerCase();
1108
+ if (lowercase === "false") return { value: 0 };
1109
+ if (lowercase === "true") return { value: 1 };
1110
+ const big = bigIntOfString(value);
1111
+ return big !== void 0 && big <= MAX_BIGINT && big >= MIN_BIGINT ? { value: Number(big) } : void 0;
1000
1112
  }
1001
- /** `uniqueItems` (`v` is an array). */
1002
- function uniqueCheck(v) {
1003
- const list = v;
1004
- for (let i = 1; i < list.length; i++) for (let j = 0; j < i; j++) if (deepEqual(list[i], list[j])) return false;
1005
- return true;
1113
+ /** TypeBox's TryBigInt of a string: `12n`, `12.5` (truncated) or `12`; true and false are not reached from tryNumber. */
1114
+ function bigIntOfString(value) {
1115
+ if (/^-?(0|[1-9]\d*)n$/.test(value)) return BigInt(value.slice(0, -1));
1116
+ if (/^-?(0|[1-9]\d*)\.\d+$/.test(value)) return BigInt(value.split(".")[0]);
1117
+ if (/^-?(0|[1-9]\d*)$/.test(value)) return BigInt(value);
1006
1118
  }
1007
- /** `enum`: up to 8 primitives by a scan, more by a Set, objects and arrays by deep equality. */
1008
- function enumCheck(values) {
1009
- const primitives = values.filter((x) => typeof x !== "object" || x === null);
1010
- if (primitives.length === values.length && primitives.length <= 8) return (v) => {
1011
- for (let i = 0; i < primitives.length; i++) if (primitives[i] === v) return true;
1012
- return false;
1013
- };
1014
- const set = new Set(primitives);
1015
- const objects = values.filter((x) => typeof x === "object" && x !== null);
1016
- return (v) => set.has(v) || typeof v === "object" && objects.some((o) => deepEqual(v, o));
1119
+ /** TypeBox's TryBigInt: booleans to 1n/0n, numbers truncated, null and undefined to 0n, bigint-like strings. */
1120
+ function tryBigInt(value) {
1121
+ if (typeof value === "bigint") return { value };
1122
+ if (typeof value === "boolean") return { value: value ? 1n : 0n };
1123
+ if (isFiniteNumber(value)) return { value: BigInt(Math.trunc(value)) };
1124
+ if (value === null || value === void 0) return { value: 0n };
1125
+ if (typeof value !== "string") return void 0;
1126
+ const big = bigIntOfString(value);
1127
+ if (big !== void 0) return { value: big };
1128
+ const lowercase = value.toLowerCase();
1129
+ return lowercase === "false" ? { value: 0n } : lowercase === "true" ? { value: 1n } : void 0;
1017
1130
  }
1018
- /** `const`. */
1019
- function constCheck(value) {
1020
- if (typeof value !== "object" || value === null) return (v) => v === value;
1021
- return (v) => deepEqual(v, value);
1131
+ /** TypeBox's TryUndefined: 0n, false, 0, null, `"undefined"`, `"null"`, `""` and `"0"` to undefined. */
1132
+ function tryUndefined(value) {
1133
+ if (value === void 0 || value === null || value === 0n || value === false || value === 0) return { value: void 0 };
1134
+ if (typeof value !== "string") return void 0;
1135
+ const lowercase = value.toLowerCase();
1136
+ return lowercase === "undefined" || lowercase === "null" || value === "" || value === "0" ? { value: void 0 } : void 0;
1022
1137
  }
1023
- /** `anyOf`. */
1024
- function anyCheck(checks) {
1025
- return (v) => {
1026
- for (let i = 0; i < checks.length; i++) if (checks[i](v)) return true;
1027
- return false;
1028
- };
1138
+ function tryBoolean(value) {
1139
+ if (typeof value === "bigint") return value === 0n ? { value: false } : value === 1n ? { value: true } : void 0;
1140
+ if (typeof value === "boolean") return { value };
1141
+ if (isFiniteNumber(value)) return value === 0 ? { value: false } : value === 1 ? { value: true } : void 0;
1142
+ if (value === null || value === void 0) return { value: false };
1143
+ if (typeof value !== "string") return void 0;
1144
+ const lowercase = value.toLowerCase();
1145
+ if (lowercase === "false" || value === "0") return { value: false };
1146
+ if (lowercase === "true" || value === "1") return { value: true };
1029
1147
  }
1030
- /** `oneOf`. */
1031
- function oneCheck(checks) {
1032
- return (v) => {
1033
- let n = 0;
1034
- for (let i = 0; i < checks.length; i++) if (checks[i](v) && ++n > 1) return false;
1035
- return n === 1;
1036
- };
1148
+ function tryNull(value) {
1149
+ if (typeof value === "bigint") return value === 0n ? { value: null } : void 0;
1150
+ if (typeof value === "boolean") return value === false ? { value: null } : void 0;
1151
+ if (isFiniteNumber(value)) return value === 0 ? { value: null } : void 0;
1152
+ if (value === null || value === void 0) return { value: null };
1153
+ if (typeof value !== "string") return void 0;
1154
+ const lowercase = value.toLowerCase();
1155
+ return lowercase === "undefined" || lowercase === "null" || value === "" || value === "0" ? { value: null } : void 0;
1037
1156
  }
1038
- /** `not`. */
1039
- function notCheck(check$1) {
1040
- return (v) => !check$1(v);
1157
+ function isFiniteNumber(value) {
1158
+ return typeof value === "number" && Number.isFinite(value);
1041
1159
  }
1042
- /** `if`, `then` and `else`. */
1043
- function ifCheck(cond, then, otherwise) {
1044
- return (v) => cond(v) ? then(v) : otherwise(v);
1160
+ function typesOf(schema) {
1161
+ if (typeof schema.type === "string") return [schema.type];
1162
+ if (Array.isArray(schema.type)) return schema.type.filter((type) => typeof type === "string");
1163
+ return [];
1045
1164
  }
1046
- /** A union of objects tagged by a const on `key`: the tag selects the one branch that can match. */
1047
- function taggedCheck(key, tags, checks) {
1048
- return (v) => {
1049
- if (!isObject(v)) return false;
1050
- const tag = v[key];
1051
- for (let i = 0; i < tags.length; i++) if (tags[i] === tag) return checks[i](v);
1052
- return false;
1053
- };
1165
+ function matchesType(value, type) {
1166
+ switch (type) {
1167
+ case "number": return Number.isFinite(value);
1168
+ case "integer": return typeof value === "number" && Number.isInteger(value);
1169
+ case "boolean": return typeof value === "boolean";
1170
+ case "string": return typeof value === "string";
1171
+ case "null": return value === null;
1172
+ case "bigint": return typeof value === "bigint";
1173
+ case "undefined": return value === void 0;
1174
+ case "array": return Array.isArray(value);
1175
+ case "object": return isPlainObject(value);
1176
+ default: return false;
1177
+ }
1054
1178
  }
1055
- /** JSON equality: same primitives, arrays with equal items in order, objects with the same keys and equal values. */
1056
- function deepEqual(a, b) {
1057
- if (a === b) return true;
1058
- if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
1059
- const array = Array.isArray(a);
1060
- if (array !== Array.isArray(b)) return false;
1061
- if (array) {
1062
- const x$1 = a;
1063
- const y$1 = b;
1064
- if (x$1.length !== y$1.length) return false;
1065
- for (let i = 0; i < x$1.length; i++) if (!deepEqual(x$1[i], y$1[i])) return false;
1066
- return true;
1179
+ function coercePrimitive(value, type) {
1180
+ switch (type) {
1181
+ case "number":
1182
+ case "integer":
1183
+ if (value === null) return 0;
1184
+ if (typeof value === "string" && value.trim() !== "") {
1185
+ const parsed = Number(value);
1186
+ if (type === "number" ? Number.isFinite(parsed) : Number.isInteger(parsed)) return parsed;
1187
+ }
1188
+ if (typeof value === "boolean") return value ? 1 : 0;
1189
+ return value;
1190
+ case "boolean":
1191
+ if (value === null) return false;
1192
+ if (value === "true" || value === 1) return true;
1193
+ if (value === "false" || value === 0) return false;
1194
+ return value;
1195
+ case "string":
1196
+ if (value === null) return "";
1197
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
1198
+ return value;
1199
+ case "null": return value === "" || value === 0 || value === false ? null : value;
1200
+ default: return value;
1067
1201
  }
1068
- const x = a;
1069
- const y = b;
1070
- const keys = Object.keys(x);
1071
- if (keys.length !== Object.keys(y).length) return false;
1072
- for (const k of keys) if (!Object.hasOwn(y, k) || !deepEqual(x[k], y[k])) return false;
1073
- return true;
1202
+ }
1203
+ function isPlainObject(value) {
1204
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1074
1205
  }
1075
1206
  /** Empty annotations. */
1076
1207
  function newAnnotations() {
@@ -1303,8 +1434,9 @@ var Compiler = class {
1303
1434
  } else {
1304
1435
  const string = this.stringGroup(node, single === "string");
1305
1436
  if (string !== void 0) parts.push(string);
1306
- const numeric = single === "number" || single === "integer";
1307
- const number$1 = this.numberGroup(node, single === "number" ? 1 : single === "integer" ? 2 : 0);
1437
+ const numeric = single === "number" || single === "integer" || single === "bigint";
1438
+ const mode = single === "number" ? 1 : single === "integer" ? 2 : single === "bigint" ? 3 : 0;
1439
+ const number$1 = this.numberGroup(node, mode);
1308
1440
  if (number$1 !== void 0) parts.push(number$1);
1309
1441
  const object = this.objectGroup(node, base, single === "object");
1310
1442
  if (object !== void 0) parts.push(object);
@@ -1333,7 +1465,7 @@ var Compiler = class {
1333
1465
  }
1334
1466
  union(branches, base, exactlyOne) {
1335
1467
  const checks = () => branches.map((s) => this.check(s, base));
1336
- return constUnion(branches, exactlyOne) ?? this.tagged(branches, base) ?? (exactlyOne ? oneCheck(checks()) : anyCheck(checks()));
1468
+ return constUnion(branches, exactlyOne) ?? this.tagged(branches, base) ?? (exactlyOne ? oneCheck(checks()) : anyOf(checks()));
1337
1469
  }
1338
1470
  ref(ref, base) {
1339
1471
  const target = this.r.resolve(ref, base);
@@ -1409,11 +1541,11 @@ var Compiler = class {
1409
1541
  return stringCheck(strict, min, max, re, format);
1410
1542
  }
1411
1543
  numberGroup(node, mode) {
1412
- const min = number(node.minimum);
1413
- const max = number(node.maximum);
1414
- const xmin = number(node.exclusiveMinimum);
1415
- const xmax = number(node.exclusiveMaximum);
1416
- const multipleOf = number(node.multipleOf);
1544
+ const min = bound(node.minimum);
1545
+ const max = bound(node.maximum);
1546
+ const xmin = bound(node.exclusiveMinimum);
1547
+ const xmax = bound(node.exclusiveMaximum);
1548
+ const multipleOf = bound(node.multipleOf);
1417
1549
  if ([
1418
1550
  min,
1419
1551
  max,
@@ -1457,8 +1589,9 @@ var Compiler = class {
1457
1589
  const entries$1 = [];
1458
1590
  for (const k of keys) {
1459
1591
  const check$1 = this.check(properties[k], base);
1460
- const flags = (LEAF_CODES.get(check$1) ?? 0) | (required.includes(k) ? REQUIRED : 0) | (k in Object.prototype ? INHERITED : 0);
1461
- entries$1.push(k, check$1, flags);
1592
+ const leaf$1 = leafOf(check$1);
1593
+ const flags = (leaf$1?.[0] ?? 0) | (required.includes(k) ? REQUIRED : 0) | (k in Object.prototype ? INHERITED : 0);
1594
+ entries$1.push(k, leaf$1 === void 0 ? check$1 : leaf$1[1], flags);
1462
1595
  }
1463
1596
  const extraRequired = required.filter((k) => !Object.hasOwn(properties, k));
1464
1597
  const pats = patternKeys.map((p) => [sharedRegex(p), this.check(patterns[p], base)]);
@@ -1630,6 +1763,18 @@ function constUnion(branches, exactlyOne) {
1630
1763
  }
1631
1764
  return enumCheck(values);
1632
1765
  }
1766
+ /** `anyOf`: its bare-type branches (`nullable` gives one) run as one type test. */
1767
+ function anyOf(checks) {
1768
+ const types = [];
1769
+ const rest = [];
1770
+ for (const check$1 of checks) {
1771
+ const type = BARE_TYPES.get(check$1);
1772
+ if (type === void 0) rest.push(check$1);
1773
+ else types.push(type);
1774
+ }
1775
+ return types.length === 0 ? anyCheck(rest) : maskedAnyCheck(typeMask(types), rest);
1776
+ }
1777
+ const BARE_TYPES = new Map([...TYPE_CHECKS].map(([type, check$1]) => [check$1, type]));
1633
1778
  function tagOf(branch, key) {
1634
1779
  const properties = branch.properties;
1635
1780
  if (branch.type !== "object" || !Array.isArray(branch.required) || !branch.required.includes(key)) return void 0;
@@ -1637,6 +1782,10 @@ function tagOf(branch, key) {
1637
1782
  if (!isSchemaObject(tag) || !Object.hasOwn(tag, "const")) return void 0;
1638
1783
  return typeof tag.const === "object" && tag.const !== null ? void 0 : tag.const;
1639
1784
  }
1785
+ /** A number keyword's value: a number, or a bigint (TypeBox's `Type.BigInt` bounds). */
1786
+ function bound(value) {
1787
+ return typeof value === "number" || typeof value === "bigint" ? value : void 0;
1788
+ }
1640
1789
  function number(value) {
1641
1790
  return typeof value === "number" ? value : void 0;
1642
1791
  }
@@ -1711,7 +1860,7 @@ var Explainer = class {
1711
1860
  if (Object.hasOwn(node, "const") && !constCheck(node.const)(v)) push("const", `expected ${JSON.stringify(node.const)}`);
1712
1861
  if (typeof v === "string" && node.type !== "bytes") this.walkString(node, v, push);
1713
1862
  if (v instanceof Uint8Array && node.type === "bytes") this.walkBytes(node, v, push);
1714
- if (typeof v === "number") this.walkNumber(node, v, push);
1863
+ if (typeof v === "number" || typeof v === "bigint") this.walkNumber(node, v, push);
1715
1864
  if (isObject(v)) this.walkObject(node, b, v, path, out, push);
1716
1865
  if (Array.isArray(v)) this.walkArray(node, b, v, path, out, push);
1717
1866
  if (Array.isArray(node.allOf)) for (const s of node.allOf) this.walk(s, b, v, path, out);
@@ -1773,16 +1922,21 @@ var Explainer = class {
1773
1922
  if (typeof node.maxLength === "number" && v.length > node.maxLength) push("maxLength", `expected at most ${node.maxLength} bytes`);
1774
1923
  }
1775
1924
  walkNumber(node, v, push) {
1776
- if (typeof node.minimum === "number" && v < node.minimum) push("minimum", `expected >= ${node.minimum}`);
1777
- if (typeof node.maximum === "number" && v > node.maximum) push("maximum", `expected <= ${node.maximum}`);
1778
- if (typeof node.exclusiveMinimum === "number" && v <= node.exclusiveMinimum) push("exclusiveMinimum", `expected > ${node.exclusiveMinimum}`);
1779
- if (typeof node.exclusiveMaximum === "number" && v >= node.exclusiveMaximum) push("exclusiveMaximum", `expected < ${node.exclusiveMaximum}`);
1780
- if (typeof node.multipleOf === "number" && !isMultiple(v, node.multipleOf)) push("multipleOf", `expected a multiple of ${node.multipleOf}`);
1925
+ const { minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf } = node;
1926
+ if (isBound(minimum) && v < minimum) push("minimum", `expected >= ${minimum}`);
1927
+ if (isBound(maximum) && v > maximum) push("maximum", `expected <= ${maximum}`);
1928
+ if (isBound(exclusiveMinimum) && v <= exclusiveMinimum) push("exclusiveMinimum", `expected > ${exclusiveMinimum}`);
1929
+ if (isBound(exclusiveMaximum) && v >= exclusiveMaximum) push("exclusiveMaximum", `expected < ${exclusiveMaximum}`);
1930
+ if (isBound(multipleOf) && !isMultiple(v, multipleOf)) push("multipleOf", `expected a multiple of ${multipleOf}`);
1781
1931
  }
1782
1932
  walkObject(node, b, v, path, out, push) {
1783
1933
  const properties = isSchemaObject(node.properties) ? node.properties : {};
1784
1934
  const required = Array.isArray(node.required) ? node.required : [];
1785
- for (const k of required) if (typeof k === "string" && (!Object.hasOwn(v, k) || v[k] === void 0)) push("required", "missing", pointer(path, k));
1935
+ for (const k of required) {
1936
+ if (typeof k !== "string") continue;
1937
+ const accepted = v[k] === void 0 && Object.hasOwn(properties, k) && this.ok(properties[k], b, void 0);
1938
+ if (!Object.hasOwn(v, k) || v[k] === void 0 && !accepted) push("required", "missing", pointer(path, k));
1939
+ }
1786
1940
  for (const k of Object.keys(properties)) if (Object.hasOwn(v, k) && v[k] !== void 0) this.walk(properties[k], b, v[k], pointer(path, k), out);
1787
1941
  const patterns = isSchemaObject(node.patternProperties) ? Object.entries(node.patternProperties) : [];
1788
1942
  for (const k of Object.keys(v)) {
@@ -1877,12 +2031,12 @@ var Explainer = class {
1877
2031
  }
1878
2032
  /** How many of the keys of `v` the branch `s` (through a `$ref`) declares in `properties`. */
1879
2033
  declaredKeys(s, b, v) {
1880
- const node = isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, b).node : s;
2034
+ const node = isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, this.c.r.baseFor(s, b)).node : s;
1881
2035
  const properties = isSchemaObject(node) && isSchemaObject(node.properties) ? node.properties : {};
1882
2036
  return Object.keys(v).filter((k) => Object.hasOwn(properties, k)).length;
1883
2037
  }
1884
2038
  tagOf(branches, b) {
1885
- const resolved = branches.map((s) => isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, b).node : s);
2039
+ const resolved = branches.map((s) => isSchemaObject(s) && typeof s.$ref === "string" ? this.c.r.resolve(s.$ref, this.c.r.baseFor(s, b)).node : s);
1886
2040
  const first = resolved[0];
1887
2041
  if (!isSchemaObject(first) || !isSchemaObject(first.properties)) return void 0;
1888
2042
  for (const key of Object.keys(first.properties)) {
@@ -1897,7 +2051,7 @@ var Explainer = class {
1897
2051
  }
1898
2052
  }
1899
2053
  walkUnevaluated(node, b, v, path, out) {
1900
- const { unevaluatedProperties: unevaluatedProperties$1, unevaluatedItems: unevaluatedItems$1,...rest } = node;
2054
+ const { unevaluatedProperties: unevaluatedProperties$1, unevaluatedItems: unevaluatedItems$1, $id: _id,...rest } = node;
1901
2055
  const a = newAnnotations();
1902
2056
  this.c.annotating(rest, b)(v, a);
1903
2057
  if (isObject(v) && unevaluatedProperties$1 !== void 0 && !a.allProps) for (const k of Object.keys(v)) {
@@ -1931,6 +2085,9 @@ function containsMessage(min, max) {
1931
2085
  if (min === 0) return `expected at most ${max} matching ${max === 1 ? "item" : "items"}`;
1932
2086
  return `expected between ${min} and ${max} matching items`;
1933
2087
  }
2088
+ function isBound(value) {
2089
+ return typeof value === "number" || typeof value === "bigint";
2090
+ }
1934
2091
  function pointer(path, key) {
1935
2092
  return `${path}/${String(key).replace(/~/g, "~0").replace(/\//g, "~1")}`;
1936
2093
  }
@@ -1938,7 +2095,9 @@ function typeName(v) {
1938
2095
  if (v === null) return "null";
1939
2096
  if (Array.isArray(v)) return "array";
1940
2097
  if (v instanceof Uint8Array) return "bytes";
2098
+ if (typeof v === "bigint") return "bigint";
1941
2099
  if (typeof v === "number" && Number.isInteger(v)) return "integer";
2100
+ if (typeof v === "number" && !Number.isFinite(v)) return String(v);
1942
2101
  return typeof v;
1943
2102
  }
1944
2103
  function compile(schema, options = NO_OPTIONS) {
@@ -1967,6 +2126,23 @@ var Validator = class {
1967
2126
  this.explainer ??= new Explainer(compiler(this.schema, this.options), this.schema);
1968
2127
  return this.explainer.errors(value);
1969
2128
  }
2129
+ /**
2130
+ * `value` itself when it is valid, else its errors: check() and errors() in one result, without coercion.
2131
+ *
2132
+ * Not TypeBox's `Value.Parse`, which also clones, fills defaults, converts and strips undeclared properties before
2133
+ * it checks. This only checks: the strict replacement where `Value.Parse` is used to validate (its other steps
2134
+ * change nothing for a valid value of a schema without defaults or extra properties). Use `parse()` with
2135
+ * `coerce: true` for conversion.
2136
+ */
2137
+ validate(value) {
2138
+ return this.check(value) ? {
2139
+ ok: true,
2140
+ value
2141
+ } : {
2142
+ ok: false,
2143
+ errors: this.errors(value)
2144
+ };
2145
+ }
1970
2146
  };
1971
2147
  /** A validator compiled with `coerce: true`: it also coerces loosely typed input towards the schema. */
1972
2148
  var CoercingValidator = class extends Validator {
@@ -1983,14 +2159,7 @@ var CoercingValidator = class extends Validator {
1983
2159
  }
1984
2160
  /** Coerces `value`, then checks it: the coerced value when it is valid, else its errors. */
1985
2161
  parse(value) {
1986
- const coerced = this.coerce(value);
1987
- return this.check(coerced) ? {
1988
- ok: true,
1989
- value: coerced
1990
- } : {
1991
- ok: false,
1992
- errors: this.errors(coerced)
1993
- };
2162
+ return this.validate(this.coerce(value));
1994
2163
  }
1995
2164
  };
1996
2165
  function compileCheck(schema, options) {