@orkestrel/contract 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/combinators.d.ts +2 -2
- package/dist/src/core/compilers.d.ts +45 -3
- package/dist/src/core/constants.d.ts +20 -0
- package/dist/src/core/helpers.d.ts +48 -1
- package/dist/src/core/index.cjs +478 -36
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.js +473 -37
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/shapers.d.ts +20 -4
- package/dist/src/core/types.d.ts +96 -6
- package/dist/src/core/validators.d.ts +57 -2
- package/package.json +2 -2
package/dist/src/core/index.cjs
CHANGED
|
@@ -27,6 +27,26 @@ var JSON_SCHEMA_TYPES = Object.freeze([
|
|
|
27
27
|
"integer",
|
|
28
28
|
"string"
|
|
29
29
|
]);
|
|
30
|
+
/**
|
|
31
|
+
* The maximum number of {@link Fault} entries a single `explain` report ever
|
|
32
|
+
* returns, frozen.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* Bounds the report against adversarial input (a giant array, a wide record) —
|
|
36
|
+
* `compileReporter` collects faults in stable pre-order and stops once this cap
|
|
37
|
+
* is reached, so the report size (and the work to build it) stays finite and
|
|
38
|
+
* deterministic regardless of the input's size.
|
|
39
|
+
*/
|
|
40
|
+
var FAULT_LIMIT = 64;
|
|
41
|
+
/**
|
|
42
|
+
* The maximum character length of a {@link preview}-rendered string, frozen.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* A previewed string longer than this is clipped with a trailing `…` so a
|
|
46
|
+
* {@link Fault}'s `received` field never embeds an unbounded amount of
|
|
47
|
+
* untrusted text.
|
|
48
|
+
*/
|
|
49
|
+
var PREVIEW_LIMIT = 64;
|
|
30
50
|
//#endregion
|
|
31
51
|
//#region src/core/validators.ts
|
|
32
52
|
/** Determine whether a value is `null`.
|
|
@@ -63,7 +83,14 @@ function isUndefined(value) {
|
|
|
63
83
|
function isDefined(value) {
|
|
64
84
|
return value !== null && value !== void 0;
|
|
65
85
|
}
|
|
66
|
-
/** Determine whether a value is a string.
|
|
86
|
+
/** Determine whether a value is a string.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* isString('hi') // true
|
|
91
|
+
* isString(42) // false
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
67
94
|
function isString(value) {
|
|
68
95
|
return typeof value === "string";
|
|
69
96
|
}
|
|
@@ -72,11 +99,26 @@ function isString(value) {
|
|
|
72
99
|
*
|
|
73
100
|
* @remarks
|
|
74
101
|
* Includes `NaN` and `±Infinity` — use {@link isFiniteNumber} to exclude them.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* isNumber(42) // true
|
|
106
|
+
* isNumber(Number.NaN) // true — NaN is still a number
|
|
107
|
+
* isNumber('42') // false
|
|
108
|
+
* ```
|
|
75
109
|
*/
|
|
76
110
|
function isNumber(value) {
|
|
77
111
|
return typeof value === "number";
|
|
78
112
|
}
|
|
79
|
-
/** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`).
|
|
113
|
+
/** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`).
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* isFiniteNumber(42) // true
|
|
118
|
+
* isFiniteNumber(Number.NaN) // false
|
|
119
|
+
* isFiniteNumber(Infinity) // false
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
80
122
|
function isFiniteNumber(value) {
|
|
81
123
|
return typeof value === "number" && Number.isFinite(value);
|
|
82
124
|
}
|
|
@@ -193,6 +235,35 @@ function isNullableNumber(value) {
|
|
|
193
235
|
function isNullableBoolean(value) {
|
|
194
236
|
return value === null || isBoolean(value);
|
|
195
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Determine whether a value is an instance of a constructor, contained against
|
|
240
|
+
* a throwing `instanceof` check.
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* The low-level total helper every `instanceof`-based guard in this file (and
|
|
244
|
+
* the `instanceOf` combinator) routes through. A bare `value instanceof X` is
|
|
245
|
+
* NOT total (AGENTS §14): it invokes `getPrototypeOf` on `value` — which a
|
|
246
|
+
* revoked `Proxy` or a `getPrototypeOf`-trap `Proxy` throws from — and, when
|
|
247
|
+
* `X[Symbol.hasInstance]` is user-defined, can throw from arbitrary code. This
|
|
248
|
+
* wraps the check in {@link attempt} (see ./helpers.js) so any such throw
|
|
249
|
+
* yields `false` instead of escaping.
|
|
250
|
+
*
|
|
251
|
+
* @param value - The value to test
|
|
252
|
+
* @param ctor - The constructor to test against
|
|
253
|
+
* @returns `true` when `value instanceof ctor`, `false` on a non-match or a
|
|
254
|
+
* contained throw
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* isInstance(new Date(), Date) // true
|
|
259
|
+
* isInstance({}, Date) // false
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
function isInstance(value, ctor) {
|
|
263
|
+
const target = ctor;
|
|
264
|
+
const outcome = attempt(() => isFunction(target) && value instanceof target);
|
|
265
|
+
return outcome.success && outcome.value;
|
|
266
|
+
}
|
|
196
267
|
/** Determine whether a value is a `Date`.
|
|
197
268
|
*
|
|
198
269
|
* @example
|
|
@@ -202,7 +273,7 @@ function isNullableBoolean(value) {
|
|
|
202
273
|
* ```
|
|
203
274
|
*/
|
|
204
275
|
function isDate(value) {
|
|
205
|
-
return value
|
|
276
|
+
return isInstance(value, Date);
|
|
206
277
|
}
|
|
207
278
|
/** Determine whether a value is a `RegExp`.
|
|
208
279
|
*
|
|
@@ -213,7 +284,7 @@ function isDate(value) {
|
|
|
213
284
|
* ```
|
|
214
285
|
*/
|
|
215
286
|
function isRegExp(value) {
|
|
216
|
-
return value
|
|
287
|
+
return isInstance(value, RegExp);
|
|
217
288
|
}
|
|
218
289
|
/** Determine whether a value is an `Error`.
|
|
219
290
|
*
|
|
@@ -224,7 +295,7 @@ function isRegExp(value) {
|
|
|
224
295
|
* ```
|
|
225
296
|
*/
|
|
226
297
|
function isError(value) {
|
|
227
|
-
return value
|
|
298
|
+
return isInstance(value, Error);
|
|
228
299
|
}
|
|
229
300
|
/** Determine whether a value is a native `Promise` (use {@link isPromiseLike} for any thenable).
|
|
230
301
|
*
|
|
@@ -235,7 +306,7 @@ function isError(value) {
|
|
|
235
306
|
* ```
|
|
236
307
|
*/
|
|
237
308
|
function isPromise(value) {
|
|
238
|
-
return value
|
|
309
|
+
return isInstance(value, Promise);
|
|
239
310
|
}
|
|
240
311
|
/**
|
|
241
312
|
* Determine whether a value is promise-like — an object exposing callable
|
|
@@ -271,7 +342,7 @@ function isPromiseLike(value) {
|
|
|
271
342
|
* ```
|
|
272
343
|
*/
|
|
273
344
|
function isArrayBuffer(value) {
|
|
274
|
-
return value
|
|
345
|
+
return isInstance(value, ArrayBuffer);
|
|
275
346
|
}
|
|
276
347
|
/**
|
|
277
348
|
* Determine whether a value is a `SharedArrayBuffer`.
|
|
@@ -287,7 +358,7 @@ function isArrayBuffer(value) {
|
|
|
287
358
|
* ```
|
|
288
359
|
*/
|
|
289
360
|
function isSharedArrayBuffer(value) {
|
|
290
|
-
return typeof SharedArrayBuffer !== "undefined" && value
|
|
361
|
+
return typeof SharedArrayBuffer !== "undefined" && isInstance(value, SharedArrayBuffer);
|
|
291
362
|
}
|
|
292
363
|
/**
|
|
293
364
|
* Determine whether a value implements the iterable protocol (`Symbol.iterator`).
|
|
@@ -356,6 +427,14 @@ function isObject(value) {
|
|
|
356
427
|
* instance's runs through the class's own prototype. The whole body runs
|
|
357
428
|
* inside `attempt` (AGENTS §14) so a revoked `Proxy` or a hostile
|
|
358
429
|
* `getPrototypeOf` trap cannot escape as a thrown error.
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```ts
|
|
433
|
+
* isRecord({ a: 1 }) // true
|
|
434
|
+
* isRecord(Object.create(null)) // true
|
|
435
|
+
* isRecord([]) // false
|
|
436
|
+
* isRecord(new Date()) // false
|
|
437
|
+
* ```
|
|
359
438
|
*/
|
|
360
439
|
function isRecord(value) {
|
|
361
440
|
const outcome = attempt(() => {
|
|
@@ -374,7 +453,7 @@ function isRecord(value) {
|
|
|
374
453
|
* ```
|
|
375
454
|
*/
|
|
376
455
|
function isMap(value) {
|
|
377
|
-
return value
|
|
456
|
+
return isInstance(value, Map);
|
|
378
457
|
}
|
|
379
458
|
/** Determine whether a value is a `Set`.
|
|
380
459
|
*
|
|
@@ -385,7 +464,7 @@ function isMap(value) {
|
|
|
385
464
|
* ```
|
|
386
465
|
*/
|
|
387
466
|
function isSet(value) {
|
|
388
|
-
return value
|
|
467
|
+
return isInstance(value, Set);
|
|
389
468
|
}
|
|
390
469
|
/** Determine whether a value is a `WeakMap`.
|
|
391
470
|
*
|
|
@@ -396,7 +475,7 @@ function isSet(value) {
|
|
|
396
475
|
* ```
|
|
397
476
|
*/
|
|
398
477
|
function isWeakMap(value) {
|
|
399
|
-
return value
|
|
478
|
+
return isInstance(value, WeakMap);
|
|
400
479
|
}
|
|
401
480
|
/** Determine whether a value is a `WeakSet`.
|
|
402
481
|
*
|
|
@@ -407,7 +486,7 @@ function isWeakMap(value) {
|
|
|
407
486
|
* ```
|
|
408
487
|
*/
|
|
409
488
|
function isWeakSet(value) {
|
|
410
|
-
return value
|
|
489
|
+
return isInstance(value, WeakSet);
|
|
411
490
|
}
|
|
412
491
|
/** Determine whether a value is an array.
|
|
413
492
|
*
|
|
@@ -429,7 +508,7 @@ function isArray(value) {
|
|
|
429
508
|
* ```
|
|
430
509
|
*/
|
|
431
510
|
function isDataView(value) {
|
|
432
|
-
return value
|
|
511
|
+
return isInstance(value, DataView);
|
|
433
512
|
}
|
|
434
513
|
/** Determine whether a value is an `ArrayBufferView` (any typed array or `DataView`).
|
|
435
514
|
*
|
|
@@ -451,7 +530,7 @@ function isArrayBufferView(value) {
|
|
|
451
530
|
* ```
|
|
452
531
|
*/
|
|
453
532
|
function isInt8Array(value) {
|
|
454
|
-
return value
|
|
533
|
+
return isInstance(value, Int8Array);
|
|
455
534
|
}
|
|
456
535
|
/** Determine whether a value is a `Uint8Array`.
|
|
457
536
|
*
|
|
@@ -462,7 +541,7 @@ function isInt8Array(value) {
|
|
|
462
541
|
* ```
|
|
463
542
|
*/
|
|
464
543
|
function isUint8Array(value) {
|
|
465
|
-
return value
|
|
544
|
+
return isInstance(value, Uint8Array);
|
|
466
545
|
}
|
|
467
546
|
/** Determine whether a value is a `Uint8ClampedArray`.
|
|
468
547
|
*
|
|
@@ -473,7 +552,7 @@ function isUint8Array(value) {
|
|
|
473
552
|
* ```
|
|
474
553
|
*/
|
|
475
554
|
function isUint8ClampedArray(value) {
|
|
476
|
-
return value
|
|
555
|
+
return isInstance(value, Uint8ClampedArray);
|
|
477
556
|
}
|
|
478
557
|
/** Determine whether a value is an `Int16Array`.
|
|
479
558
|
*
|
|
@@ -484,7 +563,7 @@ function isUint8ClampedArray(value) {
|
|
|
484
563
|
* ```
|
|
485
564
|
*/
|
|
486
565
|
function isInt16Array(value) {
|
|
487
|
-
return value
|
|
566
|
+
return isInstance(value, Int16Array);
|
|
488
567
|
}
|
|
489
568
|
/** Determine whether a value is a `Uint16Array`.
|
|
490
569
|
*
|
|
@@ -495,7 +574,7 @@ function isInt16Array(value) {
|
|
|
495
574
|
* ```
|
|
496
575
|
*/
|
|
497
576
|
function isUint16Array(value) {
|
|
498
|
-
return value
|
|
577
|
+
return isInstance(value, Uint16Array);
|
|
499
578
|
}
|
|
500
579
|
/** Determine whether a value is an `Int32Array`.
|
|
501
580
|
*
|
|
@@ -506,7 +585,7 @@ function isUint16Array(value) {
|
|
|
506
585
|
* ```
|
|
507
586
|
*/
|
|
508
587
|
function isInt32Array(value) {
|
|
509
|
-
return value
|
|
588
|
+
return isInstance(value, Int32Array);
|
|
510
589
|
}
|
|
511
590
|
/** Determine whether a value is a `Uint32Array`.
|
|
512
591
|
*
|
|
@@ -517,7 +596,7 @@ function isInt32Array(value) {
|
|
|
517
596
|
* ```
|
|
518
597
|
*/
|
|
519
598
|
function isUint32Array(value) {
|
|
520
|
-
return value
|
|
599
|
+
return isInstance(value, Uint32Array);
|
|
521
600
|
}
|
|
522
601
|
/** Determine whether a value is a `Float32Array`.
|
|
523
602
|
*
|
|
@@ -528,7 +607,7 @@ function isUint32Array(value) {
|
|
|
528
607
|
* ```
|
|
529
608
|
*/
|
|
530
609
|
function isFloat32Array(value) {
|
|
531
|
-
return value
|
|
610
|
+
return isInstance(value, Float32Array);
|
|
532
611
|
}
|
|
533
612
|
/** Determine whether a value is a `Float64Array`.
|
|
534
613
|
*
|
|
@@ -539,7 +618,7 @@ function isFloat32Array(value) {
|
|
|
539
618
|
* ```
|
|
540
619
|
*/
|
|
541
620
|
function isFloat64Array(value) {
|
|
542
|
-
return value
|
|
621
|
+
return isInstance(value, Float64Array);
|
|
543
622
|
}
|
|
544
623
|
/**
|
|
545
624
|
* Determine whether a value is a `BigInt64Array`.
|
|
@@ -555,7 +634,7 @@ function isFloat64Array(value) {
|
|
|
555
634
|
* ```
|
|
556
635
|
*/
|
|
557
636
|
function isBigInt64Array(value) {
|
|
558
|
-
return typeof BigInt64Array !== "undefined" && value
|
|
637
|
+
return typeof BigInt64Array !== "undefined" && isInstance(value, BigInt64Array);
|
|
559
638
|
}
|
|
560
639
|
/**
|
|
561
640
|
* Determine whether a value is a `BigUint64Array`.
|
|
@@ -571,7 +650,7 @@ function isBigInt64Array(value) {
|
|
|
571
650
|
* ```
|
|
572
651
|
*/
|
|
573
652
|
function isBigUint64Array(value) {
|
|
574
|
-
return typeof BigUint64Array !== "undefined" && value
|
|
653
|
+
return typeof BigUint64Array !== "undefined" && isInstance(value, BigUint64Array);
|
|
575
654
|
}
|
|
576
655
|
/** Determine whether a value is the empty string `''`.
|
|
577
656
|
*
|
|
@@ -605,7 +684,8 @@ function isEmptyArray(value) {
|
|
|
605
684
|
*/
|
|
606
685
|
function isEmptyObject(value) {
|
|
607
686
|
if (!isRecord(value)) return false;
|
|
608
|
-
|
|
687
|
+
const outcome = attempt(() => Object.keys(value).length === 0 && enumerableSymbolCount(value) === 0);
|
|
688
|
+
return outcome.success && outcome.value;
|
|
609
689
|
}
|
|
610
690
|
/** Determine whether a value is an empty `Map`.
|
|
611
691
|
*
|
|
@@ -616,7 +696,7 @@ function isEmptyObject(value) {
|
|
|
616
696
|
* ```
|
|
617
697
|
*/
|
|
618
698
|
function isEmptyMap(value) {
|
|
619
|
-
return value
|
|
699
|
+
return isMap(value) && value.size === 0;
|
|
620
700
|
}
|
|
621
701
|
/** Determine whether a value is an empty `Set`.
|
|
622
702
|
*
|
|
@@ -627,7 +707,7 @@ function isEmptyMap(value) {
|
|
|
627
707
|
* ```
|
|
628
708
|
*/
|
|
629
709
|
function isEmptySet(value) {
|
|
630
|
-
return value
|
|
710
|
+
return isSet(value) && value.size === 0;
|
|
631
711
|
}
|
|
632
712
|
/** Determine whether a value is a non-empty string (at least one character).
|
|
633
713
|
*
|
|
@@ -661,7 +741,8 @@ function isNonEmptyArray(value) {
|
|
|
661
741
|
*/
|
|
662
742
|
function isNonEmptyObject(value) {
|
|
663
743
|
if (!isRecord(value)) return false;
|
|
664
|
-
|
|
744
|
+
const outcome = attempt(() => Object.keys(value).length > 0 || enumerableSymbolCount(value) > 0);
|
|
745
|
+
return outcome.success && outcome.value;
|
|
665
746
|
}
|
|
666
747
|
/** Determine whether a value is a non-empty `Map` (at least one entry).
|
|
667
748
|
*
|
|
@@ -672,7 +753,7 @@ function isNonEmptyObject(value) {
|
|
|
672
753
|
* ```
|
|
673
754
|
*/
|
|
674
755
|
function isNonEmptyMap(value) {
|
|
675
|
-
return value
|
|
756
|
+
return isMap(value) && value.size > 0;
|
|
676
757
|
}
|
|
677
758
|
/** Determine whether a value is a non-empty `Set` (at least one element).
|
|
678
759
|
*
|
|
@@ -683,7 +764,7 @@ function isNonEmptyMap(value) {
|
|
|
683
764
|
* ```
|
|
684
765
|
*/
|
|
685
766
|
function isNonEmptySet(value) {
|
|
686
|
-
return value
|
|
767
|
+
return isSet(value) && value.size > 0;
|
|
687
768
|
}
|
|
688
769
|
/** Determine whether a value is a function that declares zero parameters (`Function.length === 0`).
|
|
689
770
|
*
|
|
@@ -1019,6 +1100,79 @@ function enumerableSymbolCount(value) {
|
|
|
1019
1100
|
function schemaToParameters(schema) {
|
|
1020
1101
|
return isRecord(schema) ? schema : void 0;
|
|
1021
1102
|
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Render a short, safe, TOTAL preview of an unknown value for a {@link Fault}'s
|
|
1105
|
+
* `received` field.
|
|
1106
|
+
*
|
|
1107
|
+
* @remarks
|
|
1108
|
+
* A primitive renders as its literal: a string is `JSON.stringify`-escaped and
|
|
1109
|
+
* clipped to {@link PREVIEW_LIMIT} characters (with a trailing `…` when
|
|
1110
|
+
* clipped); a number / boolean / bigint / symbol renders via `String`; `null`
|
|
1111
|
+
* and `undefined` render as their own name. Everything else — a plain object,
|
|
1112
|
+
* an array, a function, a class instance, a `Map` — is NEVER traversed or
|
|
1113
|
+
* stringified; it renders as its bare `typeof` tag (`'object'` / `'function'`),
|
|
1114
|
+
* so a hostile or enormous structure can never blow up the preview.
|
|
1115
|
+
*
|
|
1116
|
+
* @param value - The value to preview
|
|
1117
|
+
* @returns A short descriptive string, always safe to embed in a diagnostic
|
|
1118
|
+
*
|
|
1119
|
+
* @example
|
|
1120
|
+
* ```ts
|
|
1121
|
+
* preview('hi') // '"hi"'
|
|
1122
|
+
* preview(42) // '42'
|
|
1123
|
+
* preview(null) // 'null'
|
|
1124
|
+
* preview({ a: 1 }) // 'object'
|
|
1125
|
+
* preview([1, 2, 3]) // 'object'
|
|
1126
|
+
* ```
|
|
1127
|
+
*/
|
|
1128
|
+
function preview(value) {
|
|
1129
|
+
if (value === null) return "null";
|
|
1130
|
+
if (value === void 0) return "undefined";
|
|
1131
|
+
if (isString(value)) {
|
|
1132
|
+
const text = JSON.stringify(value);
|
|
1133
|
+
return text.length > 64 ? `${text.slice(0, 64)}…` : text;
|
|
1134
|
+
}
|
|
1135
|
+
if (isNumber(value) || isBoolean(value)) return String(value);
|
|
1136
|
+
if (isBigInt(value)) return `${value}n`;
|
|
1137
|
+
if (isSymbol(value)) return value.toString();
|
|
1138
|
+
return typeof value;
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Project a {@link ContractShape} to the {@link FaultKind} it describes.
|
|
1142
|
+
*
|
|
1143
|
+
* @remarks
|
|
1144
|
+
* A structural mapping used by {@link compileReporter} to fill a `Fault`'s
|
|
1145
|
+
* `expected` field: most shapes map to their own `type` (`numberShape` maps to
|
|
1146
|
+
* `'integer'` when `integer: true`, else `'number'`); `optionalShape` /
|
|
1147
|
+
* `nullableShape` project through to their inner shape's kind, and `rawShape`
|
|
1148
|
+
* (an arbitrary embedded schema with no fixed kind) projects to `'json'`.
|
|
1149
|
+
*
|
|
1150
|
+
* @param shape - The shape to project
|
|
1151
|
+
* @returns The shape's {@link FaultKind}
|
|
1152
|
+
*
|
|
1153
|
+
* @example
|
|
1154
|
+
* ```ts
|
|
1155
|
+
* shapeToKind(stringShape()) // 'string'
|
|
1156
|
+
* shapeToKind(integerShape()) // 'integer'
|
|
1157
|
+
* shapeToKind(optionalShape(nullShape())) // 'null'
|
|
1158
|
+
* ```
|
|
1159
|
+
*/
|
|
1160
|
+
function shapeToKind(shape) {
|
|
1161
|
+
switch (shape.type) {
|
|
1162
|
+
case "string": return "string";
|
|
1163
|
+
case "number": return shape.integer === true ? "integer" : "number";
|
|
1164
|
+
case "boolean": return "boolean";
|
|
1165
|
+
case "null": return "null";
|
|
1166
|
+
case "literal": return "literal";
|
|
1167
|
+
case "array": return "array";
|
|
1168
|
+
case "object": return "object";
|
|
1169
|
+
case "union": return "union";
|
|
1170
|
+
case "json": return "json";
|
|
1171
|
+
case "optional": return shapeToKind(shape.inner);
|
|
1172
|
+
case "nullable": return shapeToKind(shape.inner);
|
|
1173
|
+
case "raw": return "json";
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1022
1176
|
//#endregion
|
|
1023
1177
|
//#region src/core/combinators.ts
|
|
1024
1178
|
function arrayOf(elementGuard) {
|
|
@@ -1071,7 +1225,7 @@ function literalOf(...literals) {
|
|
|
1071
1225
|
* ```
|
|
1072
1226
|
*/
|
|
1073
1227
|
function instanceOf(ctor) {
|
|
1074
|
-
return (value) => isConstructor(ctor) && isObject(value) && value
|
|
1228
|
+
return (value) => isConstructor(ctor) && isObject(value) && isInstance(value, ctor);
|
|
1075
1229
|
}
|
|
1076
1230
|
/**
|
|
1077
1231
|
* Build a guard from a native `enum` or any object whose values are strings or
|
|
@@ -1986,7 +2140,11 @@ function compileGuard(shape) {
|
|
|
1986
2140
|
return outcome.success && outcome.value;
|
|
1987
2141
|
};
|
|
1988
2142
|
}
|
|
1989
|
-
case "union":
|
|
2143
|
+
case "union": {
|
|
2144
|
+
const guards = shape.variants.map((variant) => compileGuard(variant));
|
|
2145
|
+
if (shape.mode === "oneOf") return (value) => guards.filter((guard) => guard(value)).length === 1;
|
|
2146
|
+
return unionOf(...guards);
|
|
2147
|
+
}
|
|
1990
2148
|
case "optional": return orOf(isUndefined, compileGuard(shape.inner));
|
|
1991
2149
|
case "nullable": return nullableOf(compileGuard(shape.inner));
|
|
1992
2150
|
case "raw": return (_value) => true;
|
|
@@ -2093,6 +2251,11 @@ function compileParser(shape) {
|
|
|
2093
2251
|
parse: compileParser(variant),
|
|
2094
2252
|
guard: compileGuard(variant)
|
|
2095
2253
|
}));
|
|
2254
|
+
if (shape.mode === "oneOf") return (value) => {
|
|
2255
|
+
const matches = variants.filter((variant) => variant.guard(value));
|
|
2256
|
+
const [only] = matches;
|
|
2257
|
+
return matches.length === 1 && only !== void 0 ? only.parse(value) : void 0;
|
|
2258
|
+
};
|
|
2096
2259
|
return (value) => {
|
|
2097
2260
|
for (const variant of variants) if (variant.guard(value)) return value;
|
|
2098
2261
|
for (const variant of variants) {
|
|
@@ -2187,6 +2350,260 @@ function compileGenerator(shape, random = seededRandom(Date.now())) {
|
|
|
2187
2350
|
case "raw": throw new Error("compileGenerator: a raw shape embeds an arbitrary JSON Schema and cannot be auto-generated — supply values another way");
|
|
2188
2351
|
}
|
|
2189
2352
|
}
|
|
2353
|
+
/**
|
|
2354
|
+
* Compile a {@link ContractShape} into a structured fault report for a value —
|
|
2355
|
+
* the diagnostic counterpart of {@link compileGuard} / {@link compileParser}.
|
|
2356
|
+
*
|
|
2357
|
+
* @remarks
|
|
2358
|
+
* MIRROR-PARSE semantics: reuses the exact leaf parsers/guards
|
|
2359
|
+
* {@link compileParser} uses (`parseString` / `parseNumber` / `parseBoolean` /
|
|
2360
|
+
* `isJSONValue` / …), so the soundness invariant
|
|
2361
|
+
* `compileReporter(shape, v).length === 0 ⟺ compileParser(shape)(v) !== undefined`
|
|
2362
|
+
* holds structurally — `explain` mirrors `parse`, not the stricter `is` (a
|
|
2363
|
+
* coercible value like `'42'` against a `numberShape` reports no fault, the
|
|
2364
|
+
* same leniency `parse` grants, even though the strict guard would reject it).
|
|
2365
|
+
*
|
|
2366
|
+
* Faults are collected in stable pre-order (declared key/index order); every
|
|
2367
|
+
* call — object, array, and union alike, including a union's `oneOf` "no
|
|
2368
|
+
* match" / "no consensus" summary fault prepended to its closest variant's
|
|
2369
|
+
* faults — slices its return to at most {@link FAULT_LIMIT} entries, so the
|
|
2370
|
+
* bound holds at every level of nesting, not just the outermost container, on
|
|
2371
|
+
* adversarial input (a huge array, a wide record, a wide union of wide
|
|
2372
|
+
* records). A closed object's extra keys never
|
|
2373
|
+
* fault — `parse` silently drops them, so `explain` mirrors that leniency; an
|
|
2374
|
+
* open object with a constraining `additionalProperties` shape recurses extras
|
|
2375
|
+
* against it instead. A hostile getter or throwing `Proxy` trap is contained
|
|
2376
|
+
* via {@link attempt} and surfaces as a single top-level type fault, never a
|
|
2377
|
+
* throw (AGENTS §14).
|
|
2378
|
+
*
|
|
2379
|
+
* @param shape - The shape to report against
|
|
2380
|
+
* @param value - The value to check
|
|
2381
|
+
* @param path - The path prefix for faults produced at this call (defaults to the root)
|
|
2382
|
+
* @returns The faults found, empty when the value parses successfully
|
|
2383
|
+
*
|
|
2384
|
+
* @example
|
|
2385
|
+
* ```ts
|
|
2386
|
+
* const user = objectShape({ name: stringShape({ min: 1 }) })
|
|
2387
|
+
* compileReporter(user, { name: '' })
|
|
2388
|
+
* // [{ reason: 'constraint', path: ['name'], expected: 'string', constraint: 'min', limit: 1, received: '""' }]
|
|
2389
|
+
* compileReporter(user, { name: 'Ada' }) // []
|
|
2390
|
+
* ```
|
|
2391
|
+
*/
|
|
2392
|
+
function compileReporter(shape, value, path = []) {
|
|
2393
|
+
switch (shape.type) {
|
|
2394
|
+
case "string": {
|
|
2395
|
+
const parsed = parseString(value);
|
|
2396
|
+
if (parsed === void 0) return [{
|
|
2397
|
+
reason: "type",
|
|
2398
|
+
path,
|
|
2399
|
+
expected: "string",
|
|
2400
|
+
received: preview(value)
|
|
2401
|
+
}];
|
|
2402
|
+
const faults = [];
|
|
2403
|
+
if (shape.min !== void 0 && parsed.length < shape.min) faults.push({
|
|
2404
|
+
reason: "constraint",
|
|
2405
|
+
path,
|
|
2406
|
+
expected: "string",
|
|
2407
|
+
constraint: "min",
|
|
2408
|
+
limit: shape.min,
|
|
2409
|
+
received: preview(parsed)
|
|
2410
|
+
});
|
|
2411
|
+
if (shape.max !== void 0 && parsed.length > shape.max) faults.push({
|
|
2412
|
+
reason: "constraint",
|
|
2413
|
+
path,
|
|
2414
|
+
expected: "string",
|
|
2415
|
+
constraint: "max",
|
|
2416
|
+
limit: shape.max,
|
|
2417
|
+
received: preview(parsed)
|
|
2418
|
+
});
|
|
2419
|
+
if (shape.pattern !== void 0 && !shape.pattern.test(parsed)) faults.push({
|
|
2420
|
+
reason: "constraint",
|
|
2421
|
+
path,
|
|
2422
|
+
expected: "string",
|
|
2423
|
+
constraint: "pattern",
|
|
2424
|
+
limit: shape.pattern.source,
|
|
2425
|
+
received: preview(parsed)
|
|
2426
|
+
});
|
|
2427
|
+
return faults;
|
|
2428
|
+
}
|
|
2429
|
+
case "number": {
|
|
2430
|
+
const kind = shape.integer === true ? "integer" : "number";
|
|
2431
|
+
const parsed = parseNumber(value);
|
|
2432
|
+
if (parsed === void 0) return [{
|
|
2433
|
+
reason: "type",
|
|
2434
|
+
path,
|
|
2435
|
+
expected: kind,
|
|
2436
|
+
received: preview(value)
|
|
2437
|
+
}];
|
|
2438
|
+
const faults = [];
|
|
2439
|
+
if (shape.integer === true && !Number.isInteger(parsed)) faults.push({
|
|
2440
|
+
reason: "constraint",
|
|
2441
|
+
path,
|
|
2442
|
+
expected: kind,
|
|
2443
|
+
constraint: "integer",
|
|
2444
|
+
received: preview(parsed)
|
|
2445
|
+
});
|
|
2446
|
+
if (shape.min !== void 0 && parsed < shape.min) faults.push({
|
|
2447
|
+
reason: "constraint",
|
|
2448
|
+
path,
|
|
2449
|
+
expected: kind,
|
|
2450
|
+
constraint: "min",
|
|
2451
|
+
limit: shape.min,
|
|
2452
|
+
received: preview(parsed)
|
|
2453
|
+
});
|
|
2454
|
+
if (shape.max !== void 0 && parsed > shape.max) faults.push({
|
|
2455
|
+
reason: "constraint",
|
|
2456
|
+
path,
|
|
2457
|
+
expected: kind,
|
|
2458
|
+
constraint: "max",
|
|
2459
|
+
limit: shape.max,
|
|
2460
|
+
received: preview(parsed)
|
|
2461
|
+
});
|
|
2462
|
+
return faults;
|
|
2463
|
+
}
|
|
2464
|
+
case "boolean": return parseBoolean(value) === void 0 ? [{
|
|
2465
|
+
reason: "type",
|
|
2466
|
+
path,
|
|
2467
|
+
expected: "boolean",
|
|
2468
|
+
received: preview(value)
|
|
2469
|
+
}] : [];
|
|
2470
|
+
case "null": return value === null ? [] : [{
|
|
2471
|
+
reason: "type",
|
|
2472
|
+
path,
|
|
2473
|
+
expected: "null",
|
|
2474
|
+
received: preview(value)
|
|
2475
|
+
}];
|
|
2476
|
+
case "json": return isJSONValue(value) ? [] : [{
|
|
2477
|
+
reason: "type",
|
|
2478
|
+
path,
|
|
2479
|
+
expected: "json",
|
|
2480
|
+
received: preview(value)
|
|
2481
|
+
}];
|
|
2482
|
+
case "literal": {
|
|
2483
|
+
const allowed = new Set(shape.values);
|
|
2484
|
+
return allowed.has(value) || isString(value) && allowed.has(value.trim()) ? [] : [{
|
|
2485
|
+
reason: "type",
|
|
2486
|
+
path,
|
|
2487
|
+
expected: "literal",
|
|
2488
|
+
received: preview(value)
|
|
2489
|
+
}];
|
|
2490
|
+
}
|
|
2491
|
+
case "array": {
|
|
2492
|
+
if (!isArray(value)) return [{
|
|
2493
|
+
reason: "type",
|
|
2494
|
+
path,
|
|
2495
|
+
expected: "array",
|
|
2496
|
+
received: preview(value)
|
|
2497
|
+
}];
|
|
2498
|
+
const faults = [];
|
|
2499
|
+
if (!attempt(() => {
|
|
2500
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
2501
|
+
if (faults.length >= 64) return;
|
|
2502
|
+
faults.push(...compileReporter(shape.items, value[index], [...path, String(index)]));
|
|
2503
|
+
}
|
|
2504
|
+
}).success) return [{
|
|
2505
|
+
reason: "type",
|
|
2506
|
+
path,
|
|
2507
|
+
expected: "array",
|
|
2508
|
+
received: preview(value)
|
|
2509
|
+
}];
|
|
2510
|
+
if (shape.min !== void 0 && value.length < shape.min) faults.push({
|
|
2511
|
+
reason: "constraint",
|
|
2512
|
+
path,
|
|
2513
|
+
expected: "array",
|
|
2514
|
+
constraint: "min",
|
|
2515
|
+
limit: shape.min,
|
|
2516
|
+
received: String(value.length)
|
|
2517
|
+
});
|
|
2518
|
+
if (shape.max !== void 0 && value.length > shape.max) faults.push({
|
|
2519
|
+
reason: "constraint",
|
|
2520
|
+
path,
|
|
2521
|
+
expected: "array",
|
|
2522
|
+
constraint: "max",
|
|
2523
|
+
limit: shape.max,
|
|
2524
|
+
received: String(value.length)
|
|
2525
|
+
});
|
|
2526
|
+
return faults.length > 64 ? faults.slice(0, 64) : faults;
|
|
2527
|
+
}
|
|
2528
|
+
case "object": {
|
|
2529
|
+
if (!isRecord(value)) return [{
|
|
2530
|
+
reason: "type",
|
|
2531
|
+
path,
|
|
2532
|
+
expected: "object",
|
|
2533
|
+
received: preview(value)
|
|
2534
|
+
}];
|
|
2535
|
+
const record = value;
|
|
2536
|
+
const faults = [];
|
|
2537
|
+
const known = /* @__PURE__ */ new Set();
|
|
2538
|
+
if (!attempt(() => {
|
|
2539
|
+
for (const key of Object.keys(shape.properties)) {
|
|
2540
|
+
if (faults.length >= 64) return;
|
|
2541
|
+
const child = shape.properties[key];
|
|
2542
|
+
if (child === void 0) continue;
|
|
2543
|
+
known.add(key);
|
|
2544
|
+
const optional = child.type === "optional";
|
|
2545
|
+
const inner = optional ? child.inner : child;
|
|
2546
|
+
const raw = record[key];
|
|
2547
|
+
if (raw === void 0) {
|
|
2548
|
+
if (!optional) faults.push({
|
|
2549
|
+
reason: "missing",
|
|
2550
|
+
path: [...path, key],
|
|
2551
|
+
expected: shapeToKind(inner)
|
|
2552
|
+
});
|
|
2553
|
+
continue;
|
|
2554
|
+
}
|
|
2555
|
+
faults.push(...compileReporter(inner, raw, [...path, key]));
|
|
2556
|
+
}
|
|
2557
|
+
const extra = shape.additionalProperties;
|
|
2558
|
+
if (extra !== void 0 && extra !== true && extra !== false) for (const key of Object.keys(record)) {
|
|
2559
|
+
if (faults.length >= 64) return;
|
|
2560
|
+
if (known.has(key)) continue;
|
|
2561
|
+
faults.push(...compileReporter(extra, record[key], [...path, key]));
|
|
2562
|
+
}
|
|
2563
|
+
}).success) return [{
|
|
2564
|
+
reason: "type",
|
|
2565
|
+
path,
|
|
2566
|
+
expected: "object",
|
|
2567
|
+
received: preview(value)
|
|
2568
|
+
}];
|
|
2569
|
+
return faults.length > 64 ? faults.slice(0, 64) : faults;
|
|
2570
|
+
}
|
|
2571
|
+
case "union": {
|
|
2572
|
+
const perVariant = shape.variants.map((variant) => compileReporter(variant, value, path));
|
|
2573
|
+
let bestIndex = 0;
|
|
2574
|
+
for (let index = 1; index < perVariant.length; index += 1) {
|
|
2575
|
+
const current = perVariant[index];
|
|
2576
|
+
const best = perVariant[bestIndex];
|
|
2577
|
+
if (current !== void 0 && best !== void 0 && current.length < best.length) bestIndex = index;
|
|
2578
|
+
}
|
|
2579
|
+
const closest = perVariant[bestIndex] ?? [];
|
|
2580
|
+
if (shape.mode === "oneOf") {
|
|
2581
|
+
let matched = 0;
|
|
2582
|
+
for (const variant of shape.variants) if (compileGuard(variant)(value)) matched += 1;
|
|
2583
|
+
if (matched === 1) return [];
|
|
2584
|
+
if (matched === 0) return [{
|
|
2585
|
+
reason: "oneOf",
|
|
2586
|
+
path,
|
|
2587
|
+
matched: 0
|
|
2588
|
+
}, ...closest].slice(0, 64);
|
|
2589
|
+
return [{
|
|
2590
|
+
reason: "oneOf",
|
|
2591
|
+
path,
|
|
2592
|
+
matched
|
|
2593
|
+
}];
|
|
2594
|
+
}
|
|
2595
|
+
if (perVariant.some((variantFaults) => variantFaults.length === 0)) return [];
|
|
2596
|
+
return [{
|
|
2597
|
+
reason: "variant",
|
|
2598
|
+
path,
|
|
2599
|
+
variants: shape.variants.length
|
|
2600
|
+
}, ...closest].slice(0, 64);
|
|
2601
|
+
}
|
|
2602
|
+
case "optional": return value === void 0 ? [] : compileReporter(shape.inner, value, path);
|
|
2603
|
+
case "nullable": return value === null ? [] : compileReporter(shape.inner, value, path);
|
|
2604
|
+
case "raw": return [];
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2190
2607
|
function createContract(shape) {
|
|
2191
2608
|
validateShape(shape);
|
|
2192
2609
|
const schema = compileSchema(shape);
|
|
@@ -2198,6 +2615,9 @@ function createContract(shape) {
|
|
|
2198
2615
|
parse(value) {
|
|
2199
2616
|
return parser(value);
|
|
2200
2617
|
},
|
|
2618
|
+
explain(value) {
|
|
2619
|
+
return compileReporter(shape, value);
|
|
2620
|
+
},
|
|
2201
2621
|
generate(random) {
|
|
2202
2622
|
return compileGenerator(shape, random);
|
|
2203
2623
|
}
|
|
@@ -2416,16 +2836,32 @@ function unionShape(...variants) {
|
|
|
2416
2836
|
* Build a {@link UnionShape} that emits `oneOf` (exactly one match) in JSON Schema.
|
|
2417
2837
|
*
|
|
2418
2838
|
* @remarks
|
|
2419
|
-
*
|
|
2420
|
-
*
|
|
2839
|
+
* Unlike {@link unionShape} (`anyOf` — at least one variant matches),
|
|
2840
|
+
* `oneOfShape`'s compiled guard and parser enforce EXACTLY one match:
|
|
2841
|
+
*
|
|
2842
|
+
* - **Guard**: accepts the value only when exactly one variant's guard
|
|
2843
|
+
* accepts it. A value matching two-or-more variants — which would violate
|
|
2844
|
+
* the emitted `oneOf` schema — is rejected, even though it would pass
|
|
2845
|
+
* {@link unionShape}'s guard.
|
|
2846
|
+
* - **Parser**: judged on the RAW input's guard matches only, with NO
|
|
2847
|
+
* coercion fallback for an ambiguous input. When exactly one variant's
|
|
2848
|
+
* guard accepts the raw value, that variant's parser runs. Zero matches or
|
|
2849
|
+
* two-or-more matches both parse to `undefined` — a value ambiguous
|
|
2850
|
+
* between variants has no well-defined coercion target.
|
|
2851
|
+
*
|
|
2852
|
+
* Prefer {@link unionShape} when a value may legitimately satisfy more than
|
|
2853
|
+
* one variant (e.g. overlapping shapes) and any match is acceptable. Prefer
|
|
2854
|
+
* `oneOfShape` when overlap between variants indicates malformed input that
|
|
2855
|
+
* must be rejected.
|
|
2421
2856
|
*
|
|
2422
2857
|
* @param variants - The candidate shapes
|
|
2423
2858
|
* @returns A union shape with `mode: 'oneOf'`
|
|
2424
2859
|
*
|
|
2425
2860
|
* @example
|
|
2426
2861
|
* ```ts
|
|
2427
|
-
* const id = oneOfShape(
|
|
2428
|
-
* //
|
|
2862
|
+
* const id = oneOfShape(numberShape(), integerShape())
|
|
2863
|
+
* // 3 fails — matches both numberShape and integerShape
|
|
2864
|
+
* // 3.5 passes — matches numberShape only
|
|
2429
2865
|
* ```
|
|
2430
2866
|
*/
|
|
2431
2867
|
function oneOfShape(...variants) {
|
|
@@ -2513,7 +2949,9 @@ function rawShape(schema) {
|
|
|
2513
2949
|
};
|
|
2514
2950
|
}
|
|
2515
2951
|
//#endregion
|
|
2952
|
+
exports.FAULT_LIMIT = FAULT_LIMIT;
|
|
2516
2953
|
exports.JSON_SCHEMA_TYPES = JSON_SCHEMA_TYPES;
|
|
2954
|
+
exports.PREVIEW_LIMIT = PREVIEW_LIMIT;
|
|
2517
2955
|
exports.andOf = andOf;
|
|
2518
2956
|
exports.arrayOf = arrayOf;
|
|
2519
2957
|
exports.arrayShape = arrayShape;
|
|
@@ -2523,6 +2961,7 @@ exports.boundsOf = boundsOf;
|
|
|
2523
2961
|
exports.compileGenerator = compileGenerator;
|
|
2524
2962
|
exports.compileGuard = compileGuard;
|
|
2525
2963
|
exports.compileParser = compileParser;
|
|
2964
|
+
exports.compileReporter = compileReporter;
|
|
2526
2965
|
exports.compileSchema = compileSchema;
|
|
2527
2966
|
exports.complementOf = complementOf;
|
|
2528
2967
|
exports.createContract = createContract;
|
|
@@ -2557,6 +2996,7 @@ exports.isFloat32Array = isFloat32Array;
|
|
|
2557
2996
|
exports.isFloat64Array = isFloat64Array;
|
|
2558
2997
|
exports.isFunction = isFunction;
|
|
2559
2998
|
exports.isGeneratorFunction = isGeneratorFunction;
|
|
2999
|
+
exports.isInstance = isInstance;
|
|
2560
3000
|
exports.isInt16Array = isInt16Array;
|
|
2561
3001
|
exports.isInt32Array = isInt32Array;
|
|
2562
3002
|
exports.isInt8Array = isInt8Array;
|
|
@@ -2636,6 +3076,7 @@ exports.parseRecordField = parseRecordField;
|
|
|
2636
3076
|
exports.parseString = parseString;
|
|
2637
3077
|
exports.parseStringField = parseStringField;
|
|
2638
3078
|
exports.pickOf = pickOf;
|
|
3079
|
+
exports.preview = preview;
|
|
2639
3080
|
exports.rawShape = rawShape;
|
|
2640
3081
|
exports.recordOf = recordOf;
|
|
2641
3082
|
exports.recordShape = recordShape;
|
|
@@ -2643,6 +3084,7 @@ exports.resolveField = resolveField;
|
|
|
2643
3084
|
exports.schemaToParameters = schemaToParameters;
|
|
2644
3085
|
exports.seededRandom = seededRandom;
|
|
2645
3086
|
exports.setOf = setOf;
|
|
3087
|
+
exports.shapeToKind = shapeToKind;
|
|
2646
3088
|
exports.stringOf = stringOf;
|
|
2647
3089
|
exports.stringShape = stringShape;
|
|
2648
3090
|
exports.transformOf = transformOf;
|