bupkis 0.2.0 → 0.4.0
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/CHANGELOG.md +27 -0
- package/README.md +35 -11
- package/dist/commonjs/assertion/assertion-async.d.ts +2 -1
- package/dist/commonjs/assertion/assertion-async.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion-async.js +84 -2
- package/dist/commonjs/assertion/assertion-async.js.map +1 -1
- package/dist/commonjs/assertion/assertion-sync.d.ts +1 -1
- package/dist/commonjs/assertion/assertion-sync.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion-sync.js +5 -1
- package/dist/commonjs/assertion/assertion-sync.js.map +1 -1
- package/dist/commonjs/assertion/assertion-types.d.ts +6 -2
- package/dist/commonjs/assertion/assertion-types.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion.d.ts +1 -1
- package/dist/commonjs/assertion/assertion.d.ts.map +1 -1
- package/dist/commonjs/assertion/assertion.js +1 -14
- package/dist/commonjs/assertion/assertion.js.map +1 -1
- package/dist/commonjs/assertion/impl/async.d.ts +122 -21
- package/dist/commonjs/assertion/impl/async.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/async.js +118 -90
- package/dist/commonjs/assertion/impl/async.js.map +1 -1
- package/dist/commonjs/assertion/impl/callback.d.ts +104 -0
- package/dist/commonjs/assertion/impl/callback.d.ts.map +1 -0
- package/dist/commonjs/assertion/impl/callback.js +694 -0
- package/dist/commonjs/assertion/impl/callback.js.map +1 -0
- package/dist/commonjs/assertion/impl/index.d.ts +1 -1
- package/dist/commonjs/assertion/impl/index.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/index.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync-basic.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/sync-basic.js +1 -1
- package/dist/commonjs/assertion/impl/sync-basic.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync-collection.d.ts +1 -1
- package/dist/commonjs/assertion/impl/sync-collection.js +3 -3
- package/dist/commonjs/assertion/impl/sync-collection.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync-esoteric.js +1 -1
- package/dist/commonjs/assertion/impl/sync-esoteric.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync-parametric.d.ts +22 -28
- package/dist/commonjs/assertion/impl/sync-parametric.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/sync-parametric.js +35 -50
- package/dist/commonjs/assertion/impl/sync-parametric.js.map +1 -1
- package/dist/commonjs/assertion/impl/sync.d.ts +68 -30
- package/dist/commonjs/assertion/impl/sync.d.ts.map +1 -1
- package/dist/commonjs/assertion/impl/sync.js +4 -1
- package/dist/commonjs/assertion/impl/sync.js.map +1 -1
- package/dist/commonjs/bootstrap.d.ts +147 -52
- package/dist/commonjs/bootstrap.d.ts.map +1 -1
- package/dist/commonjs/bootstrap.js +2 -3
- package/dist/commonjs/bootstrap.js.map +1 -1
- package/dist/commonjs/constant.d.ts +1 -1
- package/dist/commonjs/constant.d.ts.map +1 -1
- package/dist/commonjs/constant.js +8 -1
- package/dist/commonjs/constant.js.map +1 -1
- package/dist/commonjs/error.d.ts +22 -2
- package/dist/commonjs/error.d.ts.map +1 -1
- package/dist/commonjs/error.js +44 -4
- package/dist/commonjs/error.js.map +1 -1
- package/dist/commonjs/expect.d.ts.map +1 -1
- package/dist/commonjs/expect.js +1 -1
- package/dist/commonjs/expect.js.map +1 -1
- package/dist/commonjs/guards.d.ts +96 -5
- package/dist/commonjs/guards.d.ts.map +1 -1
- package/dist/commonjs/guards.js +104 -25
- package/dist/commonjs/guards.js.map +1 -1
- package/dist/commonjs/index.d.ts +146 -51
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/schema.d.ts +84 -18
- package/dist/commonjs/schema.d.ts.map +1 -1
- package/dist/commonjs/schema.js +107 -22
- package/dist/commonjs/schema.js.map +1 -1
- package/dist/commonjs/types.d.ts +171 -9
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/commonjs/use.d.ts.map +1 -1
- package/dist/commonjs/use.js +15 -1
- package/dist/commonjs/use.js.map +1 -1
- package/dist/commonjs/util.d.ts +66 -50
- package/dist/commonjs/util.d.ts.map +1 -1
- package/dist/commonjs/util.js +169 -156
- package/dist/commonjs/util.js.map +1 -1
- package/dist/commonjs/value-to-schema.d.ts +122 -0
- package/dist/commonjs/value-to-schema.d.ts.map +1 -0
- package/dist/commonjs/value-to-schema.js +329 -0
- package/dist/commonjs/value-to-schema.js.map +1 -0
- package/dist/esm/assertion/assertion-async.d.ts +2 -1
- package/dist/esm/assertion/assertion-async.d.ts.map +1 -1
- package/dist/esm/assertion/assertion-async.js +85 -3
- package/dist/esm/assertion/assertion-async.js.map +1 -1
- package/dist/esm/assertion/assertion-sync.d.ts +1 -1
- package/dist/esm/assertion/assertion-sync.d.ts.map +1 -1
- package/dist/esm/assertion/assertion-sync.js +6 -2
- package/dist/esm/assertion/assertion-sync.js.map +1 -1
- package/dist/esm/assertion/assertion-types.d.ts +6 -2
- package/dist/esm/assertion/assertion-types.d.ts.map +1 -1
- package/dist/esm/assertion/assertion.d.ts +1 -1
- package/dist/esm/assertion/assertion.d.ts.map +1 -1
- package/dist/esm/assertion/assertion.js +1 -14
- package/dist/esm/assertion/assertion.js.map +1 -1
- package/dist/esm/assertion/impl/async.d.ts +122 -21
- package/dist/esm/assertion/impl/async.d.ts.map +1 -1
- package/dist/esm/assertion/impl/async.js +118 -90
- package/dist/esm/assertion/impl/async.js.map +1 -1
- package/dist/esm/assertion/impl/callback.d.ts +104 -0
- package/dist/esm/assertion/impl/callback.d.ts.map +1 -0
- package/dist/esm/assertion/impl/callback.js +691 -0
- package/dist/esm/assertion/impl/callback.js.map +1 -0
- package/dist/esm/assertion/impl/index.d.ts +1 -1
- package/dist/esm/assertion/impl/index.d.ts.map +1 -1
- package/dist/esm/assertion/impl/index.js +1 -1
- package/dist/esm/assertion/impl/index.js.map +1 -1
- package/dist/esm/assertion/impl/sync-basic.d.ts.map +1 -1
- package/dist/esm/assertion/impl/sync-basic.js +2 -2
- package/dist/esm/assertion/impl/sync-basic.js.map +1 -1
- package/dist/esm/assertion/impl/sync-collection.d.ts +1 -1
- package/dist/esm/assertion/impl/sync-collection.js +3 -3
- package/dist/esm/assertion/impl/sync-collection.js.map +1 -1
- package/dist/esm/assertion/impl/sync-esoteric.js +2 -2
- package/dist/esm/assertion/impl/sync-esoteric.js.map +1 -1
- package/dist/esm/assertion/impl/sync-parametric.d.ts +22 -28
- package/dist/esm/assertion/impl/sync-parametric.d.ts.map +1 -1
- package/dist/esm/assertion/impl/sync-parametric.js +36 -51
- package/dist/esm/assertion/impl/sync-parametric.js.map +1 -1
- package/dist/esm/assertion/impl/sync.d.ts +68 -30
- package/dist/esm/assertion/impl/sync.d.ts.map +1 -1
- package/dist/esm/assertion/impl/sync.js +3 -1
- package/dist/esm/assertion/impl/sync.js.map +1 -1
- package/dist/esm/bootstrap.d.ts +147 -52
- package/dist/esm/bootstrap.d.ts.map +1 -1
- package/dist/esm/bootstrap.js +1 -2
- package/dist/esm/bootstrap.js.map +1 -1
- package/dist/esm/constant.d.ts +1 -1
- package/dist/esm/constant.d.ts.map +1 -1
- package/dist/esm/constant.js +7 -0
- package/dist/esm/constant.js.map +1 -1
- package/dist/esm/error.d.ts +22 -2
- package/dist/esm/error.d.ts.map +1 -1
- package/dist/esm/error.js +43 -4
- package/dist/esm/error.js.map +1 -1
- package/dist/esm/expect.d.ts.map +1 -1
- package/dist/esm/expect.js +2 -2
- package/dist/esm/expect.js.map +1 -1
- package/dist/esm/guards.d.ts +96 -5
- package/dist/esm/guards.d.ts.map +1 -1
- package/dist/esm/guards.js +98 -21
- package/dist/esm/guards.js.map +1 -1
- package/dist/esm/index.d.ts +146 -51
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/schema.d.ts +84 -18
- package/dist/esm/schema.d.ts.map +1 -1
- package/dist/esm/schema.js +107 -22
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/types.d.ts +171 -9
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/use.d.ts.map +1 -1
- package/dist/esm/use.js +15 -1
- package/dist/esm/use.js.map +1 -1
- package/dist/esm/util.d.ts +66 -50
- package/dist/esm/util.d.ts.map +1 -1
- package/dist/esm/util.js +153 -154
- package/dist/esm/util.js.map +1 -1
- package/dist/esm/value-to-schema.d.ts +122 -0
- package/dist/esm/value-to-schema.d.ts.map +1 -0
- package/dist/esm/value-to-schema.js +325 -0
- package/dist/esm/value-to-schema.js.map +1 -0
- package/package.json +16 -13
- package/src/assertion/assertion-async.ts +113 -3
- package/src/assertion/assertion-sync.ts +5 -2
- package/src/assertion/assertion-types.ts +14 -4
- package/src/assertion/assertion.ts +2 -17
- package/src/assertion/impl/async.ts +137 -93
- package/src/assertion/impl/callback.ts +882 -0
- package/src/assertion/impl/index.ts +1 -1
- package/src/assertion/impl/sync-basic.ts +5 -2
- package/src/assertion/impl/sync-collection.ts +3 -3
- package/src/assertion/impl/sync-esoteric.ts +2 -2
- package/src/assertion/impl/sync-parametric.ts +47 -54
- package/src/assertion/impl/sync.ts +3 -0
- package/src/bootstrap.ts +1 -2
- package/src/constant.ts +10 -0
- package/src/error.ts +57 -3
- package/src/expect.ts +6 -2
- package/src/guards.ts +125 -18
- package/src/index.ts +3 -0
- package/src/schema.ts +121 -23
- package/src/types.ts +205 -10
- package/src/use.ts +22 -0
- package/src/util.ts +168 -223
- package/src/value-to-schema.ts +489 -0
package/src/schema.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This module provides reusable Zod schemas for validating constructors,
|
|
5
5
|
* functions, property keys, promises, and other common JavaScript types used
|
|
6
|
-
* throughout the assertion system. These tend to work around the
|
|
6
|
+
* throughout the assertion system. These tend to work around the impedance
|
|
7
7
|
* mismatch between **BUPKIS** and Zod.
|
|
8
8
|
*
|
|
9
9
|
* These are used internally, but consumers may also find them useful.
|
|
@@ -36,46 +36,52 @@ import { z } from 'zod/v4';
|
|
|
36
36
|
|
|
37
37
|
import {
|
|
38
38
|
isA,
|
|
39
|
-
|
|
39
|
+
isConstructible,
|
|
40
|
+
isExpectItExecutor,
|
|
40
41
|
isFunction,
|
|
41
42
|
isNonNullObject,
|
|
42
43
|
isPromiseLike,
|
|
43
44
|
} from './guards.js';
|
|
44
45
|
import { BupkisRegistry } from './metadata.js';
|
|
45
|
-
import {
|
|
46
|
+
import {
|
|
47
|
+
type Constructor,
|
|
48
|
+
type ExpectItExecutor,
|
|
49
|
+
type MutableOrReadonly,
|
|
50
|
+
type SatisfyPatternValue,
|
|
51
|
+
} from './types.js';
|
|
46
52
|
|
|
47
53
|
/**
|
|
48
|
-
* A Zod schema that validates JavaScript
|
|
54
|
+
* A Zod schema that validates JavaScript constructible functions.
|
|
49
55
|
*
|
|
50
56
|
* This schema validates values that can be used as constructors, including ES6
|
|
51
57
|
* classes, traditional constructor functions, and built-in constructors. It
|
|
52
|
-
* uses the {@link
|
|
58
|
+
* uses the {@link isConstructible} guard function to determine if a value can be
|
|
53
59
|
* invoked with the `new` operator to create object instances.
|
|
54
60
|
*
|
|
55
61
|
* @privateRemarks
|
|
56
62
|
* The schema is registered in the {@link BupkisRegistry} with the name
|
|
57
|
-
* `
|
|
63
|
+
* `ConstructibleSchema` for later reference and type checking purposes.
|
|
58
64
|
* @example
|
|
59
65
|
*
|
|
60
66
|
* ```typescript
|
|
61
67
|
* class MyClass {}
|
|
62
68
|
* function MyConstructor() {}
|
|
63
69
|
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
+
* ConstructibleSchema.parse(MyClass); // ✓ Valid
|
|
71
|
+
* ConstructibleSchema.parse(MyConstructor); // ✓ Valid
|
|
72
|
+
* ConstructibleSchema.parse(Array); // ✓ Valid
|
|
73
|
+
* ConstructibleSchema.parse(Date); // ✓ Valid
|
|
74
|
+
* ConstructibleSchema.parse(() => {}); // ✗ Throws validation error
|
|
75
|
+
* ConstructibleSchema.parse({}); // ✗ Throws validation error
|
|
70
76
|
* ```
|
|
71
77
|
*
|
|
72
78
|
* @group Schema
|
|
73
79
|
*/
|
|
74
80
|
|
|
75
|
-
export const
|
|
76
|
-
.custom<Constructor>(
|
|
77
|
-
.register(BupkisRegistry, { name: '
|
|
78
|
-
.describe('
|
|
81
|
+
export const ConstructibleSchema = z
|
|
82
|
+
.custom<Constructor>(isConstructible)
|
|
83
|
+
.register(BupkisRegistry, { name: 'ConstructibleSchema' })
|
|
84
|
+
.describe('Constructible Function');
|
|
79
85
|
|
|
80
86
|
/**
|
|
81
87
|
* A Zod schema that validates any JavaScript function.
|
|
@@ -242,9 +248,12 @@ export const StrongSetSchema = z
|
|
|
242
248
|
* `Object.prototype`, making them useful as pure data containers or
|
|
243
249
|
* dictionaries.
|
|
244
250
|
*
|
|
245
|
-
* @
|
|
251
|
+
* @privateRemarks
|
|
246
252
|
* The schema is registered in the `BupkisRegistry` with the name
|
|
247
253
|
* `ObjectWithNullPrototype` for later reference and type checking purposes.
|
|
254
|
+
*
|
|
255
|
+
* Changing this to be a `ZodRecord` would be nice, but that would end up
|
|
256
|
+
* blasting away the original object's prototype.
|
|
248
257
|
* @example
|
|
249
258
|
*
|
|
250
259
|
* ```typescript
|
|
@@ -260,14 +269,22 @@ export const StrongSetSchema = z
|
|
|
260
269
|
* ```
|
|
261
270
|
*
|
|
262
271
|
* @group Schema
|
|
272
|
+
* @see Aliases: {@link NullProtoObjectSchema}, {@link DictionarySchema}
|
|
263
273
|
*/
|
|
264
|
-
export const
|
|
274
|
+
export const DictionarySchema = z
|
|
265
275
|
.custom<Record<PropertyKey, unknown>>(
|
|
266
276
|
(value) => isNonNullObject(value) && Object.getPrototypeOf(value) === null,
|
|
267
277
|
)
|
|
268
278
|
.describe('Object with null prototype')
|
|
269
279
|
.register(BupkisRegistry, { name: 'ObjectWithNullPrototype' });
|
|
270
280
|
|
|
281
|
+
/**
|
|
282
|
+
* {@inheritDoc DictionarySchema}
|
|
283
|
+
*
|
|
284
|
+
* @group Schema
|
|
285
|
+
*/
|
|
286
|
+
export const NullProtoObjectSchema = DictionarySchema;
|
|
287
|
+
|
|
271
288
|
/**
|
|
272
289
|
* A Zod schema that validates functions declared with the `async` keyword.
|
|
273
290
|
*
|
|
@@ -276,7 +293,7 @@ export const NullProtoObjectSchema = z
|
|
|
276
293
|
* function's internal `[[ToString]]` representation to distinguish async
|
|
277
294
|
* functions from regular functions that might return Promises.
|
|
278
295
|
*
|
|
279
|
-
* @
|
|
296
|
+
* @privateRemarks
|
|
280
297
|
* The schema is registered in the `BupkisRegistry` with the name
|
|
281
298
|
* `AsyncFunctionSchema` for later reference and type checking purposes. This
|
|
282
299
|
* schema cannot reliably detect functions that return Promises but are not
|
|
@@ -318,7 +335,7 @@ export const AsyncFunctionSchema = FunctionSchema.refine(
|
|
|
318
335
|
* if it converts to `true` when evaluated in a boolean context - essentially
|
|
319
336
|
* any value that is not one of the eight falsy values.
|
|
320
337
|
*
|
|
321
|
-
* @
|
|
338
|
+
* @privateRemarks
|
|
322
339
|
* The schema is registered in the `BupkisRegistry` with the name `Truthy` and
|
|
323
340
|
* indicates that it accepts anything as valid input for evaluation.
|
|
324
341
|
* @example
|
|
@@ -354,7 +371,7 @@ export const TruthySchema = z
|
|
|
354
371
|
* in JavaScript are: `false`, `0`, `-0`, `0n`, `""` (empty string), `null`,
|
|
355
372
|
* `undefined`, and `NaN`.
|
|
356
373
|
*
|
|
357
|
-
* @
|
|
374
|
+
* @privateRemarks
|
|
358
375
|
* The schema is registered in the `BupkisRegistry` with the name `Falsy` and
|
|
359
376
|
* indicates that it accepts anything as valid input for evaluation.
|
|
360
377
|
* @example
|
|
@@ -392,7 +409,7 @@ export const FalsySchema = z
|
|
|
392
409
|
* distinguishing them from objects and functions which are non-primitive
|
|
393
410
|
* reference types.
|
|
394
411
|
*
|
|
395
|
-
* @
|
|
412
|
+
* @privateRemarks
|
|
396
413
|
* The schema is registered in the `BupkisRegistry` with the name `Primitive`
|
|
397
414
|
* and indicates that it accepts primitive values as valid input.
|
|
398
415
|
* @example
|
|
@@ -474,7 +491,7 @@ export const ArrayLikeSchema = z
|
|
|
474
491
|
* It ensures the validated value is a proper regular expression object with all
|
|
475
492
|
* associated methods and properties.
|
|
476
493
|
*
|
|
477
|
-
* @
|
|
494
|
+
* @privateRemarks
|
|
478
495
|
* The schema is registered in the `BupkisRegistry` with the name `RegExp` for
|
|
479
496
|
* later reference and type checking purposes.
|
|
480
497
|
* @example
|
|
@@ -495,3 +512,84 @@ export const RegExpSchema = z
|
|
|
495
512
|
.instanceof(RegExp)
|
|
496
513
|
.describe('A RegExp instance')
|
|
497
514
|
.register(BupkisRegistry, { name: 'RegExp' });
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* A recursive Zod schema that validates values allowed in "to satisfy"
|
|
518
|
+
* assertion patterns.
|
|
519
|
+
*
|
|
520
|
+
* This schema defines the allowed structure for patterns used with "to satisfy"
|
|
521
|
+
* and "to be like" assertions. It supports a recursive structure that can
|
|
522
|
+
* contain:
|
|
523
|
+
*
|
|
524
|
+
* - **Primitive values**: strings, numbers, booleans, null, undefined, bigint,
|
|
525
|
+
* symbols
|
|
526
|
+
* - **Regular expressions**: for pattern matching against string values
|
|
527
|
+
* - **ExpectItExecutor functions**: nested assertions created via `expect.it()`
|
|
528
|
+
* - **Objects**: with properties that recursively follow this same pattern
|
|
529
|
+
* - **Arrays**: with elements that recursively follow this same pattern
|
|
530
|
+
*
|
|
531
|
+
* The schema handles the special semantics required for "to satisfy"
|
|
532
|
+
* assertions:
|
|
533
|
+
*
|
|
534
|
+
* - RegExp values are used for pattern testing rather than exact matching
|
|
535
|
+
* - ExpectItExecutor functions are executed as nested assertions
|
|
536
|
+
* - Objects and arrays can contain any combination of the above recursively
|
|
537
|
+
*
|
|
538
|
+
* @privateRemarks
|
|
539
|
+
* Uses `z.lazy()` to handle recursive object and array structures without
|
|
540
|
+
* causing infinite recursion during schema definition. The schema is registered
|
|
541
|
+
* in the `BupkisRegistry` for reference and type checking.
|
|
542
|
+
* @example
|
|
543
|
+
*
|
|
544
|
+
* ```typescript
|
|
545
|
+
* // Primitive values
|
|
546
|
+
* SatisfyPatternSchema.parse('hello'); // ✓ Valid
|
|
547
|
+
* SatisfyPatternSchema.parse(42); // ✓ Valid
|
|
548
|
+
* SatisfyPatternSchema.parse(true); // ✓ Valid
|
|
549
|
+
*
|
|
550
|
+
* // Regular expressions for pattern matching
|
|
551
|
+
* SatisfyPatternSchema.parse(/test/); // ✓ Valid
|
|
552
|
+
*
|
|
553
|
+
* // ExpectItExecutor functions
|
|
554
|
+
* SatisfyPatternSchema.parse(expect.it('to be a string')); // ✓ Valid
|
|
555
|
+
*
|
|
556
|
+
* // Complex nested structures
|
|
557
|
+
* SatisfyPatternSchema.parse({
|
|
558
|
+
* name: expect.it('to be a string'),
|
|
559
|
+
* email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
560
|
+
* age: expect.it('to be greater than', 0),
|
|
561
|
+
* tags: [expect.it('to be a string')],
|
|
562
|
+
* }); // ✓ Valid
|
|
563
|
+
*
|
|
564
|
+
* // Invalid values
|
|
565
|
+
* SatisfyPatternSchema.parse(new Date()); // ✗ Invalid (not supported type)
|
|
566
|
+
* SatisfyPatternSchema.parse(() => {}); // ✗ Invalid (regular function, not ExpectItExecutor)
|
|
567
|
+
* ```
|
|
568
|
+
*
|
|
569
|
+
* @group Schema
|
|
570
|
+
*/
|
|
571
|
+
export const SatisfyPatternSchema: z.ZodType<SatisfyPatternValue> = z
|
|
572
|
+
.lazy(() =>
|
|
573
|
+
z.union([
|
|
574
|
+
// Primitive types
|
|
575
|
+
z.string(),
|
|
576
|
+
z.number(),
|
|
577
|
+
z.boolean(),
|
|
578
|
+
z.null(),
|
|
579
|
+
z.undefined(),
|
|
580
|
+
z.bigint(),
|
|
581
|
+
z.symbol(),
|
|
582
|
+
|
|
583
|
+
// Special types for "to satisfy" semantics
|
|
584
|
+
z.instanceof(RegExp), // For pattern matching
|
|
585
|
+
z.custom<ExpectItExecutor<any>>(isExpectItExecutor, {
|
|
586
|
+
message:
|
|
587
|
+
'Expected an ExpectItExecutor function (created with expect.it())',
|
|
588
|
+
}), // For nested assertions
|
|
589
|
+
|
|
590
|
+
// Recursive structures
|
|
591
|
+
z.array(SatisfyPatternSchema), // Arrays of satisfy patterns
|
|
592
|
+
z.record(z.string(), SatisfyPatternSchema), // Objects with satisfy pattern values
|
|
593
|
+
]),
|
|
594
|
+
)
|
|
595
|
+
.register(BupkisRegistry, { name: 'SatisfyPatternSchema' });
|
package/src/types.ts
CHANGED
|
@@ -53,6 +53,8 @@ import type {
|
|
|
53
53
|
RawAssertionImplSchemaSync,
|
|
54
54
|
} from './assertion/assertion-types.js';
|
|
55
55
|
|
|
56
|
+
import { type kExpectIt } from './constant.js';
|
|
57
|
+
|
|
56
58
|
/**
|
|
57
59
|
* Creates a negated version of a tuple of
|
|
58
60
|
* {@link AssertionPart | AssertionParts}.
|
|
@@ -110,8 +112,6 @@ export interface BaseExpect {
|
|
|
110
112
|
fail: FailFn;
|
|
111
113
|
}
|
|
112
114
|
|
|
113
|
-
export type * from './assertion/assertion-types.js';
|
|
114
|
-
|
|
115
115
|
/**
|
|
116
116
|
* The main API as returned by a {@link UseFn}.
|
|
117
117
|
*
|
|
@@ -166,6 +166,8 @@ export interface Bupkis<
|
|
|
166
166
|
>;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
+
export type * from './assertion/assertion-types.js';
|
|
170
|
+
|
|
169
171
|
/**
|
|
170
172
|
* Helper type to concatenate two tuples
|
|
171
173
|
*/
|
|
@@ -269,14 +271,7 @@ export interface CreateAsyncAssertionFn {
|
|
|
269
271
|
parts: Parts,
|
|
270
272
|
impl: Impl,
|
|
271
273
|
): AssertionFunctionAsync<Parts, Impl, Slots>;
|
|
272
|
-
}
|
|
273
|
-
* @template BaseSyncAssertions Base set of synchronous
|
|
274
|
-
* {@link Assertion | Assertions}; will be the builtin sync assertions, at
|
|
275
|
-
* minimum)
|
|
276
|
-
* @template BaseAsyncAssertions Base set of asynchronous
|
|
277
|
-
* {@link Assertion | Assertions}; will be the builtin async assertions, at
|
|
278
|
-
* minimum)
|
|
279
|
-
*/
|
|
274
|
+
}
|
|
280
275
|
|
|
281
276
|
/**
|
|
282
277
|
* The main synchronous assertion function.
|
|
@@ -405,6 +400,125 @@ export type ExpectFunction<
|
|
|
405
400
|
}>
|
|
406
401
|
>;
|
|
407
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Creates embeddable assertion functions that can be used with `to satisfy`.
|
|
405
|
+
*
|
|
406
|
+
* This type generates a union of all possible `expect.it` function signatures
|
|
407
|
+
* based on the available synchronous assertions. Each assertion contributes its
|
|
408
|
+
* own function signature to create embeddable executors that can be used within
|
|
409
|
+
* object patterns for complex validation scenarios.
|
|
410
|
+
*
|
|
411
|
+
* The resulting functions are designed to be used exclusively within `'to
|
|
412
|
+
* satisfy'` assertion contexts, where they provide type-safe pattern matching
|
|
413
|
+
* for nested object structures. Direct execution of these functions outside of
|
|
414
|
+
* their intended context is not supported.
|
|
415
|
+
*
|
|
416
|
+
* @example
|
|
417
|
+
*
|
|
418
|
+
* ```typescript
|
|
419
|
+
* // Create embeddable assertion functions
|
|
420
|
+
* const isString = expect.it('to be a string');
|
|
421
|
+
* const isPositive = expect.it('to be greater than', 0);
|
|
422
|
+
*
|
|
423
|
+
* // Use within 'to satisfy' patterns
|
|
424
|
+
* expect(user, 'to satisfy', {
|
|
425
|
+
* name: isString,
|
|
426
|
+
* age: isPositive,
|
|
427
|
+
* email: /\S+@\S+/,
|
|
428
|
+
* });
|
|
429
|
+
* ```
|
|
430
|
+
*
|
|
431
|
+
* @template SyncAssertions - Array of synchronous assertion objects that define
|
|
432
|
+
* the available assertion logic for embeddable functions
|
|
433
|
+
* @see {@link ExpectItFunction} for individual function signature generation
|
|
434
|
+
* @see {@link ExpectItExecutor} for the executor function interface
|
|
435
|
+
*/
|
|
436
|
+
export type ExpectIt<
|
|
437
|
+
SyncAssertions extends AnySyncAssertions = BuiltinSyncAssertions,
|
|
438
|
+
> = UnionToIntersection<
|
|
439
|
+
TupleToUnion<{
|
|
440
|
+
[K in keyof SyncAssertions]: SyncAssertions[K] extends AnySyncAssertion
|
|
441
|
+
? SyncAssertions[K]['parts'] extends AssertionParts
|
|
442
|
+
? ExpectItFunction<SyncAssertions[K]['parts']>
|
|
443
|
+
: never
|
|
444
|
+
: never;
|
|
445
|
+
}>
|
|
446
|
+
>;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Interface for executor functions created by `expect.it()`.
|
|
450
|
+
*
|
|
451
|
+
* ExpectItExecutor functions are the result of calling `expect.it()` with
|
|
452
|
+
* assertion parameters. They encapsulate the assertion logic and can be
|
|
453
|
+
* executed later within `'to satisfy'` pattern matching contexts. These
|
|
454
|
+
* functions are marked with an internal symbol to distinguish them from regular
|
|
455
|
+
* functions during pattern validation.
|
|
456
|
+
*
|
|
457
|
+
* The executor accepts a subject value and performs the embedded assertion
|
|
458
|
+
* logic against it. The subject type is constrained by the Zod schema that
|
|
459
|
+
* represents the first part of the assertion definition, ensuring type safety
|
|
460
|
+
* during pattern matching.
|
|
461
|
+
*
|
|
462
|
+
* @example
|
|
463
|
+
*
|
|
464
|
+
* ```typescript
|
|
465
|
+
* const isStringExecutor = expect.it('to be a string');
|
|
466
|
+
* // isStringExecutor is an ExpectItExecutor<z.ZodString>
|
|
467
|
+
*
|
|
468
|
+
* // Used within satisfy patterns
|
|
469
|
+
* expect({ name: 'Alice' }, 'to satisfy', {
|
|
470
|
+
* name: isStringExecutor, // Validates that name is a string
|
|
471
|
+
* });
|
|
472
|
+
* ```
|
|
473
|
+
*
|
|
474
|
+
* @template Subject - The Zod schema type that constrains the subject parameter
|
|
475
|
+
* @see {@link ExpectItFunction} for the factory function that creates executors
|
|
476
|
+
*/
|
|
477
|
+
export interface ExpectItExecutor<Subject extends z.ZodType> {
|
|
478
|
+
(subject: z.infer<Subject>): void;
|
|
479
|
+
[kExpectIt]: true;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Function signature for creating ExpectItExecutor instances.
|
|
484
|
+
*
|
|
485
|
+
* This type represents the factory function that creates embeddable assertion
|
|
486
|
+
* executors from assertion parts. It takes the assertion parameters (excluding
|
|
487
|
+
* the subject) and returns an executor function that can be embedded within
|
|
488
|
+
* `'to satisfy'` patterns.
|
|
489
|
+
*
|
|
490
|
+
* The function signature is derived from assertion parts by removing the first
|
|
491
|
+
* part (which becomes the subject type for the executor) and using the
|
|
492
|
+
* remaining parts as parameters. This allows for natural language assertion
|
|
493
|
+
* creation that mirrors the main `expect()` function but produces reusable
|
|
494
|
+
* executor functions.
|
|
495
|
+
*
|
|
496
|
+
* The resulting executor is constrained to only work with subjects that match
|
|
497
|
+
* the first assertion part, providing compile-time type safety for pattern
|
|
498
|
+
* matching scenarios.
|
|
499
|
+
*
|
|
500
|
+
* @example
|
|
501
|
+
*
|
|
502
|
+
* ```typescript
|
|
503
|
+
* // For assertion parts: [z.string(), 'to match', z.instanceof(RegExp)]
|
|
504
|
+
* // Results in function: (pattern: RegExp) => ExpectItExecutor<z.ZodString>
|
|
505
|
+
* const matchesPattern = expect.it('to match', /^[A-Z]/);
|
|
506
|
+
*
|
|
507
|
+
* expect({ code: 'ABC123' }, 'to satisfy', {
|
|
508
|
+
* code: matchesPattern, // Validates that code matches the pattern
|
|
509
|
+
* });
|
|
510
|
+
* ```
|
|
511
|
+
*
|
|
512
|
+
* @template Parts - Tuple of assertion parts that define the function signature
|
|
513
|
+
* and executor constraints
|
|
514
|
+
* @see {@link ExpectItExecutor} for the returned executor interface
|
|
515
|
+
* @see {@link TupleTail} for parameter extraction from assertion parts
|
|
516
|
+
* @see {@link SlotsFromParts} for type slot generation
|
|
517
|
+
*/
|
|
518
|
+
export type ExpectItFunction<Parts extends AssertionParts> = (
|
|
519
|
+
...args: MutableOrReadonly<TupleTail<SlotsFromParts<Parts>>>
|
|
520
|
+
) => Parts[0] extends z.ZodType ? ExpectItExecutor<Parts[0]> : never;
|
|
521
|
+
|
|
408
522
|
/**
|
|
409
523
|
* Properties of {@link expect}.
|
|
410
524
|
*/
|
|
@@ -419,6 +533,8 @@ export interface ExpectSyncProps<
|
|
|
419
533
|
*/
|
|
420
534
|
assertions: SyncAssertions;
|
|
421
535
|
|
|
536
|
+
it: ExpectIt<SyncAssertions>;
|
|
537
|
+
|
|
422
538
|
/**
|
|
423
539
|
* Function to add more assertions to this `expect()`, returning a new
|
|
424
540
|
* `expect()` and `expectAsync()` pair with the combined assertions.
|
|
@@ -607,6 +723,23 @@ export type MutableOrReadonly<Tuple extends readonly unknown[]> =
|
|
|
607
723
|
*/
|
|
608
724
|
export type Negation<S extends string> = `not ${S}`;
|
|
609
725
|
|
|
726
|
+
export type SatisfyPatternValue =
|
|
727
|
+
| bigint
|
|
728
|
+
| boolean
|
|
729
|
+
| ExpectItExecutor<any>
|
|
730
|
+
| Map<SatisfyPatternValue, SatisfyPatternValue>
|
|
731
|
+
| null
|
|
732
|
+
| number
|
|
733
|
+
| RegExp
|
|
734
|
+
| SatisfyPatternValue[]
|
|
735
|
+
| Set<SatisfyPatternValue>
|
|
736
|
+
| string
|
|
737
|
+
| symbol
|
|
738
|
+
| undefined
|
|
739
|
+
| WeakMap<Extract<SatisfyPatternValue, object>, SatisfyPatternValue>
|
|
740
|
+
| WeakSet<Extract<SatisfyPatternValue, object>>
|
|
741
|
+
| { [key: string]: SatisfyPatternValue };
|
|
742
|
+
|
|
610
743
|
/**
|
|
611
744
|
* Converts AssertionParts to complete function parameter types for expect
|
|
612
745
|
* functions.
|
|
@@ -653,6 +786,29 @@ export type SlotsFromParts<Parts extends AssertionParts> = NoNeverTuple<
|
|
|
653
786
|
: never
|
|
654
787
|
>;
|
|
655
788
|
|
|
789
|
+
/**
|
|
790
|
+
* Gets the tail (all elements except the first) of a tuple type.
|
|
791
|
+
*
|
|
792
|
+
* Unlike ArrayTail from type-fest, this preserves the tuple structure as a
|
|
793
|
+
* readonly tuple rather than converting to an array type.
|
|
794
|
+
*
|
|
795
|
+
* @example
|
|
796
|
+
*
|
|
797
|
+
* ```typescript
|
|
798
|
+
* type Example = TupleTail<readonly [string, number, boolean]>; // readonly [number, boolean]
|
|
799
|
+
* type Single = TupleTail<readonly [string]>; // readonly []
|
|
800
|
+
* type Empty = TupleTail<readonly []>; // readonly []
|
|
801
|
+
* ```
|
|
802
|
+
*
|
|
803
|
+
* @template T - The tuple type to get the tail of
|
|
804
|
+
*/
|
|
805
|
+
export type TupleTail<T extends readonly unknown[]> = T extends readonly [
|
|
806
|
+
unknown,
|
|
807
|
+
...infer Rest,
|
|
808
|
+
]
|
|
809
|
+
? Rest
|
|
810
|
+
: readonly [];
|
|
811
|
+
|
|
656
812
|
/**
|
|
657
813
|
* The type of a `use()` function.
|
|
658
814
|
*/
|
|
@@ -684,3 +840,42 @@ export interface UseFn<
|
|
|
684
840
|
ExtendedAsyncAssertions
|
|
685
841
|
>;
|
|
686
842
|
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Maps Zod `def.type` strings to their corresponding ZodType classes.
|
|
846
|
+
*
|
|
847
|
+
* This allows for type-safe discrimination of ZodTypes based on their internal
|
|
848
|
+
* `def.type` property in Zod v4.
|
|
849
|
+
*/
|
|
850
|
+
export interface ZodTypeMap {
|
|
851
|
+
any: z.ZodAny;
|
|
852
|
+
array: z.ZodArray<any>;
|
|
853
|
+
bigint: z.ZodBigInt;
|
|
854
|
+
boolean: z.ZodBoolean;
|
|
855
|
+
catch: z.ZodCatch<any>;
|
|
856
|
+
date: z.ZodDate;
|
|
857
|
+
default: z.ZodDefault<any>;
|
|
858
|
+
enum: z.ZodEnum<any>;
|
|
859
|
+
function: z.ZodFunction<any, any>;
|
|
860
|
+
lazy: z.ZodLazy<any>;
|
|
861
|
+
literal: z.ZodLiteral<any>;
|
|
862
|
+
map: z.ZodMap<any, any>;
|
|
863
|
+
never: z.ZodNever;
|
|
864
|
+
null: z.ZodNull;
|
|
865
|
+
nullable: z.ZodNullable<any>;
|
|
866
|
+
number: z.ZodNumber;
|
|
867
|
+
object: z.ZodObject<any>;
|
|
868
|
+
optional: z.ZodOptional<any>;
|
|
869
|
+
pipe: z.ZodPipe<any, any>;
|
|
870
|
+
promise: z.ZodPromise<any>;
|
|
871
|
+
readonly: z.ZodReadonly<any>;
|
|
872
|
+
record: z.ZodRecord<any, any>;
|
|
873
|
+
set: z.ZodSet<any>;
|
|
874
|
+
string: z.ZodString;
|
|
875
|
+
symbol: z.ZodSymbol;
|
|
876
|
+
tuple: z.ZodTuple<any>;
|
|
877
|
+
undefined: z.ZodUndefined;
|
|
878
|
+
union: z.ZodUnion<any>;
|
|
879
|
+
unknown: z.ZodUnknown;
|
|
880
|
+
void: z.ZodVoid;
|
|
881
|
+
}
|
package/src/use.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
type AnyAsyncAssertions,
|
|
6
6
|
type AnySyncAssertions,
|
|
7
7
|
} from './assertion/assertion-types.js';
|
|
8
|
+
import { kExpectIt } from './constant.js';
|
|
8
9
|
import {
|
|
9
10
|
createBaseExpect,
|
|
10
11
|
createExpectAsyncFunction,
|
|
@@ -12,11 +13,31 @@ import {
|
|
|
12
13
|
} from './expect.js';
|
|
13
14
|
import {
|
|
14
15
|
type Concat,
|
|
16
|
+
type ExpectIt,
|
|
15
17
|
type FilterAsyncAssertions,
|
|
16
18
|
type FilterSyncAssertions,
|
|
17
19
|
type UseFn,
|
|
18
20
|
} from './types.js';
|
|
19
21
|
|
|
22
|
+
const createExpectIt = <SyncAssertions extends AnySyncAssertions>(
|
|
23
|
+
expect: any,
|
|
24
|
+
): ExpectIt<SyncAssertions> => {
|
|
25
|
+
const expectIt = (...args: readonly unknown[]) => {
|
|
26
|
+
const func = Object.assign(
|
|
27
|
+
(subject: unknown) => {
|
|
28
|
+
const allArgs = [subject, ...args];
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
30
|
+
expect(...allArgs);
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
[kExpectIt]: true,
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
return func;
|
|
37
|
+
};
|
|
38
|
+
return expectIt as unknown as ExpectIt<SyncAssertions>;
|
|
39
|
+
};
|
|
40
|
+
|
|
20
41
|
export function createUse<
|
|
21
42
|
const T extends AnySyncAssertions,
|
|
22
43
|
const U extends AnyAsyncAssertions,
|
|
@@ -50,6 +71,7 @@ export function createUse<
|
|
|
50
71
|
const expect = Object.assign(
|
|
51
72
|
expectFunction,
|
|
52
73
|
createBaseExpect(allSyncAssertions, allAsyncAssertions, 'sync'),
|
|
74
|
+
{ it: createExpectIt(expectFunction) },
|
|
53
75
|
);
|
|
54
76
|
const expectAsync = Object.assign(
|
|
55
77
|
expectAsyncFunction,
|