decoders 2.10.1 → 2.12.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
@@ -109,7 +109,6 @@ type DecodeResult<T> = Result<T, Annotation>;
109
109
  * param. One of these should be called and its value returned.
110
110
  */
111
111
  type AcceptanceFn<O, I = unknown> = (blob: I, ok: (value: O) => DecodeResult<O>, err: (msg: string | Annotation) => DecodeResult<O>) => DecodeResult<O>;
112
- type Next<O, I = unknown> = Decoder<O> | ((blob: I, ok: (value: O) => DecodeResult<O>, err: (msg: string | Annotation) => DecodeResult<O>) => DecodeResult<O> | Decoder<O>);
113
112
  interface Decoder<T> {
114
113
  /**
115
114
  * Verifies untrusted input. Either returns a value, or throws a decoding
@@ -151,16 +150,18 @@ interface Decoder<T> {
151
150
  */
152
151
  describe(message: string): Decoder<T>;
153
152
  /**
154
- * Send the output of the current decoder into another decoder or acceptance
155
- * function. The given acceptance function will receive the output of the
156
- * current decoder as its input.
153
+ * Send the output of the current decoder into an acceptance function. The
154
+ * given acceptance function will receive the output of the current decoder
155
+ * as its input.
157
156
  *
158
157
  * > _**NOTE:** This is an advanced, low-level, API. It's not recommended
159
158
  * > to reach for this construct unless there is no other way. Most cases can
160
159
  * > be covered more elegantly by `.transform()`, `.refine()`, or `.pipe()`
161
160
  * > instead._
162
161
  */
163
- chain<V>(next: Next<V, T>): Decoder<V>;
162
+ chain<V>(next: (blob: T, ok: (value: V) => DecodeResult<V>, err: (msg: string | Annotation) => DecodeResult<V>) => DecodeResult<V> | Decoder<V>): Decoder<V>;
163
+ /** @deprecated To send the output into another decoder, use `.pipe()` instead. */
164
+ chain<V>(next: Decoder<V>): Decoder<V>;
164
165
  /**
165
166
  * Send the output of this decoder as input to another decoder.
166
167
  *
@@ -178,7 +179,7 @@ interface Decoder<T> {
178
179
  /**
179
180
  * The Standard Schema interface for this decoder.
180
181
  */
181
- '~standard': StandardSchemaV1.Props<unknown, T>;
182
+ readonly '~standard': StandardSchemaV1.Props<unknown, T>;
182
183
  }
183
184
  /**
184
185
  * Helper type to return the output type of a Decoder.
@@ -218,6 +219,46 @@ type Formatter = (err: Annotation) => string | Error;
218
219
  declare function formatInline(ann: Annotation): string;
219
220
  declare function formatShort(ann: Annotation): string;
220
221
 
222
+ /**
223
+ * Forces TypeScript to "evaluate" named helper types, making API signatures
224
+ * clearer in IDEs.
225
+ *
226
+ * @see https://effectivetypescript.com/2022/02/25/gentips-4-display/
227
+ */
228
+ type Resolve$1<T> = T extends (...args: unknown[]) => unknown ? T : {
229
+ [K in keyof T]: T[K];
230
+ };
231
+ /**
232
+ * Relaxes a discriminated union type definition, by explicitly adding
233
+ * properties defined in any other member as optional `never`.
234
+ *
235
+ * This makes accessing the members much more relaxed in TypeScript.
236
+ */
237
+ type Relax<T> = DistributiveRelax<T, T extends any ? keyof T : never>;
238
+ type DistributiveRelax<T, Ks extends string | number | symbol> = T extends any ? Resolve$1<{
239
+ [K in keyof T]: T[K];
240
+ } & {
241
+ [K in Exclude<Ks, keyof T>]?: never;
242
+ }> : never;
243
+
244
+ type SizeOptions = Relax<{
245
+ size: number;
246
+ } | {
247
+ min: number;
248
+ max?: number;
249
+ } | {
250
+ min?: number;
251
+ max: number;
252
+ }>;
253
+ /**
254
+ * Anything with a .length or .size property, like strings, arrays, or sets.
255
+ */
256
+ type Sized = Relax<{
257
+ length: number;
258
+ } | {
259
+ size: number;
260
+ }>;
261
+
221
262
  /**
222
263
  * Accepts any array, but doesn't validate its items further.
223
264
  *
@@ -227,7 +268,7 @@ declare const poja: Decoder<unknown[]>;
227
268
  /**
228
269
  * Accepts arrays of whatever the given decoder accepts.
229
270
  */
230
- declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
271
+ declare function array<T>(decoder: Decoder<T>, options?: SizeOptions): Decoder<T[]>;
231
272
  /**
232
273
  * Like `array()`, but will reject arrays with 0 elements.
233
274
  */
@@ -365,7 +406,7 @@ declare const isoDate: Decoder<Date>;
365
406
  declare const flexDate: Decoder<Date>;
366
407
  /** @deprecated Renamed to `isoDateString`. This alias will be removed in 3.x. */
367
408
  declare const dateString: Decoder<string>;
368
- /** Alias of `isoDate`. */
409
+ /** @deprecated Renamed to `isoDate`. */
369
410
  declare const iso8601: Decoder<Date>;
370
411
  /** @deprecated Renamed to `flexDate`. This alias will be removed in 3.x. */
371
412
  declare const datelike: Decoder<Date>;
@@ -401,46 +442,6 @@ declare const jsonArray: Decoder<JSONArray>;
401
442
  */
402
443
  declare const json: Decoder<JSONValue>;
403
444
 
404
- /**
405
- * Forces TypeScript to "evaluate" named helper types, making API signatures
406
- * clearer in IDEs.
407
- *
408
- * @see https://effectivetypescript.com/2022/02/25/gentips-4-display/
409
- */
410
- type Resolve$1<T> = T extends (...args: unknown[]) => unknown ? T : {
411
- [K in keyof T]: T[K];
412
- };
413
- /**
414
- * Relaxes a discriminated union type definition, by explicitly adding
415
- * properties defined in any other member as optional `never`.
416
- *
417
- * This makes accessing the members much more relaxed in TypeScript.
418
- */
419
- type Relax<T> = DistributiveRelax<T, T extends any ? keyof T : never>;
420
- type DistributiveRelax<T, Ks extends string | number | symbol> = T extends any ? Resolve$1<{
421
- [K in keyof T]: T[K];
422
- } & {
423
- [K in Exclude<Ks, keyof T>]?: never;
424
- }> : never;
425
-
426
- type SizeOptions = Relax<{
427
- size: number;
428
- } | {
429
- min: number;
430
- max?: number;
431
- } | {
432
- min?: number;
433
- max: number;
434
- }>;
435
- /**
436
- * Anything with a .length or .size property, like strings, arrays, or sets.
437
- */
438
- type Sized = Relax<{
439
- length: number;
440
- } | {
441
- size: number;
442
- }>;
443
-
444
445
  interface Klass<T> extends Function {
445
446
  new (...args: readonly any[]): T;
446
447
  }
@@ -475,21 +476,28 @@ declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<
475
476
  */
476
477
  declare const anyNumber: Decoder<number>;
477
478
  /**
478
- * Accepts finite numbers (can be integer or float values). Values `NaN`,
479
- * or positive and negative `Infinity` will get rejected.
479
+ * Accepts only finite numbers (e.g. -3.14, 0, 1, 42, ...).
480
+ * Integers or floats, but not `NaN` or `Infinity`.
480
481
  */
481
482
  declare const number: Decoder<number>;
482
483
  /**
483
- * Accepts only finite whole numbers.
484
+ * Accepts only integers (e.g. ..., -2, -1, 0, 1, 2, ...).
485
+ * Whole numbers, and finite.
484
486
  */
485
487
  declare const integer: Decoder<number>;
486
488
  /**
487
- * Accepts only non-negative (zero or positive) finite numbers.
489
+ * Accepts only non-negative numbers (e.g. 0, 0.5, 1, 3.14, ...).
490
+ * Integers or floats, >= 0, and finite.
488
491
  */
489
- declare const positiveNumber: Decoder<number>;
492
+ declare const nonNegativeNumber: Decoder<number>;
490
493
  /**
491
- * Accepts only non-negative (zero or positive) finite whole numbers.
494
+ * Accepts only the natural numbers (e.g. 0, 1, 2, 3, ...).
495
+ * Whole numbers, >= 0, and finite.
492
496
  */
497
+ declare const natural: Decoder<number>;
498
+ /** @deprecated Renamed to `nonNegativeNumber`. */
499
+ declare const positiveNumber: Decoder<number>;
500
+ /** @deprecated Renamed to `natural`. */
493
501
  declare const positiveInteger: Decoder<number>;
494
502
  /**
495
503
  * Accepts numbers greater than or equal to the given minimum.
@@ -665,9 +673,10 @@ declare function either<TDecoders extends readonly Decoder<unknown>[]>(...decode
665
673
  * Accepts any value that is strictly-equal (using `===`) to one of the
666
674
  * specified values.
667
675
  */
668
- declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
676
+ declare function oneOf<const C extends Scalar>(constants: readonly C[]): Decoder<C>;
669
677
  /**
670
- * Accepts and return an enum value.
678
+ * Accepts and return an enum value. Works with TypeScript enums, as well as
679
+ * with `as const` objects.
671
680
  */
672
681
  declare function enum_<TEnum extends Record<string, string | number>>(enumObj: TEnum): Decoder<TEnum[keyof TEnum]>;
673
682
  /**
@@ -724,4 +733,4 @@ declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
724
733
  */
725
734
  declare function isPlainObject(value: unknown): value is Record<string, unknown>;
726
735
 
727
- export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, lazy, mapping, max, min, nanoid, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };
736
+ export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, lazy, mapping, max, min, nanoid, natural, never, nonEmptyArray, nonEmptyString, nonNegativeNumber, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };
package/dist/index.d.ts CHANGED
@@ -109,7 +109,6 @@ type DecodeResult<T> = Result<T, Annotation>;
109
109
  * param. One of these should be called and its value returned.
110
110
  */
111
111
  type AcceptanceFn<O, I = unknown> = (blob: I, ok: (value: O) => DecodeResult<O>, err: (msg: string | Annotation) => DecodeResult<O>) => DecodeResult<O>;
112
- type Next<O, I = unknown> = Decoder<O> | ((blob: I, ok: (value: O) => DecodeResult<O>, err: (msg: string | Annotation) => DecodeResult<O>) => DecodeResult<O> | Decoder<O>);
113
112
  interface Decoder<T> {
114
113
  /**
115
114
  * Verifies untrusted input. Either returns a value, or throws a decoding
@@ -151,16 +150,18 @@ interface Decoder<T> {
151
150
  */
152
151
  describe(message: string): Decoder<T>;
153
152
  /**
154
- * Send the output of the current decoder into another decoder or acceptance
155
- * function. The given acceptance function will receive the output of the
156
- * current decoder as its input.
153
+ * Send the output of the current decoder into an acceptance function. The
154
+ * given acceptance function will receive the output of the current decoder
155
+ * as its input.
157
156
  *
158
157
  * > _**NOTE:** This is an advanced, low-level, API. It's not recommended
159
158
  * > to reach for this construct unless there is no other way. Most cases can
160
159
  * > be covered more elegantly by `.transform()`, `.refine()`, or `.pipe()`
161
160
  * > instead._
162
161
  */
163
- chain<V>(next: Next<V, T>): Decoder<V>;
162
+ chain<V>(next: (blob: T, ok: (value: V) => DecodeResult<V>, err: (msg: string | Annotation) => DecodeResult<V>) => DecodeResult<V> | Decoder<V>): Decoder<V>;
163
+ /** @deprecated To send the output into another decoder, use `.pipe()` instead. */
164
+ chain<V>(next: Decoder<V>): Decoder<V>;
164
165
  /**
165
166
  * Send the output of this decoder as input to another decoder.
166
167
  *
@@ -178,7 +179,7 @@ interface Decoder<T> {
178
179
  /**
179
180
  * The Standard Schema interface for this decoder.
180
181
  */
181
- '~standard': StandardSchemaV1.Props<unknown, T>;
182
+ readonly '~standard': StandardSchemaV1.Props<unknown, T>;
182
183
  }
183
184
  /**
184
185
  * Helper type to return the output type of a Decoder.
@@ -218,6 +219,46 @@ type Formatter = (err: Annotation) => string | Error;
218
219
  declare function formatInline(ann: Annotation): string;
219
220
  declare function formatShort(ann: Annotation): string;
220
221
 
222
+ /**
223
+ * Forces TypeScript to "evaluate" named helper types, making API signatures
224
+ * clearer in IDEs.
225
+ *
226
+ * @see https://effectivetypescript.com/2022/02/25/gentips-4-display/
227
+ */
228
+ type Resolve$1<T> = T extends (...args: unknown[]) => unknown ? T : {
229
+ [K in keyof T]: T[K];
230
+ };
231
+ /**
232
+ * Relaxes a discriminated union type definition, by explicitly adding
233
+ * properties defined in any other member as optional `never`.
234
+ *
235
+ * This makes accessing the members much more relaxed in TypeScript.
236
+ */
237
+ type Relax<T> = DistributiveRelax<T, T extends any ? keyof T : never>;
238
+ type DistributiveRelax<T, Ks extends string | number | symbol> = T extends any ? Resolve$1<{
239
+ [K in keyof T]: T[K];
240
+ } & {
241
+ [K in Exclude<Ks, keyof T>]?: never;
242
+ }> : never;
243
+
244
+ type SizeOptions = Relax<{
245
+ size: number;
246
+ } | {
247
+ min: number;
248
+ max?: number;
249
+ } | {
250
+ min?: number;
251
+ max: number;
252
+ }>;
253
+ /**
254
+ * Anything with a .length or .size property, like strings, arrays, or sets.
255
+ */
256
+ type Sized = Relax<{
257
+ length: number;
258
+ } | {
259
+ size: number;
260
+ }>;
261
+
221
262
  /**
222
263
  * Accepts any array, but doesn't validate its items further.
223
264
  *
@@ -227,7 +268,7 @@ declare const poja: Decoder<unknown[]>;
227
268
  /**
228
269
  * Accepts arrays of whatever the given decoder accepts.
229
270
  */
230
- declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
271
+ declare function array<T>(decoder: Decoder<T>, options?: SizeOptions): Decoder<T[]>;
231
272
  /**
232
273
  * Like `array()`, but will reject arrays with 0 elements.
233
274
  */
@@ -365,7 +406,7 @@ declare const isoDate: Decoder<Date>;
365
406
  declare const flexDate: Decoder<Date>;
366
407
  /** @deprecated Renamed to `isoDateString`. This alias will be removed in 3.x. */
367
408
  declare const dateString: Decoder<string>;
368
- /** Alias of `isoDate`. */
409
+ /** @deprecated Renamed to `isoDate`. */
369
410
  declare const iso8601: Decoder<Date>;
370
411
  /** @deprecated Renamed to `flexDate`. This alias will be removed in 3.x. */
371
412
  declare const datelike: Decoder<Date>;
@@ -401,46 +442,6 @@ declare const jsonArray: Decoder<JSONArray>;
401
442
  */
402
443
  declare const json: Decoder<JSONValue>;
403
444
 
404
- /**
405
- * Forces TypeScript to "evaluate" named helper types, making API signatures
406
- * clearer in IDEs.
407
- *
408
- * @see https://effectivetypescript.com/2022/02/25/gentips-4-display/
409
- */
410
- type Resolve$1<T> = T extends (...args: unknown[]) => unknown ? T : {
411
- [K in keyof T]: T[K];
412
- };
413
- /**
414
- * Relaxes a discriminated union type definition, by explicitly adding
415
- * properties defined in any other member as optional `never`.
416
- *
417
- * This makes accessing the members much more relaxed in TypeScript.
418
- */
419
- type Relax<T> = DistributiveRelax<T, T extends any ? keyof T : never>;
420
- type DistributiveRelax<T, Ks extends string | number | symbol> = T extends any ? Resolve$1<{
421
- [K in keyof T]: T[K];
422
- } & {
423
- [K in Exclude<Ks, keyof T>]?: never;
424
- }> : never;
425
-
426
- type SizeOptions = Relax<{
427
- size: number;
428
- } | {
429
- min: number;
430
- max?: number;
431
- } | {
432
- min?: number;
433
- max: number;
434
- }>;
435
- /**
436
- * Anything with a .length or .size property, like strings, arrays, or sets.
437
- */
438
- type Sized = Relax<{
439
- length: number;
440
- } | {
441
- size: number;
442
- }>;
443
-
444
445
  interface Klass<T> extends Function {
445
446
  new (...args: readonly any[]): T;
446
447
  }
@@ -475,21 +476,28 @@ declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<
475
476
  */
476
477
  declare const anyNumber: Decoder<number>;
477
478
  /**
478
- * Accepts finite numbers (can be integer or float values). Values `NaN`,
479
- * or positive and negative `Infinity` will get rejected.
479
+ * Accepts only finite numbers (e.g. -3.14, 0, 1, 42, ...).
480
+ * Integers or floats, but not `NaN` or `Infinity`.
480
481
  */
481
482
  declare const number: Decoder<number>;
482
483
  /**
483
- * Accepts only finite whole numbers.
484
+ * Accepts only integers (e.g. ..., -2, -1, 0, 1, 2, ...).
485
+ * Whole numbers, and finite.
484
486
  */
485
487
  declare const integer: Decoder<number>;
486
488
  /**
487
- * Accepts only non-negative (zero or positive) finite numbers.
489
+ * Accepts only non-negative numbers (e.g. 0, 0.5, 1, 3.14, ...).
490
+ * Integers or floats, >= 0, and finite.
488
491
  */
489
- declare const positiveNumber: Decoder<number>;
492
+ declare const nonNegativeNumber: Decoder<number>;
490
493
  /**
491
- * Accepts only non-negative (zero or positive) finite whole numbers.
494
+ * Accepts only the natural numbers (e.g. 0, 1, 2, 3, ...).
495
+ * Whole numbers, >= 0, and finite.
492
496
  */
497
+ declare const natural: Decoder<number>;
498
+ /** @deprecated Renamed to `nonNegativeNumber`. */
499
+ declare const positiveNumber: Decoder<number>;
500
+ /** @deprecated Renamed to `natural`. */
493
501
  declare const positiveInteger: Decoder<number>;
494
502
  /**
495
503
  * Accepts numbers greater than or equal to the given minimum.
@@ -665,9 +673,10 @@ declare function either<TDecoders extends readonly Decoder<unknown>[]>(...decode
665
673
  * Accepts any value that is strictly-equal (using `===`) to one of the
666
674
  * specified values.
667
675
  */
668
- declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
676
+ declare function oneOf<const C extends Scalar>(constants: readonly C[]): Decoder<C>;
669
677
  /**
670
- * Accepts and return an enum value.
678
+ * Accepts and return an enum value. Works with TypeScript enums, as well as
679
+ * with `as const` objects.
671
680
  */
672
681
  declare function enum_<TEnum extends Record<string, string | number>>(enumObj: TEnum): Decoder<TEnum[keyof TEnum]>;
673
682
  /**
@@ -724,4 +733,4 @@ declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
724
733
  */
725
734
  declare function isPlainObject(value: unknown): value is Record<string, unknown>;
726
735
 
727
- export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, lazy, mapping, max, min, nanoid, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };
736
+ export { type Annotation, type ArrayAnnotation, type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type ObjectAnnotation, type Ok, type OpaqueAnnotation, type Relax, type Result, type Scalar, type ScalarAnnotation, type SizeOptions, type Sized, public_annotate as _annotate, always, anyNumber, anything, array, between, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, flexDate, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, isoDate, isoDateString, json, jsonArray, jsonObject, lazy, mapping, max, min, nanoid, natural, never, nonEmptyArray, nonEmptyString, nonNegativeNumber, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, sized, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, urlString, uuid, uuidv1, uuidv4 };