decoders 2.9.1-pre.0 → 2.9.2

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
@@ -351,20 +351,23 @@ declare const date: Decoder<Date>;
351
351
  /**
352
352
  * Accepts and returns [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings.
353
353
  */
354
- declare const dateString: Decoder<string>;
354
+ declare const isoDateString: Decoder<string>;
355
355
  /**
356
356
  * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
357
357
  * returns them as `Date` instances.
358
- *
359
- * This is very useful for working with dates in APIs: serialize them as
360
- * `.toISOString()` when sending, decode them with `iso8601` when receiving.
361
358
  */
362
- declare const iso8601: Decoder<Date>;
359
+ declare const isoDate: Decoder<Date>;
363
360
  /**
364
361
  * Accepts either a Date, or an ISO date string, returns a Date instance.
365
362
  * This is commonly useful to build decoders that can be reused to validate
366
363
  * object with Date instances as well as objects coming from JSON payloads.
367
364
  */
365
+ declare const flexDate: Decoder<Date>;
366
+ /** @deprecated Renamed to `isoDateString`. This alias will be removed in 3.x. */
367
+ declare const dateString: Decoder<string>;
368
+ /** Alias of `isoDate`. */
369
+ declare const iso8601: Decoder<Date>;
370
+ /** @deprecated Renamed to `flexDate`. This alias will be removed in 3.x. */
368
371
  declare const datelike: Decoder<Date>;
369
372
 
370
373
  type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
@@ -398,11 +401,45 @@ declare const jsonArray: Decoder<JSONArray>;
398
401
  */
399
402
  declare const json: Decoder<JSONValue>;
400
403
 
401
- type SizeOptions = {
402
- min?: number;
403
- max?: number;
404
- size?: number;
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];
405
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
+ }>;
406
443
 
407
444
  interface Klass<T> extends Function {
408
445
  new (...args: readonly any[]): T;
@@ -417,6 +454,10 @@ declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>
417
454
  * types for recursive data structures.
418
455
  */
419
456
  declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
457
+ /**
458
+ * Only accept strings, arrays, or sets with given length constraints.
459
+ */
460
+ declare function sized<T extends Sized>(decoder: Decoder<T>, options: SizeOptions): Decoder<T>;
420
461
  /**
421
462
  * Pre-process the data input before passing it into the decoder. This gives
422
463
  * you the ability to arbitrarily customize the input on the fly before passing
@@ -450,6 +491,24 @@ declare const positiveNumber: Decoder<number>;
450
491
  * Accepts only non-negative (zero or positive) finite whole numbers.
451
492
  */
452
493
  declare const positiveInteger: Decoder<number>;
494
+ /**
495
+ * Accepts numbers greater than or equal to the given minimum.
496
+ * Defaults to the ``number`` decoder if none is provided. Pass a
497
+ * different decoder to further restrict accepted values, e.g. ``min(0, integer)``.
498
+ */
499
+ declare function min(min: number, decoder?: Decoder<number>): Decoder<number>;
500
+ /**
501
+ * Accepts numbers less than or equal to the given maximum.
502
+ * Defaults to the ``number`` decoder if none is provided. Pass a
503
+ * different decoder to further restrict accepted values, e.g. ``max(100, integer)``.
504
+ */
505
+ declare function max(max: number, decoder?: Decoder<number>): Decoder<number>;
506
+ /**
507
+ * Accepts numbers within the given range (bounds are inclusive).
508
+ * Defaults to the ``number`` decoder if none is provided. Pass a
509
+ * different decoder to further restrict accepted values, e.g. ``between(1, 10, integer)``.
510
+ */
511
+ declare function between(min: number, max: number, decoder?: Decoder<number>): Decoder<number>;
453
512
  /**
454
513
  * Accepts any valid ``bigint`` value.
455
514
  */
@@ -535,6 +594,10 @@ declare function endsWith<S extends string>(suffix: S): Decoder<`${string}${S}`>
535
594
  * (This will not mean that the email address actually exist.)
536
595
  */
537
596
  declare const email: Decoder<string>;
597
+ /**
598
+ * Accepts strings that are valid URLs.
599
+ */
600
+ declare const urlString: Decoder<string>;
538
601
  /**
539
602
  * Accepts strings that are valid URLs, returns the value as a URL instance.
540
603
  */
@@ -550,9 +613,10 @@ declare const httpsUrl: Decoder<URL>;
550
613
  */
551
614
  declare const identifier: Decoder<string>;
552
615
  /**
553
- * Accepts and returns [nanoid](https://zelark.github.io/nano-id-cc) string
554
- * values. It assumes the default nanoid alphabet. If you're using a custom
555
- * alphabet, use `regex()` instead.
616
+ * Accepts and returns [Nano ID](https://zelark.github.io/nano-id-cc) string
617
+ * values. By default, expects a standard 21-char nanoid, but you can
618
+ * optionally specify different size constraints. It assumes the default nanoid
619
+ * alphabet.
556
620
  */
557
621
  declare function nanoid(options?: SizeOptions): Decoder<string>;
558
622
  /**
@@ -660,4 +724,4 @@ declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
660
724
  */
661
725
  declare function isPlainObject(value: unknown): value is Record<string, unknown>;
662
726
 
663
- 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 Result, type Scalar, type ScalarAnnotation, type SizeOptions, public_annotate as _annotate, always, anyNumber, anything, array, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, json, jsonArray, jsonObject, lazy, mapping, nanoid, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };
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 };
package/dist/index.d.ts CHANGED
@@ -351,20 +351,23 @@ declare const date: Decoder<Date>;
351
351
  /**
352
352
  * Accepts and returns [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings.
353
353
  */
354
- declare const dateString: Decoder<string>;
354
+ declare const isoDateString: Decoder<string>;
355
355
  /**
356
356
  * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
357
357
  * returns them as `Date` instances.
358
- *
359
- * This is very useful for working with dates in APIs: serialize them as
360
- * `.toISOString()` when sending, decode them with `iso8601` when receiving.
361
358
  */
362
- declare const iso8601: Decoder<Date>;
359
+ declare const isoDate: Decoder<Date>;
363
360
  /**
364
361
  * Accepts either a Date, or an ISO date string, returns a Date instance.
365
362
  * This is commonly useful to build decoders that can be reused to validate
366
363
  * object with Date instances as well as objects coming from JSON payloads.
367
364
  */
365
+ declare const flexDate: Decoder<Date>;
366
+ /** @deprecated Renamed to `isoDateString`. This alias will be removed in 3.x. */
367
+ declare const dateString: Decoder<string>;
368
+ /** Alias of `isoDate`. */
369
+ declare const iso8601: Decoder<Date>;
370
+ /** @deprecated Renamed to `flexDate`. This alias will be removed in 3.x. */
368
371
  declare const datelike: Decoder<Date>;
369
372
 
370
373
  type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
@@ -398,11 +401,45 @@ declare const jsonArray: Decoder<JSONArray>;
398
401
  */
399
402
  declare const json: Decoder<JSONValue>;
400
403
 
401
- type SizeOptions = {
402
- min?: number;
403
- max?: number;
404
- size?: number;
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];
405
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
+ }>;
406
443
 
407
444
  interface Klass<T> extends Function {
408
445
  new (...args: readonly any[]): T;
@@ -417,6 +454,10 @@ declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>
417
454
  * types for recursive data structures.
418
455
  */
419
456
  declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
457
+ /**
458
+ * Only accept strings, arrays, or sets with given length constraints.
459
+ */
460
+ declare function sized<T extends Sized>(decoder: Decoder<T>, options: SizeOptions): Decoder<T>;
420
461
  /**
421
462
  * Pre-process the data input before passing it into the decoder. This gives
422
463
  * you the ability to arbitrarily customize the input on the fly before passing
@@ -450,6 +491,24 @@ declare const positiveNumber: Decoder<number>;
450
491
  * Accepts only non-negative (zero or positive) finite whole numbers.
451
492
  */
452
493
  declare const positiveInteger: Decoder<number>;
494
+ /**
495
+ * Accepts numbers greater than or equal to the given minimum.
496
+ * Defaults to the ``number`` decoder if none is provided. Pass a
497
+ * different decoder to further restrict accepted values, e.g. ``min(0, integer)``.
498
+ */
499
+ declare function min(min: number, decoder?: Decoder<number>): Decoder<number>;
500
+ /**
501
+ * Accepts numbers less than or equal to the given maximum.
502
+ * Defaults to the ``number`` decoder if none is provided. Pass a
503
+ * different decoder to further restrict accepted values, e.g. ``max(100, integer)``.
504
+ */
505
+ declare function max(max: number, decoder?: Decoder<number>): Decoder<number>;
506
+ /**
507
+ * Accepts numbers within the given range (bounds are inclusive).
508
+ * Defaults to the ``number`` decoder if none is provided. Pass a
509
+ * different decoder to further restrict accepted values, e.g. ``between(1, 10, integer)``.
510
+ */
511
+ declare function between(min: number, max: number, decoder?: Decoder<number>): Decoder<number>;
453
512
  /**
454
513
  * Accepts any valid ``bigint`` value.
455
514
  */
@@ -535,6 +594,10 @@ declare function endsWith<S extends string>(suffix: S): Decoder<`${string}${S}`>
535
594
  * (This will not mean that the email address actually exist.)
536
595
  */
537
596
  declare const email: Decoder<string>;
597
+ /**
598
+ * Accepts strings that are valid URLs.
599
+ */
600
+ declare const urlString: Decoder<string>;
538
601
  /**
539
602
  * Accepts strings that are valid URLs, returns the value as a URL instance.
540
603
  */
@@ -550,9 +613,10 @@ declare const httpsUrl: Decoder<URL>;
550
613
  */
551
614
  declare const identifier: Decoder<string>;
552
615
  /**
553
- * Accepts and returns [nanoid](https://zelark.github.io/nano-id-cc) string
554
- * values. It assumes the default nanoid alphabet. If you're using a custom
555
- * alphabet, use `regex()` instead.
616
+ * Accepts and returns [Nano ID](https://zelark.github.io/nano-id-cc) string
617
+ * values. By default, expects a standard 21-char nanoid, but you can
618
+ * optionally specify different size constraints. It assumes the default nanoid
619
+ * alphabet.
556
620
  */
557
621
  declare function nanoid(options?: SizeOptions): Decoder<string>;
558
622
  /**
@@ -660,4 +724,4 @@ declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
660
724
  */
661
725
  declare function isPlainObject(value: unknown): value is Record<string, unknown>;
662
726
 
663
- 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 Result, type Scalar, type ScalarAnnotation, type SizeOptions, public_annotate as _annotate, always, anyNumber, anything, array, bigint, boolean, constant, date, dateString, datelike, decimal, define, either, email, endsWith, enum_, err, exact, fail, formatInline, formatShort, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, isDate, isDecoder, isPlainObject, isPromiseLike, iso8601, json, jsonArray, jsonObject, lazy, mapping, nanoid, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, setFromArray, startsWith, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };
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 };