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,8 +1,23 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is a `boolean`. With `coerce: true`, converts
|
|
4
|
+
* `1`/`0` and common boolean-like strings (`'true'`, `'yes'`, `'false'`,
|
|
5
|
+
* `'no'`, etc., case-insensitive) into `true`/`false`.
|
|
5
6
|
* @validator isBoolean
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The validated (and possibly coerced) `boolean` value.
|
|
9
|
+
* @throws `Value must be a boolean` if the input is not an actual `boolean`
|
|
10
|
+
* and cannot be coerced.
|
|
11
|
+
* @throws `Invalid boolean string` if `coerce: true` and a string input
|
|
12
|
+
* matches neither the true nor the false pattern.
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { isBoolean } from 'valgen';
|
|
16
|
+
*
|
|
17
|
+
* isBoolean(true); // => true
|
|
18
|
+
* isBoolean(1, { coerce: true }); // => true
|
|
19
|
+
* isBoolean('yes', { coerce: true }); // => true
|
|
20
|
+
* ```
|
|
6
21
|
*/
|
|
7
22
|
export declare function isBoolean(options?: isBoolean.Options): import("../../core/validator.js").Validator<boolean | undefined, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
8
23
|
export declare namespace isBoolean {
|
|
@@ -2,9 +2,24 @@ import { validator, } from '../../core/index.js';
|
|
|
2
2
|
const TRUE_PATTERN = /^(?:true|t|1|yes|y)$/i;
|
|
3
3
|
const FALSE_PATTERN = /^(?:false|f|0|no|n)$/i;
|
|
4
4
|
/**
|
|
5
|
-
* Validates
|
|
6
|
-
*
|
|
5
|
+
* Validates that the value is a `boolean`. With `coerce: true`, converts
|
|
6
|
+
* `1`/`0` and common boolean-like strings (`'true'`, `'yes'`, `'false'`,
|
|
7
|
+
* `'no'`, etc., case-insensitive) into `true`/`false`.
|
|
7
8
|
* @validator isBoolean
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns The validated (and possibly coerced) `boolean` value.
|
|
11
|
+
* @throws `Value must be a boolean` if the input is not an actual `boolean`
|
|
12
|
+
* and cannot be coerced.
|
|
13
|
+
* @throws `Invalid boolean string` if `coerce: true` and a string input
|
|
14
|
+
* matches neither the true nor the false pattern.
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { isBoolean } from 'valgen';
|
|
18
|
+
*
|
|
19
|
+
* isBoolean(true); // => true
|
|
20
|
+
* isBoolean(1, { coerce: true }); // => true
|
|
21
|
+
* isBoolean('yes', { coerce: true }); // => true
|
|
22
|
+
* ```
|
|
8
23
|
*/
|
|
9
24
|
export function isBoolean(options) {
|
|
10
25
|
return validator(isBoolean.name, (input, context, _this) => {
|
|
@@ -1,29 +1,71 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
5
|
-
* converts it to a Date
|
|
3
|
+
* Validates that the value is a `Date` instance, with an optional
|
|
4
|
+
* precision-trimming step. With `coerce: true`, also accepts a `number`
|
|
5
|
+
* (epoch milliseconds) or a date-like `string` and converts it to a `Date`.
|
|
6
|
+
* Without `coerce`, a date *string* is rejected even if well-formed ISO
|
|
7
|
+
* 8601 - only actual `Date` instances validate by default.
|
|
6
8
|
* @validator isDate
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns The validated `Date` instance, with fields below `trim`'s
|
|
11
|
+
* precision zeroed out when `trim` is given.
|
|
12
|
+
* @throws `Value is not valid date` if the input isn't (or can't be coerced
|
|
13
|
+
* into) a valid `Date`.
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { isDate, vg } from 'valgen';
|
|
17
|
+
*
|
|
18
|
+
* isDate(new Date(1)); // => new Date(1)
|
|
19
|
+
* isDate('2020-01-10T08:30:15Z', { coerce: true });
|
|
20
|
+
* // => new Date('2020-01-10T08:30:15Z')
|
|
21
|
+
* vg.isDate({ trim: 'day', coerce: true })('2020-05-10T08:30:15.123');
|
|
22
|
+
* // => new Date('2020-05-10T00:00:00')
|
|
23
|
+
* ```
|
|
7
24
|
*/
|
|
8
25
|
export declare function isDate(options?: isDate.Options): import("../../core/validator.js").Validator<Date, string | number | Date, import("../../core/types.js").ExecutionOptions>;
|
|
9
26
|
export declare namespace isDate {
|
|
10
27
|
type Precision = 'year' | 'yr' | 'month' | 'mo' | 'day' | 'd' | 'hours' | 'hr' | 'minutes' | 'min' | 'seconds' | 'sec' | 'milliseconds' | 'ms' | 'tz';
|
|
11
28
|
interface Options extends ValidationOptions {
|
|
29
|
+
/** Zeroes out the resulting `Date`'s fields below this precision (e.g. `'day'` zeroes hours/minutes/seconds/ms); only `year` through `seconds` have an effect - `milliseconds`/`ms`/`tz` are no-ops. */
|
|
12
30
|
trim?: Precision;
|
|
13
31
|
}
|
|
14
32
|
}
|
|
15
33
|
/**
|
|
16
|
-
* Validates
|
|
17
|
-
*
|
|
34
|
+
* Validates that the value is (or, with `coerce: true`, can be normalized
|
|
35
|
+
* into) a date-formatted string within a `[precisionMin, precisionMax]`
|
|
36
|
+
* range. Accepts ISO-8601-like strings as well as `Date`/`number` values
|
|
37
|
+
* when `coerce: true`.
|
|
18
38
|
* @validator isDateString
|
|
39
|
+
* @param options - Validation options.
|
|
40
|
+
* @returns The original string when valid (or, with `coerce: true`, the
|
|
41
|
+
* normalized/trimmed date string).
|
|
42
|
+
* @throws `Minimum date precision should be <precisionMin>` if the parsed
|
|
43
|
+
* precision is below `precisionMin`.
|
|
44
|
+
* @throws `Maximum date precision should be <precisionMax>` if the parsed
|
|
45
|
+
* precision is above `precisionMax` (and the input wasn't already a `Date`).
|
|
46
|
+
* @throws `Value "<input>" is not a valid date string` if the input can't
|
|
47
|
+
* be parsed as a date at all.
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { isDateString, vg } from 'valgen';
|
|
51
|
+
*
|
|
52
|
+
* isDateString('2020-01-10T08:30:15Z'); // => '2020-01-10T08:30:15Z'
|
|
53
|
+
* vg.isDateString({ precisionMin: 'day' })('2020-11-01'); // => '2020-11-01'
|
|
54
|
+
* vg.isDateString({ coerce: true, trim: true, precisionMax: 'day' })('2020-11-01T00:00:00+03:00');
|
|
55
|
+
* // => '2020-11-01'
|
|
56
|
+
* ```
|
|
19
57
|
*/
|
|
20
58
|
export declare function isDateString(options?: isDateString.Options): import("../../core/validator.js").Validator<string, string | number | Date, import("../../core/types.js").ExecutionOptions>;
|
|
21
59
|
export declare namespace isDateString {
|
|
22
60
|
type Precision = isDate.Precision;
|
|
23
61
|
interface Options extends ValidationOptions {
|
|
62
|
+
/** The minimum precision the input string must carry (e.g. `'day'` rejects a bare year). @defaultValue 'minutes' */
|
|
24
63
|
precisionMin?: Precision;
|
|
64
|
+
/** The maximum precision accepted; a string more precise than this fails unless the input was already a `Date`. @defaultValue 'tz' */
|
|
25
65
|
precisionMax?: Precision;
|
|
66
|
+
/** When `coerce: true`, truncates the output string down to `precisionMax` instead of only validating precision. @defaultValue false */
|
|
26
67
|
trim?: boolean;
|
|
68
|
+
/** When coercing, controls whether `-`/`:`/`T` separators are included in the output (`false` produces a compact form like `20201101102345.123`). @defaultValue true */
|
|
27
69
|
separators?: boolean;
|
|
28
70
|
}
|
|
29
71
|
}
|
|
@@ -2,10 +2,27 @@ import * as datefns from 'date-fns';
|
|
|
2
2
|
import {} from 'ts-gems';
|
|
3
3
|
import { validator, } from '../../core/index.js';
|
|
4
4
|
/**
|
|
5
|
-
* Validates
|
|
6
|
-
*
|
|
7
|
-
* converts it to a Date
|
|
5
|
+
* Validates that the value is a `Date` instance, with an optional
|
|
6
|
+
* precision-trimming step. With `coerce: true`, also accepts a `number`
|
|
7
|
+
* (epoch milliseconds) or a date-like `string` and converts it to a `Date`.
|
|
8
|
+
* Without `coerce`, a date *string* is rejected even if well-formed ISO
|
|
9
|
+
* 8601 - only actual `Date` instances validate by default.
|
|
8
10
|
* @validator isDate
|
|
11
|
+
* @param options - Validation options.
|
|
12
|
+
* @returns The validated `Date` instance, with fields below `trim`'s
|
|
13
|
+
* precision zeroed out when `trim` is given.
|
|
14
|
+
* @throws `Value is not valid date` if the input isn't (or can't be coerced
|
|
15
|
+
* into) a valid `Date`.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { isDate, vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* isDate(new Date(1)); // => new Date(1)
|
|
21
|
+
* isDate('2020-01-10T08:30:15Z', { coerce: true });
|
|
22
|
+
* // => new Date('2020-01-10T08:30:15Z')
|
|
23
|
+
* vg.isDate({ trim: 'day', coerce: true })('2020-05-10T08:30:15.123');
|
|
24
|
+
* // => new Date('2020-05-10T00:00:00')
|
|
25
|
+
* ```
|
|
9
26
|
*/
|
|
10
27
|
export function isDate(options) {
|
|
11
28
|
const trim = options?.trim;
|
|
@@ -34,9 +51,29 @@ export function isDate(options) {
|
|
|
34
51
|
}, options);
|
|
35
52
|
}
|
|
36
53
|
/**
|
|
37
|
-
* Validates
|
|
38
|
-
*
|
|
54
|
+
* Validates that the value is (or, with `coerce: true`, can be normalized
|
|
55
|
+
* into) a date-formatted string within a `[precisionMin, precisionMax]`
|
|
56
|
+
* range. Accepts ISO-8601-like strings as well as `Date`/`number` values
|
|
57
|
+
* when `coerce: true`.
|
|
39
58
|
* @validator isDateString
|
|
59
|
+
* @param options - Validation options.
|
|
60
|
+
* @returns The original string when valid (or, with `coerce: true`, the
|
|
61
|
+
* normalized/trimmed date string).
|
|
62
|
+
* @throws `Minimum date precision should be <precisionMin>` if the parsed
|
|
63
|
+
* precision is below `precisionMin`.
|
|
64
|
+
* @throws `Maximum date precision should be <precisionMax>` if the parsed
|
|
65
|
+
* precision is above `precisionMax` (and the input wasn't already a `Date`).
|
|
66
|
+
* @throws `Value "<input>" is not a valid date string` if the input can't
|
|
67
|
+
* be parsed as a date at all.
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { isDateString, vg } from 'valgen';
|
|
71
|
+
*
|
|
72
|
+
* isDateString('2020-01-10T08:30:15Z'); // => '2020-01-10T08:30:15Z'
|
|
73
|
+
* vg.isDateString({ precisionMin: 'day' })('2020-11-01'); // => '2020-11-01'
|
|
74
|
+
* vg.isDateString({ coerce: true, trim: true, precisionMax: 'day' })('2020-11-01T00:00:00+03:00');
|
|
75
|
+
* // => '2020-11-01'
|
|
76
|
+
* ```
|
|
40
77
|
*/
|
|
41
78
|
export function isDateString(options) {
|
|
42
79
|
const trim = options?.trim;
|
|
@@ -1,12 +1,34 @@
|
|
|
1
1
|
import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
3
|
+
* Validates that the value is one of a fixed set of enumeration members.
|
|
4
|
+
* `values` may be a single value, an array of allowed values, or a plain
|
|
5
|
+
* object/TypeScript enum (its non-numeric-key values are used, so both
|
|
6
|
+
* string enums and numeric enums with reverse mappings work correctly).
|
|
4
7
|
* @validator isEnum
|
|
8
|
+
* @param values - The allowed value(s): a single value, an array, or an
|
|
9
|
+
* object/enum whose values are used.
|
|
10
|
+
* @param options - Validation options.
|
|
11
|
+
* @returns The input value, unchanged (original casing is preserved even
|
|
12
|
+
* under `caseInSensitive`).
|
|
13
|
+
* @throws `Value must be one of enumeration member` (suffixed
|
|
14
|
+
* ` (<enumName>)` when `enumName` is set) if the value doesn't match any
|
|
15
|
+
* allowed member, or is `null`/`undefined`.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* vg.isEnum(['a', 'b'])('a'); // => 'a'
|
|
21
|
+
* vg.isEnum(['a', 'b'], { enumName: 'Suit' })('c');
|
|
22
|
+
* // throws: 'Value must be one of enumeration member (Suit)'
|
|
23
|
+
* vg.isEnum(['A', 'B'], { caseInSensitive: true })('a'); // => 'a'
|
|
24
|
+
* ```
|
|
5
25
|
*/
|
|
6
26
|
export declare function isEnum<T1>(values: any, options?: isEnum.Options): Validator<T1, any>;
|
|
7
27
|
export declare namespace isEnum {
|
|
8
28
|
interface Options extends ValidationOptions {
|
|
29
|
+
/** Compares `string` values case-insensitively (matching is done on `.toUpperCase()`), but the original input casing is returned. @defaultValue false */
|
|
9
30
|
caseInSensitive?: boolean;
|
|
31
|
+
/** Included in the failure message as `... (enumName)` for clearer errors. */
|
|
10
32
|
enumName?: string;
|
|
11
33
|
}
|
|
12
34
|
}
|
|
@@ -1,7 +1,27 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
3
|
+
* Validates that the value is one of a fixed set of enumeration members.
|
|
4
|
+
* `values` may be a single value, an array of allowed values, or a plain
|
|
5
|
+
* object/TypeScript enum (its non-numeric-key values are used, so both
|
|
6
|
+
* string enums and numeric enums with reverse mappings work correctly).
|
|
4
7
|
* @validator isEnum
|
|
8
|
+
* @param values - The allowed value(s): a single value, an array, or an
|
|
9
|
+
* object/enum whose values are used.
|
|
10
|
+
* @param options - Validation options.
|
|
11
|
+
* @returns The input value, unchanged (original casing is preserved even
|
|
12
|
+
* under `caseInSensitive`).
|
|
13
|
+
* @throws `Value must be one of enumeration member` (suffixed
|
|
14
|
+
* ` (<enumName>)` when `enumName` is set) if the value doesn't match any
|
|
15
|
+
* allowed member, or is `null`/`undefined`.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* vg.isEnum(['a', 'b'])('a'); // => 'a'
|
|
21
|
+
* vg.isEnum(['a', 'b'], { enumName: 'Suit' })('c');
|
|
22
|
+
* // throws: 'Value must be one of enumeration member (Suit)'
|
|
23
|
+
* vg.isEnum(['A', 'B'], { caseInSensitive: true })('a'); // => 'a'
|
|
24
|
+
* ```
|
|
5
25
|
*/
|
|
6
26
|
export function isEnum(values, options) {
|
|
7
27
|
const caseInSensitive = !!options?.caseInSensitive;
|
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
import type { Type } from 'ts-gems';
|
|
2
2
|
import { type ValidationOptions } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates that the value is an instance of a given class. With
|
|
5
|
+
* `coerce: true`, a *plain* object (per `@jsopen/objects`' `isPlainObject`)
|
|
6
|
+
* is mutated in place - via `Object.setPrototypeOf` - to become an instance
|
|
7
|
+
* of `clazz`; an object that is already an instance of some other class is
|
|
8
|
+
* not coerced.
|
|
5
9
|
* @validator isInstanceOf
|
|
10
|
+
* @param clazz - The class the value must be an instance of.
|
|
11
|
+
* @param options - Validation options.
|
|
12
|
+
* @returns The input value, unchanged (or coerced in place into an
|
|
13
|
+
* instance of `clazz`).
|
|
14
|
+
* @throws `Value must be an instance of "<clazz.name>"` if the input is
|
|
15
|
+
* `null`, `undefined`, a primitive, or an instance of a different class.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { isInstanceOf, vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* class Class1 {}
|
|
21
|
+
* const c1 = new Class1();
|
|
22
|
+
*
|
|
23
|
+
* isInstanceOf(Class1, c1); // => c1
|
|
24
|
+
* isInstanceOf(Class1, {}, { coerce: true }); // => instance of Class1
|
|
25
|
+
* ```
|
|
6
26
|
*/
|
|
7
27
|
export declare function isInstanceOf<T extends object>(clazz: Type<T>, options?: isInstanceOf.Options): import("../../core/validator.js").Validator<T, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
8
28
|
export declare namespace isInstanceOf {
|
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
import { isPlainObject } from '@jsopen/objects';
|
|
2
2
|
import { validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates that the value is an instance of a given class. With
|
|
5
|
+
* `coerce: true`, a *plain* object (per `@jsopen/objects`' `isPlainObject`)
|
|
6
|
+
* is mutated in place - via `Object.setPrototypeOf` - to become an instance
|
|
7
|
+
* of `clazz`; an object that is already an instance of some other class is
|
|
8
|
+
* not coerced.
|
|
5
9
|
* @validator isInstanceOf
|
|
10
|
+
* @param clazz - The class the value must be an instance of.
|
|
11
|
+
* @param options - Validation options.
|
|
12
|
+
* @returns The input value, unchanged (or coerced in place into an
|
|
13
|
+
* instance of `clazz`).
|
|
14
|
+
* @throws `Value must be an instance of "<clazz.name>"` if the input is
|
|
15
|
+
* `null`, `undefined`, a primitive, or an instance of a different class.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { isInstanceOf, vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* class Class1 {}
|
|
21
|
+
* const c1 = new Class1();
|
|
22
|
+
*
|
|
23
|
+
* isInstanceOf(Class1, c1); // => c1
|
|
24
|
+
* isInstanceOf(Class1, {}, { coerce: true }); // => instance of Class1
|
|
25
|
+
* ```
|
|
6
26
|
*/
|
|
7
27
|
export function isInstanceOf(clazz, options) {
|
|
8
28
|
return validator(isInstanceOf.name, (input, context, _this) => {
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is an integer `number`. With `coerce: true`,
|
|
4
|
+
* allows parsing a `string` (via `parseFloat`) or converting a lossless
|
|
5
|
+
* `bigint` into an integer `number`.
|
|
5
6
|
* @validator isInteger
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The validated (and possibly coerced) integer `number`.
|
|
9
|
+
* @throws `Value must be a valid integer value` if the input is `NaN`, a
|
|
10
|
+
* non-integer float, or (without coercion, or when coercion would lose
|
|
11
|
+
* precision for a `bigint`) not an integer `number`.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isInteger } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isInteger(1); // => 1
|
|
17
|
+
* isInteger('4', { coerce: true }); // => 4
|
|
18
|
+
* isInteger(10000000000000000001n, { coerce: true });
|
|
19
|
+
* // throws: 'Value must be a valid integer value' (precision would be lost)
|
|
20
|
+
* ```
|
|
6
21
|
*/
|
|
7
22
|
export declare function isInteger(options?: isInteger.Options): import("../../core/validator.js").Validator<number, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
8
23
|
export declare namespace isInteger {
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is an integer `number`. With `coerce: true`,
|
|
4
|
+
* allows parsing a `string` (via `parseFloat`) or converting a lossless
|
|
5
|
+
* `bigint` into an integer `number`.
|
|
5
6
|
* @validator isInteger
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The validated (and possibly coerced) integer `number`.
|
|
9
|
+
* @throws `Value must be a valid integer value` if the input is `NaN`, a
|
|
10
|
+
* non-integer float, or (without coercion, or when coercion would lose
|
|
11
|
+
* precision for a `bigint`) not an integer `number`.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isInteger } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isInteger(1); // => 1
|
|
17
|
+
* isInteger('4', { coerce: true }); // => 4
|
|
18
|
+
* isInteger(10000000000000000001n, { coerce: true });
|
|
19
|
+
* // throws: 'Value must be a valid integer value' (precision would be lost)
|
|
20
|
+
* ```
|
|
6
21
|
*/
|
|
7
22
|
export function isInteger(options) {
|
|
8
23
|
return validator(isInteger.name, (input, context, _this) => {
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
3
|
+
* Validates that the value is exactly `null`; even `undefined` fails.
|
|
4
4
|
* @validator isNull
|
|
5
|
+
* @param options - Validation options.
|
|
6
|
+
* @returns `null`.
|
|
7
|
+
* @throws `Value must be null` if the input is anything other than `null`.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { isNull } from 'valgen';
|
|
11
|
+
*
|
|
12
|
+
* isNull(null); // => null
|
|
13
|
+
* isNull(undefined); // throws ValidationError: 'Value must be null'
|
|
14
|
+
* ```
|
|
5
15
|
*/
|
|
6
16
|
export declare function isNull(options?: isNull.Options): import("../../core/validator.js").Validator<null, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
7
17
|
export declare namespace isNull {
|
|
@@ -9,8 +19,20 @@ export declare namespace isNull {
|
|
|
9
19
|
}
|
|
10
20
|
}
|
|
11
21
|
/**
|
|
12
|
-
* Validates
|
|
22
|
+
* Validates that the value is anything other than `null` - notably,
|
|
23
|
+
* `undefined`, `''`, `0`, and `NaN` all pass.
|
|
13
24
|
* @validator isNotNull
|
|
25
|
+
* @param options - Validation options.
|
|
26
|
+
* @returns The input value, unchanged.
|
|
27
|
+
* @throws `Value is null` if the input is `null`.
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { isNotNull } from 'valgen';
|
|
31
|
+
*
|
|
32
|
+
* isNotNull(undefined); // => undefined
|
|
33
|
+
* isNotNull(0); // => 0
|
|
34
|
+
* isNotNull(null); // throws ValidationError: 'Value is null'
|
|
35
|
+
* ```
|
|
14
36
|
*/
|
|
15
37
|
export declare function isNotNull(options?: isNotNull.Options): import("../../core/validator.js").Validator<unknown, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
16
38
|
export declare namespace isNotNull {
|
|
@@ -18,8 +40,20 @@ export declare namespace isNotNull {
|
|
|
18
40
|
}
|
|
19
41
|
}
|
|
20
42
|
/**
|
|
21
|
-
* Validates
|
|
43
|
+
* Validates that the value is `null` or `undefined`.
|
|
22
44
|
* @validator isNullish
|
|
45
|
+
* @param options - Validation options.
|
|
46
|
+
* @returns The input value (`null` or `undefined`), unchanged.
|
|
47
|
+
* @throws `Value is not nullish` if the input is anything other than
|
|
48
|
+
* `null` or `undefined`.
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { isNullish } from 'valgen';
|
|
52
|
+
*
|
|
53
|
+
* isNullish(null); // => null
|
|
54
|
+
* isNullish(undefined); // => undefined
|
|
55
|
+
* isNullish(''); // throws ValidationError: 'Value is not nullish'
|
|
56
|
+
* ```
|
|
23
57
|
*/
|
|
24
58
|
export declare function isNullish(options?: isNullish.Options): import("../../core/validator.js").Validator<null, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
25
59
|
export declare namespace isNullish {
|
|
@@ -27,8 +61,21 @@ export declare namespace isNullish {
|
|
|
27
61
|
}
|
|
28
62
|
}
|
|
29
63
|
/**
|
|
30
|
-
* Validates
|
|
64
|
+
* Validates that the value is neither `null` nor `undefined` - `0`, `''`,
|
|
65
|
+
* and `NaN` all pass.
|
|
31
66
|
* @validator isNotNullish
|
|
67
|
+
* @param options - Validation options.
|
|
68
|
+
* @returns The input value, unchanged.
|
|
69
|
+
* @throws `Value is null` if the input is `null`.
|
|
70
|
+
* @throws `Value is undefined` if the input is `undefined`.
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { isNotNullish } from 'valgen';
|
|
74
|
+
*
|
|
75
|
+
* isNotNullish(0); // => 0
|
|
76
|
+
* isNotNullish(null); // throws ValidationError: 'Value is null'
|
|
77
|
+
* isNotNullish(undefined); // throws ValidationError: 'Value is undefined'
|
|
78
|
+
* ```
|
|
32
79
|
*/
|
|
33
80
|
export declare function isNotNullish(options?: isNotNullish.Options): import("../../core/validator.js").Validator<unknown, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
34
81
|
export declare namespace isNotNullish {
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
3
|
+
* Validates that the value is exactly `null`; even `undefined` fails.
|
|
4
4
|
* @validator isNull
|
|
5
|
+
* @param options - Validation options.
|
|
6
|
+
* @returns `null`.
|
|
7
|
+
* @throws `Value must be null` if the input is anything other than `null`.
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { isNull } from 'valgen';
|
|
11
|
+
*
|
|
12
|
+
* isNull(null); // => null
|
|
13
|
+
* isNull(undefined); // throws ValidationError: 'Value must be null'
|
|
14
|
+
* ```
|
|
5
15
|
*/
|
|
6
16
|
export function isNull(options) {
|
|
7
17
|
return validator(isNull.name, (input, context, _this) => {
|
|
@@ -11,8 +21,20 @@ export function isNull(options) {
|
|
|
11
21
|
}, options);
|
|
12
22
|
}
|
|
13
23
|
/**
|
|
14
|
-
* Validates
|
|
24
|
+
* Validates that the value is anything other than `null` - notably,
|
|
25
|
+
* `undefined`, `''`, `0`, and `NaN` all pass.
|
|
15
26
|
* @validator isNotNull
|
|
27
|
+
* @param options - Validation options.
|
|
28
|
+
* @returns The input value, unchanged.
|
|
29
|
+
* @throws `Value is null` if the input is `null`.
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { isNotNull } from 'valgen';
|
|
33
|
+
*
|
|
34
|
+
* isNotNull(undefined); // => undefined
|
|
35
|
+
* isNotNull(0); // => 0
|
|
36
|
+
* isNotNull(null); // throws ValidationError: 'Value is null'
|
|
37
|
+
* ```
|
|
16
38
|
*/
|
|
17
39
|
export function isNotNull(options) {
|
|
18
40
|
return validator(isNotNull.name, (input, context, _this) => {
|
|
@@ -22,8 +44,20 @@ export function isNotNull(options) {
|
|
|
22
44
|
}, options);
|
|
23
45
|
}
|
|
24
46
|
/**
|
|
25
|
-
* Validates
|
|
47
|
+
* Validates that the value is `null` or `undefined`.
|
|
26
48
|
* @validator isNullish
|
|
49
|
+
* @param options - Validation options.
|
|
50
|
+
* @returns The input value (`null` or `undefined`), unchanged.
|
|
51
|
+
* @throws `Value is not nullish` if the input is anything other than
|
|
52
|
+
* `null` or `undefined`.
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { isNullish } from 'valgen';
|
|
56
|
+
*
|
|
57
|
+
* isNullish(null); // => null
|
|
58
|
+
* isNullish(undefined); // => undefined
|
|
59
|
+
* isNullish(''); // throws ValidationError: 'Value is not nullish'
|
|
60
|
+
* ```
|
|
27
61
|
*/
|
|
28
62
|
export function isNullish(options) {
|
|
29
63
|
return validator(isNullish.name, (input, context, _this) => {
|
|
@@ -33,8 +67,21 @@ export function isNullish(options) {
|
|
|
33
67
|
}, options);
|
|
34
68
|
}
|
|
35
69
|
/**
|
|
36
|
-
* Validates
|
|
70
|
+
* Validates that the value is neither `null` nor `undefined` - `0`, `''`,
|
|
71
|
+
* and `NaN` all pass.
|
|
37
72
|
* @validator isNotNullish
|
|
73
|
+
* @param options - Validation options.
|
|
74
|
+
* @returns The input value, unchanged.
|
|
75
|
+
* @throws `Value is null` if the input is `null`.
|
|
76
|
+
* @throws `Value is undefined` if the input is `undefined`.
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { isNotNullish } from 'valgen';
|
|
80
|
+
*
|
|
81
|
+
* isNotNullish(0); // => 0
|
|
82
|
+
* isNotNullish(null); // throws ValidationError: 'Value is null'
|
|
83
|
+
* isNotNullish(undefined); // throws ValidationError: 'Value is undefined'
|
|
84
|
+
* ```
|
|
38
85
|
*/
|
|
39
86
|
export function isNotNullish(options) {
|
|
40
87
|
return validator(isNotNullish.name, (input, context, _this) => {
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is a `number`. With `coerce: true`, allows
|
|
4
|
+
* parsing a `string` (via `parseFloat`) or converting a lossless `bigint`
|
|
5
|
+
* into a `number`.
|
|
5
6
|
* @validator isNumber
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The validated (and possibly coerced) `number`.
|
|
9
|
+
* @throws `Value must be a number` if the input is `NaN`, `null`,
|
|
10
|
+
* `undefined`, or (without coercion, or when coercion would lose
|
|
11
|
+
* precision for a `bigint`) not a finite `number`.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isNumber } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isNumber(1.1); // => 1.1
|
|
17
|
+
* isNumber('4.5', { coerce: true }); // => 4.5
|
|
18
|
+
* isNumber(10000000000000000001n, { coerce: true });
|
|
19
|
+
* // throws: 'Value must be a number' (precision would be lost)
|
|
20
|
+
* ```
|
|
6
21
|
*/
|
|
7
22
|
export declare function isNumber(options?: isNumber.Options): import("../../core/validator.js").Validator<number, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
8
23
|
export declare namespace isNumber {
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is a `number`. With `coerce: true`, allows
|
|
4
|
+
* parsing a `string` (via `parseFloat`) or converting a lossless `bigint`
|
|
5
|
+
* into a `number`.
|
|
5
6
|
* @validator isNumber
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The validated (and possibly coerced) `number`.
|
|
9
|
+
* @throws `Value must be a number` if the input is `NaN`, `null`,
|
|
10
|
+
* `undefined`, or (without coercion, or when coercion would lose
|
|
11
|
+
* precision for a `bigint`) not a finite `number`.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isNumber } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isNumber(1.1); // => 1.1
|
|
17
|
+
* isNumber('4.5', { coerce: true }); // => 4.5
|
|
18
|
+
* isNumber(10000000000000000001n, { coerce: true });
|
|
19
|
+
* // throws: 'Value must be a number' (precision would be lost)
|
|
20
|
+
* ```
|
|
6
21
|
*/
|
|
7
22
|
export function isNumber(options) {
|
|
8
23
|
return validator(isNumber.name, (input, context, _this) => {
|