bupkis 0.1.2 → 0.2.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.
Files changed (130) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +8 -8
  3. package/dist/commonjs/assertion/assertion-types.d.ts +33 -82
  4. package/dist/commonjs/assertion/assertion-types.d.ts.map +1 -1
  5. package/dist/commonjs/assertion/create.d.ts +5 -33
  6. package/dist/commonjs/assertion/create.d.ts.map +1 -1
  7. package/dist/commonjs/assertion/create.js +17 -6
  8. package/dist/commonjs/assertion/create.js.map +1 -1
  9. package/dist/commonjs/assertion/impl/async.d.ts +5 -5
  10. package/dist/commonjs/assertion/impl/async.d.ts.map +1 -1
  11. package/dist/commonjs/assertion/impl/async.js +2 -2
  12. package/dist/commonjs/assertion/impl/async.js.map +1 -1
  13. package/dist/commonjs/assertion/impl/sync-parametric.d.ts +24 -24
  14. package/dist/commonjs/assertion/impl/sync-parametric.d.ts.map +1 -1
  15. package/dist/commonjs/assertion/impl/sync-parametric.js +3 -3
  16. package/dist/commonjs/assertion/impl/sync-parametric.js.map +1 -1
  17. package/dist/commonjs/assertion/impl/sync.d.ts +50 -50
  18. package/dist/commonjs/assertion/impl/sync.d.ts.map +1 -1
  19. package/dist/commonjs/bootstrap.d.ts +71 -61
  20. package/dist/commonjs/bootstrap.d.ts.map +1 -1
  21. package/dist/commonjs/bootstrap.js +20 -10
  22. package/dist/commonjs/bootstrap.js.map +1 -1
  23. package/dist/commonjs/error.d.ts +12 -5
  24. package/dist/commonjs/error.d.ts.map +1 -1
  25. package/dist/commonjs/error.js +19 -4
  26. package/dist/commonjs/error.js.map +1 -1
  27. package/dist/commonjs/expect.d.ts +130 -3
  28. package/dist/commonjs/expect.d.ts.map +1 -1
  29. package/dist/commonjs/expect.js +115 -0
  30. package/dist/commonjs/expect.js.map +1 -1
  31. package/dist/commonjs/guards.d.ts +22 -15
  32. package/dist/commonjs/guards.d.ts.map +1 -1
  33. package/dist/commonjs/guards.js +34 -20
  34. package/dist/commonjs/guards.js.map +1 -1
  35. package/dist/commonjs/index.d.ts +113 -62
  36. package/dist/commonjs/index.d.ts.map +1 -1
  37. package/dist/commonjs/index.js +44 -42
  38. package/dist/commonjs/index.js.map +1 -1
  39. package/dist/commonjs/metadata.d.ts +1 -27
  40. package/dist/commonjs/metadata.d.ts.map +1 -1
  41. package/dist/commonjs/metadata.js +16 -15
  42. package/dist/commonjs/metadata.js.map +1 -1
  43. package/dist/commonjs/schema.d.ts +59 -26
  44. package/dist/commonjs/schema.d.ts.map +1 -1
  45. package/dist/commonjs/schema.js +57 -24
  46. package/dist/commonjs/schema.js.map +1 -1
  47. package/dist/commonjs/types.d.ts +449 -39
  48. package/dist/commonjs/types.d.ts.map +1 -1
  49. package/dist/commonjs/types.js +12 -2
  50. package/dist/commonjs/types.js.map +1 -1
  51. package/dist/commonjs/util.d.ts +7 -0
  52. package/dist/commonjs/util.d.ts.map +1 -1
  53. package/dist/commonjs/util.js +7 -0
  54. package/dist/commonjs/util.js.map +1 -1
  55. package/dist/esm/assertion/assertion-types.d.ts +33 -82
  56. package/dist/esm/assertion/assertion-types.d.ts.map +1 -1
  57. package/dist/esm/assertion/create.d.ts +5 -33
  58. package/dist/esm/assertion/create.d.ts.map +1 -1
  59. package/dist/esm/assertion/create.js +14 -4
  60. package/dist/esm/assertion/create.js.map +1 -1
  61. package/dist/esm/assertion/impl/async.d.ts +5 -5
  62. package/dist/esm/assertion/impl/async.d.ts.map +1 -1
  63. package/dist/esm/assertion/impl/async.js +2 -2
  64. package/dist/esm/assertion/impl/async.js.map +1 -1
  65. package/dist/esm/assertion/impl/sync-parametric.d.ts +24 -24
  66. package/dist/esm/assertion/impl/sync-parametric.d.ts.map +1 -1
  67. package/dist/esm/assertion/impl/sync-parametric.js +3 -3
  68. package/dist/esm/assertion/impl/sync-parametric.js.map +1 -1
  69. package/dist/esm/assertion/impl/sync.d.ts +50 -50
  70. package/dist/esm/assertion/impl/sync.d.ts.map +1 -1
  71. package/dist/esm/bootstrap.d.ts +71 -61
  72. package/dist/esm/bootstrap.d.ts.map +1 -1
  73. package/dist/esm/bootstrap.js +19 -9
  74. package/dist/esm/bootstrap.js.map +1 -1
  75. package/dist/esm/error.d.ts +12 -5
  76. package/dist/esm/error.d.ts.map +1 -1
  77. package/dist/esm/error.js +19 -4
  78. package/dist/esm/error.js.map +1 -1
  79. package/dist/esm/expect.d.ts +130 -3
  80. package/dist/esm/expect.d.ts.map +1 -1
  81. package/dist/esm/expect.js +115 -0
  82. package/dist/esm/expect.js.map +1 -1
  83. package/dist/esm/guards.d.ts +22 -15
  84. package/dist/esm/guards.d.ts.map +1 -1
  85. package/dist/esm/guards.js +29 -13
  86. package/dist/esm/guards.js.map +1 -1
  87. package/dist/esm/index.d.ts +113 -62
  88. package/dist/esm/index.d.ts.map +1 -1
  89. package/dist/esm/index.js +46 -7
  90. package/dist/esm/index.js.map +1 -1
  91. package/dist/esm/metadata.d.ts +1 -27
  92. package/dist/esm/metadata.d.ts.map +1 -1
  93. package/dist/esm/metadata.js +2 -1
  94. package/dist/esm/metadata.js.map +1 -1
  95. package/dist/esm/schema.d.ts +59 -26
  96. package/dist/esm/schema.d.ts.map +1 -1
  97. package/dist/esm/schema.js +57 -24
  98. package/dist/esm/schema.js.map +1 -1
  99. package/dist/esm/types.d.ts +449 -39
  100. package/dist/esm/types.d.ts.map +1 -1
  101. package/dist/esm/types.js +12 -2
  102. package/dist/esm/types.js.map +1 -1
  103. package/dist/esm/util.d.ts +7 -0
  104. package/dist/esm/util.d.ts.map +1 -1
  105. package/dist/esm/util.js +7 -0
  106. package/dist/esm/util.js.map +1 -1
  107. package/package.json +89 -15
  108. package/src/assertion/assertion-types.ts +38 -41
  109. package/src/assertion/create.ts +16 -65
  110. package/src/assertion/impl/async.ts +2 -2
  111. package/src/assertion/impl/sync-parametric.ts +3 -3
  112. package/src/bootstrap.ts +21 -10
  113. package/src/error.ts +21 -4
  114. package/src/expect.ts +269 -18
  115. package/src/guards.ts +29 -51
  116. package/src/index.ts +71 -11
  117. package/src/metadata.ts +3 -4
  118. package/src/schema.ts +59 -26
  119. package/src/types.ts +593 -72
  120. package/src/util.ts +7 -0
  121. package/dist/commonjs/api.d.ts +0 -93
  122. package/dist/commonjs/api.d.ts.map +0 -1
  123. package/dist/commonjs/api.js +0 -8
  124. package/dist/commonjs/api.js.map +0 -1
  125. package/dist/esm/api.d.ts +0 -93
  126. package/dist/esm/api.d.ts.map +0 -1
  127. package/dist/esm/api.js +0 -7
  128. package/dist/esm/api.js.map +0 -1
  129. package/src/api.ts +0 -149
  130. package/src/schema.md +0 -15
package/src/schema.ts CHANGED
@@ -1,14 +1,34 @@
1
1
  /**
2
- * Zod schema definitions for common types and validation patterns.
2
+ * Arguably-useful Zod schemas for common types and validation patterns.
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 Zod's
7
- * limitations.
6
+ * throughout the assertion system. These tend to work around the impedence
7
+ * mismatch between **BUPKIS** and Zod.
8
8
  *
9
9
  * These are used internally, but consumers may also find them useful.
10
10
  *
11
- * @document schema.md
11
+ * For example, we have {@link FunctionSchema} which accepts any
12
+ * function—regardless of its signature. We need this because Zod v4's
13
+ * `z.function()` no longer returns a `ZodType` (ref:
14
+ * {@link https://zod.dev/v4/changelog | Zod v4 Migration Guide}) and so behaves
15
+ * differently. `FunctionSchema` allows us to work with functions as _values_
16
+ * instead of something to be implemented.
17
+ *
18
+ * Similarly—but not a new development—`z.promise()` does not parse a
19
+ * {@link Promise} object; it parses the _fulfilled value_. This is not what we
20
+ * want for "is a Promise" assertions, but it _can_ be useful for making sense
21
+ * of the fulfilled value. To solve this, we have
22
+ * {@link WrappedPromiseLikeSchema} (which explicitly supports
23
+ * {@link PromiseLike}/"thenable" objects).
24
+ *
25
+ * @category API
26
+ * @example
27
+ *
28
+ * ```ts
29
+ * import * as schema from 'bupkis/schema';
30
+ * ```
31
+ *
12
32
  * @packageDocumentation
13
33
  */
14
34
 
@@ -22,7 +42,7 @@ import {
22
42
  isPromiseLike,
23
43
  } from './guards.js';
24
44
  import { BupkisRegistry } from './metadata.js';
25
- import { type Constructor } from './types.js';
45
+ import { type Constructor, type MutableOrReadonly } from './types.js';
26
46
 
27
47
  /**
28
48
  * A Zod schema that validates JavaScript classes or constructor functions.
@@ -32,10 +52,9 @@ import { type Constructor } from './types.js';
32
52
  * uses the {@link isConstructable} guard function to determine if a value can be
33
53
  * invoked with the `new` operator to create object instances.
34
54
  *
35
- * @remarks
55
+ * @privateRemarks
36
56
  * The schema is registered in the {@link BupkisRegistry} with the name
37
57
  * `ClassSchema` for later reference and type checking purposes.
38
- * @category Schema
39
58
  * @example
40
59
  *
41
60
  * ```typescript
@@ -49,6 +68,8 @@ import { type Constructor } from './types.js';
49
68
  * ClassSchema.parse(() => {}); // ✗ Throws validation error
50
69
  * ClassSchema.parse({}); // ✗ Throws validation error
51
70
  * ```
71
+ *
72
+ * @group Schema
52
73
  */
53
74
 
54
75
  export const ClassSchema = z
@@ -65,10 +86,9 @@ export const ClassSchema = z
65
86
  * including regular functions, arrow functions, async functions, generator
66
87
  * functions, and methods.
67
88
  *
68
- * @remarks
89
+ * @privateRemarks
69
90
  * The schema is registered in the {@link BupkisRegistry} with the name
70
91
  * `FunctionSchema` for later reference and type checking purposes.
71
- * @category Schema
72
92
  * @example
73
93
  *
74
94
  * ```typescript
@@ -80,9 +100,11 @@ export const ClassSchema = z
80
100
  * FunctionSchema.parse('not a function'); // ✗ Throws validation error
81
101
  * FunctionSchema.parse({}); // ✗ Throws validation error
82
102
  * ```
103
+ *
104
+ * @group Schema
83
105
  */
84
106
  export const FunctionSchema = z
85
- .custom<(...args: any[]) => any>(isFunction)
107
+ .custom<(...args: MutableOrReadonly<unknown[]>) => unknown>(isFunction)
86
108
  .register(BupkisRegistry, {
87
109
  name: 'FunctionSchema',
88
110
  })
@@ -98,10 +120,9 @@ export const FunctionSchema = z
98
120
  * types that JavaScript automatically converts to property keys when used in
99
121
  * object access or assignment operations.
100
122
  *
101
- * @remarks
123
+ * @privateRemarks
102
124
  * The schema is registered in the `BupkisRegistry` with the name
103
125
  * `PropertyKeySchema` for later reference and type checking purposes.
104
- * @category Schema
105
126
  * @example
106
127
  *
107
128
  * ```typescript
@@ -111,6 +132,8 @@ export const FunctionSchema = z
111
132
  * PropertyKeySchema.parse({}); // ✗ Throws validation error
112
133
  * PropertyKeySchema.parse(null); // ✗ Throws validation error
113
134
  * ```
135
+ *
136
+ * @group Schema
114
137
  */
115
138
  export const PropertyKeySchema = z
116
139
  .union([z.string(), z.number(), z.symbol()])
@@ -126,12 +149,11 @@ export const PropertyKeySchema = z
126
149
  * resolved value, meaning the result of parsing remains a Promise or thenable
127
150
  * object.
128
151
  *
129
- * @remarks
152
+ * @privateRemarks
130
153
  * The schema is registered in the `BupkisRegistry` with the name
131
154
  * `WrappedPromiseLikeSchema` for later reference and type checking purposes.
132
155
  * This is useful when you need to validate that something is thenable without
133
156
  * automatically resolving it.
134
- * @category Schema
135
157
  * @example
136
158
  *
137
159
  * ```typescript
@@ -140,6 +162,8 @@ export const PropertyKeySchema = z
140
162
  * WrappedPromiseLikeSchema.parse(42); // ✗ Throws validation error
141
163
  * WrappedPromiseLikeSchema.parse({}); // ✗ Throws validation error
142
164
  * ```
165
+ *
166
+ * @group Schema
143
167
  */
144
168
  export const WrappedPromiseLikeSchema = z
145
169
  .custom<PromiseLike<unknown>>((value) => isPromiseLike(value))
@@ -159,7 +183,6 @@ export const WrappedPromiseLikeSchema = z
159
183
  * @remarks
160
184
  * The schema is registered in the `BupkisRegistry` with the name
161
185
  * `StrongMapSchema` for later reference and type checking purposes.
162
- * @category Schema
163
186
  * @example
164
187
  *
165
188
  * ```typescript
@@ -172,6 +195,8 @@ export const WrappedPromiseLikeSchema = z
172
195
  * const weakMap = new WeakMap();
173
196
  * StrongMapSchema.parse(weakMap); // ✗ Throws validation error
174
197
  * ```
198
+ *
199
+ * @group Schema
175
200
  */
176
201
  export const StrongMapSchema = z
177
202
  .instanceof(Map)
@@ -190,7 +215,6 @@ export const StrongMapSchema = z
190
215
  * @remarks
191
216
  * The schema is registered in the `BupkisRegistry` with the name
192
217
  * `StrongSetSchema` for later reference and type checking purposes.
193
- * @category Schema
194
218
  * @example
195
219
  *
196
220
  * ```typescript
@@ -200,6 +224,8 @@ export const StrongMapSchema = z
200
224
  * const weakSet = new WeakSet();
201
225
  * StrongSetSchema.parse(weakSet); // ✗ Throws validation error
202
226
  * ```
227
+ *
228
+ * @group Schema
203
229
  */
204
230
  export const StrongSetSchema = z
205
231
  .instanceof(Set)
@@ -219,7 +245,6 @@ export const StrongSetSchema = z
219
245
  * @remarks
220
246
  * The schema is registered in the `BupkisRegistry` with the name
221
247
  * `ObjectWithNullPrototype` for later reference and type checking purposes.
222
- * @category Schema
223
248
  * @example
224
249
  *
225
250
  * ```typescript
@@ -233,6 +258,8 @@ export const StrongSetSchema = z
233
258
  * const emptyObj = {};
234
259
  * NullProtoObjectSchema.parse(emptyObj); // ✗ Throws validation error
235
260
  * ```
261
+ *
262
+ * @group Schema
236
263
  */
237
264
  export const NullProtoObjectSchema = z
238
265
  .custom<Record<PropertyKey, unknown>>(
@@ -255,7 +282,6 @@ export const NullProtoObjectSchema = z
255
282
  * schema cannot reliably detect functions that return Promises but are not
256
283
  * declared with `async`, as this determination requires static analysis that is
257
284
  * not available at runtime.
258
- * @category Schema
259
285
  * @example
260
286
  *
261
287
  * ```typescript
@@ -275,6 +301,8 @@ export const NullProtoObjectSchema = z
275
301
  * const regularFn = () => 42;
276
302
  * AsyncFunctionSchema.parse(regularFn); // ✗ Throws validation error
277
303
  * ```
304
+ *
305
+ * @group Schema
278
306
  */
279
307
  export const AsyncFunctionSchema = FunctionSchema.refine(
280
308
  (value) => Object.prototype.toString.call(value) === '[object AsyncFunction]',
@@ -293,7 +321,6 @@ export const AsyncFunctionSchema = FunctionSchema.refine(
293
321
  * @remarks
294
322
  * The schema is registered in the `BupkisRegistry` with the name `Truthy` and
295
323
  * indicates that it accepts anything as valid input for evaluation.
296
- * @category Schema
297
324
  * @example
298
325
  *
299
326
  * ```typescript
@@ -307,6 +334,8 @@ export const AsyncFunctionSchema = FunctionSchema.refine(
307
334
  * TruthySchema.parse(''); // ✗ Throws validation error
308
335
  * TruthySchema.parse(null); // ✗ Throws validation error
309
336
  * ```
337
+ *
338
+ * @group Schema
310
339
  */
311
340
  export const TruthySchema = z
312
341
  .any()
@@ -328,7 +357,6 @@ export const TruthySchema = z
328
357
  * @remarks
329
358
  * The schema is registered in the `BupkisRegistry` with the name `Falsy` and
330
359
  * indicates that it accepts anything as valid input for evaluation.
331
- * @category Schema
332
360
  * @example
333
361
  *
334
362
  * ```typescript
@@ -345,6 +373,8 @@ export const TruthySchema = z
345
373
  * FalsySchema.parse('hello'); // ✗ Throws validation error
346
374
  * FalsySchema.parse({}); // ✗ Throws validation error
347
375
  * ```
376
+ *
377
+ * @group Schema
348
378
  */
349
379
  export const FalsySchema = z
350
380
  .any()
@@ -365,7 +395,6 @@ export const FalsySchema = z
365
395
  * @remarks
366
396
  * The schema is registered in the `BupkisRegistry` with the name `Primitive`
367
397
  * and indicates that it accepts primitive values as valid input.
368
- * @category Schema
369
398
  * @example
370
399
  *
371
400
  * ```typescript
@@ -380,6 +409,8 @@ export const FalsySchema = z
380
409
  * PrimitiveSchema.parse([]); // ✗ Throws validation error (array)
381
410
  * PrimitiveSchema.parse(() => {}); // ✗ Throws validation error (function)
382
411
  * ```
412
+ *
413
+ * @group Schema
383
414
  */
384
415
  export const PrimitiveSchema = z
385
416
  .union([
@@ -404,12 +435,11 @@ export const PrimitiveSchema = z
404
435
  * it useful for validating collections where the specific array mutability or
405
436
  * tuple structure is not critical.
406
437
  *
407
- * @remarks
438
+ * @privateRemarks
408
439
  * The schema is registered in the {@link BupkisRegistry} with the name
409
440
  * `ArrayLike` for later reference and type checking purposes. This schema is
410
441
  * particularly useful when you need to accept various forms of array-like data
411
442
  * without being restrictive about mutability or exact tuple structure.
412
- * @category Schema
413
443
  * @example
414
444
  *
415
445
  * ```typescript
@@ -421,11 +451,13 @@ export const PrimitiveSchema = z
421
451
  * ArrayLikeSchema.parse({}); // ✗ Throws validation error
422
452
  * ArrayLikeSchema.parse(null); // ✗ Throws validation error
423
453
  * ```
454
+ *
455
+ * @group Schema
424
456
  */
425
457
  export const ArrayLikeSchema = z
426
458
  .union([
427
- z.array(z.any()),
428
- z.tuple([z.any()], z.any()),
459
+ z.array(z.unknown()),
460
+ z.tuple([z.unknown()], z.unknown()),
429
461
  z.looseObject({ length: z.number().nonnegative().int() }),
430
462
  ])
431
463
  .describe('Array-like value')
@@ -445,7 +477,6 @@ export const ArrayLikeSchema = z
445
477
  * @remarks
446
478
  * The schema is registered in the `BupkisRegistry` with the name `RegExp` for
447
479
  * later reference and type checking purposes.
448
- * @category Schema
449
480
  * @example
450
481
  *
451
482
  * ```typescript
@@ -457,6 +488,8 @@ export const ArrayLikeSchema = z
457
488
  * RegExpSchema.parse(/abc/.source); // ✗ Throws validation error (string pattern)
458
489
  * RegExpSchema.parse({}); // ✗ Throws validation error (object)
459
490
  * ```
491
+ *
492
+ * @group Schema
460
493
  */
461
494
  export const RegExpSchema = z
462
495
  .instanceof(RegExp)