valgen 3.2.1 → 4.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +12 -7
- package/cjs/core/constants.js +5 -0
- package/cjs/core/context.js +65 -0
- package/cjs/core/index.js +21 -0
- package/cjs/core/types.js +2 -0
- package/cjs/core/validation-error.js +11 -0
- package/cjs/core/validator.js +61 -0
- package/cjs/helpers/object.utils.js +19 -0
- package/cjs/helpers/string.utils.js +9 -0
- package/cjs/index.js +18 -0
- package/cjs/package.json +3 -0
- package/cjs/rules/index.js +38 -0
- package/cjs/rules/is-any.js +12 -0
- package/cjs/rules/is-array.js +38 -0
- package/cjs/rules/is-boolean.js +30 -0
- package/cjs/rules/is-date.js +63 -0
- package/cjs/rules/is-empty.js +68 -0
- package/cjs/rules/is-enum.js +30 -0
- package/cjs/rules/is-integer.js +26 -0
- package/cjs/rules/is-matches.js +24 -0
- package/cjs/rules/is-null.js +40 -0
- package/cjs/rules/is-number.js +26 -0
- package/cjs/rules/is-object-id.js +28 -0
- package/cjs/rules/is-object.js +103 -0
- package/cjs/rules/is-record.js +41 -0
- package/cjs/rules/is-string.js +79 -0
- package/cjs/rules/is-tuple.js +32 -0
- package/cjs/rules/length.js +41 -0
- package/cjs/rules/optional.js +44 -0
- package/cjs/rules/pipe.js +43 -0
- package/cjs/rules/range.js +89 -0
- package/cjs/rules/string-formats.js +296 -0
- package/cjs/rules/union.js +31 -0
- package/cjs/rules/utilities.js +31 -0
- package/esm/core/constants.js +2 -0
- package/esm/core/context.js +61 -0
- package/esm/core/index.js +5 -0
- package/esm/core/types.js +1 -0
- package/esm/core/validation-error.js +7 -0
- package/esm/core/validator.js +56 -0
- package/esm/helpers/object.utils.js +14 -0
- package/esm/helpers/string.utils.js +5 -0
- package/esm/index.js +2 -0
- package/esm/rules/index.js +22 -0
- package/esm/rules/is-any.js +8 -0
- package/esm/rules/is-array.js +34 -0
- package/esm/rules/is-boolean.js +26 -0
- package/esm/rules/is-date.js +55 -0
- package/esm/rules/is-empty.js +63 -0
- package/esm/rules/is-enum.js +26 -0
- package/esm/rules/is-integer.js +22 -0
- package/esm/rules/is-matches.js +20 -0
- package/esm/rules/is-null.js +34 -0
- package/esm/rules/is-number.js +22 -0
- package/esm/rules/is-object-id.js +21 -0
- package/esm/rules/is-object.js +99 -0
- package/esm/rules/is-record.js +37 -0
- package/esm/rules/is-string.js +70 -0
- package/esm/rules/is-tuple.js +28 -0
- package/esm/rules/length.js +35 -0
- package/esm/rules/optional.js +38 -0
- package/esm/rules/pipe.js +38 -0
- package/esm/rules/range.js +81 -0
- package/esm/rules/string-formats.js +266 -0
- package/esm/rules/union.js +27 -0
- package/esm/rules/utilities.js +26 -0
- package/package.json +33 -37
- package/typings/core/constants.d.ts +2 -0
- package/typings/core/context.d.ts +26 -0
- package/typings/core/index.d.ts +6 -0
- package/typings/core/types.d.ts +18 -0
- package/typings/core/validation-error.d.ts +5 -0
- package/typings/core/validator.d.ts +18 -0
- package/typings/helpers/object.utils.d.ts +2 -0
- package/typings/helpers/string.utils.d.ts +1 -0
- package/typings/index.d.ts +2 -0
- package/typings/rules/index.d.ts +22 -0
- package/typings/rules/is-any.d.ts +5 -0
- package/typings/rules/is-array.d.ts +7 -0
- package/typings/rules/is-boolean.d.ts +7 -0
- package/typings/rules/is-date.d.ts +19 -0
- package/typings/rules/is-empty.d.ts +13 -0
- package/typings/rules/is-enum.d.ts +9 -0
- package/typings/rules/is-integer.d.ts +7 -0
- package/typings/rules/is-matches.d.ts +9 -0
- package/typings/rules/is-null.d.ts +16 -0
- package/typings/rules/is-number.d.ts +7 -0
- package/typings/rules/is-object-id.d.ts +10 -0
- package/typings/rules/is-object.d.ts +22 -0
- package/typings/rules/is-record.d.ts +7 -0
- package/typings/rules/is-string.d.ts +42 -0
- package/typings/rules/is-tuple.d.ts +27 -0
- package/typings/rules/length.d.ts +21 -0
- package/typings/rules/optional.d.ts +17 -0
- package/typings/rules/pipe.d.ts +11 -0
- package/typings/rules/range.d.ts +48 -0
- package/typings/rules/string-formats.d.ts +140 -0
- package/typings/rules/union.d.ts +5 -0
- package/typings/rules/utilities.d.ts +11 -0
- package/lib/ArrayType.d.ts +0 -5
- package/lib/ArrayType.js +0 -43
- package/lib/DataType.d.ts +0 -25
- package/lib/DataType.js +0 -214
- package/lib/ObjectType.d.ts +0 -5
- package/lib/ObjectType.js +0 -78
- package/lib/SchemaError.d.ts +0 -3
- package/lib/SchemaError.js +0 -17
- package/lib/UnionType.d.ts +0 -5
- package/lib/UnionType.js +0 -54
- package/lib/Valgen.d.ts +0 -88
- package/lib/Valgen.js +0 -289
- package/lib/ValidationError.d.ts +0 -2
- package/lib/ValidationError.js +0 -4
- package/lib/factories/AnyFactory.d.ts +0 -28
- package/lib/factories/AnyFactory.js +0 -73
- package/lib/factories/ArrayFactory.d.ts +0 -16
- package/lib/factories/ArrayFactory.js +0 -148
- package/lib/factories/Base64Factory.d.ts +0 -12
- package/lib/factories/Base64Factory.js +0 -31
- package/lib/factories/BooleanFactory.d.ts +0 -12
- package/lib/factories/BooleanFactory.js +0 -49
- package/lib/factories/DateFactory.d.ts +0 -40
- package/lib/factories/DateFactory.js +0 -239
- package/lib/factories/FunctionFactory.d.ts +0 -12
- package/lib/factories/FunctionFactory.js +0 -28
- package/lib/factories/IntegerFactory.d.ts +0 -14
- package/lib/factories/IntegerFactory.js +0 -33
- package/lib/factories/NilFactory.d.ts +0 -4
- package/lib/factories/NilFactory.js +0 -20
- package/lib/factories/NumberFactory.d.ts +0 -36
- package/lib/factories/NumberFactory.js +0 -178
- package/lib/factories/ObjectFactory.d.ts +0 -38
- package/lib/factories/ObjectFactory.js +0 -326
- package/lib/factories/StringFactory.d.ts +0 -16
- package/lib/factories/StringFactory.js +0 -94
- package/lib/factories/UnionFactory.d.ts +0 -13
- package/lib/factories/UnionFactory.js +0 -62
- package/lib/index.d.ts +0 -19
- package/lib/index.js +0 -37
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Type, ValidationOptions, Validator } from '../core/index.js';
|
|
2
|
+
export type PropertyOptions = {
|
|
3
|
+
label?: string;
|
|
4
|
+
as?: string;
|
|
5
|
+
};
|
|
6
|
+
export type ObjectSchema = Record<string | number, Validator<any, any> | [Validator<any, any>, PropertyOptions]>;
|
|
7
|
+
export interface IsObjectOptions<T> extends ValidationOptions {
|
|
8
|
+
name?: string;
|
|
9
|
+
ctor?: Type<T>;
|
|
10
|
+
additionalFields?: boolean | Validator<any, any>;
|
|
11
|
+
caseInSensitive?: boolean;
|
|
12
|
+
detectCircular?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface ObjectValidator<T extends object = object, I = object> extends Validator<T, I> {
|
|
15
|
+
schema: ObjectSchema;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validates object according to schema
|
|
19
|
+
* Converts properties according to schema rules if coerce option is set to 'true'.
|
|
20
|
+
* @validator isObject
|
|
21
|
+
*/
|
|
22
|
+
export declare function isObject<T extends object = object, I = object>(schema: ObjectSchema, options?: IsObjectOptions<T>): ObjectValidator<T, I>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationOptions, Validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates record object according to given "key" and "value" rules
|
|
4
|
+
* Converts properties according to rules if coerce option is set to 'true'.
|
|
5
|
+
* @validator isRecord
|
|
6
|
+
*/
|
|
7
|
+
export declare function isRecord<TKeys extends string | number | symbol, TValues>(keyRule: Validator<TKeys>, valueRule: Validator<TValues>, options?: ValidationOptions): Validator<Record<TKeys, TValues>, Record<TKeys, TValues>, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ValidationOptions } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "string".
|
|
4
|
+
* Converts input value to string if coerce option is set to 'true'.
|
|
5
|
+
* @validator isString
|
|
6
|
+
*/
|
|
7
|
+
export declare function isString(options?: ValidationOptions): import("../core/validator.js").Validator<string, unknown, import("../core/types.js").ExecutionOptions>;
|
|
8
|
+
/**
|
|
9
|
+
* Process "String.replace" method
|
|
10
|
+
* @validator stringReplace
|
|
11
|
+
*/
|
|
12
|
+
export declare function stringReplace(searchValue: string | RegExp, replaceValue: string): any;
|
|
13
|
+
export declare function stringReplace(searchValue: string | RegExp, replacer: (subsring: string, ...args: any[]) => string): any;
|
|
14
|
+
export declare function stringReplace(searchValue: {
|
|
15
|
+
[Symbol.replace](string: string, replaceValue: string): string;
|
|
16
|
+
}, replaceValue: string): any;
|
|
17
|
+
export declare function stringReplace(searchValue: {
|
|
18
|
+
[Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string;
|
|
19
|
+
}, replacer: (substring: string, ...args: any[]) => string): any;
|
|
20
|
+
/**
|
|
21
|
+
* Process "String.split" method
|
|
22
|
+
* @validator split
|
|
23
|
+
*/
|
|
24
|
+
export declare function stringSplit(separator: string | RegExp, limit?: number): any;
|
|
25
|
+
export declare function stringSplit(splitter: {
|
|
26
|
+
[Symbol.split](string: string, limit?: number): string[];
|
|
27
|
+
}, limit?: number): any;
|
|
28
|
+
/**
|
|
29
|
+
* Removes whitespace from both ends of a string
|
|
30
|
+
* @validator trim
|
|
31
|
+
*/
|
|
32
|
+
export declare function trim(): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
33
|
+
/**
|
|
34
|
+
* Removes whitespace from the end of a string
|
|
35
|
+
* @validator trimEnd
|
|
36
|
+
*/
|
|
37
|
+
export declare const trimEnd: () => import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
38
|
+
/**
|
|
39
|
+
* Removes whitespace from the beginning of a string
|
|
40
|
+
* @validator trimStart
|
|
41
|
+
*/
|
|
42
|
+
export declare const trimStart: () => import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ValidationOptions, Validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "tuple" and applies validation for each item.
|
|
4
|
+
* Converts input value to tuple if coerce option is set to 'true'.
|
|
5
|
+
* @validator isTuple
|
|
6
|
+
*/
|
|
7
|
+
export declare function isTuple<T1, I1>(items: [Validator<T1, I1>], options?: ValidationOptions): Validator<[T1], [I1]>;
|
|
8
|
+
export declare function isTuple<T1, I1, T2, I2>(items: [Validator<T1, I1>, Validator<T2, I2>], options?: ValidationOptions): Validator<[T1, T2], [I1, I2]>;
|
|
9
|
+
export declare function isTuple<T1, I1, T2, I2, T3, I3>(items: [Validator<T1, I1>, Validator<T2, I2>, Validator<T3, I3>], options?: ValidationOptions): Validator<[T1, T2, T3], [I1, I2, I3]>;
|
|
10
|
+
export declare function isTuple<T1, I1, T2, I2, T3, I3, T4, I4>(items: [Validator<T1, I1>, Validator<T2, I2>, Validator<T3, I3>, Validator<T4, I4>], options?: ValidationOptions): Validator<[T1, T2, T3, T4], [I1, I2, I3, I4]>;
|
|
11
|
+
export declare function isTuple<T1, I1, T2, I2, T3, I3, T4, I4>(items: [Validator<T1, I1>, Validator<T2, I2>, Validator<T3, I3>, Validator<T4, I4>], options?: ValidationOptions): Validator<[T1, T2, T3, T4], [I1, I2, I3, I4]>;
|
|
12
|
+
export declare function isTuple<T1, I1, T2, I2, T3, I3, T4, I4, T5, I5>(items: [
|
|
13
|
+
Validator<T1, I1>,
|
|
14
|
+
Validator<T2, I2>,
|
|
15
|
+
Validator<T3, I3>,
|
|
16
|
+
Validator<T4, I4>,
|
|
17
|
+
Validator<T5, I5>
|
|
18
|
+
], options?: ValidationOptions): Validator<[T1, T2, T3, T4, T5], [I1, I2, I3, I4, I5]>;
|
|
19
|
+
export declare function isTuple<T1, I1, T2, I2, T3, I3, T4, I4, T5, I5, T6, I6>(items: [
|
|
20
|
+
Validator<T1, I1>,
|
|
21
|
+
Validator<T2, I2>,
|
|
22
|
+
Validator<T3, I3>,
|
|
23
|
+
Validator<T4, I4>,
|
|
24
|
+
Validator<T5, I5>,
|
|
25
|
+
Validator<T6, I6>,
|
|
26
|
+
...Validator<any, any>[]
|
|
27
|
+
], options?: ValidationOptions): Validator<[T1, T2, T3, T4, T5, T6, ...any[]], [I1, I2, I3, I4, I5, I5, ...any[]]>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns length of an Array, String, ArrayBuffer, Buffer or any object with the "length" property.
|
|
3
|
+
* @validator getLength
|
|
4
|
+
*/
|
|
5
|
+
export declare const extractLength: () => import("../core/validator.js").Validator<number, ExtractLengthInput, import("../core/types.js").ExecutionOptions>;
|
|
6
|
+
type ExtractLengthInput = string | any[] | ArrayBuffer | {
|
|
7
|
+
length: number;
|
|
8
|
+
} | {
|
|
9
|
+
size: number;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Checks the length is at least "minValue"
|
|
13
|
+
* @validator lengthMin
|
|
14
|
+
*/
|
|
15
|
+
export declare const lengthMin: (minValue: number) => import("../core/validator.js").Validator<any, any, import("../core/types.js").ExecutionOptions>;
|
|
16
|
+
/**
|
|
17
|
+
* Checks if the length is at most "maxValue"
|
|
18
|
+
* @validator charLengthMax
|
|
19
|
+
*/
|
|
20
|
+
export declare const lengthMax: (maxValue: number) => import("../core/validator.js").Validator<any, any, import("../core/types.js").ExecutionOptions>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Nullish } from 'ts-gems';
|
|
2
|
+
import { ValidationOptions, Validator } from '../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Makes sub-rule optional
|
|
5
|
+
* @validator optional
|
|
6
|
+
*/
|
|
7
|
+
export declare function optional<T, I>(nested: Validator<T, I>, options?: ValidationOptions): Validator<Nullish<T>, Nullish<I>, import("../core/types.js").ExecutionOptions>;
|
|
8
|
+
/**
|
|
9
|
+
* Makes sub-rule required
|
|
10
|
+
* @validator required
|
|
11
|
+
*/
|
|
12
|
+
export declare function required<T, I>(nested: Validator<T, I>, options?: ValidationOptions): Validator<Nullish<T>, I, import("../core/types.js").ExecutionOptions>;
|
|
13
|
+
/**
|
|
14
|
+
* Validates if property exists
|
|
15
|
+
* @validator exists
|
|
16
|
+
*/
|
|
17
|
+
export declare function exists(options?: ValidationOptions): Validator<any, unknown, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @validator pipe
|
|
5
|
+
*/
|
|
6
|
+
export declare function pipe<T>(...rules: [...Validator<any, any>[], Validator<T, any>]): Validator<T, any, import("../core/types.js").ExecutionOptions>;
|
|
7
|
+
/**
|
|
8
|
+
* Test given value against to all codecs and returns original input
|
|
9
|
+
* @validator allOf
|
|
10
|
+
*/
|
|
11
|
+
export declare function allOf(...rules: Validator<any, any>[]): Validator<any, any, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ValidationOptions, Validator } from '../core/index.js';
|
|
2
|
+
type RangeInput = number | bigint | Date | string;
|
|
3
|
+
/**
|
|
4
|
+
* Checks if value is grater than "minValue"
|
|
5
|
+
* @validator iGt
|
|
6
|
+
*/
|
|
7
|
+
export declare function isGt(minValue: number, options?: ValidationOptions): Validator<number, number>;
|
|
8
|
+
export declare function isGt(minValue: bigint, options?: ValidationOptions): Validator<bigint, bigint | number>;
|
|
9
|
+
export declare function isGt(minValue: Date, options?: ValidationOptions): Validator<Date, Date>;
|
|
10
|
+
export declare function isGt(minValue: string, options?: ValidationOptions & {
|
|
11
|
+
caseInsensitive?: boolean;
|
|
12
|
+
}): Validator<string, string>;
|
|
13
|
+
/**
|
|
14
|
+
* Checks if value is grater than or equal to minValue
|
|
15
|
+
* @validator isGte
|
|
16
|
+
*/
|
|
17
|
+
export declare function isGte(minValue: number, options?: ValidationOptions): Validator<number, number>;
|
|
18
|
+
export declare function isGte(minValue: bigint, options?: ValidationOptions): Validator<bigint, bigint | number>;
|
|
19
|
+
export declare function isGte(minValue: Date, options?: ValidationOptions): Validator<Date, Date>;
|
|
20
|
+
export declare function isGte(minValue: string, options?: ValidationOptions & {
|
|
21
|
+
caseInsensitive?: boolean;
|
|
22
|
+
}): Validator<string, string>;
|
|
23
|
+
/**
|
|
24
|
+
* Checks if number value is lover than maxValue
|
|
25
|
+
* @validator isLt
|
|
26
|
+
*/
|
|
27
|
+
export declare function isLt(maxValue: number, options?: ValidationOptions): Validator<number, number>;
|
|
28
|
+
export declare function isLt(maxValue: bigint, options?: ValidationOptions): Validator<bigint, bigint | number>;
|
|
29
|
+
export declare function isLt(maxValue: Date, options?: ValidationOptions): Validator<Date, Date>;
|
|
30
|
+
export declare function isLt(maxValue: string, options?: ValidationOptions & {
|
|
31
|
+
caseInsensitive?: boolean;
|
|
32
|
+
}): Validator<string, string>;
|
|
33
|
+
/**
|
|
34
|
+
* Checks if value is lover than or equal to maxValue
|
|
35
|
+
* @validator isLte
|
|
36
|
+
*/
|
|
37
|
+
export declare function isLte(maxValue: number, options?: ValidationOptions): Validator<number, number>;
|
|
38
|
+
export declare function isLte(maxValue: bigint, options?: ValidationOptions): Validator<bigint, bigint | number>;
|
|
39
|
+
export declare function isLte(maxValue: Date, options?: ValidationOptions): Validator<Date, Date>;
|
|
40
|
+
export declare function isLte(maxValue: string, options?: ValidationOptions & {
|
|
41
|
+
caseInsensitive?: boolean;
|
|
42
|
+
}): Validator<string, string>;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if value is between minValue and maxValue
|
|
45
|
+
* @validator range
|
|
46
|
+
*/
|
|
47
|
+
export declare function range<T extends RangeInput>(minValue: T, maxValue: T, options?: ValidationOptions): Validator<T, T, import("../core/types.js").ExecutionOptions>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import validatorJS from 'validator';
|
|
2
|
+
import { ValidationOptions } from '../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is an "UUID".
|
|
5
|
+
* @validator isUUID
|
|
6
|
+
*/
|
|
7
|
+
export declare function isUUID(version?: 1 | 2 | 3 | 4 | 5, options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
8
|
+
export interface IsEmailOptions extends ValidationOptions, validatorJS.IsEmailOptions {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Validates if value is a valid Email
|
|
12
|
+
* @validator isEmail
|
|
13
|
+
*/
|
|
14
|
+
export declare function isEmail(options?: IsEmailOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
15
|
+
export interface IsMobilePhoneOptions extends ValidationOptions, validatorJS.IsMobilePhoneOptions {
|
|
16
|
+
locale?: 'any' | validatorJS.MobilePhoneLocale | validatorJS.MobilePhoneLocale[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Validates if value is a valid Email
|
|
20
|
+
* @validator isMobilePhone
|
|
21
|
+
*/
|
|
22
|
+
export declare function isMobilePhone(options?: IsMobilePhoneOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
23
|
+
/**
|
|
24
|
+
* Validates if value is an IP
|
|
25
|
+
* @validator isIP
|
|
26
|
+
*/
|
|
27
|
+
export declare function isIP(version?: 4 | 6, options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
28
|
+
/**
|
|
29
|
+
* Validates if value is an IP
|
|
30
|
+
* @validator isUUID
|
|
31
|
+
*/
|
|
32
|
+
export declare function isIPRange(version?: 4 | 6, options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
33
|
+
export interface IsMACAddressOptions extends ValidationOptions, validatorJS.IsMACAddressOptions {
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Validates if value is an MACAddress
|
|
37
|
+
* @validator isMACAddress
|
|
38
|
+
*/
|
|
39
|
+
export declare function isMACAddress(options?: IsMACAddressOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
40
|
+
/**
|
|
41
|
+
* Validates if value is a port number
|
|
42
|
+
* @validator isPort
|
|
43
|
+
*/
|
|
44
|
+
export declare function isPort(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
45
|
+
export interface IsURLOptions extends ValidationOptions, validatorJS.IsURLOptions {
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Validates if value is an MACAddress
|
|
49
|
+
* @validator isURL
|
|
50
|
+
*/
|
|
51
|
+
export declare function isURL(options?: IsURLOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
52
|
+
export interface Base64ValidatorOptions extends ValidationOptions, validatorJS.IsBase64Options {
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Validates if value is a "Base64" string.
|
|
56
|
+
* @validator isBase64
|
|
57
|
+
*/
|
|
58
|
+
export declare function isBase64(options?: Base64ValidatorOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
59
|
+
/**
|
|
60
|
+
* Validates if value is a BIC (Bank Identification Code) or SWIFT code
|
|
61
|
+
* @validator isBIC
|
|
62
|
+
*/
|
|
63
|
+
export declare function isBIC(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
64
|
+
export interface CreditCardValidatorOptions extends ValidationOptions, validatorJS.IsCreditCardOptions {
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Validates if value is a "Base64" formatted string.
|
|
68
|
+
* @validator isCreditCard
|
|
69
|
+
*/
|
|
70
|
+
export declare function isCreditCard(options?: CreditCardValidatorOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
71
|
+
/**
|
|
72
|
+
* Validates if value is an IBAN (International Bank Account Number)
|
|
73
|
+
* @validator isEAN
|
|
74
|
+
*/
|
|
75
|
+
export declare function isIBAN(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
76
|
+
/**
|
|
77
|
+
* Validates if value is an passport number
|
|
78
|
+
* @validator isPassportNumber
|
|
79
|
+
*/
|
|
80
|
+
export declare function isPassportNumber(countryCode: string, options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
81
|
+
/**
|
|
82
|
+
* Validates if value is an EAN (European Article Number)
|
|
83
|
+
* @validator isEAN
|
|
84
|
+
*/
|
|
85
|
+
export declare function isEAN(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
86
|
+
export interface IsFQDNOptions extends ValidationOptions, validatorJS.IsFQDNOptions {
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Validates if value is an FQDN
|
|
90
|
+
* @validator isFQDN
|
|
91
|
+
*/
|
|
92
|
+
export declare function isFQDN(options?: IsFQDNOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
93
|
+
export interface IsISSNOptions extends ValidationOptions, validatorJS.IsISSNOptions {
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Validates if value is an ISSN
|
|
97
|
+
* @validator isISSN
|
|
98
|
+
*/
|
|
99
|
+
export declare function isISSN(options?: IsISSNOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
100
|
+
/**
|
|
101
|
+
* Validates if value is an VAT number
|
|
102
|
+
* @validator isVAT
|
|
103
|
+
*/
|
|
104
|
+
export declare function isVAT(countryCode: string, options?: IsISSNOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
105
|
+
/**
|
|
106
|
+
* Validates if value is a BTC address.
|
|
107
|
+
* @validator isBtcAddress
|
|
108
|
+
*/
|
|
109
|
+
export declare function isBtcAddress(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
110
|
+
/**
|
|
111
|
+
* Validates if value is a ETH (Ethereum) address.
|
|
112
|
+
* @validator isBtcAddress
|
|
113
|
+
*/
|
|
114
|
+
export declare function isETHAddress(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
115
|
+
export type HashAlgorithm = 'crc32' | 'crc32b' | 'md4' | 'md5' | 'ripemd128' | 'ripemd160' | 'sha1' | 'sha256' | 'sha384' | 'sha512' | 'tiger128' | 'tiger160' | 'tiger192';
|
|
116
|
+
/**
|
|
117
|
+
* Validates if value a hash of type algorithm
|
|
118
|
+
* @validator isHash
|
|
119
|
+
*/
|
|
120
|
+
export declare function isHash(algorithm: HashAlgorithm, options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
121
|
+
/**
|
|
122
|
+
* Validates if value is a Hex Color
|
|
123
|
+
* @validator isHexColor
|
|
124
|
+
*/
|
|
125
|
+
export declare function isHexColor(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
126
|
+
/**
|
|
127
|
+
* Validates if value a valid JWT token
|
|
128
|
+
* @validator isHash
|
|
129
|
+
*/
|
|
130
|
+
export declare function isJWT(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
131
|
+
/**
|
|
132
|
+
* Validates if value a Lowercase string
|
|
133
|
+
* @validator isLowercase
|
|
134
|
+
*/
|
|
135
|
+
export declare function isLowercase(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
136
|
+
/**
|
|
137
|
+
* Validates if value a Uppercase string
|
|
138
|
+
* @validator isUppercase
|
|
139
|
+
*/
|
|
140
|
+
export declare function isUppercase(options?: ValidationOptions): import("../core/validator.js").Validator<string, string, import("../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Context, Validator } from '../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Forwards codec process to a sub codec. Useful for circular checks
|
|
4
|
+
* @validator forwardRef
|
|
5
|
+
*/
|
|
6
|
+
export declare function forwardRef<T, I>(fn: (context: Context) => Validator<T, I>): Validator<T, I, import("../core/types.js").ExecutionOptions>;
|
|
7
|
+
/**
|
|
8
|
+
* if "check" codec passes returns "then", "else" otherwise
|
|
9
|
+
* @validator iif
|
|
10
|
+
*/
|
|
11
|
+
export declare function iif<TOutput1, TOutput2, TDefault1, TDefault2>(check: Validator<any>, than_: TDefault1 | Validator<TOutput1, any>, else_?: TDefault2 | Validator<TOutput2, any>): any;
|
package/lib/ArrayType.d.ts
DELETED
package/lib/ArrayType.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {DataType} = require('./DataType');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
* @class ArrayType
|
|
8
|
-
*/
|
|
9
|
-
class ArrayType extends DataType {
|
|
10
|
-
|
|
11
|
-
create(instance, schema) {
|
|
12
|
-
instance = super.create(instance, schema);
|
|
13
|
-
if (schema.items) {
|
|
14
|
-
const library = this.library;
|
|
15
|
-
const stack = library._stack;
|
|
16
|
-
stack.push('items');
|
|
17
|
-
instance.items = library.getType(schema.items);
|
|
18
|
-
stack.pop();
|
|
19
|
-
}
|
|
20
|
-
return instance;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
get(attr) {
|
|
24
|
-
if (attr === 'items' && this.items)
|
|
25
|
-
return this.items;
|
|
26
|
-
return super.get(attr);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
normalizeSchema(schema) {
|
|
30
|
-
const normalized = super.normalizeSchema(schema);
|
|
31
|
-
const items = normalized.items;
|
|
32
|
-
if (items) {
|
|
33
|
-
const stack = this.library._stack;
|
|
34
|
-
stack.push('items');
|
|
35
|
-
normalized.items = this.library.normalizeSchema(items);
|
|
36
|
-
stack.pop();
|
|
37
|
-
}
|
|
38
|
-
return normalized;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
module.exports = {ArrayType};
|
package/lib/DataType.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {Valgen} from "./Valgen";
|
|
2
|
-
|
|
3
|
-
export declare const Failed: unique symbol;
|
|
4
|
-
|
|
5
|
-
export class DataType {
|
|
6
|
-
readonly library: Valgen;
|
|
7
|
-
readonly typeName: string;
|
|
8
|
-
readonly factory: Valgen.ITypeFactory;
|
|
9
|
-
readonly predecessors?: DataType[];
|
|
10
|
-
id: number;
|
|
11
|
-
name?: string;
|
|
12
|
-
schema: { [index: string]: any };
|
|
13
|
-
|
|
14
|
-
constructor(library: Valgen, typeName: string, factory: Valgen.ITypeFactory);
|
|
15
|
-
|
|
16
|
-
create(instance: any, schema: any): DataType;
|
|
17
|
-
|
|
18
|
-
generate(options?: Valgen.IGenerateOptions): Valgen.ValidateFunction;
|
|
19
|
-
|
|
20
|
-
get(attr: string): any;
|
|
21
|
-
|
|
22
|
-
set(key: string, value: any);
|
|
23
|
-
|
|
24
|
-
[index: string]: any;
|
|
25
|
-
}
|
package/lib/DataType.js
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const promisify = require('putil-promisify');
|
|
4
|
-
const objectHash = require('object-hash');
|
|
5
|
-
const {coalesce} = require('putil-varhelpers');
|
|
6
|
-
const ValidationError = require('./ValidationError');
|
|
7
|
-
|
|
8
|
-
const Failed = Symbol.for('Failed');
|
|
9
|
-
const PROPERTY_PATTERN = /^[a-zA-Z]\w+$/;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* @class DataType
|
|
14
|
-
*/
|
|
15
|
-
class DataType {
|
|
16
|
-
|
|
17
|
-
constructor(library, typeName, factory) {
|
|
18
|
-
if (!typeName)
|
|
19
|
-
throw new TypeError(`You must provide type name.`);
|
|
20
|
-
if (!factory || typeof factory !== 'object')
|
|
21
|
-
throw new TypeError('Factory argument must be an object');
|
|
22
|
-
if (typeof factory.generate !== 'function')
|
|
23
|
-
throw new TypeError('Factory must contain a "generate" function');
|
|
24
|
-
Object.defineProperties(this, {
|
|
25
|
-
library: {
|
|
26
|
-
writable: false,
|
|
27
|
-
configurable: false,
|
|
28
|
-
value: library
|
|
29
|
-
},
|
|
30
|
-
factory: {
|
|
31
|
-
writable: false,
|
|
32
|
-
configurable: false,
|
|
33
|
-
value: factory
|
|
34
|
-
},
|
|
35
|
-
typeName: {
|
|
36
|
-
writable: false,
|
|
37
|
-
configurable: false,
|
|
38
|
-
value: typeName
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
this.options = {};
|
|
42
|
-
this._fnCache = {};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
create(instance, schema) {
|
|
46
|
-
let proto = this;
|
|
47
|
-
while (proto.predecessors) {
|
|
48
|
-
const p = Object.getPrototypeOf(proto);
|
|
49
|
-
if (!p.predecessors)
|
|
50
|
-
break;
|
|
51
|
-
proto = p;
|
|
52
|
-
}
|
|
53
|
-
Object.setPrototypeOf(instance, proto);
|
|
54
|
-
instance.predecessors = [];
|
|
55
|
-
schema = instance.schema = this.normalizeSchema(schema);
|
|
56
|
-
|
|
57
|
-
if (schema.type) {
|
|
58
|
-
const library = this.library;
|
|
59
|
-
const stack = library._stack;
|
|
60
|
-
stack.push('type');
|
|
61
|
-
const types = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
62
|
-
for (const sch of types) {
|
|
63
|
-
if (sch === this.typeName)
|
|
64
|
-
continue;
|
|
65
|
-
const t = library.getType(sch);
|
|
66
|
-
if (!(t.factory === instance.factory)) { // noinspection ExceptionCaughtLocallyJS
|
|
67
|
-
throw new Error(`"${instance.typeName}" type can't be mixed with "${t.typeName}" type.`);
|
|
68
|
-
}
|
|
69
|
-
instance.predecessors.push(t);
|
|
70
|
-
}
|
|
71
|
-
stack.pop();
|
|
72
|
-
}
|
|
73
|
-
return instance;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
normalizeSchema(schema) {
|
|
77
|
-
const normalized = {};
|
|
78
|
-
const library = this.library;
|
|
79
|
-
if (schema.type) {
|
|
80
|
-
let a = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
81
|
-
a = a.map(s => {
|
|
82
|
-
if (typeof s === 'string')
|
|
83
|
-
s = library._parseTypeName(s);
|
|
84
|
-
if (typeof s === 'string')
|
|
85
|
-
return s;
|
|
86
|
-
return library.normalizeSchema(s);
|
|
87
|
-
});
|
|
88
|
-
if (a.length <= 1) {
|
|
89
|
-
if (typeof a[0] === 'object')
|
|
90
|
-
Object.assign(schema, a[0]);
|
|
91
|
-
else
|
|
92
|
-
normalized.type = a[0];
|
|
93
|
-
} else normalized.type = a;
|
|
94
|
-
}
|
|
95
|
-
if (this.factory.normalizeAttribute) {
|
|
96
|
-
const stack = this.library._stack;
|
|
97
|
-
for (const key of Object.keys(schema)) {
|
|
98
|
-
let v = schema[key];
|
|
99
|
-
if (v === undefined || key === 'type')
|
|
100
|
-
continue;
|
|
101
|
-
/* istanbul ignore else */
|
|
102
|
-
stack.push(key);
|
|
103
|
-
v = this.factory.normalizeAttribute(key, schema[key], schema);
|
|
104
|
-
if (v !== undefined)
|
|
105
|
-
normalized[key] = v;
|
|
106
|
-
/* istanbul ignore else */
|
|
107
|
-
stack.pop();
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return normalized;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
generate(options = {}) {
|
|
114
|
-
options.throwOnError =
|
|
115
|
-
coalesce(options.throwOnError, this.library.throwOnError);
|
|
116
|
-
|
|
117
|
-
/* istanbul ignore else */
|
|
118
|
-
const preparedOptions = this.factory.normalizeCompileOptions ?
|
|
119
|
-
this.factory.normalizeCompileOptions(options) : {...options};
|
|
120
|
-
Object.keys(preparedOptions).forEach(k => {
|
|
121
|
-
if (preparedOptions[k] === undefined)
|
|
122
|
-
delete preparedOptions[k];
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
// Lookup for cached validator for same options
|
|
126
|
-
const cacheId = objectHash({
|
|
127
|
-
schema: this.schema,
|
|
128
|
-
defaults: this.options,
|
|
129
|
-
options: preparedOptions
|
|
130
|
-
});
|
|
131
|
-
let validate = this._fnCache[cacheId];
|
|
132
|
-
if (validate)
|
|
133
|
-
return validate;
|
|
134
|
-
|
|
135
|
-
this._fnCache[cacheId] = function() {
|
|
136
|
-
return validate.apply(this, arguments);
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const fn = this.factory.generate(this, options);
|
|
140
|
-
|
|
141
|
-
const callValidate = (value, ctx) => {
|
|
142
|
-
ctx = ctx || {};
|
|
143
|
-
const _path = ctx.path = ctx.path || [];
|
|
144
|
-
ctx.errors = ctx.errors || [];
|
|
145
|
-
ctx.logError = ctx.logError || (
|
|
146
|
-
(e) => {
|
|
147
|
-
if (ctx.path.length)
|
|
148
|
-
e.path = ctx.path.slice();
|
|
149
|
-
const s = ctx.getPath();
|
|
150
|
-
if (s)
|
|
151
|
-
e.message = 'Error at "' + s + '". ' + e.message;
|
|
152
|
-
ctx.errors.push(e);
|
|
153
|
-
return Failed;
|
|
154
|
-
});
|
|
155
|
-
ctx.getPath = ctx.getPath || (() => {
|
|
156
|
-
let s = '';
|
|
157
|
-
const len = _path.length;
|
|
158
|
-
for (let i = 0; i < len; i++) {
|
|
159
|
-
const x = _path[i];
|
|
160
|
-
if (typeof x === 'number')
|
|
161
|
-
s += '[' + x + ']';
|
|
162
|
-
else if (x.match(PROPERTY_PATTERN))
|
|
163
|
-
/* istanbul ignore next */
|
|
164
|
-
s += (s ? '.' : '') + x;
|
|
165
|
-
else
|
|
166
|
-
s += '[' + x.replace(/'/g, '\\\'') + ']';
|
|
167
|
-
}
|
|
168
|
-
return s;
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const v = fn(value, ctx);
|
|
172
|
-
const valid = v !== Failed;
|
|
173
|
-
if (options.throwOnError && ctx.errors.length) {
|
|
174
|
-
const ee = new ValidationError(ctx.errors[0].message);
|
|
175
|
-
// @ts-ignore
|
|
176
|
-
ee.errors = ctx.errors;
|
|
177
|
-
ee.value = value;
|
|
178
|
-
Object.assign(ee, ctx.errors[0]);
|
|
179
|
-
throw ee;
|
|
180
|
-
}
|
|
181
|
-
return ctx.errors.length ?
|
|
182
|
-
{valid, errors: ctx.errors} :
|
|
183
|
-
{valid, value: v};
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
validate = options.resolvePromises ?
|
|
187
|
-
async (value, ctx) => {
|
|
188
|
-
value = await promisify.deepResolve(value);
|
|
189
|
-
return callValidate(value, ctx);
|
|
190
|
-
} :
|
|
191
|
-
(value, ctx) => callValidate(value, ctx);
|
|
192
|
-
this._fnCache[cacheId] = validate;
|
|
193
|
-
validate.dataType = this;
|
|
194
|
-
return validate;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
get(attr) {
|
|
198
|
-
let v;
|
|
199
|
-
v = this.schema[attr];
|
|
200
|
-
if (v !== undefined)
|
|
201
|
-
return v;
|
|
202
|
-
if (this.predecessors) {
|
|
203
|
-
let i = this.predecessors.length;
|
|
204
|
-
while (i--) {
|
|
205
|
-
v = this.predecessors[i].get(attr);
|
|
206
|
-
if (v !== undefined)
|
|
207
|
-
return v;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
module.exports = {DataType, Failed};
|