decoders 2.3.0-test3 → 2.3.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/dist/index.d.cts CHANGED
@@ -1,8 +1,6 @@
1
1
  interface ObjectAnnotation {
2
2
  readonly type: 'object';
3
- readonly fields: {
4
- readonly [key: string]: Annotation;
5
- };
3
+ readonly fields: ReadonlyMap<string, Annotation>;
6
4
  readonly text?: string;
7
5
  }
8
6
  interface ArrayAnnotation {
@@ -55,8 +53,12 @@ declare function ok<T>(value: T): Ok<T>;
55
53
  */
56
54
  declare function err<E>(error: E): Err<E>;
57
55
 
58
- type Scalar = string | number | boolean | symbol | undefined | null;
59
56
  type DecodeResult<T> = Result<T, Annotation>;
57
+ /**
58
+ * A function taking a untrusted input, and returning a DecodeResult<T>. The
59
+ * `ok()` and `err()` constructor functions are provided as the 2nd and 3rd
60
+ * param. One of these should be called and its value returned.
61
+ */
60
62
  type AcceptanceFn<T, InputT = unknown> = (blob: InputT, ok: (value: T) => DecodeResult<T>, err: (msg: string | Annotation) => DecodeResult<T>) => DecodeResult<T>;
61
63
  interface Decoder<T> {
62
64
  /**
@@ -94,24 +96,37 @@ interface Decoder<T> {
94
96
  */
95
97
  describe(message: string): Decoder<T>;
96
98
  /**
97
- * Chain together the current decoder with another acceptance function.
99
+ * Send the output of the current decoder into another acceptance function.
100
+ * The given acceptance function will receive the output of the current
101
+ * decoder as its input, making it partially trusted.
102
+ *
103
+ * This works similar to how you would `define()` a new decoder, except
104
+ * that the ``blob`` param will now be ``T`` (a known type), rather than
105
+ * ``unknown``. This will allow the function to make a stronger assumption
106
+ * about its input and avoid re-refining inputs.
107
+ *
108
+ * > _**NOTE:** This is an advanced, low-level, API. It's not recommended
109
+ * > to reach for this construct unless there is no other way. Most cases can
110
+ * > be covered more elegantly by `.transform()` or `.refine()` instead._
111
+ *
112
+ * If it helps, you can think of `define(...)` as equivalent to
113
+ * `unknown.then(...)`.
98
114
  */
99
115
  then<V>(next: AcceptanceFn<V, T>): Decoder<V>;
100
- peek_UNSTABLE<V>(next: AcceptanceFn<V, [unknown, T]>): Decoder<V>;
101
116
  }
102
117
  /**
103
- * Helper type to return the "type" of a Decoder.
118
+ * Helper type to return the output type of a Decoder.
119
+ * It’s the inverse of Decoder<T>.
104
120
  *
105
- * You can use it on types:
121
+ * You can use it at the type level:
106
122
  *
107
123
  * DecoderType<Decoder<string>> // string
108
124
  * DecoderType<Decoder<number[]>> // number[]
109
125
  *
110
- * Or on "values", by using the `typeof` keyword:
126
+ * Or on decoder instances, by using the `typeof` keyword:
111
127
  *
112
128
  * DecoderType<typeof string> // string
113
129
  * DecoderType<typeof truthy> // boolean
114
- *
115
130
  */
116
131
  type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : never;
117
132
  /**
@@ -129,36 +144,34 @@ type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : neve
129
144
  */
130
145
  declare function define<T>(fn: AcceptanceFn<T>): Decoder<T>;
131
146
 
132
- type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
133
- type JSONObject = {
134
- [key: string]: JSONValue | undefined;
135
- };
136
- type JSONArray = JSONValue[];
147
+ type Formatter = (err: Annotation) => string | Error;
148
+ declare function formatInline(ann: Annotation): string;
149
+ declare function formatShort(ann: Annotation): string;
150
+
137
151
  /**
138
- * Accepts objects that contain only valid JSON values.
152
+ * Accepts any array, but doesn't validate its items further.
153
+ *
154
+ * "poja" means "plain old JavaScript array", a play on `pojo()`.
139
155
  */
140
- declare const jsonObject: Decoder<JSONObject>;
156
+ declare const poja: Decoder<unknown[]>;
141
157
  /**
142
- * Accepts arrays that contain only valid JSON values.
158
+ * Accepts arrays of whatever the given decoder accepts.
143
159
  */
144
- declare const jsonArray: Decoder<JSONArray>;
160
+ declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
145
161
  /**
146
- * Accepts any value that's a valid JSON value.
147
- *
148
- * In other words: any value returned by `JSON.parse()` should decode without
149
- * failure.
150
- *
151
- * ```typescript
152
- * type JSONValue =
153
- * | null
154
- * | string
155
- * | number
156
- * | boolean
157
- * | { [string]: JSONValue }
158
- * | JSONValue[]
159
- * ```
162
+ * Like `array()`, but will reject arrays with 0 elements.
160
163
  */
161
- declare const json: Decoder<JSONValue>;
164
+ declare function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
165
+ type TupleDecoderType<Ds extends readonly Decoder<unknown>[]> = {
166
+ [K in keyof Ds]: DecoderType<Ds[K]>;
167
+ };
168
+ /**
169
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
170
+ * _n_ given decoders.
171
+ */
172
+ declare function tuple<Ds extends readonly [first: Decoder<unknown>, ...rest: readonly Decoder<unknown>[]]>(...decoders: Ds): Decoder<TupleDecoderType<Ds>>;
173
+
174
+ type Scalar = string | number | boolean | symbol | undefined | null;
162
175
 
163
176
  /**
164
177
  * Accepts and returns only the literal `null` value.
@@ -187,8 +200,9 @@ declare function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
187
200
  declare function nullable<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
188
201
  declare function nullable<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
189
202
  /**
190
- * @deprecated
191
- * Alias of nullish().
203
+ * @deprecated Will get removed in a future version.
204
+ *
205
+ * Alias of `nullish()`.
192
206
  */
193
207
  declare const maybe: typeof nullish;
194
208
  /**
@@ -218,12 +232,13 @@ declare function always<T>(value: (() => T) | T): Decoder<T>;
218
232
  */
219
233
  declare function never(msg: string): Decoder<never>;
220
234
  /**
221
- * Alias of never().
235
+ * Alias of `never()`.
222
236
  */
223
237
  declare const fail: typeof never;
224
238
  /**
225
- * @deprecated
226
- * Alias of always.
239
+ * Alias of `always()`.
240
+ *
241
+ * @deprecated Will get removed in a future version.
227
242
  */
228
243
  declare const hardcoded: typeof always;
229
244
  /**
@@ -235,53 +250,63 @@ declare const hardcoded: typeof always;
235
250
  */
236
251
  declare const unknown: Decoder<unknown>;
237
252
  /**
238
- * @deprecated
239
- * Alias of unknown.
253
+ * Alias of `unknown`.
254
+ *
255
+ * @deprecated Will get removed in a future version.
240
256
  */
241
257
  declare const mixed: Decoder<unknown>;
242
258
 
243
259
  /**
244
- * Accepts any array, but doesn't validate its items further.
245
- *
246
- * "poja" means "plain old JavaScript array", a play on `pojo()`.
260
+ * Accepts and returns booleans.
247
261
  */
248
- declare const poja: Decoder<unknown[]>;
262
+ declare const boolean: Decoder<boolean>;
249
263
  /**
250
- * Accepts arrays of whatever the given decoder accepts.
264
+ * Accepts anything and will return its "truth" value. Will never reject.
251
265
  */
252
- declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
266
+ declare const truthy: Decoder<boolean>;
253
267
  /**
254
- * Like `array()`, but will reject arrays with 0 elements.
268
+ * Accepts numbers, but return their boolean representation.
269
+ *
270
+ * @deprecated This decoder will be removed in a future version. You can use
271
+ * `truthy` to get almost the same effect.
255
272
  */
256
- declare function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
273
+ declare const numericBoolean: Decoder<boolean>;
274
+
257
275
  /**
258
- * Similar to `array()`, but returns the result as an [ES6
259
- * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
276
+ * Accepts objects where all values match the given decoder, and returns the
277
+ * result as a `Record<string, V>`.
260
278
  */
261
- declare function set<T>(decoder: Decoder<T>): Decoder<Set<T>>;
279
+ declare function record<V>(valueDecoder: Decoder<V>): Decoder<Record<string, V>>;
262
280
  /**
263
- * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
264
- * _n_ given decoders.
281
+ * Accepts objects where all keys and values match the given decoders, and
282
+ * returns the result as a `Record<K, V>`. The given key decoder must return
283
+ * strings.
265
284
  */
266
- declare function tuple<A>(a: Decoder<A>): Decoder<[A]>;
267
- declare function tuple<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
268
- declare function tuple<A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<[A, B, C]>;
269
- declare function tuple<A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<[A, B, C, D]>;
270
- declare function tuple<A, B, C, D, E>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>): Decoder<[A, B, C, D, E]>;
271
- declare function tuple<A, B, C, D, E, F>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>): Decoder<[A, B, C, D, E, F]>;
272
-
285
+ declare function record<K extends string, V>(keyDecoder: Decoder<K>, valueDecoder: Decoder<V>): Decoder<Record<K, V>>;
273
286
  /**
274
- * Accepts and returns booleans.
287
+ * @deprecated Will get removed in a future version.
288
+ *
289
+ * Alias of `record()`.
275
290
  */
276
- declare const boolean: Decoder<boolean>;
291
+ declare const dict: typeof record;
277
292
  /**
278
- * Accepts anything and will return its "truth" value. Will never reject.
293
+ * Similar to `array()`, but returns the result as an [ES6
294
+ * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
279
295
  */
280
- declare const truthy: Decoder<boolean>;
296
+ declare function setFromArray<T>(decoder: Decoder<T>): Decoder<Set<T>>;
281
297
  /**
282
- * Accepts numbers, but return their boolean representation.
298
+ * Renamed to `setFromArray` to make room for a future `set()` decoder that
299
+ * works differently.
300
+ *
301
+ * @deprecated This decoder will change behavior in a future version.
283
302
  */
284
- declare const numericBoolean: Decoder<boolean>;
303
+ declare const set: typeof setFromArray;
304
+ /**
305
+ * Similar to `record()`, but returns the result as a `Map<string, T>` (an [ES6
306
+ * Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
307
+ * instead.
308
+ */
309
+ declare function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
285
310
 
286
311
  /**
287
312
  * Accepts and returns `Date` instances.
@@ -295,10 +320,101 @@ declare const date: Decoder<Date>;
295
320
  * `.toISOString()` when sending, decode them with `iso8601` when receiving.
296
321
  */
297
322
  declare const iso8601: Decoder<Date>;
323
+ /**
324
+ * Accepts either a Date, or an ISO date string, returns a Date instance.
325
+ * This is commonly useful to build decoders that can be reused to validate
326
+ * object with Date instances as well as objects coming from JSON payloads.
327
+ */
328
+ declare const datelike: Decoder<Date>;
329
+
330
+ type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
331
+ type JSONObject = {
332
+ [key: string]: JSONValue | undefined;
333
+ };
334
+ type JSONArray = JSONValue[];
335
+ /**
336
+ * Accepts objects that contain only valid JSON values.
337
+ */
338
+ declare const jsonObject: Decoder<JSONObject>;
339
+ /**
340
+ * Accepts arrays that contain only valid JSON values.
341
+ */
342
+ declare const jsonArray: Decoder<JSONArray>;
343
+ /**
344
+ * Accepts any value that's a valid JSON value.
345
+ *
346
+ * In other words: any value returned by `JSON.parse()` should decode without
347
+ * failure.
348
+ *
349
+ * ```typescript
350
+ * type JSONValue =
351
+ * | null
352
+ * | string
353
+ * | number
354
+ * | boolean
355
+ * | { [string]: JSONValue }
356
+ * | JSONValue[]
357
+ * ```
358
+ */
359
+ declare const json: Decoder<JSONValue>;
360
+
361
+ interface Klass<T> extends Function {
362
+ new (...args: readonly any[]): T;
363
+ }
364
+ type Instance<K> = K extends Klass<infer T> ? T : never;
365
+ /**
366
+ * Accepts any value that is an ``instanceof`` the given class.
367
+ */
368
+ declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>>;
369
+ /**
370
+ * Lazily evaluate the given decoder. This is useful to build self-referential
371
+ * types for recursive data structures.
372
+ */
373
+ declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
374
+ /**
375
+ * Pre-process the data input before passing it into the decoder. This gives
376
+ * you the ability to arbitrarily customize the input on the fly before passing
377
+ * it to the decoder. Of course, the input value at that point is still of
378
+ * ``unknown`` type, so you will have to deal with that accordingly.
379
+ */
380
+ declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<T>): Decoder<T>;
381
+
382
+ /**
383
+ * Accepts any valid ``number`` value.
384
+ *
385
+ * This also accepts special values like `NaN` and `Infinity`. Unless you
386
+ * want to deliberately accept those, you'll likely want to use the
387
+ * `number` decoder instead.
388
+ */
389
+ declare const anyNumber: Decoder<number>;
390
+ /**
391
+ * Accepts finite numbers (can be integer or float values). Values `NaN`,
392
+ * or positive and negative `Infinity` will get rejected.
393
+ */
394
+ declare const number: Decoder<number>;
395
+ /**
396
+ * Accepts only finite whole numbers.
397
+ */
398
+ declare const integer: Decoder<number>;
399
+ /**
400
+ * Accepts only non-negative (zero or positive) finite numbers.
401
+ */
402
+ declare const positiveNumber: Decoder<number>;
403
+ /**
404
+ * Accepts only non-negative (zero or positive) finite whole numbers.
405
+ */
406
+ declare const positiveInteger: Decoder<number>;
407
+ /**
408
+ * Accepts any valid ``bigint`` value.
409
+ */
410
+ declare const bigint: Decoder<bigint>;
298
411
 
299
412
  type RequiredKeys<T extends object> = {
300
413
  [K in keyof T]: undefined extends T[K] ? never : K;
301
414
  }[keyof T];
415
+ type Resolve<T> = T extends (...args: readonly unknown[]) => unknown ? T : {
416
+ [K in keyof T]: T[K];
417
+ };
302
418
  /**
303
419
  * Transforms an object type, by marking all fields that contain "undefined"
304
420
  * with a question mark, i.e. allowing implicit-undefineds when
@@ -321,12 +437,8 @@ type RequiredKeys<T extends object> = {
321
437
  * }
322
438
  */
323
439
  type UndefinedToOptional<T extends object> = Resolve<Pick<Required<T>, RequiredKeys<T>> & Partial<T>>;
324
- type Resolve<T> = T extends (...args: readonly unknown[]) => unknown ? T : {
325
- [K in keyof T]: T[K];
326
- };
327
-
328
- type ObjectDecoderType<T> = UndefinedToOptional<{
329
- [K in keyof T]: T[K] extends Decoder<infer V> ? V : never;
440
+ type ObjectDecoderType<Ds extends Record<string, Decoder<unknown>>> = UndefinedToOptional<{
441
+ [K in keyof Ds]: DecoderType<Ds[K]>;
330
442
  }>;
331
443
  /**
332
444
  * Accepts any "plain old JavaScript object", but doesn't validate its keys or
@@ -337,88 +449,20 @@ declare const pojo: Decoder<Record<string, unknown>>;
337
449
  * Accepts objects with fields matching the given decoders. Extra fields that
338
450
  * exist on the input object are ignored and will not be returned.
339
451
  */
340
- declare function object(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
341
- declare function object<DS extends Record<string, Decoder<any>>>(decodersByKey: DS): Decoder<ObjectDecoderType<DS>>;
452
+ declare function object(decoders: Record<any, never>): Decoder<Record<string, never>>;
453
+ declare function object<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds>>;
342
454
  /**
343
455
  * Like `object()`, but will reject inputs that contain extra fields that are
344
456
  * not specified explicitly.
345
457
  */
346
- declare function exact(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
347
- declare function exact<O extends Record<string, Decoder<any>>>(decodersByKey: O): Decoder<{
348
- [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
349
- }>;
458
+ declare function exact(decoders: Record<any, never>): Decoder<Record<string, never>>;
459
+ declare function exact<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds>>;
350
460
  /**
351
461
  * Like `object()`, but will pass through any extra fields on the input object
352
462
  * unvalidated that will thus be of `unknown` type statically.
353
463
  */
354
- declare function inexact(decodersByKey: Record<any, never>): Decoder<Record<string, unknown>>;
355
- declare function inexact<O extends Record<string, Decoder<any>>>(decodersByKey: O): Decoder<{
356
- [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
357
- } & Record<string, unknown>>;
358
- /**
359
- * Accepts objects where all values match the given decoder, and returns the
360
- * result as a `Record<string, T>`.
361
- *
362
- * The main difference between `object()` and `dict()` is that you'd typically
363
- * use `object()` if this is a record-like object, where all field names are
364
- * known and the values are heterogeneous. Whereas with `dict()` the keys are
365
- * typically dynamic and the values homogeneous, like in a dictionary,
366
- * a lookup table, or a cache.
367
- */
368
- declare function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>>;
369
- /**
370
- * Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
371
- * Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
372
- * instead.
373
- */
374
- declare function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
375
-
376
- type Values<T extends object> = T[keyof T];
377
- type DecoderTypes<T> = T extends readonly Decoder<infer U>[] ? U : never;
378
- /**
379
- * Accepts values accepted by any of the given decoders.
380
- *
381
- * The decoders are tried on the input one by one, in the given order. The
382
- * first one that accepts the input "wins". If all decoders reject the input,
383
- * the input gets rejected.
384
- */
385
- declare function either<T extends readonly Decoder<any>[]>(...decoders: T): Decoder<DecoderTypes<T>>;
386
- /**
387
- * Accepts any value that is strictly-equal (using `===`) to one of the
388
- * specified values.
389
- */
390
- declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
391
- /**
392
- * If you are decoding tagged unions you may want to use the `taggedUnion()`
393
- * decoder instead of the general purpose `either()` decoder to get better
394
- * error messages and better performance.
395
- *
396
- * This decoder is optimized for [tagged
397
- * unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
398
- * objects where one field is used as the discriminator.
399
- *
400
- * ```ts
401
- * const A = object({ tag: constant('A'), foo: string });
402
- * const B = object({ tag: constant('B'), bar: number });
403
- *
404
- * const AorB = taggedUnion('tag', { A, B });
405
- * // ^^^
406
- * ```
407
- *
408
- * Decoding now works in two steps:
409
- *
410
- * 1. Look at the `'tag'` field in the incoming object (this is the field
411
- * that decides which decoder will be used)
412
- * 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
413
- * decoder `B` will be used. Otherwise, this will fail.
414
- *
415
- * This is effectively equivalent to `either(A, B)`, but will provide better
416
- * error messages and is more performant at runtime because it doesn't have to
417
- * try all decoders one by one.
418
- */
419
- declare function taggedUnion<O extends Record<string, Decoder<any>>>(field: string, mapping: O): Decoder<Values<{
420
- [key in keyof O]: DecoderType<O[key]>;
421
- }>>;
464
+ declare function inexact(decoders: Record<any, never>): Decoder<Record<string, unknown>>;
465
+ declare function inexact<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds> & Record<string, unknown>>;
422
466
 
423
467
  /**
424
468
  * Accepts and returns strings.
@@ -464,56 +508,77 @@ declare const uuidv1: Decoder<string>;
464
508
  * strings.
465
509
  */
466
510
  declare const uuidv4: Decoder<string>;
467
-
468
- interface Klass<T> extends Function {
469
- new (...args: readonly any[]): T;
470
- }
471
- type Instance<K> = K extends Klass<infer T> ? T : never;
472
511
  /**
473
- * Accepts any value that is an ``instanceof`` the given class.
512
+ * Accepts and returns strings with decimal digits only (base-10).
513
+ * To convert these to numbers, use the `numeric` decoder.
474
514
  */
475
- declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>>;
515
+ declare const decimal: Decoder<string>;
476
516
  /**
477
- * Lazily evaluate the given decoder. This is useful to build self-referential
478
- * types for recursive data structures.
517
+ * Accepts and returns strings with hexadecimal digits only (base-16).
479
518
  */
480
- declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
519
+ declare const hexadecimal: Decoder<string>;
481
520
  /**
482
- * Pre-process the data input before passing it into the decoder. This gives
483
- * you the ability to arbitrarily customize the input on the fly before passing
484
- * it to the decoder. Of course, the input value at that point is still of
485
- * ``unknown`` type, so you will have to deal with that accordingly.
521
+ * Accepts valid numerical strings (in base-10) and returns them as a number.
522
+ * To only accept numerical strings and keep them as string values, use the
523
+ * `decimal` decoder.
486
524
  */
487
- declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<T>): Decoder<T>;
525
+ declare const numeric: Decoder<number>;
488
526
 
489
527
  /**
490
- * Accepts any valid ``number`` value.
528
+ * Accepts values accepted by any of the given decoders.
491
529
  *
492
- * This also accepts special values like `NaN` and `Infinity`. Unless you
493
- * want to deliberately accept those, you'll likely want to use the
494
- * `number` decoder instead.
530
+ * The decoders are tried on the input one by one, in the given order. The
531
+ * first one that accepts the input "wins". If all decoders reject the input,
532
+ * the input gets rejected.
495
533
  */
496
- declare const anyNumber: Decoder<number>;
534
+ declare function either<TDecoders extends readonly Decoder<unknown>[]>(...decoders: TDecoders): Decoder<DecoderType<TDecoders[number]>>;
497
535
  /**
498
- * Accepts finite numbers (can be integer or float values). Values `NaN`,
499
- * or positive and negative `Infinity` will get rejected.
536
+ * Accepts any value that is strictly-equal (using `===`) to one of the
537
+ * specified values.
500
538
  */
501
- declare const number: Decoder<number>;
539
+ declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
502
540
  /**
503
- * Accepts only finite whole numbers.
541
+ * Accepts and return an enum value.
504
542
  */
505
- declare const integer: Decoder<number>;
543
+ declare function enum_<TEnum extends Record<string, string | number>>(enumObj: TEnum): Decoder<TEnum[keyof TEnum]>;
506
544
  /**
507
- * Accepts only non-negative (zero or positive) finite numbers.
545
+ * If you are decoding tagged unions you may want to use the `taggedUnion()`
546
+ * decoder instead of the general purpose `either()` decoder to get better
547
+ * error messages and better performance.
548
+ *
549
+ * This decoder is optimized for [tagged
550
+ * unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
551
+ * objects where one field is used as the discriminator.
552
+ *
553
+ * ```ts
554
+ * const A = object({ tag: constant('A'), foo: string });
555
+ * const B = object({ tag: constant('B'), bar: number });
556
+ *
557
+ * const AorB = taggedUnion('tag', { A, B });
558
+ * // ^^^
559
+ * ```
560
+ *
561
+ * Decoding now works in two steps:
562
+ *
563
+ * 1. Look at the `'tag'` field in the incoming object (this is the field
564
+ * that decides which decoder will be used)
565
+ * 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
566
+ * decoder `B` will be used. Otherwise, this will fail.
567
+ *
568
+ * This is effectively equivalent to `either(A, B)`, but will provide better
569
+ * error messages and is more performant at runtime because it doesn't have to
570
+ * try all decoders one by one.
508
571
  */
509
- declare const positiveNumber: Decoder<number>;
572
+ declare function taggedUnion<O extends Record<string, Decoder<unknown>>, T = DecoderType<O[keyof O]>>(field: string, mapping: O): Decoder<T>;
510
573
  /**
511
- * Accepts only non-negative (zero or positive) finite whole numbers.
574
+ * Briefly peek at a runtime input using a "scout" decoder first, then decide
575
+ * which decoder to run on the (original) input, based on the information that
576
+ * the "scout" extracted.
577
+ *
578
+ * It serves a similar purpose as `taggedUnion()`, but is a generalization that
579
+ * works even if there isn't a single discriminator, or the discriminator isn't
580
+ * a string.
512
581
  */
513
- declare const positiveInteger: Decoder<number>;
514
-
515
- type Formatter = (err: Annotation) => string | Error;
516
- declare function formatInline(ann: Annotation): string;
517
- declare function formatShort(ann: Annotation): string;
582
+ declare function select<T, D extends Decoder<unknown>>(scout: Decoder<T>, selectFn: (result: T) => D): Decoder<DecoderType<D>>;
518
583
 
519
- export { type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type Ok, type Result, type Scalar, always, anyNumber, array, boolean, constant, date, define, dict, either, email, err, exact, fail, formatInline, formatShort, hardcoded, httpsUrl, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, mapping, maybe, mixed, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numericBoolean, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, regex, set, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };
584
+ export { type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type Ok, type Result, type Scalar, always, anyNumber, array, bigint, boolean, constant, date, datelike, decimal, define, dict, either, email, enum_, err, exact, fail, formatInline, formatShort, hardcoded, hexadecimal, httpsUrl, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, mapping, maybe, mixed, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numeric, numericBoolean, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, set, setFromArray, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };