@orkestrel/contract 0.0.2 → 0.0.4
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 +3 -0
- package/dist/src/core/index.cjs +123 -103
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.js +123 -104
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/shapers.d.ts +22 -6
- package/dist/src/core/types.d.ts +81 -11
- package/dist/src/core/validators.d.ts +57 -2
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FromGuards, Guard, GuardsShape, GuardType, IntersectionFromGuards, OptionalFromGuards, TupleFromGuards } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Build a guard that accepts arrays whose every element satisfies `elementGuard`.
|
|
4
4
|
*
|
|
@@ -50,7 +50,7 @@ export declare function literalOf<const Literals extends ReadonlyArray<string |
|
|
|
50
50
|
* isDateValue({}) // false
|
|
51
51
|
* ```
|
|
52
52
|
*/
|
|
53
|
-
export declare function instanceOf<C>(ctor: C): Guard<InstanceType<C
|
|
53
|
+
export declare function instanceOf<C extends abstract new (...args: never) => object>(ctor: C): Guard<InstanceType<C>>;
|
|
54
54
|
/**
|
|
55
55
|
* Build a guard from a native `enum` or any object whose values are strings or
|
|
56
56
|
* numbers.
|
|
@@ -70,6 +70,7 @@ export declare function compileSchema(shape: ContractShape): JSONSchema;
|
|
|
70
70
|
* isUser({ name: 'Ada' }) // true
|
|
71
71
|
* ```
|
|
72
72
|
*/
|
|
73
|
+
export declare function compileGuard<S extends ContractShape>(shape: S): Guard<Infer<S>>;
|
|
73
74
|
export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
74
75
|
/**
|
|
75
76
|
* Compile a {@link ContractShape} into an input parser.
|
|
@@ -96,6 +97,7 @@ export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
|
96
97
|
* parseUser({ name: 'Ada' }) // { name: 'Ada' }
|
|
97
98
|
* ```
|
|
98
99
|
*/
|
|
100
|
+
export declare function compileParser<S extends ContractShape>(shape: S): Parser<Infer<S>>;
|
|
99
101
|
export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
100
102
|
/**
|
|
101
103
|
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
@@ -121,6 +123,7 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
121
123
|
* compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
|
|
122
124
|
* ```
|
|
123
125
|
*/
|
|
126
|
+
export declare function compileGenerator<S extends ContractShape>(shape: S, random?: RandomFunction): Infer<S>;
|
|
124
127
|
export declare function compileGenerator(shape: ContractShape, random?: RandomFunction): unknown;
|
|
125
128
|
/**
|
|
126
129
|
* Compile a {@link ContractShape} into a {@link ContractInterface} — the four
|
package/dist/src/core/index.cjs
CHANGED
|
@@ -63,7 +63,14 @@ function isUndefined(value) {
|
|
|
63
63
|
function isDefined(value) {
|
|
64
64
|
return value !== null && value !== void 0;
|
|
65
65
|
}
|
|
66
|
-
/** Determine whether a value is a string.
|
|
66
|
+
/** Determine whether a value is a string.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* isString('hi') // true
|
|
71
|
+
* isString(42) // false
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
67
74
|
function isString(value) {
|
|
68
75
|
return typeof value === "string";
|
|
69
76
|
}
|
|
@@ -72,11 +79,26 @@ function isString(value) {
|
|
|
72
79
|
*
|
|
73
80
|
* @remarks
|
|
74
81
|
* Includes `NaN` and `±Infinity` — use {@link isFiniteNumber} to exclude them.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* isNumber(42) // true
|
|
86
|
+
* isNumber(Number.NaN) // true — NaN is still a number
|
|
87
|
+
* isNumber('42') // false
|
|
88
|
+
* ```
|
|
75
89
|
*/
|
|
76
90
|
function isNumber(value) {
|
|
77
91
|
return typeof value === "number";
|
|
78
92
|
}
|
|
79
|
-
/** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`).
|
|
93
|
+
/** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`).
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* isFiniteNumber(42) // true
|
|
98
|
+
* isFiniteNumber(Number.NaN) // false
|
|
99
|
+
* isFiniteNumber(Infinity) // false
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
80
102
|
function isFiniteNumber(value) {
|
|
81
103
|
return typeof value === "number" && Number.isFinite(value);
|
|
82
104
|
}
|
|
@@ -193,6 +215,35 @@ function isNullableNumber(value) {
|
|
|
193
215
|
function isNullableBoolean(value) {
|
|
194
216
|
return value === null || isBoolean(value);
|
|
195
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Determine whether a value is an instance of a constructor, contained against
|
|
220
|
+
* a throwing `instanceof` check.
|
|
221
|
+
*
|
|
222
|
+
* @remarks
|
|
223
|
+
* The low-level total helper every `instanceof`-based guard in this file (and
|
|
224
|
+
* the `instanceOf` combinator) routes through. A bare `value instanceof X` is
|
|
225
|
+
* NOT total (AGENTS §14): it invokes `getPrototypeOf` on `value` — which a
|
|
226
|
+
* revoked `Proxy` or a `getPrototypeOf`-trap `Proxy` throws from — and, when
|
|
227
|
+
* `X[Symbol.hasInstance]` is user-defined, can throw from arbitrary code. This
|
|
228
|
+
* wraps the check in {@link attempt} (see ./helpers.js) so any such throw
|
|
229
|
+
* yields `false` instead of escaping.
|
|
230
|
+
*
|
|
231
|
+
* @param value - The value to test
|
|
232
|
+
* @param ctor - The constructor to test against
|
|
233
|
+
* @returns `true` when `value instanceof ctor`, `false` on a non-match or a
|
|
234
|
+
* contained throw
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* isInstance(new Date(), Date) // true
|
|
239
|
+
* isInstance({}, Date) // false
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
function isInstance(value, ctor) {
|
|
243
|
+
const target = ctor;
|
|
244
|
+
const outcome = attempt(() => isFunction(target) && value instanceof target);
|
|
245
|
+
return outcome.success && outcome.value;
|
|
246
|
+
}
|
|
196
247
|
/** Determine whether a value is a `Date`.
|
|
197
248
|
*
|
|
198
249
|
* @example
|
|
@@ -202,7 +253,7 @@ function isNullableBoolean(value) {
|
|
|
202
253
|
* ```
|
|
203
254
|
*/
|
|
204
255
|
function isDate(value) {
|
|
205
|
-
return value
|
|
256
|
+
return isInstance(value, Date);
|
|
206
257
|
}
|
|
207
258
|
/** Determine whether a value is a `RegExp`.
|
|
208
259
|
*
|
|
@@ -213,7 +264,7 @@ function isDate(value) {
|
|
|
213
264
|
* ```
|
|
214
265
|
*/
|
|
215
266
|
function isRegExp(value) {
|
|
216
|
-
return value
|
|
267
|
+
return isInstance(value, RegExp);
|
|
217
268
|
}
|
|
218
269
|
/** Determine whether a value is an `Error`.
|
|
219
270
|
*
|
|
@@ -224,7 +275,7 @@ function isRegExp(value) {
|
|
|
224
275
|
* ```
|
|
225
276
|
*/
|
|
226
277
|
function isError(value) {
|
|
227
|
-
return value
|
|
278
|
+
return isInstance(value, Error);
|
|
228
279
|
}
|
|
229
280
|
/** Determine whether a value is a native `Promise` (use {@link isPromiseLike} for any thenable).
|
|
230
281
|
*
|
|
@@ -235,7 +286,7 @@ function isError(value) {
|
|
|
235
286
|
* ```
|
|
236
287
|
*/
|
|
237
288
|
function isPromise(value) {
|
|
238
|
-
return value
|
|
289
|
+
return isInstance(value, Promise);
|
|
239
290
|
}
|
|
240
291
|
/**
|
|
241
292
|
* Determine whether a value is promise-like — an object exposing callable
|
|
@@ -271,7 +322,7 @@ function isPromiseLike(value) {
|
|
|
271
322
|
* ```
|
|
272
323
|
*/
|
|
273
324
|
function isArrayBuffer(value) {
|
|
274
|
-
return value
|
|
325
|
+
return isInstance(value, ArrayBuffer);
|
|
275
326
|
}
|
|
276
327
|
/**
|
|
277
328
|
* Determine whether a value is a `SharedArrayBuffer`.
|
|
@@ -287,7 +338,7 @@ function isArrayBuffer(value) {
|
|
|
287
338
|
* ```
|
|
288
339
|
*/
|
|
289
340
|
function isSharedArrayBuffer(value) {
|
|
290
|
-
return typeof SharedArrayBuffer !== "undefined" && value
|
|
341
|
+
return typeof SharedArrayBuffer !== "undefined" && isInstance(value, SharedArrayBuffer);
|
|
291
342
|
}
|
|
292
343
|
/**
|
|
293
344
|
* Determine whether a value implements the iterable protocol (`Symbol.iterator`).
|
|
@@ -356,6 +407,14 @@ function isObject(value) {
|
|
|
356
407
|
* instance's runs through the class's own prototype. The whole body runs
|
|
357
408
|
* inside `attempt` (AGENTS §14) so a revoked `Proxy` or a hostile
|
|
358
409
|
* `getPrototypeOf` trap cannot escape as a thrown error.
|
|
410
|
+
*
|
|
411
|
+
* @example
|
|
412
|
+
* ```ts
|
|
413
|
+
* isRecord({ a: 1 }) // true
|
|
414
|
+
* isRecord(Object.create(null)) // true
|
|
415
|
+
* isRecord([]) // false
|
|
416
|
+
* isRecord(new Date()) // false
|
|
417
|
+
* ```
|
|
359
418
|
*/
|
|
360
419
|
function isRecord(value) {
|
|
361
420
|
const outcome = attempt(() => {
|
|
@@ -374,7 +433,7 @@ function isRecord(value) {
|
|
|
374
433
|
* ```
|
|
375
434
|
*/
|
|
376
435
|
function isMap(value) {
|
|
377
|
-
return value
|
|
436
|
+
return isInstance(value, Map);
|
|
378
437
|
}
|
|
379
438
|
/** Determine whether a value is a `Set`.
|
|
380
439
|
*
|
|
@@ -385,7 +444,7 @@ function isMap(value) {
|
|
|
385
444
|
* ```
|
|
386
445
|
*/
|
|
387
446
|
function isSet(value) {
|
|
388
|
-
return value
|
|
447
|
+
return isInstance(value, Set);
|
|
389
448
|
}
|
|
390
449
|
/** Determine whether a value is a `WeakMap`.
|
|
391
450
|
*
|
|
@@ -396,7 +455,7 @@ function isSet(value) {
|
|
|
396
455
|
* ```
|
|
397
456
|
*/
|
|
398
457
|
function isWeakMap(value) {
|
|
399
|
-
return value
|
|
458
|
+
return isInstance(value, WeakMap);
|
|
400
459
|
}
|
|
401
460
|
/** Determine whether a value is a `WeakSet`.
|
|
402
461
|
*
|
|
@@ -407,7 +466,7 @@ function isWeakMap(value) {
|
|
|
407
466
|
* ```
|
|
408
467
|
*/
|
|
409
468
|
function isWeakSet(value) {
|
|
410
|
-
return value
|
|
469
|
+
return isInstance(value, WeakSet);
|
|
411
470
|
}
|
|
412
471
|
/** Determine whether a value is an array.
|
|
413
472
|
*
|
|
@@ -429,7 +488,7 @@ function isArray(value) {
|
|
|
429
488
|
* ```
|
|
430
489
|
*/
|
|
431
490
|
function isDataView(value) {
|
|
432
|
-
return value
|
|
491
|
+
return isInstance(value, DataView);
|
|
433
492
|
}
|
|
434
493
|
/** Determine whether a value is an `ArrayBufferView` (any typed array or `DataView`).
|
|
435
494
|
*
|
|
@@ -451,7 +510,7 @@ function isArrayBufferView(value) {
|
|
|
451
510
|
* ```
|
|
452
511
|
*/
|
|
453
512
|
function isInt8Array(value) {
|
|
454
|
-
return value
|
|
513
|
+
return isInstance(value, Int8Array);
|
|
455
514
|
}
|
|
456
515
|
/** Determine whether a value is a `Uint8Array`.
|
|
457
516
|
*
|
|
@@ -462,7 +521,7 @@ function isInt8Array(value) {
|
|
|
462
521
|
* ```
|
|
463
522
|
*/
|
|
464
523
|
function isUint8Array(value) {
|
|
465
|
-
return value
|
|
524
|
+
return isInstance(value, Uint8Array);
|
|
466
525
|
}
|
|
467
526
|
/** Determine whether a value is a `Uint8ClampedArray`.
|
|
468
527
|
*
|
|
@@ -473,7 +532,7 @@ function isUint8Array(value) {
|
|
|
473
532
|
* ```
|
|
474
533
|
*/
|
|
475
534
|
function isUint8ClampedArray(value) {
|
|
476
|
-
return value
|
|
535
|
+
return isInstance(value, Uint8ClampedArray);
|
|
477
536
|
}
|
|
478
537
|
/** Determine whether a value is an `Int16Array`.
|
|
479
538
|
*
|
|
@@ -484,7 +543,7 @@ function isUint8ClampedArray(value) {
|
|
|
484
543
|
* ```
|
|
485
544
|
*/
|
|
486
545
|
function isInt16Array(value) {
|
|
487
|
-
return value
|
|
546
|
+
return isInstance(value, Int16Array);
|
|
488
547
|
}
|
|
489
548
|
/** Determine whether a value is a `Uint16Array`.
|
|
490
549
|
*
|
|
@@ -495,7 +554,7 @@ function isInt16Array(value) {
|
|
|
495
554
|
* ```
|
|
496
555
|
*/
|
|
497
556
|
function isUint16Array(value) {
|
|
498
|
-
return value
|
|
557
|
+
return isInstance(value, Uint16Array);
|
|
499
558
|
}
|
|
500
559
|
/** Determine whether a value is an `Int32Array`.
|
|
501
560
|
*
|
|
@@ -506,7 +565,7 @@ function isUint16Array(value) {
|
|
|
506
565
|
* ```
|
|
507
566
|
*/
|
|
508
567
|
function isInt32Array(value) {
|
|
509
|
-
return value
|
|
568
|
+
return isInstance(value, Int32Array);
|
|
510
569
|
}
|
|
511
570
|
/** Determine whether a value is a `Uint32Array`.
|
|
512
571
|
*
|
|
@@ -517,7 +576,7 @@ function isInt32Array(value) {
|
|
|
517
576
|
* ```
|
|
518
577
|
*/
|
|
519
578
|
function isUint32Array(value) {
|
|
520
|
-
return value
|
|
579
|
+
return isInstance(value, Uint32Array);
|
|
521
580
|
}
|
|
522
581
|
/** Determine whether a value is a `Float32Array`.
|
|
523
582
|
*
|
|
@@ -528,7 +587,7 @@ function isUint32Array(value) {
|
|
|
528
587
|
* ```
|
|
529
588
|
*/
|
|
530
589
|
function isFloat32Array(value) {
|
|
531
|
-
return value
|
|
590
|
+
return isInstance(value, Float32Array);
|
|
532
591
|
}
|
|
533
592
|
/** Determine whether a value is a `Float64Array`.
|
|
534
593
|
*
|
|
@@ -539,7 +598,7 @@ function isFloat32Array(value) {
|
|
|
539
598
|
* ```
|
|
540
599
|
*/
|
|
541
600
|
function isFloat64Array(value) {
|
|
542
|
-
return value
|
|
601
|
+
return isInstance(value, Float64Array);
|
|
543
602
|
}
|
|
544
603
|
/**
|
|
545
604
|
* Determine whether a value is a `BigInt64Array`.
|
|
@@ -555,7 +614,7 @@ function isFloat64Array(value) {
|
|
|
555
614
|
* ```
|
|
556
615
|
*/
|
|
557
616
|
function isBigInt64Array(value) {
|
|
558
|
-
return typeof BigInt64Array !== "undefined" && value
|
|
617
|
+
return typeof BigInt64Array !== "undefined" && isInstance(value, BigInt64Array);
|
|
559
618
|
}
|
|
560
619
|
/**
|
|
561
620
|
* Determine whether a value is a `BigUint64Array`.
|
|
@@ -571,7 +630,7 @@ function isBigInt64Array(value) {
|
|
|
571
630
|
* ```
|
|
572
631
|
*/
|
|
573
632
|
function isBigUint64Array(value) {
|
|
574
|
-
return typeof BigUint64Array !== "undefined" && value
|
|
633
|
+
return typeof BigUint64Array !== "undefined" && isInstance(value, BigUint64Array);
|
|
575
634
|
}
|
|
576
635
|
/** Determine whether a value is the empty string `''`.
|
|
577
636
|
*
|
|
@@ -605,7 +664,8 @@ function isEmptyArray(value) {
|
|
|
605
664
|
*/
|
|
606
665
|
function isEmptyObject(value) {
|
|
607
666
|
if (!isRecord(value)) return false;
|
|
608
|
-
|
|
667
|
+
const outcome = attempt(() => Object.keys(value).length === 0 && enumerableSymbolCount(value) === 0);
|
|
668
|
+
return outcome.success && outcome.value;
|
|
609
669
|
}
|
|
610
670
|
/** Determine whether a value is an empty `Map`.
|
|
611
671
|
*
|
|
@@ -616,7 +676,7 @@ function isEmptyObject(value) {
|
|
|
616
676
|
* ```
|
|
617
677
|
*/
|
|
618
678
|
function isEmptyMap(value) {
|
|
619
|
-
return value
|
|
679
|
+
return isMap(value) && value.size === 0;
|
|
620
680
|
}
|
|
621
681
|
/** Determine whether a value is an empty `Set`.
|
|
622
682
|
*
|
|
@@ -627,7 +687,7 @@ function isEmptyMap(value) {
|
|
|
627
687
|
* ```
|
|
628
688
|
*/
|
|
629
689
|
function isEmptySet(value) {
|
|
630
|
-
return value
|
|
690
|
+
return isSet(value) && value.size === 0;
|
|
631
691
|
}
|
|
632
692
|
/** Determine whether a value is a non-empty string (at least one character).
|
|
633
693
|
*
|
|
@@ -661,7 +721,8 @@ function isNonEmptyArray(value) {
|
|
|
661
721
|
*/
|
|
662
722
|
function isNonEmptyObject(value) {
|
|
663
723
|
if (!isRecord(value)) return false;
|
|
664
|
-
|
|
724
|
+
const outcome = attempt(() => Object.keys(value).length > 0 || enumerableSymbolCount(value) > 0);
|
|
725
|
+
return outcome.success && outcome.value;
|
|
665
726
|
}
|
|
666
727
|
/** Determine whether a value is a non-empty `Map` (at least one entry).
|
|
667
728
|
*
|
|
@@ -672,7 +733,7 @@ function isNonEmptyObject(value) {
|
|
|
672
733
|
* ```
|
|
673
734
|
*/
|
|
674
735
|
function isNonEmptyMap(value) {
|
|
675
|
-
return value
|
|
736
|
+
return isMap(value) && value.size > 0;
|
|
676
737
|
}
|
|
677
738
|
/** Determine whether a value is a non-empty `Set` (at least one element).
|
|
678
739
|
*
|
|
@@ -683,7 +744,7 @@ function isNonEmptyMap(value) {
|
|
|
683
744
|
* ```
|
|
684
745
|
*/
|
|
685
746
|
function isNonEmptySet(value) {
|
|
686
|
-
return value
|
|
747
|
+
return isSet(value) && value.size > 0;
|
|
687
748
|
}
|
|
688
749
|
/** Determine whether a value is a function that declares zero parameters (`Function.length === 0`).
|
|
689
750
|
*
|
|
@@ -1071,7 +1132,7 @@ function literalOf(...literals) {
|
|
|
1071
1132
|
* ```
|
|
1072
1133
|
*/
|
|
1073
1134
|
function instanceOf(ctor) {
|
|
1074
|
-
return (value) => isConstructor(ctor) && isObject(value) && value
|
|
1135
|
+
return (value) => isConstructor(ctor) && isObject(value) && isInstance(value, ctor);
|
|
1075
1136
|
}
|
|
1076
1137
|
/**
|
|
1077
1138
|
* Build a guard from a native `enum` or any object whose values are strings or
|
|
@@ -1934,24 +1995,6 @@ function compileSchema(shape) {
|
|
|
1934
1995
|
case "raw": return shape.schema;
|
|
1935
1996
|
}
|
|
1936
1997
|
}
|
|
1937
|
-
/**
|
|
1938
|
-
* Compile a {@link ContractShape} into a runtime type guard.
|
|
1939
|
-
*
|
|
1940
|
-
* @remarks
|
|
1941
|
-
* Reuses the combinators: `literalOf` for literals, `arrayOf` for arrays,
|
|
1942
|
-
* `recordOf` for closed objects, `unionOf` for unions, `nullableOf` for nullable,
|
|
1943
|
-
* and `whereOf` for constraint refinement. Like every guard it is total — it
|
|
1944
|
-
* never throws (AGENTS §14).
|
|
1945
|
-
*
|
|
1946
|
-
* @param shape - The shape to compile
|
|
1947
|
-
* @returns A guard narrowing to the shape's inferred type
|
|
1948
|
-
*
|
|
1949
|
-
* @example
|
|
1950
|
-
* ```ts
|
|
1951
|
-
* const isUser = compileGuard(objectShape({ name: stringShape() }))
|
|
1952
|
-
* isUser({ name: 'Ada' }) // true
|
|
1953
|
-
* ```
|
|
1954
|
-
*/
|
|
1955
1998
|
function compileGuard(shape) {
|
|
1956
1999
|
switch (shape.type) {
|
|
1957
2000
|
case "string": return stringOf({
|
|
@@ -2004,37 +2047,16 @@ function compileGuard(shape) {
|
|
|
2004
2047
|
return outcome.success && outcome.value;
|
|
2005
2048
|
};
|
|
2006
2049
|
}
|
|
2007
|
-
case "union":
|
|
2050
|
+
case "union": {
|
|
2051
|
+
const guards = shape.variants.map((variant) => compileGuard(variant));
|
|
2052
|
+
if (shape.mode === "oneOf") return (value) => guards.filter((guard) => guard(value)).length === 1;
|
|
2053
|
+
return unionOf(...guards);
|
|
2054
|
+
}
|
|
2008
2055
|
case "optional": return orOf(isUndefined, compileGuard(shape.inner));
|
|
2009
2056
|
case "nullable": return nullableOf(compileGuard(shape.inner));
|
|
2010
2057
|
case "raw": return (_value) => true;
|
|
2011
2058
|
}
|
|
2012
2059
|
}
|
|
2013
|
-
/**
|
|
2014
|
-
* Compile a {@link ContractShape} into an input parser.
|
|
2015
|
-
*
|
|
2016
|
-
* @remarks
|
|
2017
|
-
* Reuses the leaf parsers (`parseString` / `parseInteger` / `parseNumber` /
|
|
2018
|
-
* `parseBoolean` / `parseRecord`) and coerces structurally. An object fails as a
|
|
2019
|
-
* whole on any required-field failure; a union returns a guard-valid value
|
|
2020
|
-
* unchanged, otherwise the first variant that both parses and guards wins.
|
|
2021
|
-
*
|
|
2022
|
-
* After coercing a leaf, it re-applies that leaf's REFINEMENTS through the same
|
|
2023
|
-
* combinators `compileGuard` uses — `stringOf` for a string's length/pattern and
|
|
2024
|
-
* `boundsOf` for a number's value and an array's length — so a value that coerces
|
|
2025
|
-
* but violates a bound parses to `undefined`. The result is full parse↔guard
|
|
2026
|
-
* soundness (AGENTS §14): a non-`undefined` parse always satisfies the contract's
|
|
2027
|
-
* `is`, refinements included.
|
|
2028
|
-
*
|
|
2029
|
-
* @param shape - The shape to compile
|
|
2030
|
-
* @returns A parser yielding the shape's inferred type or `undefined`
|
|
2031
|
-
*
|
|
2032
|
-
* @example
|
|
2033
|
-
* ```ts
|
|
2034
|
-
* const parseUser = compileParser(objectShape({ name: stringShape() }))
|
|
2035
|
-
* parseUser({ name: 'Ada' }) // { name: 'Ada' }
|
|
2036
|
-
* ```
|
|
2037
|
-
*/
|
|
2038
2060
|
function compileParser(shape) {
|
|
2039
2061
|
switch (shape.type) {
|
|
2040
2062
|
case "string": {
|
|
@@ -2136,6 +2158,11 @@ function compileParser(shape) {
|
|
|
2136
2158
|
parse: compileParser(variant),
|
|
2137
2159
|
guard: compileGuard(variant)
|
|
2138
2160
|
}));
|
|
2161
|
+
if (shape.mode === "oneOf") return (value) => {
|
|
2162
|
+
const matches = variants.filter((variant) => variant.guard(value));
|
|
2163
|
+
const [only] = matches;
|
|
2164
|
+
return matches.length === 1 && only !== void 0 ? only.parse(value) : void 0;
|
|
2165
|
+
};
|
|
2139
2166
|
return (value) => {
|
|
2140
2167
|
for (const variant of variants) if (variant.guard(value)) return value;
|
|
2141
2168
|
for (const variant of variants) {
|
|
@@ -2155,30 +2182,6 @@ function compileParser(shape) {
|
|
|
2155
2182
|
case "raw": return (value) => value;
|
|
2156
2183
|
}
|
|
2157
2184
|
}
|
|
2158
|
-
/**
|
|
2159
|
-
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
2160
|
-
*
|
|
2161
|
-
* @remarks
|
|
2162
|
-
* The same shape and the same `random` source always produce the same value, so
|
|
2163
|
-
* seed data is reproducible. Defaults to a {@link seededRandom} source seeded
|
|
2164
|
-
* from the wall clock when none is supplied. Throws on a degenerate empty
|
|
2165
|
-
* `literalShape` / `unionShape`, on a pattern-constrained `stringShape` whose
|
|
2166
|
-
* generated sample cannot satisfy the pattern, or on a `rawShape` (its embedded
|
|
2167
|
-
* schema is arbitrary and cannot be auto-generated) — a programmer error that
|
|
2168
|
-
* cannot generate a value (AGENTS §12). `createContract` runs
|
|
2169
|
-
* {@link validateShape} first, so a degenerate `literalShape` / `unionShape` /
|
|
2170
|
-
* bounded shape is normally caught there; these throws remain here as defense
|
|
2171
|
-
* for standalone `compileGenerator` use.
|
|
2172
|
-
*
|
|
2173
|
-
* @param shape - The shape to generate from
|
|
2174
|
-
* @param random - A seeded random source (defaults to `seededRandom(Date.now())`)
|
|
2175
|
-
* @returns A value matching the shape
|
|
2176
|
-
*
|
|
2177
|
-
* @example
|
|
2178
|
-
* ```ts
|
|
2179
|
-
* compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
|
|
2180
|
-
* ```
|
|
2181
|
-
*/
|
|
2182
2185
|
function compileGenerator(shape, random = seededRandom(Date.now())) {
|
|
2183
2186
|
switch (shape.type) {
|
|
2184
2187
|
case "string": {
|
|
@@ -2483,16 +2486,32 @@ function unionShape(...variants) {
|
|
|
2483
2486
|
* Build a {@link UnionShape} that emits `oneOf` (exactly one match) in JSON Schema.
|
|
2484
2487
|
*
|
|
2485
2488
|
* @remarks
|
|
2486
|
-
*
|
|
2487
|
-
*
|
|
2489
|
+
* Unlike {@link unionShape} (`anyOf` — at least one variant matches),
|
|
2490
|
+
* `oneOfShape`'s compiled guard and parser enforce EXACTLY one match:
|
|
2491
|
+
*
|
|
2492
|
+
* - **Guard**: accepts the value only when exactly one variant's guard
|
|
2493
|
+
* accepts it. A value matching two-or-more variants — which would violate
|
|
2494
|
+
* the emitted `oneOf` schema — is rejected, even though it would pass
|
|
2495
|
+
* {@link unionShape}'s guard.
|
|
2496
|
+
* - **Parser**: judged on the RAW input's guard matches only, with NO
|
|
2497
|
+
* coercion fallback for an ambiguous input. When exactly one variant's
|
|
2498
|
+
* guard accepts the raw value, that variant's parser runs. Zero matches or
|
|
2499
|
+
* two-or-more matches both parse to `undefined` — a value ambiguous
|
|
2500
|
+
* between variants has no well-defined coercion target.
|
|
2501
|
+
*
|
|
2502
|
+
* Prefer {@link unionShape} when a value may legitimately satisfy more than
|
|
2503
|
+
* one variant (e.g. overlapping shapes) and any match is acceptable. Prefer
|
|
2504
|
+
* `oneOfShape` when overlap between variants indicates malformed input that
|
|
2505
|
+
* must be rejected.
|
|
2488
2506
|
*
|
|
2489
2507
|
* @param variants - The candidate shapes
|
|
2490
2508
|
* @returns A union shape with `mode: 'oneOf'`
|
|
2491
2509
|
*
|
|
2492
2510
|
* @example
|
|
2493
2511
|
* ```ts
|
|
2494
|
-
* const id = oneOfShape(
|
|
2495
|
-
* //
|
|
2512
|
+
* const id = oneOfShape(numberShape(), integerShape())
|
|
2513
|
+
* // 3 fails — matches both numberShape and integerShape
|
|
2514
|
+
* // 3.5 passes — matches numberShape only
|
|
2496
2515
|
* ```
|
|
2497
2516
|
*/
|
|
2498
2517
|
function oneOfShape(...variants) {
|
|
@@ -2624,6 +2643,7 @@ exports.isFloat32Array = isFloat32Array;
|
|
|
2624
2643
|
exports.isFloat64Array = isFloat64Array;
|
|
2625
2644
|
exports.isFunction = isFunction;
|
|
2626
2645
|
exports.isGeneratorFunction = isGeneratorFunction;
|
|
2646
|
+
exports.isInstance = isInstance;
|
|
2627
2647
|
exports.isInt16Array = isInt16Array;
|
|
2628
2648
|
exports.isInt32Array = isInt32Array;
|
|
2629
2649
|
exports.isInt8Array = isInt8Array;
|