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,28 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
const TIME_PATTERN = /^(\d{2}):?(\d{2})(?::?(\d{2}))?(?:\.(\d{1,3}))?$/;
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates (and optionally coerces) a time-of-day value in `HH:mm`,
|
|
5
|
+
* `HH:mm:ss`, or `HH:mm:ss.SSS` format, with or without separators. Not a
|
|
6
|
+
* wrapper around `@browsery/validator` - it uses its own regex and checks
|
|
7
|
+
* hour ≤ 23, minutes/seconds ≤ 59.
|
|
8
|
+
*
|
|
9
|
+
* @param options - Validation options. When `coerce` is `true`, a `Date`
|
|
10
|
+
* input is converted to a `HH:mm:ss[.SSS]` string, and a compact form
|
|
11
|
+
* without separators (e.g. `'1230'`) is normalized to colon-separated form;
|
|
12
|
+
* without `coerce`, the input string is returned unchanged.
|
|
13
|
+
* @returns The validated (and possibly coerced) time string.
|
|
14
|
+
* @throws `Value must be a valid Time` when the input isn't a recognized
|
|
15
|
+
* time-of-day value.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { isTime } from 'valgen';
|
|
20
|
+
*
|
|
21
|
+
* isTime('12:30', { coerce: true }); // => '12:30'
|
|
22
|
+
* isTime('1230', { coerce: true }); // => '12:30'
|
|
23
|
+
* isTime(new Date('2025-01-10T08:30:15'), { coerce: true }); // => '08:30:15'
|
|
24
|
+
* isTime('25:00'); // throws ValidationError: "Value must be a valid Time"
|
|
25
|
+
* ```
|
|
5
26
|
* @validator isTime
|
|
6
27
|
*/
|
|
7
28
|
export function isTime(options) {
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
3
|
+
* Validates that a string contains only uppercase characters, by delegating
|
|
4
|
+
* to `validatorJS.isUppercase(input)`.
|
|
5
|
+
*
|
|
6
|
+
* @param options - Validation options.
|
|
7
|
+
* @returns The validated string, unchanged.
|
|
8
|
+
* @throws `Value must be an uppercase string` when the input contains any
|
|
9
|
+
* non-uppercase characters.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { isUppercase } from 'valgen';
|
|
14
|
+
*
|
|
15
|
+
* isUppercase('ABC'); // => 'ABC'
|
|
16
|
+
* isUppercase('abc'); // throws ValidationError: "Value must be an uppercase string"
|
|
17
|
+
* ```
|
|
4
18
|
* @validator isUppercase
|
|
5
19
|
*/
|
|
6
20
|
export declare function isUppercase(options?: isUppercase.Options): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
import validatorJS from '@browsery/validator';
|
|
2
2
|
import { validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates that a string contains only uppercase characters, by delegating
|
|
5
|
+
* to `validatorJS.isUppercase(input)`.
|
|
6
|
+
*
|
|
7
|
+
* @param options - Validation options.
|
|
8
|
+
* @returns The validated string, unchanged.
|
|
9
|
+
* @throws `Value must be an uppercase string` when the input contains any
|
|
10
|
+
* non-uppercase characters.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isUppercase } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isUppercase('ABC'); // => 'ABC'
|
|
17
|
+
* isUppercase('abc'); // throws ValidationError: "Value must be an uppercase string"
|
|
18
|
+
* ```
|
|
5
19
|
* @validator isUppercase
|
|
6
20
|
*/
|
|
7
21
|
export function isUppercase(options) {
|
|
@@ -1,11 +1,109 @@
|
|
|
1
1
|
import { type IsURLOptions as _IsURLOptions } from '@browsery/validator';
|
|
2
2
|
import { type ValidationOptions } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates that a string is a valid URL, by delegating directly to
|
|
5
|
+
* `validatorJS.isURL(input, options)`; all `IsURLOptions` fields pass through
|
|
6
|
+
* unchanged.
|
|
7
|
+
*
|
|
8
|
+
* @param options - Validation options - see {@link isURL.Options} for the
|
|
9
|
+
* forwarded `IsURLOptions` fields.
|
|
10
|
+
* @returns The validated URL string, unchanged.
|
|
11
|
+
* @throws `Value must be a valid URL` when the input isn't a valid URL.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { isURL, vg } from 'valgen';
|
|
16
|
+
*
|
|
17
|
+
* isURL('https://example.com'); // => 'https://example.com'
|
|
18
|
+
* isURL('not a url'); // throws ValidationError: "Value must be a valid URL"
|
|
19
|
+
*
|
|
20
|
+
* const httpsOnly = vg.isURL({ protocols: ['https'] });
|
|
21
|
+
* httpsOnly('http://example.com'); // throws ValidationError: "Value must be a valid URL"
|
|
22
|
+
* ```
|
|
5
23
|
* @validator isURL
|
|
6
24
|
*/
|
|
7
25
|
export declare function isURL(options?: isURL.Options): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
8
26
|
export declare namespace isURL {
|
|
9
27
|
interface Options extends ValidationOptions, _IsURLOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Allowed protocols.
|
|
30
|
+
* @defaultValue `['http','https','ftp']`
|
|
31
|
+
*/
|
|
32
|
+
protocols?: string[];
|
|
33
|
+
/**
|
|
34
|
+
* Require a top-level domain.
|
|
35
|
+
* @defaultValue `true`
|
|
36
|
+
*/
|
|
37
|
+
require_tld?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Require the protocol (`scheme://`) to be present.
|
|
40
|
+
* @defaultValue `false`
|
|
41
|
+
*/
|
|
42
|
+
require_protocol?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Require a host part.
|
|
45
|
+
* @defaultValue `true`
|
|
46
|
+
*/
|
|
47
|
+
require_host?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Require an explicit port.
|
|
50
|
+
* @defaultValue `false`
|
|
51
|
+
*/
|
|
52
|
+
require_port?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Restrict to protocols in `protocols`.
|
|
55
|
+
* @defaultValue `true`
|
|
56
|
+
*/
|
|
57
|
+
require_valid_protocol?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Allow underscores in the host.
|
|
60
|
+
* @defaultValue `false`
|
|
61
|
+
*/
|
|
62
|
+
allow_underscores?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Only accept these hosts.
|
|
65
|
+
* @defaultValue `false`
|
|
66
|
+
*/
|
|
67
|
+
host_whitelist?: (string | RegExp)[];
|
|
68
|
+
/**
|
|
69
|
+
* Reject these hosts.
|
|
70
|
+
* @defaultValue `false`
|
|
71
|
+
*/
|
|
72
|
+
host_blacklist?: (string | RegExp)[];
|
|
73
|
+
/**
|
|
74
|
+
* Allow a trailing `.` after the domain.
|
|
75
|
+
* @defaultValue `false`
|
|
76
|
+
*/
|
|
77
|
+
allow_trailing_dot?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Allow `//example.com`-style URLs.
|
|
80
|
+
* @defaultValue `false`
|
|
81
|
+
*/
|
|
82
|
+
allow_protocol_relative_urls?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Reject `user:pass@host` credentials.
|
|
85
|
+
* @defaultValue `false`
|
|
86
|
+
*/
|
|
87
|
+
disallow_auth?: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Allow a `#fragment`.
|
|
90
|
+
* @defaultValue `true`
|
|
91
|
+
*/
|
|
92
|
+
allow_fragments?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Allow a `?query=components` section.
|
|
95
|
+
* @defaultValue `true`
|
|
96
|
+
*/
|
|
97
|
+
allow_query_components?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Enforce the max length check.
|
|
100
|
+
* @defaultValue `true`
|
|
101
|
+
*/
|
|
102
|
+
validate_length?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Max URL length when `validate_length` is enabled.
|
|
105
|
+
* @defaultValue `2084`
|
|
106
|
+
*/
|
|
107
|
+
max_allowed_length?: number | false;
|
|
10
108
|
}
|
|
11
109
|
}
|
|
@@ -1,7 +1,25 @@
|
|
|
1
1
|
import validatorJS, {} from '@browsery/validator';
|
|
2
2
|
import { validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates that a string is a valid URL, by delegating directly to
|
|
5
|
+
* `validatorJS.isURL(input, options)`; all `IsURLOptions` fields pass through
|
|
6
|
+
* unchanged.
|
|
7
|
+
*
|
|
8
|
+
* @param options - Validation options - see {@link isURL.Options} for the
|
|
9
|
+
* forwarded `IsURLOptions` fields.
|
|
10
|
+
* @returns The validated URL string, unchanged.
|
|
11
|
+
* @throws `Value must be a valid URL` when the input isn't a valid URL.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { isURL, vg } from 'valgen';
|
|
16
|
+
*
|
|
17
|
+
* isURL('https://example.com'); // => 'https://example.com'
|
|
18
|
+
* isURL('not a url'); // throws ValidationError: "Value must be a valid URL"
|
|
19
|
+
*
|
|
20
|
+
* const httpsOnly = vg.isURL({ protocols: ['https'] });
|
|
21
|
+
* httpsOnly('http://example.com'); // throws ValidationError: "Value must be a valid URL"
|
|
22
|
+
* ```
|
|
5
23
|
* @validator isURL
|
|
6
24
|
*/
|
|
7
25
|
export function isURL(options) {
|
|
@@ -1,7 +1,29 @@
|
|
|
1
1
|
import { type UUIDVersion as _UUIDVersion } from '@browsery/validator';
|
|
2
2
|
import { type ValidationOptions } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates that a string is a valid UUID, optionally restricted to a
|
|
5
|
+
* specific version, by delegating to `validatorJS.isUUID(input, version)`.
|
|
6
|
+
* Without a version, any UUID version passes; with a version, only a UUID of
|
|
7
|
+
* that version passes.
|
|
8
|
+
*
|
|
9
|
+
* @param version - The UUID version to restrict to (e.g. `1`-`8`, `'nil'`,
|
|
10
|
+
* `'max'`, `'loose'`, `'all'`); omit to accept any version.
|
|
11
|
+
* @param options - Validation options.
|
|
12
|
+
* @returns The validated UUID string, unchanged.
|
|
13
|
+
* @throws `Value must be a valid UUID` (or `Value must be a valid UUID v<version>`
|
|
14
|
+
* when `version` is given) when the input isn't a matching UUID.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { isUUID, isUUID4, vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* const uuidV4 = '01e0fee8-60d5-42a5-997c-b55a4f3e973f';
|
|
21
|
+
* isUUID(uuidV4); // => uuidV4 (any version accepted)
|
|
22
|
+
* isUUID4(uuidV4); // => uuidV4
|
|
23
|
+
*
|
|
24
|
+
* const isUUIDv1 = vg.isUUID(1);
|
|
25
|
+
* isUUIDv1(uuidV4); // throws ValidationError: "Value must be a valid UUID v1"
|
|
26
|
+
* ```
|
|
5
27
|
* @validator isUUID
|
|
6
28
|
*/
|
|
7
29
|
export declare function isUUID(version?: isUUID.UUIDVersion, options?: isUUID.Options): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -1,7 +1,29 @@
|
|
|
1
1
|
import validatorJS, {} from '@browsery/validator';
|
|
2
2
|
import { validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
4
|
+
* Validates that a string is a valid UUID, optionally restricted to a
|
|
5
|
+
* specific version, by delegating to `validatorJS.isUUID(input, version)`.
|
|
6
|
+
* Without a version, any UUID version passes; with a version, only a UUID of
|
|
7
|
+
* that version passes.
|
|
8
|
+
*
|
|
9
|
+
* @param version - The UUID version to restrict to (e.g. `1`-`8`, `'nil'`,
|
|
10
|
+
* `'max'`, `'loose'`, `'all'`); omit to accept any version.
|
|
11
|
+
* @param options - Validation options.
|
|
12
|
+
* @returns The validated UUID string, unchanged.
|
|
13
|
+
* @throws `Value must be a valid UUID` (or `Value must be a valid UUID v<version>`
|
|
14
|
+
* when `version` is given) when the input isn't a matching UUID.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { isUUID, isUUID4, vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* const uuidV4 = '01e0fee8-60d5-42a5-997c-b55a4f3e973f';
|
|
21
|
+
* isUUID(uuidV4); // => uuidV4 (any version accepted)
|
|
22
|
+
* isUUID4(uuidV4); // => uuidV4
|
|
23
|
+
*
|
|
24
|
+
* const isUUIDv1 = vg.isUUID(1);
|
|
25
|
+
* isUUIDv1(uuidV4); // throws ValidationError: "Value must be a valid UUID v1"
|
|
26
|
+
* ```
|
|
5
27
|
* @validator isUUID
|
|
6
28
|
*/
|
|
7
29
|
export function isUUID(version, options) {
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import { type VATCountryCode as _VATCountryCode } from '@browsery/validator';
|
|
2
2
|
import { type ValidationOptions } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
5
|
-
*
|
|
4
|
+
* Validates that a string is a valid VAT (Value Added Tax) number for the
|
|
5
|
+
* given EU country code, by delegating to `validatorJS.isVAT(input, countryCode)`.
|
|
6
|
+
*
|
|
7
|
+
* @param countryCode - The EU VAT country code to validate the number against
|
|
8
|
+
* (e.g. `'AT'`, `'BE'`, `'BG'`, ...).
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns The validated VAT number string, unchanged.
|
|
11
|
+
* @throws `Value must be a valid VAT number` when the input isn't a valid VAT
|
|
12
|
+
* number for `countryCode`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { vg } from 'valgen';
|
|
17
|
+
*
|
|
18
|
+
* const isATVat = vg.isVATNumber('AT');
|
|
19
|
+
* isATVat('ATU12345678'); // => 'ATU12345678'
|
|
20
|
+
* isATVat('12345'); // throws ValidationError: "Value must be a valid VAT number"
|
|
21
|
+
* ```
|
|
22
|
+
* @validator isVATNumber
|
|
6
23
|
*/
|
|
7
24
|
export declare function isVATNumber(countryCode: isVATNumber.CountryCode, options?: isVATNumber.Options): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
8
25
|
export declare namespace isVATNumber {
|
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
import validatorJS, {} from '@browsery/validator';
|
|
2
2
|
import { validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
5
|
-
*
|
|
4
|
+
* Validates that a string is a valid VAT (Value Added Tax) number for the
|
|
5
|
+
* given EU country code, by delegating to `validatorJS.isVAT(input, countryCode)`.
|
|
6
|
+
*
|
|
7
|
+
* @param countryCode - The EU VAT country code to validate the number against
|
|
8
|
+
* (e.g. `'AT'`, `'BE'`, `'BG'`, ...).
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns The validated VAT number string, unchanged.
|
|
11
|
+
* @throws `Value must be a valid VAT number` when the input isn't a valid VAT
|
|
12
|
+
* number for `countryCode`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { vg } from 'valgen';
|
|
17
|
+
*
|
|
18
|
+
* const isATVat = vg.isVATNumber('AT');
|
|
19
|
+
* isATVat('ATU12345678'); // => 'ATU12345678'
|
|
20
|
+
* isATVat('12345'); // throws ValidationError: "Value must be a valid VAT number"
|
|
21
|
+
* ```
|
|
22
|
+
* @validator isVATNumber
|
|
6
23
|
*/
|
|
7
24
|
export function isVATNumber(countryCode, options) {
|
|
8
25
|
return validator(isVATNumber.name, (input, context, _this) => {
|
|
@@ -1,11 +1,37 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
3
|
+
* Validates that a string matches a given regular expression pattern.
|
|
4
|
+
* Returns `undefined` for nullish input instead of failing. A string pattern
|
|
5
|
+
* is compiled via `new RegExp(format)`.
|
|
6
|
+
*
|
|
7
|
+
* @param format - The pattern to match against - a `RegExp`, or a string
|
|
8
|
+
* compiled into one.
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns The validated string, unchanged, or `undefined` when the input
|
|
11
|
+
* was `null`/`undefined`.
|
|
12
|
+
* @throws `Value must match <formatName> format` (using `'requested'` when
|
|
13
|
+
* `formatName` is omitted) when the input isn't a string or doesn't match.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { vg } from 'valgen';
|
|
18
|
+
*
|
|
19
|
+
* const isDigits = vg.matches(/\d+/);
|
|
20
|
+
* isDigits('0123'); // => '0123'
|
|
21
|
+
* isDigits('abc'); // throws ValidationError: "Value must match requested format"
|
|
22
|
+
*
|
|
23
|
+
* const isPositiveNumber = vg.matches(/\d+/, { formatName: 'positive number' });
|
|
24
|
+
* isPositiveNumber('abc'); // throws ValidationError: "Value must match positive number format"
|
|
25
|
+
* ```
|
|
4
26
|
* @validator matches
|
|
5
27
|
*/
|
|
6
28
|
export declare function matches(format: string | RegExp, options?: matches.Options): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
7
29
|
export declare namespace matches {
|
|
8
30
|
interface Options extends ValidationOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Name used in the error message (`Value must match <formatName> format`).
|
|
33
|
+
* @defaultValue `'requested'`
|
|
34
|
+
*/
|
|
9
35
|
formatName?: string;
|
|
10
36
|
}
|
|
11
37
|
}
|
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
3
|
+
* Validates that a string matches a given regular expression pattern.
|
|
4
|
+
* Returns `undefined` for nullish input instead of failing. A string pattern
|
|
5
|
+
* is compiled via `new RegExp(format)`.
|
|
6
|
+
*
|
|
7
|
+
* @param format - The pattern to match against - a `RegExp`, or a string
|
|
8
|
+
* compiled into one.
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns The validated string, unchanged, or `undefined` when the input
|
|
11
|
+
* was `null`/`undefined`.
|
|
12
|
+
* @throws `Value must match <formatName> format` (using `'requested'` when
|
|
13
|
+
* `formatName` is omitted) when the input isn't a string or doesn't match.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { vg } from 'valgen';
|
|
18
|
+
*
|
|
19
|
+
* const isDigits = vg.matches(/\d+/);
|
|
20
|
+
* isDigits('0123'); // => '0123'
|
|
21
|
+
* isDigits('abc'); // throws ValidationError: "Value must match requested format"
|
|
22
|
+
*
|
|
23
|
+
* const isPositiveNumber = vg.matches(/\d+/, { formatName: 'positive number' });
|
|
24
|
+
* isPositiveNumber('abc'); // throws ValidationError: "Value must match positive number format"
|
|
25
|
+
* ```
|
|
4
26
|
* @validator matches
|
|
5
27
|
*/
|
|
6
28
|
export function matches(format, options) {
|
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
* defined
|
|
3
|
+
* Validates that a value is not `undefined`. Note that `null` is considered
|
|
4
|
+
* defined - use `isNotNullish` to reject both `undefined` and `null`.
|
|
5
|
+
*
|
|
6
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
7
|
+
* @returns The input value unchanged as long as it is not `undefined`,
|
|
8
|
+
* including `0`, `''`, and `null`.
|
|
9
|
+
* @throws `Value must be defined` if the input is `undefined`.
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* isDefined(0); // => 0
|
|
13
|
+
* isDefined(null); // => null
|
|
14
|
+
* isDefined(undefined); // throws ValidationError: "Value must be defined"
|
|
15
|
+
* ```
|
|
5
16
|
* @validator isDefined
|
|
6
17
|
*/
|
|
7
18
|
export declare function isDefined(options?: isDefined.Options): import("../../core/validator.js").Validator<any, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
8
19
|
export declare namespace isDefined {
|
|
20
|
+
/** Options for {@link isDefined} - adds no properties beyond `ValidationOptions`. */
|
|
9
21
|
interface Options extends ValidationOptions {
|
|
10
22
|
}
|
|
11
23
|
}
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
* defined
|
|
3
|
+
* Validates that a value is not `undefined`. Note that `null` is considered
|
|
4
|
+
* defined - use `isNotNullish` to reject both `undefined` and `null`.
|
|
5
|
+
*
|
|
6
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
7
|
+
* @returns The input value unchanged as long as it is not `undefined`,
|
|
8
|
+
* including `0`, `''`, and `null`.
|
|
9
|
+
* @throws `Value must be defined` if the input is `undefined`.
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* isDefined(0); // => 0
|
|
13
|
+
* isDefined(null); // => null
|
|
14
|
+
* isDefined(undefined); // throws ValidationError: "Value must be defined"
|
|
15
|
+
* ```
|
|
5
16
|
* @validator isDefined
|
|
6
17
|
*/
|
|
7
18
|
export function isDefined(options) {
|
|
@@ -1,19 +1,57 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is empty. The value should be a string, array, Set,
|
|
4
|
+
* Map, Buffer, ArrayBuffer, or plain object.
|
|
5
|
+
*
|
|
6
|
+
* `null`/`undefined` pass through unchanged (treated as empty), and `Date`
|
|
7
|
+
* instances always pass since a date has no "empty" concept.
|
|
8
|
+
*
|
|
9
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
10
|
+
* @returns The input value unchanged when it is considered empty.
|
|
11
|
+
* @throws `Value must be an empty string` / `Value must be an empty array` /
|
|
12
|
+
* `Value must be an empty Set` / `Value must be an empty Map` / `Value must
|
|
13
|
+
* be an empty Buffer` / `Value must be an empty ArrayBuffer` / `Value must be
|
|
14
|
+
* an empty Object`, depending on the input's type, or the generic `Value
|
|
15
|
+
* must be empty` for any other non-empty type (e.g. `NaN`).
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* isEmpty(''); // => ''
|
|
19
|
+
* isEmpty([]); // => []
|
|
20
|
+
* isEmpty('dd'); // throws ValidationError: "Value must be an empty string"
|
|
21
|
+
* ```
|
|
4
22
|
* @validator isEmpty
|
|
5
23
|
*/
|
|
6
24
|
export declare function isEmpty(options?: isEmpty.Options): import("../../core/validator.js").Validator<any, any, import("../../core/types.js").ExecutionOptions>;
|
|
7
25
|
export declare namespace isEmpty {
|
|
26
|
+
/** Options for {@link isEmpty} - adds no properties beyond `ValidationOptions`. */
|
|
8
27
|
interface Options extends ValidationOptions {
|
|
9
28
|
}
|
|
10
29
|
}
|
|
11
30
|
/**
|
|
12
|
-
* Checks
|
|
31
|
+
* Checks that the value is not empty. The value should be a string, array,
|
|
32
|
+
* Set, Map, Buffer, ArrayBuffer, or plain object.
|
|
33
|
+
*
|
|
34
|
+
* Unlike `isEmpty`, nullish input is rejected here rather than treated as
|
|
35
|
+
* passing, and `Date` instances always pass.
|
|
36
|
+
*
|
|
37
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
38
|
+
* @returns The input value unchanged when it is considered non-empty.
|
|
39
|
+
* @throws `Value must not be empty` for `null`/`undefined`/an empty string,
|
|
40
|
+
* `Value must not be NaN` for `NaN`, `Array must not be empty`, `Set must
|
|
41
|
+
* not be empty`, `Map must not be empty`, `Buffer must not be empty`,
|
|
42
|
+
* `ArrayBuffer must not be empty`, or `Object must not be empty`, depending
|
|
43
|
+
* on the input's type.
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* isNotEmpty('abc'); // => 'abc'
|
|
47
|
+
* isNotEmpty(''); // throws ValidationError: "Value must not be empty"
|
|
48
|
+
* isNotEmpty([]); // throws ValidationError: "Array must not be empty"
|
|
49
|
+
* ```
|
|
13
50
|
* @validator isNotEmpty
|
|
14
51
|
*/
|
|
15
52
|
export declare function isNotEmpty(options?: isNotEmpty.Options): import("../../core/validator.js").Validator<any, any, import("../../core/types.js").ExecutionOptions>;
|
|
16
53
|
export declare namespace isNotEmpty {
|
|
54
|
+
/** Options for {@link isNotEmpty} - adds no properties beyond `ValidationOptions`. */
|
|
17
55
|
interface Options extends ValidationOptions {
|
|
18
56
|
}
|
|
19
57
|
}
|
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Checks
|
|
3
|
+
* Checks that the value is empty. The value should be a string, array, Set,
|
|
4
|
+
* Map, Buffer, ArrayBuffer, or plain object.
|
|
5
|
+
*
|
|
6
|
+
* `null`/`undefined` pass through unchanged (treated as empty), and `Date`
|
|
7
|
+
* instances always pass since a date has no "empty" concept.
|
|
8
|
+
*
|
|
9
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
10
|
+
* @returns The input value unchanged when it is considered empty.
|
|
11
|
+
* @throws `Value must be an empty string` / `Value must be an empty array` /
|
|
12
|
+
* `Value must be an empty Set` / `Value must be an empty Map` / `Value must
|
|
13
|
+
* be an empty Buffer` / `Value must be an empty ArrayBuffer` / `Value must be
|
|
14
|
+
* an empty Object`, depending on the input's type, or the generic `Value
|
|
15
|
+
* must be empty` for any other non-empty type (e.g. `NaN`).
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* isEmpty(''); // => ''
|
|
19
|
+
* isEmpty([]); // => []
|
|
20
|
+
* isEmpty('dd'); // throws ValidationError: "Value must be an empty string"
|
|
21
|
+
* ```
|
|
4
22
|
* @validator isEmpty
|
|
5
23
|
*/
|
|
6
24
|
export function isEmpty(options) {
|
|
@@ -48,7 +66,25 @@ export function isEmpty(options) {
|
|
|
48
66
|
}, options);
|
|
49
67
|
}
|
|
50
68
|
/**
|
|
51
|
-
* Checks
|
|
69
|
+
* Checks that the value is not empty. The value should be a string, array,
|
|
70
|
+
* Set, Map, Buffer, ArrayBuffer, or plain object.
|
|
71
|
+
*
|
|
72
|
+
* Unlike `isEmpty`, nullish input is rejected here rather than treated as
|
|
73
|
+
* passing, and `Date` instances always pass.
|
|
74
|
+
*
|
|
75
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
76
|
+
* @returns The input value unchanged when it is considered non-empty.
|
|
77
|
+
* @throws `Value must not be empty` for `null`/`undefined`/an empty string,
|
|
78
|
+
* `Value must not be NaN` for `NaN`, `Array must not be empty`, `Set must
|
|
79
|
+
* not be empty`, `Map must not be empty`, `Buffer must not be empty`,
|
|
80
|
+
* `ArrayBuffer must not be empty`, or `Object must not be empty`, depending
|
|
81
|
+
* on the input's type.
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* isNotEmpty('abc'); // => 'abc'
|
|
85
|
+
* isNotEmpty(''); // throws ValidationError: "Value must not be empty"
|
|
86
|
+
* isNotEmpty([]); // throws ValidationError: "Array must not be empty"
|
|
87
|
+
* ```
|
|
52
88
|
* @validator isNotEmpty
|
|
53
89
|
*/
|
|
54
90
|
export function isNotEmpty(options) {
|
|
@@ -1,21 +1,48 @@
|
|
|
1
1
|
import { type ValidationOptions } 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 declare function isEqual<T>(compare: T, options?: isEqual.Options): import("../../core/validator.js").Validator<any, any, import("../../core/types.js").ExecutionOptions>;
|
|
8
21
|
export declare namespace isEqual {
|
|
22
|
+
/** Options for {@link isEqual} - adds no properties beyond `ValidationOptions`. */
|
|
9
23
|
interface Options extends ValidationOptions {
|
|
10
24
|
}
|
|
11
25
|
}
|
|
12
26
|
/**
|
|
13
|
-
* Validates
|
|
14
|
-
* `null`/`undefined` input is passed through unchanged
|
|
27
|
+
* Validates that the value is not strictly equal (`===`) to `compare`.
|
|
28
|
+
* `null`/`undefined` input is always passed through unchanged, regardless of
|
|
29
|
+
* `compare`.
|
|
30
|
+
*
|
|
31
|
+
* @param compare - The value the input must not strictly equal.
|
|
32
|
+
* @param options - Validation options (`onFail`, `coerce`, ...).
|
|
33
|
+
* @returns The input value unchanged when it does not strictly equal
|
|
34
|
+
* `compare` (or is nullish).
|
|
35
|
+
* @throws `Value must not be equal to "<compare>"` if `input === compare`.
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* vg.isNotEqual('a')('b'); // => 'b'
|
|
39
|
+
* vg.isNotEqual('a')('a'); // throws ValidationError: "Value must not be equal to..."
|
|
40
|
+
* ```
|
|
15
41
|
* @validator isNotEqual
|
|
16
42
|
*/
|
|
17
43
|
export declare function isNotEqual(compare: any, options?: isNotEqual.Options): import("../../core/validator.js").Validator<any, any, import("../../core/types.js").ExecutionOptions>;
|
|
18
44
|
export declare namespace isNotEqual {
|
|
45
|
+
/** Options for {@link isNotEqual} - adds no properties beyond `ValidationOptions`. */
|
|
19
46
|
interface Options extends ValidationOptions {
|
|
20
47
|
}
|
|
21
48
|
}
|