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,13 +1,46 @@
|
|
|
1
1
|
import { type Type, type ValidationOptions, type Validator as Validator_ } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* `
|
|
9
|
-
* validates itself, e.g. via `forwardRef`).
|
|
3
|
+
* Validates an object against a property `schema`, with support for nested
|
|
4
|
+
* schemas, renaming/relabeling fields via `[Validator, PropertyOptions]`
|
|
5
|
+
* tuples, controlling unknown properties via `additionalFields`,
|
|
6
|
+
* case-insensitive key matching via `caseInSensitive`, and
|
|
7
|
+
* circular-reference detection via `detectCircular`. Converts properties
|
|
8
|
+
* according to schema rules if the `coerce` option is set to `true`.
|
|
10
9
|
* @validator isObject
|
|
10
|
+
* @typeParam T - The validated output object type.
|
|
11
|
+
* @typeParam I - The accepted input type.
|
|
12
|
+
* @param schema - Maps property keys to a `Validator` (or a
|
|
13
|
+
* `[Validator, PropertyOptions]` tuple to customize the error `label` or
|
|
14
|
+
* rename the output key via `as`); omit for a schema-less "is this an
|
|
15
|
+
* object" check.
|
|
16
|
+
* @param options - Validation options.
|
|
17
|
+
* @returns The validated object; when `ctor` is set, its prototype is
|
|
18
|
+
* `ctor.prototype` (so `instanceof ctor` is `true`). Properties whose
|
|
19
|
+
* validated value is `undefined` are omitted from the output.
|
|
20
|
+
* @throws `Value must be an object` if the (possibly JSON-parsed) input is
|
|
21
|
+
* not a non-null object.
|
|
22
|
+
* @throws `Object has no field '<key>' and does not accept additional
|
|
23
|
+
* fields` (or `<name> has no field...` when `name`/`ctor` is set) if
|
|
24
|
+
* `additionalFields: 'error'` and the input has an undeclared property.
|
|
25
|
+
* @throws Whatever the failing property's own rule throws, located at that
|
|
26
|
+
* property's path (e.g. `address.country`).
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { isNumber, isString, vg } from 'valgen';
|
|
30
|
+
*
|
|
31
|
+
* class Person {
|
|
32
|
+
* declare name: string;
|
|
33
|
+
* declare age: number;
|
|
34
|
+
* }
|
|
35
|
+
* const personDef: vg.isObject.Schema = {
|
|
36
|
+
* name: [vg.required(isString), { label: 'Full Name', as: 'fullName' }],
|
|
37
|
+
* age: [vg.required(isNumber), { label: 'Age' }],
|
|
38
|
+
* };
|
|
39
|
+
* const validatePerson = vg.isObject(personDef, { ctor: Person });
|
|
40
|
+
*
|
|
41
|
+
* validatePerson({ name: 'John', age: '22' }, { coerce: true });
|
|
42
|
+
* // => { fullName: 'John', age: 22 } (instance of Person)
|
|
43
|
+
* ```
|
|
11
44
|
*/
|
|
12
45
|
export declare function isObject<T extends object = object, I = object | string>(schema?: isObject.Schema, options?: isObject.Options<T>): isObject.Validator<T, I>;
|
|
13
46
|
export declare namespace isObject {
|
|
@@ -20,10 +53,15 @@ export declare namespace isObject {
|
|
|
20
53
|
};
|
|
21
54
|
type Schema = Record<string | number, Validator_ | [Validator_, PropertyOptions]>;
|
|
22
55
|
interface Options<T> extends ValidationOptions {
|
|
56
|
+
/** Used as the error `context` label for this object (e.g. a class name). @defaultValue `ctor?.name` */
|
|
23
57
|
name?: string;
|
|
58
|
+
/** Sets the prototype of the output object to `ctor.prototype` (`instanceof ctor` becomes `true`); may also define static `[preValidation]`/`[postValidation]` hooks invoked before/after this rule's own logic. */
|
|
24
59
|
ctor?: Type<T>;
|
|
60
|
+
/** Controls properties not declared in `schema`: `true` passes them through unchanged, `false` silently drops them, a `Validator` applies that rule to each of them, and `'error'` throws when any are present. @defaultValue `true` if `schema` is omitted entirely; otherwise `false` */
|
|
25
61
|
additionalFields?: boolean | Validator_ | 'error';
|
|
62
|
+
/** Matches input property names to schema keys case-insensitively; on a case-insensitive duplicate (e.g. both `name` and `Name` present), only the first-encountered key is used. @defaultValue false */
|
|
26
63
|
caseInSensitive?: boolean;
|
|
64
|
+
/** Tracks input objects already being converted (per root validation call) and returns the same (in-progress) output object instead of recursing infinitely when a cycle is found. @defaultValue false */
|
|
27
65
|
detectCircular?: boolean;
|
|
28
66
|
}
|
|
29
67
|
}
|
|
@@ -1,14 +1,47 @@
|
|
|
1
1
|
import { postValidation, preValidation } from '../../constants.js';
|
|
2
2
|
import { isValidator, validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
* Validates
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* `
|
|
10
|
-
* validates itself, e.g. via `forwardRef`).
|
|
4
|
+
* Validates an object against a property `schema`, with support for nested
|
|
5
|
+
* schemas, renaming/relabeling fields via `[Validator, PropertyOptions]`
|
|
6
|
+
* tuples, controlling unknown properties via `additionalFields`,
|
|
7
|
+
* case-insensitive key matching via `caseInSensitive`, and
|
|
8
|
+
* circular-reference detection via `detectCircular`. Converts properties
|
|
9
|
+
* according to schema rules if the `coerce` option is set to `true`.
|
|
11
10
|
* @validator isObject
|
|
11
|
+
* @typeParam T - The validated output object type.
|
|
12
|
+
* @typeParam I - The accepted input type.
|
|
13
|
+
* @param schema - Maps property keys to a `Validator` (or a
|
|
14
|
+
* `[Validator, PropertyOptions]` tuple to customize the error `label` or
|
|
15
|
+
* rename the output key via `as`); omit for a schema-less "is this an
|
|
16
|
+
* object" check.
|
|
17
|
+
* @param options - Validation options.
|
|
18
|
+
* @returns The validated object; when `ctor` is set, its prototype is
|
|
19
|
+
* `ctor.prototype` (so `instanceof ctor` is `true`). Properties whose
|
|
20
|
+
* validated value is `undefined` are omitted from the output.
|
|
21
|
+
* @throws `Value must be an object` if the (possibly JSON-parsed) input is
|
|
22
|
+
* not a non-null object.
|
|
23
|
+
* @throws `Object has no field '<key>' and does not accept additional
|
|
24
|
+
* fields` (or `<name> has no field...` when `name`/`ctor` is set) if
|
|
25
|
+
* `additionalFields: 'error'` and the input has an undeclared property.
|
|
26
|
+
* @throws Whatever the failing property's own rule throws, located at that
|
|
27
|
+
* property's path (e.g. `address.country`).
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { isNumber, isString, vg } from 'valgen';
|
|
31
|
+
*
|
|
32
|
+
* class Person {
|
|
33
|
+
* declare name: string;
|
|
34
|
+
* declare age: number;
|
|
35
|
+
* }
|
|
36
|
+
* const personDef: vg.isObject.Schema = {
|
|
37
|
+
* name: [vg.required(isString), { label: 'Full Name', as: 'fullName' }],
|
|
38
|
+
* age: [vg.required(isNumber), { label: 'Age' }],
|
|
39
|
+
* };
|
|
40
|
+
* const validatePerson = vg.isObject(personDef, { ctor: Person });
|
|
41
|
+
*
|
|
42
|
+
* validatePerson({ name: 'John', age: '22' }, { coerce: true });
|
|
43
|
+
* // => { fullName: 'John', age: 22 } (instance of Person)
|
|
44
|
+
* ```
|
|
12
45
|
*/
|
|
13
46
|
export function isObject(schema, options) {
|
|
14
47
|
const ctor = options?.ctor;
|
|
@@ -1,8 +1,27 @@
|
|
|
1
1
|
import type { ValidationOptions, Validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates a plain object as a "dictionary": every key must satisfy
|
|
4
|
+
* `keyRule` and every value must satisfy `valueRule`. With `coerce: true`,
|
|
5
|
+
* both keys and values may be coerced by their respective rules.
|
|
5
6
|
* @validator isRecord
|
|
7
|
+
* @param keyRule - Rule every own key of the input is validated against.
|
|
8
|
+
* @param valueRule - Rule every value of the input is validated against.
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns An object with the validated (and possibly coerced) keys and values.
|
|
11
|
+
* @throws `Value must be an object` if the input is `null`, `undefined`, or
|
|
12
|
+
* not an object.
|
|
13
|
+
* @throws `<key> is not a valid key. <underlying message>` if a key fails
|
|
14
|
+
* `keyRule`.
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { isAny, isString, vg } from 'valgen';
|
|
18
|
+
*
|
|
19
|
+
* const validate = vg.isRecord(isString, isAny);
|
|
20
|
+
* validate(null as any); // throws ValidationError: 'Value must be an object'
|
|
21
|
+
*
|
|
22
|
+
* vg.isRecord(isString, isString)({ a: 1 as any, b: true as any }, { coerce: true });
|
|
23
|
+
* // => { a: '1', b: 'true' }
|
|
24
|
+
* ```
|
|
6
25
|
*/
|
|
7
26
|
export declare function isRecord<TKeys extends string | number | symbol, TValues>(keyRule: Validator<TKeys>, valueRule: Validator<TValues>, options?: isRecord.Options): Validator<Record<TKeys, TValues>, Record<TKeys, TValues>, import("../../core/types.js").ExecutionOptions>;
|
|
8
27
|
export declare namespace isRecord {
|
|
@@ -1,8 +1,27 @@
|
|
|
1
1
|
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates a plain object as a "dictionary": every key must satisfy
|
|
4
|
+
* `keyRule` and every value must satisfy `valueRule`. With `coerce: true`,
|
|
5
|
+
* both keys and values may be coerced by their respective rules.
|
|
5
6
|
* @validator isRecord
|
|
7
|
+
* @param keyRule - Rule every own key of the input is validated against.
|
|
8
|
+
* @param valueRule - Rule every value of the input is validated against.
|
|
9
|
+
* @param options - Validation options.
|
|
10
|
+
* @returns An object with the validated (and possibly coerced) keys and values.
|
|
11
|
+
* @throws `Value must be an object` if the input is `null`, `undefined`, or
|
|
12
|
+
* not an object.
|
|
13
|
+
* @throws `<key> is not a valid key. <underlying message>` if a key fails
|
|
14
|
+
* `keyRule`.
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { isAny, isString, vg } from 'valgen';
|
|
18
|
+
*
|
|
19
|
+
* const validate = vg.isRecord(isString, isAny);
|
|
20
|
+
* validate(null as any); // throws ValidationError: 'Value must be an object'
|
|
21
|
+
*
|
|
22
|
+
* vg.isRecord(isString, isString)({ a: 1 as any, b: true as any }, { coerce: true });
|
|
23
|
+
* // => { a: '1', b: 'true' }
|
|
24
|
+
* ```
|
|
6
25
|
*/
|
|
7
26
|
export function isRecord(keyRule, valueRule, options) {
|
|
8
27
|
return validator(isRecord.name, (input, context, _this) => {
|
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is a `string`. With `coerce: true`, converts any
|
|
4
|
+
* non-`string`, non-nullish value into a `string` - an object with a
|
|
5
|
+
* `toJSON` method is stringified via `toJSON()`, other objects via
|
|
6
|
+
* `JSON.stringify`, and everything else via `String()`.
|
|
5
7
|
* @validator isString
|
|
8
|
+
* @param options - Validation options.
|
|
9
|
+
* @returns The validated (and possibly coerced) `string`.
|
|
10
|
+
* @throws `Value must be a string` if the input is `null`, `undefined`, or
|
|
11
|
+
* (without coercion) not a `string`.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isString } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isString('1'); // => '1'
|
|
17
|
+
* isString(1, { coerce: true }); // => '1'
|
|
18
|
+
* isString({ toJSON: () => 'test' }, { coerce: true }); // => 'test'
|
|
19
|
+
* ```
|
|
6
20
|
*/
|
|
7
21
|
export declare function isString(options?: isString.Options): import("../../core/validator.js").Validator<string, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
8
22
|
export declare namespace isString {
|
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is a `string`. With `coerce: true`, converts any
|
|
4
|
+
* non-`string`, non-nullish value into a `string` - an object with a
|
|
5
|
+
* `toJSON` method is stringified via `toJSON()`, other objects via
|
|
6
|
+
* `JSON.stringify`, and everything else via `String()`.
|
|
5
7
|
* @validator isString
|
|
8
|
+
* @param options - Validation options.
|
|
9
|
+
* @returns The validated (and possibly coerced) `string`.
|
|
10
|
+
* @throws `Value must be a string` if the input is `null`, `undefined`, or
|
|
11
|
+
* (without coercion) not a `string`.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isString } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isString('1'); // => '1'
|
|
17
|
+
* isString(1, { coerce: true }); // => '1'
|
|
18
|
+
* isString({ toJSON: () => 'test' }, { coerce: true }); // => 'test'
|
|
19
|
+
* ```
|
|
6
20
|
*/
|
|
7
21
|
export function isString(options) {
|
|
8
22
|
return validator(isString.name, (input, context, _this) => {
|
|
@@ -1,8 +1,27 @@
|
|
|
1
1
|
import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
3
|
+
* Validates that the value is an array of a fixed length, applying a
|
|
4
|
+
* distinct validator to each positional item. With `coerce: true`, a
|
|
5
|
+
* non-array, non-null input is wrapped into a single-item array before
|
|
6
|
+
* validating (only useful when `items.length === 1`).
|
|
5
7
|
* @validator isTuple
|
|
8
|
+
* @param items - The per-position validators; the input array must have
|
|
9
|
+
* exactly this many elements.
|
|
10
|
+
* @param options - Validation options.
|
|
11
|
+
* @returns A new array with each item replaced by its validated (and
|
|
12
|
+
* possibly coerced) value.
|
|
13
|
+
* @throws `Value must be a tuple` if the input is `null`, `undefined`, or
|
|
14
|
+
* not an array (and not coercible into one).
|
|
15
|
+
* @throws `Value must be a tuple of length <N>` if the array's length
|
|
16
|
+
* doesn't exactly match `items.length`.
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { isBoolean, isNumber, isString, vg } from 'valgen';
|
|
20
|
+
*
|
|
21
|
+
* vg.isTuple([isBoolean])([true]); // => [true]
|
|
22
|
+
* vg.isTuple([isString, isNumber, isBoolean])([1, '2', 0], { coerce: true });
|
|
23
|
+
* // => ['1', 2, false]
|
|
24
|
+
* ```
|
|
6
25
|
*/
|
|
7
26
|
export declare function isTuple<T1, I1>(items: [Validator<T1, I1>], options?: isTuple.Options): Validator<[T1], [I1]>;
|
|
8
27
|
export declare function isTuple<T1, I1, T2, I2>(items: [Validator<T1, I1>, Validator<T2, I2>], options?: isTuple.Options): Validator<[T1, T2], [I1, I2]>;
|
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* Validates that the value is `undefined`. With `coerce: true`, the rule
|
|
4
|
+
* never fails - every input, including `null` or `5`, resolves to
|
|
5
|
+
* `undefined` ("coerce" here means "force to undefined", not "convert
|
|
6
|
+
* values that merely look like undefined").
|
|
7
7
|
* @validator isUndefined
|
|
8
|
+
* @param options - Validation options.
|
|
9
|
+
* @returns `undefined`.
|
|
10
|
+
* @throws `Value must be undefined` if the input is anything other than
|
|
11
|
+
* `undefined` (and `coerce` is not set).
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isUndefined } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isUndefined(undefined); // => undefined
|
|
17
|
+
* isUndefined(5); // throws ValidationError: 'Value must be undefined'
|
|
18
|
+
* isUndefined(0, { coerce: true }); // => undefined
|
|
19
|
+
* ```
|
|
8
20
|
*/
|
|
9
21
|
export declare function isUndefined(options?: isUndefined.Options): import("../../core/validator.js").Validator<any, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
10
22
|
export declare namespace isUndefined {
|
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Validates
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* Validates that the value is `undefined`. With `coerce: true`, the rule
|
|
4
|
+
* never fails - every input, including `null` or `5`, resolves to
|
|
5
|
+
* `undefined` ("coerce" here means "force to undefined", not "convert
|
|
6
|
+
* values that merely look like undefined").
|
|
7
7
|
* @validator isUndefined
|
|
8
|
+
* @param options - Validation options.
|
|
9
|
+
* @returns `undefined`.
|
|
10
|
+
* @throws `Value must be undefined` if the input is anything other than
|
|
11
|
+
* `undefined` (and `coerce` is not set).
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { isUndefined } from 'valgen';
|
|
15
|
+
*
|
|
16
|
+
* isUndefined(undefined); // => undefined
|
|
17
|
+
* isUndefined(5); // throws ValidationError: 'Value must be undefined'
|
|
18
|
+
* isUndefined(0, { coerce: true }); // => undefined
|
|
19
|
+
* ```
|
|
8
20
|
*/
|
|
9
21
|
export function isUndefined(options) {
|
|
10
22
|
return validator(isUndefined.name, (input, context, _this) => {
|
|
@@ -1,10 +1,37 @@
|
|
|
1
1
|
import type { ValidationOptions, Validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs every rule in `rules` against the same, unchanged input and requires
|
|
4
|
+
* all of them to pass. Unlike {@link pipe}, one rule's output never feeds
|
|
5
|
+
* into the next - every rule sees the original input.
|
|
6
|
+
*
|
|
7
|
+
* Does not short-circuit: every rule is evaluated even after an earlier one
|
|
8
|
+
* has already failed, so a failing input can surface issues from multiple
|
|
9
|
+
* rules at once. The accumulated failures are thrown together as a single
|
|
10
|
+
* `ValidationError` when the outermost call completes.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - The output type on success (the unchanged input's type).
|
|
13
|
+
* @param rules - The validators to run, each against the same input.
|
|
14
|
+
* @param options - Shared validation options (`coerce`, `onFail`); `allOf`
|
|
15
|
+
* has no options of its own.
|
|
16
|
+
* @returns A validator that returns the original input unchanged if every
|
|
17
|
+
* rule in `rules` passes.
|
|
18
|
+
* @throws {@link ValidationError} if one or more rules fail - reporting each
|
|
19
|
+
* failing rule's own message.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { isNumber, vg } from 'valgen';
|
|
24
|
+
*
|
|
25
|
+
* // must be a number AND greater than 5 AND less than 10
|
|
26
|
+
* const codec = vg.allOf([isNumber, vg.isGt(5), vg.isLt(10)]);
|
|
27
|
+
* codec(6); // => 6
|
|
28
|
+
* codec('x'); // throws: "Value must be a number"
|
|
29
|
+
* ```
|
|
4
30
|
* @validator allOf
|
|
5
31
|
*/
|
|
6
32
|
export declare function allOf<T = any>(rules: Validator[], options?: allOf.Options): Validator<T>;
|
|
7
33
|
export declare namespace allOf {
|
|
34
|
+
/** Options accepted by {@link allOf}. Only the shared {@link ValidationOptions} - no `allOf`-specific fields. */
|
|
8
35
|
interface Options extends ValidationOptions {
|
|
9
36
|
}
|
|
10
37
|
}
|
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Runs every rule in `rules` against the same, unchanged input and requires
|
|
4
|
+
* all of them to pass. Unlike {@link pipe}, one rule's output never feeds
|
|
5
|
+
* into the next - every rule sees the original input.
|
|
6
|
+
*
|
|
7
|
+
* Does not short-circuit: every rule is evaluated even after an earlier one
|
|
8
|
+
* has already failed, so a failing input can surface issues from multiple
|
|
9
|
+
* rules at once. The accumulated failures are thrown together as a single
|
|
10
|
+
* `ValidationError` when the outermost call completes.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - The output type on success (the unchanged input's type).
|
|
13
|
+
* @param rules - The validators to run, each against the same input.
|
|
14
|
+
* @param options - Shared validation options (`coerce`, `onFail`); `allOf`
|
|
15
|
+
* has no options of its own.
|
|
16
|
+
* @returns A validator that returns the original input unchanged if every
|
|
17
|
+
* rule in `rules` passes.
|
|
18
|
+
* @throws {@link ValidationError} if one or more rules fail - reporting each
|
|
19
|
+
* failing rule's own message.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { isNumber, vg } from 'valgen';
|
|
24
|
+
*
|
|
25
|
+
* // must be a number AND greater than 5 AND less than 10
|
|
26
|
+
* const codec = vg.allOf([isNumber, vg.isGt(5), vg.isLt(10)]);
|
|
27
|
+
* codec(6); // => 6
|
|
28
|
+
* codec('x'); // throws: "Value must be a number"
|
|
29
|
+
* ```
|
|
4
30
|
* @validator allOf
|
|
5
31
|
*/
|
|
6
32
|
export function allOf(rules, options) {
|
|
@@ -1,10 +1,39 @@
|
|
|
1
1
|
import { type ValidationOptions } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Checks that a property is present on its parent object, independent of
|
|
4
|
+
* whether its value is `undefined`.
|
|
5
|
+
*
|
|
6
|
+
* Passes when `input !== undefined` (any defined value, including `null` or
|
|
7
|
+
* `0`, is fine). When `input === undefined`, it still passes if the
|
|
8
|
+
* surrounding object explicitly *has* the property (checked via
|
|
9
|
+
* `Object.getOwnPropertyDescriptor` on `context.scope`/`context.property`) -
|
|
10
|
+
* this only works when `exists()` is nested inside an object schema (e.g.
|
|
11
|
+
* `vg.isObject({...})`) that populates `context.scope`/`context.property`;
|
|
12
|
+
* used standalone, an `undefined` input always fails.
|
|
13
|
+
*
|
|
14
|
+
* @param options - Shared validation options (`coerce`, `onFail`); `exists`
|
|
15
|
+
* has no options of its own.
|
|
16
|
+
* @returns A validator that returns the input unchanged when the property exists.
|
|
17
|
+
* @throws {@link ValidationError} with `` `{{label}}` must exist `` (e.g.
|
|
18
|
+
* `` `a` must exist ``, or `Value must exist` at the root) when the
|
|
19
|
+
* property is truly absent.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { vg } from 'valgen';
|
|
24
|
+
*
|
|
25
|
+
* vg.exists()(0); // => 0
|
|
26
|
+
* vg.exists()(null); // => null
|
|
27
|
+
* vg.exists()(undefined); // throws: "Value must exist"
|
|
28
|
+
*
|
|
29
|
+
* const objVal = vg.isObject({ a: vg.exists() });
|
|
30
|
+
* objVal({}); // throws: "`a` must exist"
|
|
31
|
+
* ```
|
|
4
32
|
* @validator exists
|
|
5
33
|
*/
|
|
6
34
|
export declare function exists(options?: exists.Options): import("../../core/validator.js").Validator<any, unknown, import("../../core/types.js").ExecutionOptions>;
|
|
7
35
|
export declare namespace exists {
|
|
36
|
+
/** Options accepted by {@link exists}. Only the shared {@link ValidationOptions} - no `exists`-specific fields. */
|
|
8
37
|
interface Options extends ValidationOptions {
|
|
9
38
|
}
|
|
10
39
|
}
|
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Checks that a property is present on its parent object, independent of
|
|
4
|
+
* whether its value is `undefined`.
|
|
5
|
+
*
|
|
6
|
+
* Passes when `input !== undefined` (any defined value, including `null` or
|
|
7
|
+
* `0`, is fine). When `input === undefined`, it still passes if the
|
|
8
|
+
* surrounding object explicitly *has* the property (checked via
|
|
9
|
+
* `Object.getOwnPropertyDescriptor` on `context.scope`/`context.property`) -
|
|
10
|
+
* this only works when `exists()` is nested inside an object schema (e.g.
|
|
11
|
+
* `vg.isObject({...})`) that populates `context.scope`/`context.property`;
|
|
12
|
+
* used standalone, an `undefined` input always fails.
|
|
13
|
+
*
|
|
14
|
+
* @param options - Shared validation options (`coerce`, `onFail`); `exists`
|
|
15
|
+
* has no options of its own.
|
|
16
|
+
* @returns A validator that returns the input unchanged when the property exists.
|
|
17
|
+
* @throws {@link ValidationError} with `` `{{label}}` must exist `` (e.g.
|
|
18
|
+
* `` `a` must exist ``, or `Value must exist` at the root) when the
|
|
19
|
+
* property is truly absent.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { vg } from 'valgen';
|
|
24
|
+
*
|
|
25
|
+
* vg.exists()(0); // => 0
|
|
26
|
+
* vg.exists()(null); // => null
|
|
27
|
+
* vg.exists()(undefined); // throws: "Value must exist"
|
|
28
|
+
*
|
|
29
|
+
* const objVal = vg.isObject({ a: vg.exists() });
|
|
30
|
+
* objVal({}); // throws: "`a` must exist"
|
|
31
|
+
* ```
|
|
4
32
|
* @validator exists
|
|
5
33
|
*/
|
|
6
34
|
export function exists(options) {
|
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Ignores
|
|
2
|
+
* Ignores whatever input it is given and always returns the same constant
|
|
3
|
+
* value. Never inspects or validates the input and never calls
|
|
4
|
+
* `context.fail` - it always succeeds. Useful for injecting a constant field
|
|
5
|
+
* into an object schema, or as the "otherwise" branch of another combinator
|
|
6
|
+
* (e.g. `iif`).
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - The type of the constant `value` returned on every call.
|
|
9
|
+
* @typeParam I - The (ignored) input type.
|
|
10
|
+
* @param value - The constant value to return, regardless of what is passed
|
|
11
|
+
* in at call time.
|
|
12
|
+
* @returns A validator that always returns `value`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { vg } from 'valgen';
|
|
17
|
+
*
|
|
18
|
+
* vg.fixed(0)(1); // => 0
|
|
19
|
+
* vg.fixed(null)(1); // => null
|
|
20
|
+
* ```
|
|
3
21
|
* @validator fixed
|
|
4
22
|
*/
|
|
5
23
|
export declare function fixed<T, I>(value: T): import("../../core/validator.js").Validator<T, I, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Ignores
|
|
3
|
+
* Ignores whatever input it is given and always returns the same constant
|
|
4
|
+
* value. Never inspects or validates the input and never calls
|
|
5
|
+
* `context.fail` - it always succeeds. Useful for injecting a constant field
|
|
6
|
+
* into an object schema, or as the "otherwise" branch of another combinator
|
|
7
|
+
* (e.g. `iif`).
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - The type of the constant `value` returned on every call.
|
|
10
|
+
* @typeParam I - The (ignored) input type.
|
|
11
|
+
* @param value - The constant value to return, regardless of what is passed
|
|
12
|
+
* in at call time.
|
|
13
|
+
* @returns A validator that always returns `value`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { vg } from 'valgen';
|
|
18
|
+
*
|
|
19
|
+
* vg.fixed(0)(1); // => 0
|
|
20
|
+
* vg.fixed(null)(1); // => null
|
|
21
|
+
* ```
|
|
4
22
|
* @validator fixed
|
|
5
23
|
*/
|
|
6
24
|
export function fixed(value) {
|
|
@@ -4,8 +4,31 @@ type ExtractLengthInput = string | any[] | ArrayBuffer | {
|
|
|
4
4
|
size: number;
|
|
5
5
|
};
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* object
|
|
7
|
+
* Extracts a numeric length or size from a string, array, `ArrayBuffer`,
|
|
8
|
+
* `Set`/`Map`, or any object exposing a `length`/`size` property. Takes no
|
|
9
|
+
* options; it's typically piped into a comparison rule to enforce a min/max
|
|
10
|
+
* length.
|
|
11
|
+
*
|
|
12
|
+
* @returns A validator that returns `input.length` for strings and arrays,
|
|
13
|
+
* `input.byteLength` for an `ArrayBuffer`, `input.length` for any object
|
|
14
|
+
* with a numeric `length` property (checked before `size`), or otherwise
|
|
15
|
+
* `input.size` for any object with a numeric `size` property (this covers
|
|
16
|
+
* `Set` and `Map`).
|
|
17
|
+
* @throws {@link ValidationError} with `Unable to get length` for anything
|
|
18
|
+
* else (e.g. numbers, booleans, plain objects without `length`/`size`).
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { vg } from 'valgen';
|
|
23
|
+
*
|
|
24
|
+
* vg.getLength()('1234'); // => 4
|
|
25
|
+
* vg.getLength()([1, 2, 3, 4]); // => 4
|
|
26
|
+
* vg.getLength()(new Set([1, 2, 3, 4])); // => 4
|
|
27
|
+
*
|
|
28
|
+
* // combine with a comparison rule to enforce a minimum length
|
|
29
|
+
* const minLength3 = vg.pipe([vg.getLength(), vg.isGte(3)]);
|
|
30
|
+
* minLength3('ab'); // throws (length 2 < 3)
|
|
31
|
+
* ```
|
|
9
32
|
* @validator getLength
|
|
10
33
|
*/
|
|
11
34
|
export declare function getLength(): import("../../core/validator.js").Validator<number, ExtractLengthInput, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* object
|
|
3
|
+
* Extracts a numeric length or size from a string, array, `ArrayBuffer`,
|
|
4
|
+
* `Set`/`Map`, or any object exposing a `length`/`size` property. Takes no
|
|
5
|
+
* options; it's typically piped into a comparison rule to enforce a min/max
|
|
6
|
+
* length.
|
|
7
|
+
*
|
|
8
|
+
* @returns A validator that returns `input.length` for strings and arrays,
|
|
9
|
+
* `input.byteLength` for an `ArrayBuffer`, `input.length` for any object
|
|
10
|
+
* with a numeric `length` property (checked before `size`), or otherwise
|
|
11
|
+
* `input.size` for any object with a numeric `size` property (this covers
|
|
12
|
+
* `Set` and `Map`).
|
|
13
|
+
* @throws {@link ValidationError} with `Unable to get length` for anything
|
|
14
|
+
* else (e.g. numbers, booleans, plain objects without `length`/`size`).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { vg } from 'valgen';
|
|
19
|
+
*
|
|
20
|
+
* vg.getLength()('1234'); // => 4
|
|
21
|
+
* vg.getLength()([1, 2, 3, 4]); // => 4
|
|
22
|
+
* vg.getLength()(new Set([1, 2, 3, 4])); // => 4
|
|
23
|
+
*
|
|
24
|
+
* // combine with a comparison rule to enforce a minimum length
|
|
25
|
+
* const minLength3 = vg.pipe([vg.getLength(), vg.isGte(3)]);
|
|
26
|
+
* minLength3('ab'); // throws (length 2 < 3)
|
|
27
|
+
* ```
|
|
5
28
|
* @validator getLength
|
|
6
29
|
*/
|
|
7
30
|
export function getLength() {
|
|
@@ -1,11 +1,38 @@
|
|
|
1
1
|
import type { Nullish } from 'ts-gems';
|
|
2
2
|
import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Wraps a rule so that `null` or `undefined` pass through untouched, and
|
|
5
|
+
* anything else is delegated to the nested rule.
|
|
6
|
+
*
|
|
7
|
+
* If `input == null` (i.e. `null` **or** `undefined`), it is returned as-is
|
|
8
|
+
* without ever calling the nested rule - despite the name, `nullable`
|
|
9
|
+
* tolerates both `null` and `undefined`, not just `null`. For any other
|
|
10
|
+
* input, delegates to `nested(input)` and returns/throws exactly what the
|
|
11
|
+
* nested rule does.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam T - The nested validator's output type.
|
|
14
|
+
* @typeParam I - The nested validator's input type.
|
|
15
|
+
* @param nested - The validator to delegate to for non-nullish input.
|
|
16
|
+
* @param options - Shared validation options (`coerce`, `onFail`); `nullable`
|
|
17
|
+
* has no options of its own.
|
|
18
|
+
* @returns A validator that returns `null`/`undefined` unchanged, or the
|
|
19
|
+
* nested rule's result for anything else.
|
|
20
|
+
* @throws Whatever `nested` throws when the input is non-nullish and fails
|
|
21
|
+
* the nested rule.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { isString, vg } from 'valgen';
|
|
26
|
+
*
|
|
27
|
+
* vg.nullable(isString)(''); // => ''
|
|
28
|
+
* vg.nullable(isString)(undefined); // => undefined
|
|
29
|
+
* vg.nullable(isString)(null); // => null
|
|
30
|
+
* ```
|
|
5
31
|
* @validator nullable
|
|
6
32
|
*/
|
|
7
33
|
export declare function nullable<T, I>(nested: Validator<T, I>, options?: nullable.Options): Validator<Nullish<T>, Nullish<I>, import("../../core/types.js").ExecutionOptions>;
|
|
8
34
|
export declare namespace nullable {
|
|
35
|
+
/** Options accepted by {@link nullable}. Only the shared {@link ValidationOptions} - no `nullable`-specific fields. */
|
|
9
36
|
interface Options extends ValidationOptions {
|
|
10
37
|
}
|
|
11
38
|
}
|