valgen 7.0.0 → 7.0.3
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/README.md +1 -1
- package/constants.js +1 -1
- package/core/constants.d.ts +11 -0
- package/core/constants.js +11 -0
- package/core/context.d.ts +57 -0
- package/core/context.js +57 -0
- package/core/types.d.ts +40 -0
- package/core/utilities.d.ts +42 -2
- package/core/utilities.js +21 -1
- package/core/validation-error.d.ts +13 -0
- package/core/validation-error.js +13 -0
- package/core/validator.d.ts +100 -0
- package/core/validator.js +6 -0
- package/package.json +1 -1
- package/rules/format-rules/is-alpha.d.ts +10 -1
- package/rules/format-rules/is-alpha.js +10 -1
- package/rules/format-rules/is-alphanumeric.d.ts +10 -1
- package/rules/format-rules/is-alphanumeric.js +10 -1
- package/rules/format-rules/is-ascii.d.ts +10 -1
- package/rules/format-rules/is-ascii.js +10 -1
- package/rules/format-rules/is-base64.d.ts +20 -1
- package/rules/format-rules/is-base64.js +10 -1
- package/rules/format-rules/is-btc-address.d.ts +10 -1
- package/rules/format-rules/is-btc-address.js +10 -1
- package/rules/format-rules/is-credit-card.d.ts +16 -1
- package/rules/format-rules/is-credit-card.js +11 -1
- package/rules/format-rules/is-decimal.d.ts +12 -2
- package/rules/format-rules/is-decimal.js +12 -2
- package/rules/format-rules/is-ean.d.ts +11 -1
- package/rules/format-rules/is-ean.js +11 -1
- package/rules/format-rules/is-email.d.ts +19 -7
- package/rules/format-rules/is-email.js +13 -1
- package/rules/format-rules/is-eth-address.d.ts +10 -1
- package/rules/format-rules/is-eth-address.js +10 -1
- package/rules/format-rules/is-fqdn.d.ts +13 -2
- package/rules/format-rules/is-fqdn.js +12 -1
- package/rules/format-rules/is-hash.d.ts +17 -1
- package/rules/format-rules/is-hash.js +16 -1
- package/rules/format-rules/is-hex-color.d.ts +13 -1
- package/rules/format-rules/is-hex-color.js +13 -1
- package/rules/format-rules/is-hex.d.ts +13 -1
- package/rules/format-rules/is-hex.js +13 -1
- package/rules/format-rules/is-iban.d.ts +33 -2
- package/rules/format-rules/is-iban.js +19 -1
- package/rules/format-rules/is-ip.d.ts +44 -2
- package/rules/format-rules/is-ip.js +42 -2
- package/rules/format-rules/is-issn.d.ts +19 -2
- package/rules/format-rules/is-issn.js +18 -1
- package/rules/format-rules/is-jwt.d.ts +16 -1
- package/rules/format-rules/is-jwt.js +16 -1
- package/rules/format-rules/is-lowercase.d.ts +13 -1
- package/rules/format-rules/is-lowercase.js +13 -1
- package/rules/format-rules/is-mac-address.d.ts +19 -2
- package/rules/format-rules/is-mac-address.js +16 -1
- package/rules/format-rules/is-mobile-phone.d.ts +19 -3
- package/rules/format-rules/is-mobile-phone.js +17 -1
- package/rules/format-rules/is-object-id.d.ts +20 -1
- package/rules/format-rules/is-object-id.js +19 -1
- package/rules/format-rules/is-passport-number.d.ts +17 -1
- package/rules/format-rules/is-passport-number.js +17 -1
- package/rules/format-rules/is-port.d.ts +16 -1
- package/rules/format-rules/is-port.js +16 -1
- package/rules/format-rules/is-swift.d.ts +15 -1
- package/rules/format-rules/is-swift.js +15 -1
- package/rules/format-rules/is-time.d.ts +22 -1
- package/rules/format-rules/is-time.js +22 -1
- package/rules/format-rules/is-uppercase.d.ts +15 -1
- package/rules/format-rules/is-uppercase.js +15 -1
- package/rules/format-rules/is-url.d.ts +99 -1
- package/rules/format-rules/is-url.js +19 -1
- package/rules/format-rules/is-uuid.d.ts +23 -1
- package/rules/format-rules/is-uuid.js +23 -1
- package/rules/format-rules/is-vat-number.d.ts +19 -2
- package/rules/format-rules/is-vat-number.js +19 -2
- package/rules/format-rules/matches.d.ts +27 -1
- package/rules/format-rules/matches.js +23 -1
- package/rules/logical-rules/is-defined.d.ts +14 -2
- package/rules/logical-rules/is-defined.js +13 -2
- package/rules/logical-rules/is-empty.d.ts +40 -2
- package/rules/logical-rules/is-empty.js +38 -2
- package/rules/logical-rules/is-equal.d.ts +31 -4
- package/rules/logical-rules/is-equal.js +29 -4
- package/rules/logical-rules/is-gt.d.ts +26 -1
- package/rules/logical-rules/is-gt.js +20 -1
- package/rules/logical-rules/is-gte.d.ts +25 -1
- package/rules/logical-rules/is-gte.js +20 -1
- package/rules/logical-rules/is-lt.d.ts +24 -1
- package/rules/logical-rules/is-lt.js +19 -1
- package/rules/logical-rules/is-lte.d.ts +25 -1
- package/rules/logical-rules/is-lte.js +20 -1
- package/rules/logical-rules/length.d.ts +38 -2
- package/rules/logical-rules/length.js +38 -2
- package/rules/logical-rules/range.d.ts +25 -1
- package/rules/logical-rules/range.js +23 -1
- package/rules/type-rules/is-any.d.ts +12 -1
- package/rules/type-rules/is-any.js +12 -1
- package/rules/type-rules/is-array.d.ts +23 -2
- package/rules/type-rules/is-array.js +23 -2
- package/rules/type-rules/is-bigint.d.ts +14 -2
- package/rules/type-rules/is-bigint.js +14 -2
- package/rules/type-rules/is-boolean.d.ts +17 -2
- package/rules/type-rules/is-boolean.js +17 -2
- package/rules/type-rules/is-date.d.ts +47 -5
- package/rules/type-rules/is-date.js +42 -5
- package/rules/type-rules/is-enum.d.ts +23 -1
- package/rules/type-rules/is-enum.js +21 -1
- package/rules/type-rules/is-instanceof.d.ts +21 -1
- package/rules/type-rules/is-instanceof.js +21 -1
- package/rules/type-rules/is-integer.d.ts +17 -2
- package/rules/type-rules/is-integer.js +17 -2
- package/rules/type-rules/is-null.d.ts +51 -4
- package/rules/type-rules/is-null.js +51 -4
- package/rules/type-rules/is-number.d.ts +17 -2
- package/rules/type-rules/is-number.js +17 -2
- package/rules/type-rules/is-object.d.ts +45 -7
- package/rules/type-rules/is-object.js +40 -7
- package/rules/type-rules/is-record.d.ts +21 -2
- package/rules/type-rules/is-record.js +21 -2
- package/rules/type-rules/is-string.d.ts +16 -2
- package/rules/type-rules/is-string.js +16 -2
- package/rules/type-rules/is-tuple.d.ts +21 -2
- package/rules/type-rules/is-undefined.d.ts +16 -4
- package/rules/type-rules/is-undefined.js +16 -4
- package/rules/utility-rules/all-of.d.ts +28 -1
- package/rules/utility-rules/all-of.js +27 -1
- package/rules/utility-rules/exists.d.ts +30 -1
- package/rules/utility-rules/exists.js +29 -1
- package/rules/utility-rules/fixed.d.ts +19 -1
- package/rules/utility-rules/fixed.js +19 -1
- package/rules/utility-rules/get-length.d.ts +25 -2
- package/rules/utility-rules/get-length.js +25 -2
- package/rules/utility-rules/nullable.d.ts +28 -1
- package/rules/utility-rules/nullable.js +27 -1
- package/rules/utility-rules/one-of.d.ts +52 -3
- package/rules/utility-rules/one-of.js +81 -9
- package/rules/utility-rules/optional.d.ts +29 -1
- package/rules/utility-rules/optional.js +28 -1
- package/rules/utility-rules/pipe.d.ts +38 -1
- package/rules/utility-rules/pipe.js +30 -1
- package/rules/utility-rules/required.d.ts +39 -1
- package/rules/utility-rules/required.js +32 -1
- package/rules/utility-rules/string-utils.d.ts +99 -5
- package/rules/utility-rules/string-utils.js +53 -3
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
* `null`/`undefined` input is passed through unchanged
|
|
3
|
+
* Validates that the value is strictly equal (`===`) to `compare`.
|
|
4
|
+
* `null`/`undefined` input is always passed through unchanged, regardless of
|
|
5
|
+
* `compare`.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The type of `compare`.
|
|
8
|
+
* @param compare - The value the input must strictly equal.
|
|
9
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
10
|
+
* @returns The input value unchanged when it strictly equals `compare` (or
|
|
11
|
+
* is nullish).
|
|
12
|
+
* @throws `Value must be equal to "<compare>"` if `input !== compare`.
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* vg.isEqual('a')('a'); // => 'a'
|
|
16
|
+
* vg.isEqual('a')('b'); // throws ValidationError: "Value must be equal to..."
|
|
17
|
+
* ```
|
|
5
18
|
* @validator isEqual
|
|
6
19
|
*/
|
|
7
20
|
export function isEqual(compare, options) {
|
|
@@ -14,8 +27,20 @@ export function isEqual(compare, options) {
|
|
|
14
27
|
}, options);
|
|
15
28
|
}
|
|
16
29
|
/**
|
|
17
|
-
* Validates
|
|
18
|
-
* `null`/`undefined` input is passed through unchanged
|
|
30
|
+
* Validates that the value is not strictly equal (`===`) to `compare`.
|
|
31
|
+
* `null`/`undefined` input is always passed through unchanged, regardless of
|
|
32
|
+
* `compare`.
|
|
33
|
+
*
|
|
34
|
+
* @param compare - The value the input must not strictly equal.
|
|
35
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
36
|
+
* @returns The input value unchanged when it does not strictly equal
|
|
37
|
+
* `compare` (or is nullish).
|
|
38
|
+
* @throws `Value must not be equal to "<compare>"` if `input === compare`.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* vg.isNotEqual('a')('b'); // => 'b'
|
|
42
|
+
* vg.isNotEqual('a')('a'); // throws ValidationError: "Value must not be equal to..."
|
|
43
|
+
* ```
|
|
19
44
|
* @validator isNotEqual
|
|
20
45
|
*/
|
|
21
46
|
export function isNotEqual(compare, options) {
|
|
@@ -1,12 +1,37 @@
|
|
|
1
1
|
import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
2
2
|
import type { range } from './range.js';
|
|
3
3
|
/**
|
|
4
|
-
* Checks
|
|
4
|
+
* Checks that the value is greater than `minValue`. Supports `number`,
|
|
5
|
+
* `bigint`, `Date`, and `string` comparisons (numbers and bigints can be
|
|
6
|
+
* compared against each other). Despite the `Nullish<T>` return type, there
|
|
7
|
+
* is **no** null/undefined passthrough here - nullish input falls through
|
|
8
|
+
* every branch and fails.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The type of `minValue` (`range.Input`: `number | bigint | Date | string`).
|
|
11
|
+
* @param minValue - The value the input must be strictly greater than.
|
|
12
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
13
|
+
* string comparisons.
|
|
14
|
+
* @returns The input value unchanged when it is greater than `minValue`.
|
|
15
|
+
* @throws `Value must be greater than <minValue>` (string `minValue` quoted,
|
|
16
|
+
* e.g. `"B"`) if the comparison fails, or if `input`/`minValue` are not a
|
|
17
|
+
* matching comparable type.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* vg.isGt(5)(6); // => 6
|
|
21
|
+
* vg.isGt(5)(5); // throws ValidationError: "...must be greater than 5"
|
|
22
|
+
* vg.isGt('B')('C'); // => 'C'
|
|
23
|
+
* ```
|
|
5
24
|
* @validator isGt
|
|
6
25
|
*/
|
|
7
26
|
export declare function isGt<T extends range.Input>(minValue: T, options?: isGt.Options): Validator;
|
|
8
27
|
export declare namespace isGt {
|
|
9
28
|
interface Options extends ValidationOptions {
|
|
29
|
+
/**
|
|
30
|
+
* For string comparisons, also passes if the lower-cased input is
|
|
31
|
+
* greater than the lower-cased `minValue` (in addition to the
|
|
32
|
+
* case-sensitive comparison).
|
|
33
|
+
* @defaultValue false
|
|
34
|
+
*/
|
|
10
35
|
caseInsensitive?: boolean;
|
|
11
36
|
}
|
|
12
37
|
}
|
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is greater than `minValue`. Supports `number`,
|
|
4
|
+
* `bigint`, `Date`, and `string` comparisons (numbers and bigints can be
|
|
5
|
+
* compared against each other). Despite the `Nullish<T>` return type, there
|
|
6
|
+
* is **no** null/undefined passthrough here - nullish input falls through
|
|
7
|
+
* every branch and fails.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - The type of `minValue` (`range.Input`: `number | bigint | Date | string`).
|
|
10
|
+
* @param minValue - The value the input must be strictly greater than.
|
|
11
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
12
|
+
* string comparisons.
|
|
13
|
+
* @returns The input value unchanged when it is greater than `minValue`.
|
|
14
|
+
* @throws `Value must be greater than <minValue>` (string `minValue` quoted,
|
|
15
|
+
* e.g. `"B"`) if the comparison fails, or if `input`/`minValue` are not a
|
|
16
|
+
* matching comparable type.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* vg.isGt(5)(6); // => 6
|
|
20
|
+
* vg.isGt(5)(5); // throws ValidationError: "...must be greater than 5"
|
|
21
|
+
* vg.isGt('B')('C'); // => 'C'
|
|
22
|
+
* ```
|
|
4
23
|
* @validator isGt
|
|
5
24
|
*/
|
|
6
25
|
export function isGt(minValue, options) {
|
|
@@ -1,12 +1,36 @@
|
|
|
1
1
|
import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
2
2
|
import type { range } from './range.js';
|
|
3
3
|
/**
|
|
4
|
-
* Checks
|
|
4
|
+
* Checks that the value is greater than or equal to `minValue`. Supports
|
|
5
|
+
* `number`, `bigint`, `Date`, and `string` comparisons. Despite the
|
|
6
|
+
* `Nullish<T>` return type, there is **no** null/undefined passthrough here
|
|
7
|
+
* - nullish input falls through every branch and fails. This validator is
|
|
8
|
+
* also reused internally by `lengthMin` via
|
|
9
|
+
* `pipe([getLength(), isGte(minValue, { onFail: ... })])`.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The type of `minValue` (`range.Input`: `number | bigint | Date | string`).
|
|
12
|
+
* @param minValue - The value the input must be greater than or equal to.
|
|
13
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
14
|
+
* string comparisons.
|
|
15
|
+
* @returns The input value unchanged when it is `>= minValue`.
|
|
16
|
+
* @throws `Value must be greater than or equal to <minValue>` (string
|
|
17
|
+
* `minValue` quoted) if the comparison fails, or if `input`/`minValue` are
|
|
18
|
+
* not a matching comparable type.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* vg.isGte(5)(5); // => 5
|
|
22
|
+
* vg.isGte(5)(4); // throws ValidationError: "...must be greater than or equal to 5"
|
|
23
|
+
* ```
|
|
5
24
|
* @validator isGte
|
|
6
25
|
*/
|
|
7
26
|
export declare function isGte<T extends range.Input>(minValue: T, options?: isGte.Options): Validator<T>;
|
|
8
27
|
export declare namespace isGte {
|
|
9
28
|
interface Options extends ValidationOptions {
|
|
29
|
+
/**
|
|
30
|
+
* For string comparisons, also passes if the lower-cased input is `>=`
|
|
31
|
+
* the lower-cased `minValue`.
|
|
32
|
+
* @defaultValue false
|
|
33
|
+
*/
|
|
10
34
|
caseInsensitive?: boolean;
|
|
11
35
|
}
|
|
12
36
|
}
|
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is greater than or equal to `minValue`. Supports
|
|
4
|
+
* `number`, `bigint`, `Date`, and `string` comparisons. Despite the
|
|
5
|
+
* `Nullish<T>` return type, there is **no** null/undefined passthrough here
|
|
6
|
+
* - nullish input falls through every branch and fails. This validator is
|
|
7
|
+
* also reused internally by `lengthMin` via
|
|
8
|
+
* `pipe([getLength(), isGte(minValue, { onFail: ... })])`.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The type of `minValue` (`range.Input`: `number | bigint | Date | string`).
|
|
11
|
+
* @param minValue - The value the input must be greater than or equal to.
|
|
12
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
13
|
+
* string comparisons.
|
|
14
|
+
* @returns The input value unchanged when it is `>= minValue`.
|
|
15
|
+
* @throws `Value must be greater than or equal to <minValue>` (string
|
|
16
|
+
* `minValue` quoted) if the comparison fails, or if `input`/`minValue` are
|
|
17
|
+
* not a matching comparable type.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* vg.isGte(5)(5); // => 5
|
|
21
|
+
* vg.isGte(5)(4); // throws ValidationError: "...must be greater than or equal to 5"
|
|
22
|
+
* ```
|
|
4
23
|
* @validator isGte
|
|
5
24
|
*/
|
|
6
25
|
export function isGte(minValue, options) {
|
|
@@ -1,12 +1,35 @@
|
|
|
1
1
|
import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
2
2
|
import type { range } from './range.js';
|
|
3
3
|
/**
|
|
4
|
-
* Checks
|
|
4
|
+
* Checks that the value is lower than `maxValue`. Supports `number`,
|
|
5
|
+
* `bigint`, `Date`, and `string` comparisons. Despite the `Nullish<T>`
|
|
6
|
+
* return type, there is **no** null/undefined passthrough here - nullish
|
|
7
|
+
* input falls through every branch and fails.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - The type of `maxValue` (`range.Input`: `number | bigint | Date | string`).
|
|
10
|
+
* @param maxValue - The value the input must be strictly lower than.
|
|
11
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
12
|
+
* string comparisons.
|
|
13
|
+
* @returns The input value unchanged when it is lower than `maxValue`.
|
|
14
|
+
* @throws `Value must be lower than <maxValue>` (string `maxValue` quoted)
|
|
15
|
+
* if the comparison fails, or if `input`/`maxValue` are not a matching
|
|
16
|
+
* comparable type.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* vg.isLt(5)(4); // => 4
|
|
20
|
+
* vg.isLt(5)(5); // throws ValidationError: "...must be lower than 5"
|
|
21
|
+
* vg.isLt('B')('A'); // => 'A'
|
|
22
|
+
* ```
|
|
5
23
|
* @validator isLt
|
|
6
24
|
*/
|
|
7
25
|
export declare function isLt<T extends range.Input>(maxValue: T, options?: isLt.Options): Validator;
|
|
8
26
|
export declare namespace isLt {
|
|
9
27
|
interface Options extends ValidationOptions {
|
|
28
|
+
/**
|
|
29
|
+
* For string comparisons, also passes if the lower-cased input is lower
|
|
30
|
+
* than the lower-cased `maxValue`.
|
|
31
|
+
* @defaultValue false
|
|
32
|
+
*/
|
|
10
33
|
caseInsensitive?: boolean;
|
|
11
34
|
}
|
|
12
35
|
}
|
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is lower than `maxValue`. Supports `number`,
|
|
4
|
+
* `bigint`, `Date`, and `string` comparisons. Despite the `Nullish<T>`
|
|
5
|
+
* return type, there is **no** null/undefined passthrough here - nullish
|
|
6
|
+
* input falls through every branch and fails.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - The type of `maxValue` (`range.Input`: `number | bigint | Date | string`).
|
|
9
|
+
* @param maxValue - The value the input must be strictly lower than.
|
|
10
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
11
|
+
* string comparisons.
|
|
12
|
+
* @returns The input value unchanged when it is lower than `maxValue`.
|
|
13
|
+
* @throws `Value must be lower than <maxValue>` (string `maxValue` quoted)
|
|
14
|
+
* if the comparison fails, or if `input`/`maxValue` are not a matching
|
|
15
|
+
* comparable type.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* vg.isLt(5)(4); // => 4
|
|
19
|
+
* vg.isLt(5)(5); // throws ValidationError: "...must be lower than 5"
|
|
20
|
+
* vg.isLt('B')('A'); // => 'A'
|
|
21
|
+
* ```
|
|
4
22
|
* @validator isLt
|
|
5
23
|
*/
|
|
6
24
|
export function isLt(maxValue, options) {
|
|
@@ -1,12 +1,36 @@
|
|
|
1
1
|
import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
2
2
|
import type { range } from './range.js';
|
|
3
3
|
/**
|
|
4
|
-
* Checks
|
|
4
|
+
* Checks that the value is lower than or equal to `maxValue`. Supports
|
|
5
|
+
* `number`, `bigint`, `Date`, and `string` comparisons. Despite the
|
|
6
|
+
* `Nullish<T>` return type, there is **no** null/undefined passthrough here
|
|
7
|
+
* - nullish input falls through every branch and fails. This validator is
|
|
8
|
+
* also reused internally by `lengthMax` via
|
|
9
|
+
* `pipe([getLength(), isLte(maxValue, { onFail: ... })])`.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The type of `maxValue` (`range.Input`: `number | bigint | Date | string`).
|
|
12
|
+
* @param maxValue - The value the input must be lower than or equal to.
|
|
13
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
14
|
+
* string comparisons.
|
|
15
|
+
* @returns The input value unchanged when it is `<= maxValue`.
|
|
16
|
+
* @throws `Value must be lower than or equal to <maxValue>` (string
|
|
17
|
+
* `maxValue` quoted) if the comparison fails, or if `input`/`maxValue` are
|
|
18
|
+
* not a matching comparable type.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* vg.isLte(5)(5); // => 5
|
|
22
|
+
* vg.isLte(5)(6); // throws ValidationError: "...must be lower than or equal to 5"
|
|
23
|
+
* ```
|
|
5
24
|
* @validator isLte
|
|
6
25
|
*/
|
|
7
26
|
export declare function isLte<T extends range.Input>(maxValue: T, options?: isLte.Options): Validator;
|
|
8
27
|
export declare namespace isLte {
|
|
9
28
|
interface Options extends ValidationOptions {
|
|
29
|
+
/**
|
|
30
|
+
* For string comparisons, also passes if the lower-cased input is `<=`
|
|
31
|
+
* the lower-cased `maxValue`.
|
|
32
|
+
* @defaultValue false
|
|
33
|
+
*/
|
|
10
34
|
caseInsensitive?: boolean;
|
|
11
35
|
}
|
|
12
36
|
}
|
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is lower than or equal to `maxValue`. Supports
|
|
4
|
+
* `number`, `bigint`, `Date`, and `string` comparisons. Despite the
|
|
5
|
+
* `Nullish<T>` return type, there is **no** null/undefined passthrough here
|
|
6
|
+
* - nullish input falls through every branch and fails. This validator is
|
|
7
|
+
* also reused internally by `lengthMax` via
|
|
8
|
+
* `pipe([getLength(), isLte(maxValue, { onFail: ... })])`.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The type of `maxValue` (`range.Input`: `number | bigint | Date | string`).
|
|
11
|
+
* @param maxValue - The value the input must be lower than or equal to.
|
|
12
|
+
* @param options - Validation options, including `caseInsensitive` for
|
|
13
|
+
* string comparisons.
|
|
14
|
+
* @returns The input value unchanged when it is `<= maxValue`.
|
|
15
|
+
* @throws `Value must be lower than or equal to <maxValue>` (string
|
|
16
|
+
* `maxValue` quoted) if the comparison fails, or if `input`/`maxValue` are
|
|
17
|
+
* not a matching comparable type.
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* vg.isLte(5)(5); // => 5
|
|
21
|
+
* vg.isLte(5)(6); // throws ValidationError: "...must be lower than or equal to 5"
|
|
22
|
+
* ```
|
|
4
23
|
* @validator isLte
|
|
5
24
|
*/
|
|
6
25
|
export function isLte(maxValue, options) {
|
|
@@ -1,10 +1,46 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Checks the length is at least
|
|
2
|
+
* Checks that the length of the value is at least `minValue`.
|
|
3
|
+
*
|
|
4
|
+
* Not a hand-rolled rule - it is composed from other validators:
|
|
5
|
+
* `allOf([pipe([getLength(), isGte(minValue, { onFail: ... })])])`.
|
|
6
|
+
* `getLength()` extracts the length/size of a string, array, or any object
|
|
7
|
+
* exposing a `length`/`size` property; `pipe` feeds that length into
|
|
8
|
+
* `isGte`; `allOf` wraps the pipeline so the composed validator still
|
|
9
|
+
* returns the *original* input (not the extracted length) on success.
|
|
10
|
+
*
|
|
11
|
+
* @param minValue - The minimum length the value must have.
|
|
12
|
+
* @returns The original input value unchanged when its length is
|
|
13
|
+
* `>= minValue`.
|
|
14
|
+
* @throws `Value length must be at least <minValue>` if the value's length
|
|
15
|
+
* is too short (or `Unable to get length` if `getLength()` cannot determine
|
|
16
|
+
* a length for the input).
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* vg.lengthMin(3)('1234'); // => '1234'
|
|
20
|
+
* vg.lengthMin(3)('ab'); // throws ValidationError: "Value length must be at least 3"
|
|
21
|
+
* ```
|
|
3
22
|
* @validator lengthMin
|
|
4
23
|
*/
|
|
5
24
|
export declare const lengthMin: (minValue: number) => import("../../index.js").Validator<any, any, import("../../index.js").ExecutionOptions>;
|
|
6
25
|
/**
|
|
7
|
-
* Checks
|
|
26
|
+
* Checks that the length of the value is at most `maxValue`.
|
|
27
|
+
*
|
|
28
|
+
* Like `lengthMin`, this is composed from other validators:
|
|
29
|
+
* `allOf([pipe([getLength(), isLte(maxValue, { onFail: ... })])])`.
|
|
30
|
+
*
|
|
31
|
+
* @param maxValue - The maximum length the value may have.
|
|
32
|
+
* @returns The original input value unchanged when its length is
|
|
33
|
+
* `<= maxValue`.
|
|
34
|
+
* @throws `The length of {{label}} must be at most <maxValue>` if the
|
|
35
|
+
* value's length is too long (`{{label}}` resolves to `Value` when no
|
|
36
|
+
* `label`/`location`/`property` is set on the context), or
|
|
37
|
+
* `Unable to get length` if `getLength()` cannot determine a length for the
|
|
38
|
+
* input.
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* vg.lengthMax(3)('12'); // => '12'
|
|
42
|
+
* vg.lengthMax(3)('1245'); // throws ValidationError: "The length of Value must be at most 3"
|
|
43
|
+
* ```
|
|
8
44
|
* @validator lengthMax
|
|
9
45
|
*/
|
|
10
46
|
export declare const lengthMax: (maxValue: number) => import("../../index.js").Validator<any, any, import("../../index.js").ExecutionOptions>;
|
|
@@ -4,7 +4,26 @@ import { pipe } from '../utility-rules/pipe.js';
|
|
|
4
4
|
import { isGte } from './is-gte.js';
|
|
5
5
|
import { isLte } from './is-lte.js';
|
|
6
6
|
/**
|
|
7
|
-
* Checks the length is at least
|
|
7
|
+
* Checks that the length of the value is at least `minValue`.
|
|
8
|
+
*
|
|
9
|
+
* Not a hand-rolled rule - it is composed from other validators:
|
|
10
|
+
* `allOf([pipe([getLength(), isGte(minValue, { onFail: ... })])])`.
|
|
11
|
+
* `getLength()` extracts the length/size of a string, array, or any object
|
|
12
|
+
* exposing a `length`/`size` property; `pipe` feeds that length into
|
|
13
|
+
* `isGte`; `allOf` wraps the pipeline so the composed validator still
|
|
14
|
+
* returns the *original* input (not the extracted length) on success.
|
|
15
|
+
*
|
|
16
|
+
* @param minValue - The minimum length the value must have.
|
|
17
|
+
* @returns The original input value unchanged when its length is
|
|
18
|
+
* `>= minValue`.
|
|
19
|
+
* @throws `Value length must be at least <minValue>` if the value's length
|
|
20
|
+
* is too short (or `Unable to get length` if `getLength()` cannot determine
|
|
21
|
+
* a length for the input).
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* vg.lengthMin(3)('1234'); // => '1234'
|
|
25
|
+
* vg.lengthMin(3)('ab'); // throws ValidationError: "Value length must be at least 3"
|
|
26
|
+
* ```
|
|
8
27
|
* @validator lengthMin
|
|
9
28
|
*/
|
|
10
29
|
export const lengthMin = (minValue) => allOf([
|
|
@@ -16,7 +35,24 @@ export const lengthMin = (minValue) => allOf([
|
|
|
16
35
|
]),
|
|
17
36
|
]);
|
|
18
37
|
/**
|
|
19
|
-
* Checks
|
|
38
|
+
* Checks that the length of the value is at most `maxValue`.
|
|
39
|
+
*
|
|
40
|
+
* Like `lengthMin`, this is composed from other validators:
|
|
41
|
+
* `allOf([pipe([getLength(), isLte(maxValue, { onFail: ... })])])`.
|
|
42
|
+
*
|
|
43
|
+
* @param maxValue - The maximum length the value may have.
|
|
44
|
+
* @returns The original input value unchanged when its length is
|
|
45
|
+
* `<= maxValue`.
|
|
46
|
+
* @throws `The length of {{label}} must be at most <maxValue>` if the
|
|
47
|
+
* value's length is too long (`{{label}}` resolves to `Value` when no
|
|
48
|
+
* `label`/`location`/`property` is set on the context), or
|
|
49
|
+
* `Unable to get length` if `getLength()` cannot determine a length for the
|
|
50
|
+
* input.
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* vg.lengthMax(3)('12'); // => '12'
|
|
54
|
+
* vg.lengthMax(3)('1245'); // throws ValidationError: "The length of Value must be at most 3"
|
|
55
|
+
* ```
|
|
20
56
|
* @validator lengthMax
|
|
21
57
|
*/
|
|
22
58
|
export const lengthMax = (maxValue) => allOf([
|
|
@@ -1,10 +1,34 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is between `minValue` and `maxValue`, inclusive on
|
|
4
|
+
* both ends. Supports `number`, `bigint`, `Date`, and `string` comparisons.
|
|
5
|
+
* Despite the `Nullish<T>` return type, there is **no** null/undefined
|
|
6
|
+
* passthrough here - nullish input falls through every branch and fails.
|
|
7
|
+
* Unlike `isGt`/`isGte`/`isLt`/`isLte`, numbers and bigints are not mixed
|
|
8
|
+
* across `input`/`minValue`/`maxValue` here - all three must independently
|
|
9
|
+
* be a number or a bigint - and there is no `caseInsensitive` option for
|
|
10
|
+
* string comparisons.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - The type of `minValue`/`maxValue` (`range.Input`:
|
|
13
|
+
* `number | bigint | Date | string`).
|
|
14
|
+
* @param minValue - The lower bound (inclusive).
|
|
15
|
+
* @param maxValue - The upper bound (inclusive).
|
|
16
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
17
|
+
* @returns The input value unchanged when `minValue <= input <= maxValue`.
|
|
18
|
+
* @throws `Value must be between <minValue> and <maxValue>` (values
|
|
19
|
+
* interpolated directly, not quoted even for strings) if the input is out
|
|
20
|
+
* of bounds, or if its type doesn't match `minValue`/`maxValue`.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* vg.range(5, 10)(7); // => 7
|
|
24
|
+
* vg.range(5, 10)(4); // throws ValidationError: "Value must be between 5 and 10"
|
|
25
|
+
* ```
|
|
4
26
|
* @validator range
|
|
5
27
|
*/
|
|
6
28
|
export declare function range<T extends range.Input>(minValue: T, maxValue: T, options?: range.Options): import("../../core/validator.js").Validator<T, T, import("../../core/types.js").ExecutionOptions>;
|
|
7
29
|
export declare namespace range {
|
|
30
|
+
/** The value types `range` (and `isGt`/`isGte`/`isLt`/`isLte`) support comparing. */
|
|
8
31
|
type Input = number | bigint | Date | string;
|
|
32
|
+
/** Options for {@link range} - a plain alias for `ValidationOptions`, adding no extra properties. */
|
|
9
33
|
type Options = ValidationOptions;
|
|
10
34
|
}
|
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is between `minValue` and `maxValue`, inclusive on
|
|
4
|
+
* both ends. Supports `number`, `bigint`, `Date`, and `string` comparisons.
|
|
5
|
+
* Despite the `Nullish<T>` return type, there is **no** null/undefined
|
|
6
|
+
* passthrough here - nullish input falls through every branch and fails.
|
|
7
|
+
* Unlike `isGt`/`isGte`/`isLt`/`isLte`, numbers and bigints are not mixed
|
|
8
|
+
* across `input`/`minValue`/`maxValue` here - all three must independently
|
|
9
|
+
* be a number or a bigint - and there is no `caseInsensitive` option for
|
|
10
|
+
* string comparisons.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - The type of `minValue`/`maxValue` (`range.Input`:
|
|
13
|
+
* `number | bigint | Date | string`).
|
|
14
|
+
* @param minValue - The lower bound (inclusive).
|
|
15
|
+
* @param maxValue - The upper bound (inclusive).
|
|
16
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
17
|
+
* @returns The input value unchanged when `minValue <= input <= maxValue`.
|
|
18
|
+
* @throws `Value must be between <minValue> and <maxValue>` (values
|
|
19
|
+
* interpolated directly, not quoted even for strings) if the input is out
|
|
20
|
+
* of bounds, or if its type doesn't match `minValue`/`maxValue`.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* vg.range(5, 10)(7); // => 7
|
|
24
|
+
* vg.range(5, 10)(4); // throws ValidationError: "Value must be between 5 and 10"
|
|
25
|
+
* ```
|
|
4
26
|
* @validator range
|
|
5
27
|
*/
|
|
6
28
|
export function range(minValue, maxValue, options) {
|
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Does nothing,
|
|
2
|
+
* Does nothing and always succeeds, returning the original input value
|
|
3
|
+
* unchanged - useful as a permissive item/value rule inside `isArray`,
|
|
4
|
+
* `isRecord`, `isTuple`, etc.
|
|
3
5
|
* @validator isAny
|
|
6
|
+
* @returns The input value, unmodified.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { isAny, vg } from 'valgen';
|
|
10
|
+
*
|
|
11
|
+
* isAny('anything'); // => 'anything'
|
|
12
|
+
* isAny(null); // => null
|
|
13
|
+
* vg.isRecord(vg.isString, isAny)({ a: 1, b: 'x' }); // => { a: 1, b: 'x' }
|
|
14
|
+
* ```
|
|
4
15
|
*/
|
|
5
16
|
export declare const isAny: () => import("../../core/validator.js").Validator<any, any, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Does nothing,
|
|
3
|
+
* Does nothing and always succeeds, returning the original input value
|
|
4
|
+
* unchanged - useful as a permissive item/value rule inside `isArray`,
|
|
5
|
+
* `isRecord`, `isTuple`, etc.
|
|
4
6
|
* @validator isAny
|
|
7
|
+
* @returns The input value, unmodified.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { isAny, vg } from 'valgen';
|
|
11
|
+
*
|
|
12
|
+
* isAny('anything'); // => 'anything'
|
|
13
|
+
* isAny(null); // => null
|
|
14
|
+
* vg.isRecord(vg.isString, isAny)({ a: 1, b: 'x' }); // => { a: 1, b: 'x' }
|
|
15
|
+
* ```
|
|
5
16
|
*/
|
|
6
17
|
export const isAny = () => validator(isAny.name, (input) => input);
|
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
import type { ValidationOptions, Validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is an array, optionally validating (and
|
|
4
|
+
* coercing) each item against `itemValidator`. With `coerce: true`, a
|
|
5
|
+
* non-null, non-array input is wrapped into a single-item array before
|
|
6
|
+
* validating.
|
|
5
7
|
* @validator isArray
|
|
8
|
+
* @param itemValidator - Optional rule applied to each array element; when
|
|
9
|
+
* omitted, only array-ness is checked.
|
|
10
|
+
* @param options - Validation options.
|
|
11
|
+
* @returns The validated array, with each item replaced by its (possibly
|
|
12
|
+
* coerced) validated value.
|
|
13
|
+
* @throws `Value must be an array` if the input is `null`, `undefined`, or
|
|
14
|
+
* not an array (and not coercible into one).
|
|
15
|
+
* @throws `Item at index [i] is not valid. <underlying message>` if an item
|
|
16
|
+
* fails `itemValidator`.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { isArray, vg } from 'valgen';
|
|
20
|
+
* import { isInteger } from 'valgen';
|
|
21
|
+
*
|
|
22
|
+
* isArray([true]); // => [true]
|
|
23
|
+
* vg.isArray(isInteger)([1, 2]); // => [1, 2]
|
|
24
|
+
* vg.isArray(isInteger)(['1', '2']);
|
|
25
|
+
* // throws: 'Item at index [0] is not valid. Value must be a valid integer value'
|
|
26
|
+
* ```
|
|
6
27
|
*/
|
|
7
28
|
export declare function isArray<T, I>(itemValidator?: Validator<T, I>, options?: isArray.Options): Validator<T[], I | I[], import("../../core/types.js").ExecutionOptions>;
|
|
8
29
|
export declare namespace isArray {
|
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is an array, optionally validating (and
|
|
4
|
+
* coercing) each item against `itemValidator`. With `coerce: true`, a
|
|
5
|
+
* non-null, non-array input is wrapped into a single-item array before
|
|
6
|
+
* validating.
|
|
5
7
|
* @validator isArray
|
|
8
|
+
* @param itemValidator - Optional rule applied to each array element; when
|
|
9
|
+
* omitted, only array-ness is checked.
|
|
10
|
+
* @param options - Validation options.
|
|
11
|
+
* @returns The validated array, with each item replaced by its (possibly
|
|
12
|
+
* coerced) validated value.
|
|
13
|
+
* @throws `Value must be an array` if the input is `null`, `undefined`, or
|
|
14
|
+
* not an array (and not coercible into one).
|
|
15
|
+
* @throws `Item at index [i] is not valid. <underlying message>` if an item
|
|
16
|
+
* fails `itemValidator`.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { isArray, vg } from 'valgen';
|
|
20
|
+
* import { isInteger } from 'valgen';
|
|
21
|
+
*
|
|
22
|
+
* isArray([true]); // => [true]
|
|
23
|
+
* vg.isArray(isInteger)([1, 2]); // => [1, 2]
|
|
24
|
+
* vg.isArray(isInteger)(['1', '2']);
|
|
25
|
+
* // throws: 'Item at index [0] is not valid. Value must be a valid integer value'
|
|
26
|
+
* ```
|
|
6
27
|
*/
|
|
7
28
|
export function isArray(itemValidator, options) {
|
|
8
29
|
return validator(isArray.name, (input, context, _this) => {
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is a `bigint`. With `coerce: true`, allows
|
|
4
|
+
* converting a `number` or a numeric `string` to `bigint` via
|
|
5
|
+
* `BigInt(input)`.
|
|
5
6
|
* @validator isBigint
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The `bigint` value.
|
|
9
|
+
* @throws `Value must be a BigInt` if the input is not a `bigint` (and,
|
|
10
|
+
* with `coerce`, cannot be converted into one via `BigInt(input)`).
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { isBigint } from 'valgen';
|
|
14
|
+
*
|
|
15
|
+
* isBigint(1n); // => 1n
|
|
16
|
+
* isBigint('4', { coerce: true }); // => 4n
|
|
17
|
+
* ```
|
|
6
18
|
*/
|
|
7
19
|
export declare function isBigint(options?: isBigint.Options): import("../../core/validator.js").Validator<bigint, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
8
20
|
export declare namespace isBigint {
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is a `bigint`. With `coerce: true`, allows
|
|
4
|
+
* converting a `number` or a numeric `string` to `bigint` via
|
|
5
|
+
* `BigInt(input)`.
|
|
5
6
|
* @validator isBigint
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The `bigint` value.
|
|
9
|
+
* @throws `Value must be a BigInt` if the input is not a `bigint` (and,
|
|
10
|
+
* with `coerce`, cannot be converted into one via `BigInt(input)`).
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { isBigint } from 'valgen';
|
|
14
|
+
*
|
|
15
|
+
* isBigint(1n); // => 1n
|
|
16
|
+
* isBigint('4', { coerce: true }); // => 4n
|
|
17
|
+
* ```
|
|
6
18
|
*/
|
|
7
19
|
export function isBigint(options) {
|
|
8
20
|
return validator(isBigint.name, (input, context, _this) => {
|