valgen 5.2.2 → 5.3.1
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 +16 -0
- package/cjs/core/context.js +7 -4
- package/cjs/core/validator.js +4 -4
- package/cjs/index.js +20 -5
- package/cjs/rules/format-rules/is-alpha.js +52 -0
- package/cjs/rules/format-rules/is-ascii.js +40 -0
- package/cjs/rules/format-rules/is-base64.js +40 -0
- package/cjs/rules/format-rules/is-btc-address.js +40 -0
- package/cjs/rules/format-rules/is-credit-card.js +40 -0
- package/cjs/rules/format-rules/is-decimal.js +41 -0
- package/cjs/rules/format-rules/is-ean.js +40 -0
- package/cjs/rules/format-rules/is-email.js +97 -0
- package/cjs/rules/format-rules/is-eth-address.js +40 -0
- package/cjs/rules/format-rules/is-fqdn.js +44 -0
- package/cjs/rules/format-rules/is-hash.js +40 -0
- package/cjs/rules/format-rules/is-hex-color.js +40 -0
- package/cjs/rules/format-rules/is-hex.js +40 -0
- package/cjs/rules/format-rules/is-iban.js +52 -0
- package/cjs/rules/format-rules/is-ip.js +52 -0
- package/cjs/rules/format-rules/is-issn.js +44 -0
- package/cjs/rules/format-rules/is-jwt.js +40 -0
- package/cjs/rules/format-rules/is-lowercase.js +52 -0
- package/cjs/rules/format-rules/is-mac-address.js +45 -0
- package/cjs/rules/format-rules/is-mobile-phone.js +43 -0
- package/cjs/rules/{is-object-id.js → format-rules/is-object-id.js} +4 -4
- package/cjs/rules/format-rules/is-passport-number.js +40 -0
- package/cjs/rules/format-rules/is-port.js +40 -0
- package/cjs/rules/format-rules/is-url.js +40 -0
- package/cjs/rules/format-rules/is-uuid.js +40 -0
- package/cjs/rules/format-rules/is-vat-number.js +40 -0
- package/cjs/rules/{is-regexp.js → format-rules/matches.js} +8 -5
- package/cjs/rules/index.js +49 -22
- package/cjs/rules/logical-rules/is-defined.js +30 -0
- package/cjs/rules/{is-empty.js → logical-rules/is-empty.js} +13 -3
- package/cjs/rules/logical-rules/range.js +114 -0
- package/cjs/rules/{is-any.js → type-rules/is-any.js} +1 -1
- package/cjs/rules/{is-array.js → type-rules/is-array.js} +5 -6
- package/cjs/rules/{is-bigint.js → type-rules/is-bigint.js} +5 -4
- package/cjs/rules/{is-boolean.js → type-rules/is-boolean.js} +5 -3
- package/cjs/rules/{is-date.js → type-rules/is-date.js} +12 -10
- package/cjs/rules/{is-enum.js → type-rules/is-enum.js} +6 -5
- package/cjs/rules/{is-integer.js → type-rules/is-integer.js} +5 -3
- package/cjs/rules/type-rules/is-null.js +34 -0
- package/cjs/rules/{is-number.js → type-rules/is-number.js} +6 -4
- package/cjs/rules/{is-object.js → type-rules/is-object.js} +5 -5
- package/cjs/rules/{is-record.js → type-rules/is-record.js} +1 -1
- package/cjs/rules/{is-string.js → type-rules/is-string.js} +3 -2
- package/cjs/rules/{is-tuple.js → type-rules/is-tuple.js} +5 -5
- package/cjs/rules/utility-rules/exists.js +16 -0
- package/cjs/rules/utility-rules/get-length.js +24 -0
- package/cjs/rules/utility-rules/optional.js +16 -0
- package/cjs/rules/{pipe.js → utility-rules/pipe.js} +1 -1
- package/cjs/rules/utility-rules/required.js +18 -0
- package/cjs/rules/{union.js → utility-rules/union.js} +3 -3
- package/esm/core/context.js +7 -4
- package/esm/core/validator.js +4 -4
- package/esm/index.js +10 -3
- package/esm/rules/format-rules/is-alpha.js +24 -0
- package/esm/rules/format-rules/is-ascii.js +13 -0
- package/esm/rules/format-rules/is-base64.js +13 -0
- package/esm/rules/format-rules/is-btc-address.js +13 -0
- package/esm/rules/format-rules/is-credit-card.js +13 -0
- package/esm/rules/format-rules/is-decimal.js +14 -0
- package/esm/rules/format-rules/is-ean.js +13 -0
- package/esm/rules/format-rules/is-email.js +70 -0
- package/esm/rules/format-rules/is-eth-address.js +13 -0
- package/esm/rules/format-rules/is-fqdn.js +17 -0
- package/esm/rules/format-rules/is-hash.js +13 -0
- package/esm/rules/format-rules/is-hex-color.js +13 -0
- package/esm/rules/format-rules/is-hex.js +13 -0
- package/esm/rules/format-rules/is-iban.js +24 -0
- package/esm/rules/format-rules/is-ip.js +24 -0
- package/esm/rules/format-rules/is-issn.js +17 -0
- package/esm/rules/format-rules/is-jwt.js +13 -0
- package/esm/rules/format-rules/is-lowercase.js +24 -0
- package/esm/rules/format-rules/is-mac-address.js +18 -0
- package/esm/rules/format-rules/is-mobile-phone.js +16 -0
- package/esm/rules/{is-object-id.js → format-rules/is-object-id.js} +4 -4
- package/esm/rules/format-rules/is-passport-number.js +13 -0
- package/esm/rules/format-rules/is-port.js +13 -0
- package/esm/rules/format-rules/is-url.js +13 -0
- package/esm/rules/format-rules/is-uuid.js +13 -0
- package/esm/rules/format-rules/is-vat-number.js +13 -0
- package/esm/rules/{is-regexp.js → format-rules/matches.js} +6 -3
- package/esm/rules/index.js +49 -22
- package/esm/rules/logical-rules/is-defined.js +25 -0
- package/esm/rules/{is-empty.js → logical-rules/is-empty.js} +13 -3
- package/esm/rules/{range.js → logical-rules/range.js} +59 -36
- package/esm/rules/{is-any.js → type-rules/is-any.js} +1 -1
- package/esm/rules/{is-array.js → type-rules/is-array.js} +5 -6
- package/esm/rules/{is-bigint.js → type-rules/is-bigint.js} +5 -4
- package/esm/rules/{is-boolean.js → type-rules/is-boolean.js} +5 -3
- package/esm/rules/{is-date.js → type-rules/is-date.js} +12 -10
- package/esm/rules/{is-enum.js → type-rules/is-enum.js} +6 -5
- package/esm/rules/{is-integer.js → type-rules/is-integer.js} +5 -3
- package/esm/rules/type-rules/is-null.js +29 -0
- package/esm/rules/{is-number.js → type-rules/is-number.js} +6 -4
- package/esm/rules/{is-object.js → type-rules/is-object.js} +5 -5
- package/esm/rules/{is-record.js → type-rules/is-record.js} +1 -1
- package/esm/rules/{is-string.js → type-rules/is-string.js} +3 -2
- package/esm/rules/{is-tuple.js → type-rules/is-tuple.js} +5 -5
- package/esm/rules/utility-rules/exists.js +12 -0
- package/esm/rules/utility-rules/get-length.js +20 -0
- package/esm/rules/utility-rules/optional.js +12 -0
- package/esm/rules/{pipe.js → utility-rules/pipe.js} +1 -1
- package/esm/rules/utility-rules/required.js +14 -0
- package/esm/rules/{union.js → utility-rules/union.js} +3 -3
- package/package.json +2 -2
- package/typings/core/types.d.ts +1 -0
- package/typings/index.d.ts +11 -4
- package/typings/rules/format-rules/is-alpha.d.ts +11 -0
- package/typings/rules/format-rules/is-ascii.d.ts +6 -0
- package/typings/rules/format-rules/is-base64.d.ts +9 -0
- package/typings/rules/format-rules/is-btc-address.d.ts +6 -0
- package/typings/rules/format-rules/is-credit-card.d.ts +9 -0
- package/typings/rules/format-rules/is-decimal.d.ts +7 -0
- package/typings/rules/format-rules/is-ean.d.ts +6 -0
- package/typings/rules/format-rules/is-email.d.ts +62 -0
- package/typings/rules/format-rules/is-eth-address.d.ts +6 -0
- package/typings/rules/format-rules/is-fqdn.d.ts +13 -0
- package/typings/rules/format-rules/is-hash.d.ts +8 -0
- package/typings/rules/format-rules/is-hex-color.d.ts +6 -0
- package/typings/rules/format-rules/is-hex.d.ts +6 -0
- package/typings/rules/format-rules/is-iban.d.ts +11 -0
- package/typings/rules/format-rules/is-ip.d.ts +11 -0
- package/typings/rules/format-rules/is-issn.d.ts +14 -0
- package/typings/rules/format-rules/is-jwt.d.ts +6 -0
- package/typings/rules/format-rules/is-lowercase.d.ts +11 -0
- package/typings/rules/format-rules/is-mac-address.d.ts +21 -0
- package/typings/rules/format-rules/is-mobile-phone.d.ts +20 -0
- package/typings/rules/{is-object-id.d.ts → format-rules/is-object-id.d.ts} +2 -2
- package/typings/rules/format-rules/is-passport-number.d.ts +6 -0
- package/typings/rules/format-rules/is-port.d.ts +6 -0
- package/typings/rules/format-rules/is-url.d.ts +9 -0
- package/typings/rules/format-rules/is-uuid.d.ts +6 -0
- package/typings/rules/format-rules/is-vat-number.d.ts +6 -0
- package/typings/rules/format-rules/matches.d.ts +9 -0
- package/typings/rules/index.d.ts +49 -22
- package/typings/rules/logical-rules/is-defined.d.ts +11 -0
- package/typings/rules/{is-empty.d.ts → logical-rules/is-empty.d.ts} +3 -3
- package/typings/rules/{range.d.ts → logical-rules/range.d.ts} +14 -4
- package/typings/rules/type-rules/is-any.d.ts +5 -0
- package/typings/rules/{is-array.d.ts → type-rules/is-array.d.ts} +2 -2
- package/typings/rules/{is-bigint.d.ts → type-rules/is-bigint.d.ts} +2 -2
- package/typings/rules/{is-boolean.d.ts → type-rules/is-boolean.d.ts} +2 -2
- package/typings/rules/{is-date.d.ts → type-rules/is-date.d.ts} +3 -3
- package/typings/rules/{is-enum.d.ts → type-rules/is-enum.d.ts} +1 -1
- package/typings/rules/{is-integer.d.ts → type-rules/is-integer.d.ts} +2 -2
- package/typings/rules/type-rules/is-null.d.ts +11 -0
- package/typings/rules/{is-number.d.ts → type-rules/is-number.d.ts} +2 -2
- package/typings/rules/{is-object.d.ts → type-rules/is-object.d.ts} +1 -1
- package/typings/rules/{is-record.d.ts → type-rules/is-record.d.ts} +2 -2
- package/typings/rules/{is-string.d.ts → type-rules/is-string.d.ts} +5 -5
- package/typings/rules/{is-tuple.d.ts → type-rules/is-tuple.d.ts} +2 -8
- package/typings/rules/utility-rules/exists.d.ts +6 -0
- package/typings/rules/utility-rules/get-length.d.ts +11 -0
- package/typings/rules/utility-rules/optional.d.ts +7 -0
- package/typings/rules/{pipe.d.ts → utility-rules/pipe.d.ts} +1 -1
- package/typings/rules/utility-rules/required.d.ts +7 -0
- package/typings/rules/utility-rules/union.d.ts +5 -0
- package/cjs/rules/is-null.js +0 -58
- package/cjs/rules/length.js +0 -41
- package/cjs/rules/optional.js +0 -44
- package/cjs/rules/range.js +0 -89
- package/cjs/rules/string-formats.js +0 -377
- package/esm/rules/is-null.js +0 -51
- package/esm/rules/length.js +0 -35
- package/esm/rules/optional.js +0 -38
- package/esm/rules/string-formats.js +0 -322
- package/typings/rules/is-any.d.ts +0 -5
- package/typings/rules/is-null.d.ts +0 -21
- package/typings/rules/is-regexp.d.ts +0 -9
- package/typings/rules/length.d.ts +0 -21
- package/typings/rules/optional.d.ts +0 -17
- package/typings/rules/string-formats.d.ts +0 -166
- package/typings/rules/union.d.ts +0 -5
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { validator } from '
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Validates if given value is one of enum values.
|
|
4
4
|
* @validator isEnum
|
|
5
5
|
*/
|
|
6
6
|
export function isEnum(values, options) {
|
|
7
|
-
const caseInSensitive = !!
|
|
7
|
+
const caseInSensitive = !!options?.caseInSensitive;
|
|
8
8
|
const enumName = options?.enumName;
|
|
9
9
|
// Prepare an object for fast lookup
|
|
10
|
-
const valObj = (Array.isArray(values) ? values : [values])
|
|
11
|
-
.reduce((a, v) => {
|
|
10
|
+
const valObj = (Array.isArray(values) ? values : [values]).reduce((a, v) => {
|
|
12
11
|
if (typeof v === 'string' && caseInSensitive)
|
|
13
12
|
a[v.toUpperCase()] = true;
|
|
14
13
|
else
|
|
@@ -18,6 +17,8 @@ export function isEnum(values, options) {
|
|
|
18
17
|
return validator('isEnum', function (input, context, _this) {
|
|
19
18
|
if (input != null && valObj[typeof input === 'string' && caseInSensitive ? input.toUpperCase() : input])
|
|
20
19
|
return input;
|
|
21
|
-
context.fail(_this, `Value must be one of enumeration member${enumName ? ` (${enumName})` : ''}`, input, {
|
|
20
|
+
context.fail(_this, `Value must be one of enumeration member${enumName ? ` (${enumName})` : ''}`, input, {
|
|
21
|
+
enum: enumName,
|
|
22
|
+
});
|
|
22
23
|
}, options);
|
|
23
24
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { validator } from '
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Validates if value is "integer".
|
|
4
4
|
* Converts input value to integer number if coerce option is set to 'true'.
|
|
@@ -6,7 +6,8 @@ import { validator } from '../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isInteger(options) {
|
|
8
8
|
return validator('isInteger', function (input, context, _this) {
|
|
9
|
-
|
|
9
|
+
const coerce = options?.coerce || context.coerce;
|
|
10
|
+
if (input != null && typeof input !== 'number' && coerce) {
|
|
10
11
|
if (typeof input === 'string')
|
|
11
12
|
input = parseFloat(input);
|
|
12
13
|
if (typeof input === 'bigint') {
|
|
@@ -17,6 +18,7 @@ export function isInteger(options) {
|
|
|
17
18
|
}
|
|
18
19
|
if (typeof input === 'number' && !isNaN(input) && Number.isInteger(input))
|
|
19
20
|
return input;
|
|
20
|
-
|
|
21
|
+
const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
|
|
22
|
+
context.fail(_this, `${t}"{{value}}" is not a valid integer value`, input);
|
|
21
23
|
}, options);
|
|
22
24
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is "null".
|
|
4
|
+
* @validator isNull
|
|
5
|
+
*/
|
|
6
|
+
export function isNull(options) {
|
|
7
|
+
return validator('isNull', function (input, context, _this) {
|
|
8
|
+
if (input === null)
|
|
9
|
+
return input;
|
|
10
|
+
const coerce = options?.coerce || context.coerce;
|
|
11
|
+
if (coerce)
|
|
12
|
+
return null;
|
|
13
|
+
context.fail(_this, `"{{value}}" is not null`, input);
|
|
14
|
+
}, options);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Validates if value is "null" or "undefined".
|
|
18
|
+
* @validator isNull
|
|
19
|
+
*/
|
|
20
|
+
export function isNullish(options) {
|
|
21
|
+
return validator('isNullish', function (input, context, _this) {
|
|
22
|
+
if (input == null)
|
|
23
|
+
return input;
|
|
24
|
+
const coerce = options?.coerce || context.coerce;
|
|
25
|
+
if (coerce)
|
|
26
|
+
return null;
|
|
27
|
+
context.fail(_this, `"{{value}}" is not nullish`, input);
|
|
28
|
+
}, options);
|
|
29
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { validator } from '
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Validates if value is "number".
|
|
4
4
|
* Converts input value to number if coerce option is set to 'true'.
|
|
@@ -6,17 +6,19 @@ import { validator } from '../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isNumber(options) {
|
|
8
8
|
return validator('isNumber', function (input, context, _this) {
|
|
9
|
-
|
|
9
|
+
const coerce = options?.coerce || context.coerce;
|
|
10
|
+
if (input != null && typeof input !== 'number' && coerce) {
|
|
10
11
|
if (typeof input === 'string')
|
|
11
12
|
input = parseFloat(input);
|
|
12
13
|
if (typeof input === 'bigint') {
|
|
13
14
|
const v = Number(input);
|
|
14
15
|
if (input === BigInt(v))
|
|
15
|
-
|
|
16
|
+
return v;
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
if (typeof input === 'number' && !isNaN(input))
|
|
19
20
|
return input;
|
|
20
|
-
|
|
21
|
+
const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
|
|
22
|
+
context.fail(_this, `${t}"{{value}}" is not a valid number value`, input);
|
|
21
23
|
}, options);
|
|
22
24
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { postValidation, preValidation } from '
|
|
2
|
-
import { isValidator, validator } from '
|
|
1
|
+
import { postValidation, preValidation } from '../../constants.js';
|
|
2
|
+
import { isValidator, validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
4
|
* Validates object according to schema
|
|
5
5
|
* Converts properties according to schema rules if coerce option is set to 'true'.
|
|
@@ -34,7 +34,8 @@ export function isObject(schema, options) {
|
|
|
34
34
|
const _rule = validator('isObject', function (input, context, _this) {
|
|
35
35
|
if (ctor && ctor[preValidation])
|
|
36
36
|
input = ctor[preValidation](input, context, _this);
|
|
37
|
-
|
|
37
|
+
const coerce = options?.coerce || context.coerce;
|
|
38
|
+
if (typeof input === 'string' && coerce)
|
|
38
39
|
input = JSON.parse(input);
|
|
39
40
|
if (!(input && typeof input === 'object')) {
|
|
40
41
|
context.fail(_this, `{{label}} must be an object`, input);
|
|
@@ -65,8 +66,7 @@ export function isObject(schema, options) {
|
|
|
65
66
|
inputKey = keys[i];
|
|
66
67
|
schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
|
|
67
68
|
v = input[inputKey];
|
|
68
|
-
_propRule = propertyRules[schemaKey] ||
|
|
69
|
-
(isValidator(additionalFields) ? additionalFields : undefined);
|
|
69
|
+
_propRule = propertyRules[schemaKey] || (isValidator(additionalFields) ? additionalFields : undefined);
|
|
70
70
|
if (_propRule) {
|
|
71
71
|
if (processedSchemaKeys[schemaKey])
|
|
72
72
|
continue;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { validator } from '
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Validates if value is "string".
|
|
4
4
|
* Converts input value to string if coerce option is set to 'true'.
|
|
@@ -6,7 +6,8 @@ import { validator } from '../core/index.js';
|
|
|
6
6
|
*/
|
|
7
7
|
export function isString(options) {
|
|
8
8
|
return validator('isString', function (input, context, _this) {
|
|
9
|
-
|
|
9
|
+
const coerce = options?.coerce || context.coerce;
|
|
10
|
+
if (input != null && typeof input !== 'string' && coerce) {
|
|
10
11
|
if (typeof input === 'object') {
|
|
11
12
|
if (typeof input.toJSON === 'function')
|
|
12
13
|
input = input.toJSON();
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { validator } from '
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
2
|
export function isTuple(items, options) {
|
|
3
3
|
return validator('isTuple', function (input, context, _this) {
|
|
4
|
-
|
|
4
|
+
const coerce = options?.coerce || context.coerce;
|
|
5
|
+
if (input != null && coerce && !Array.isArray(input))
|
|
5
6
|
input = [input];
|
|
6
7
|
if (!Array.isArray(input)) {
|
|
7
|
-
context.fail(_this, `{{
|
|
8
|
+
context.fail(_this, `"{{value}}" is not a valid tuple`, input);
|
|
8
9
|
return;
|
|
9
10
|
}
|
|
10
11
|
const location = context.location || '';
|
|
@@ -21,8 +22,7 @@ export function isTuple(items, options) {
|
|
|
21
22
|
itemContext.label = context.label ? context.label + `[${i}]` : undefined;
|
|
22
23
|
itemContext.index = i;
|
|
23
24
|
itemContext.location = location + '[' + i + ']';
|
|
24
|
-
itemContext.label = (itemContext.label || itemContext.property || 'Value at ') +
|
|
25
|
-
`[${i}]`;
|
|
25
|
+
itemContext.label = (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
|
|
26
26
|
v = itemRule(input[i], itemContext);
|
|
27
27
|
out.push(v);
|
|
28
28
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if property exists
|
|
4
|
+
* @validator exists
|
|
5
|
+
*/
|
|
6
|
+
export function exists(options) {
|
|
7
|
+
return validator('exists', function (input, context, _this) {
|
|
8
|
+
if (input !== undefined || (context.scope && Object.getOwnPropertyDescriptor(context.scope, input)))
|
|
9
|
+
return input;
|
|
10
|
+
context.fail(_this, `{{label}} must exist`, input);
|
|
11
|
+
}, options);
|
|
12
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns length of an Array, String, ArrayBuffer, Buffer or any object with the "length" property.
|
|
4
|
+
* @validator getLength
|
|
5
|
+
*/
|
|
6
|
+
export function getLength() {
|
|
7
|
+
return validator('getLength', function (input, context, _this) {
|
|
8
|
+
if (typeof input === 'string')
|
|
9
|
+
return input.length;
|
|
10
|
+
if (Array.isArray(input))
|
|
11
|
+
return input.length;
|
|
12
|
+
if (input instanceof ArrayBuffer)
|
|
13
|
+
return input.byteLength;
|
|
14
|
+
if (input && typeof input === 'object' && typeof input.length === 'number')
|
|
15
|
+
return input.length;
|
|
16
|
+
if (input && typeof input === 'object' && typeof input.size === 'number')
|
|
17
|
+
return input.size;
|
|
18
|
+
context.fail(_this, `Unable to get length of {{label}}`, input);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Makes sub-rule optional
|
|
4
|
+
* @validator optional
|
|
5
|
+
*/
|
|
6
|
+
export function optional(nested, options) {
|
|
7
|
+
return validator('optional', function (input, context) {
|
|
8
|
+
if (input == null)
|
|
9
|
+
return input;
|
|
10
|
+
return nested(input, context);
|
|
11
|
+
}, options);
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Makes sub-rule required
|
|
4
|
+
* @validator required
|
|
5
|
+
*/
|
|
6
|
+
export function required(nested, options) {
|
|
7
|
+
return validator('required', function (input, context, _this) {
|
|
8
|
+
if (input == null) {
|
|
9
|
+
context.fail(_this, `{{label}} is required`, input);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
return nested(input, context);
|
|
13
|
+
}, options);
|
|
14
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { validator } from '
|
|
1
|
+
import { validator } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
3
|
*
|
|
4
4
|
*/
|
|
@@ -10,7 +10,7 @@ export function union(codecs) {
|
|
|
10
10
|
let v;
|
|
11
11
|
let passed = false;
|
|
12
12
|
// Mock fail method to prevent errors
|
|
13
|
-
context.fail = () => passed = false;
|
|
13
|
+
context.fail = () => (passed = false);
|
|
14
14
|
for (i = 0; i < l; i++) {
|
|
15
15
|
c = codecs[i];
|
|
16
16
|
try {
|
|
@@ -27,6 +27,6 @@ export function union(codecs) {
|
|
|
27
27
|
delete context.fail;
|
|
28
28
|
if (passed)
|
|
29
29
|
return v;
|
|
30
|
-
context.fail(_this, `
|
|
30
|
+
context.fail(_this, `Value didn't match any on union rules`, input);
|
|
31
31
|
});
|
|
32
32
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valgen",
|
|
3
3
|
"description": "Fast runtime type validator, converter and io (encoding/decoding) library",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.3.1",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"@types/validator": "^13.11.9",
|
|
16
16
|
"date-fns": "^3.6.0",
|
|
17
|
-
"ts-gems": "^3.
|
|
17
|
+
"ts-gems": "^3.4.0",
|
|
18
18
|
"validator": "^13.11.0"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
package/typings/core/types.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export interface ErrorIssue {
|
|
|
14
14
|
export type OnFailFunction = (issue: ErrorIssue, context: Context, _super?: OnFailFunction) => string | Omit<ErrorIssue, 'id' | 'input'>;
|
|
15
15
|
export interface ValidationOptions {
|
|
16
16
|
onFail?: OnFailFunction;
|
|
17
|
+
coerce?: boolean;
|
|
17
18
|
}
|
|
18
19
|
export interface ExecutionOptions extends ValidationOptions {
|
|
19
20
|
coerce?: boolean;
|
package/typings/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as vg from './rules/index.js';
|
|
2
2
|
export * from './constants.js';
|
|
3
3
|
export * from './core/index.js';
|
|
4
|
-
export { IsObject } from './rules/is-object.js';
|
|
4
|
+
export { IsObject } from './rules/type-rules/is-object.js';
|
|
5
5
|
declare const isAny: import("./core/validator.js").Validator<any, any, import("./core/types.js").ExecutionOptions>;
|
|
6
6
|
declare const isArray: import("./core/validator.js").Validator<unknown[], unknown, import("./core/types.js").ExecutionOptions>;
|
|
7
7
|
declare const isBigint: import("./core/validator.js").Validator<bigint, unknown, import("./core/types.js").ExecutionOptions>;
|
|
@@ -33,7 +33,7 @@ declare const isMACAddress: import("./core/validator.js").Validator<string, stri
|
|
|
33
33
|
declare const isPort: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
34
34
|
declare const isURL: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
35
35
|
declare const isBase64: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
36
|
-
declare const
|
|
36
|
+
declare const isSWIFT: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
37
37
|
declare const isCreditCard: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
38
38
|
declare const isIBAN: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
39
39
|
declare const isEAN: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
@@ -49,5 +49,12 @@ declare const isAlpha: import("./core/validator.js").Validator<string, string, i
|
|
|
49
49
|
declare const isAlphanumeric: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
50
50
|
declare const isAscii: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
51
51
|
declare const isDecimal: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
52
|
-
declare const
|
|
53
|
-
|
|
52
|
+
declare const isHex: import("./core/validator.js").Validator<string, string, import("./core/types.js").ExecutionOptions>;
|
|
53
|
+
declare const toArray: import("./core/validator.js").Validator<any[], any, import("./core/types.js").ExecutionOptions>;
|
|
54
|
+
declare const toBoolean: import("./core/validator.js").Validator<boolean | undefined, unknown, import("./core/types.js").ExecutionOptions>;
|
|
55
|
+
declare const toDate: import("./core/validator.js").Validator<Date, string | number | Date, import("./core/types.js").ExecutionOptions>;
|
|
56
|
+
declare const toDateString: import("./core/validator.js").Validator<string, string | number | Date, import("./core/types.js").ExecutionOptions>;
|
|
57
|
+
declare const toInteger: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
|
|
58
|
+
declare const toNumber: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
|
|
59
|
+
declare const toString: import("./core/validator.js").Validator<string, unknown, import("./core/types.js").ExecutionOptions>;
|
|
60
|
+
export { vg, isAny, isArray, isBigint, isBoolean, isDate, isDateString, isEmpty, isNotEmpty, isInteger, isNull, isNullish, isDefined, isUndefined, isNumber, isObject, isObjectId, isString, isUUID, isUUID1, isUUID2, isUUID3, isUUID4, isUUID5, isEmail, isMobilePhone, isIP, isIPRange, isMACAddress, isPort, isURL, isBase64, isSWIFT, isCreditCard, isIBAN, isEAN, isFQDN, isISSN, isBtcAddress, isETHAddress, isHexColor, isJWT, isLowercase, isUppercase, isAlpha, isAlphanumeric, isAscii, isDecimal, isHex, toArray, toBoolean, toDate, toDateString, toInteger, toNumber, toString, };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Check if the string contains only letters (a-zA-Z).
|
|
4
|
+
* @validator isAlpha
|
|
5
|
+
*/
|
|
6
|
+
export declare function isAlpha(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
7
|
+
/**
|
|
8
|
+
* Check if the string contains only letters and numbers.
|
|
9
|
+
* @validator isAlphanumeric
|
|
10
|
+
*/
|
|
11
|
+
export declare function isAlphanumeric(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Check if the string contains ASCII chars only.
|
|
4
|
+
* @validator isAscii
|
|
5
|
+
*/
|
|
6
|
+
export declare function isAscii(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
3
|
+
export interface Base64ValidatorOptions extends ValidationOptions, validatorJS.IsBase64Options {
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Validates if value is a "Base64" string.
|
|
7
|
+
* @validator isBase64
|
|
8
|
+
*/
|
|
9
|
+
export declare function isBase64(options?: Base64ValidatorOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is a BTC address.
|
|
4
|
+
* @validator isBtcAddress
|
|
5
|
+
*/
|
|
6
|
+
export declare function isBtcAddress(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
3
|
+
export interface CreditCardValidatorOptions extends ValidationOptions, validatorJS.IsCreditCardOptions {
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Validates if value is a credit card number
|
|
7
|
+
* @validator isCreditCard
|
|
8
|
+
*/
|
|
9
|
+
export declare function isCreditCard(options?: CreditCardValidatorOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Check if the string represents a decimal number,
|
|
4
|
+
* such as `0.1`, `.3`, `1.1`, `1.00003`, `4.0` etc.
|
|
5
|
+
* @validator isDecimal
|
|
6
|
+
*/
|
|
7
|
+
export declare function isDecimal(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is an EAN (European Article Number)
|
|
4
|
+
* @validator isEAN
|
|
5
|
+
*/
|
|
6
|
+
export declare function isEAN(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
export interface IsEmailOptions extends ValidationOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If set to `true`, the validator will also match `Display Name <email-address>`.
|
|
5
|
+
*
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
allowDisplayName?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* If set to `true`, the validator will reject strings without the format `Display Name <email-address>`.
|
|
11
|
+
*
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
requireDisplayName?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* If set to `false`, the validator will not allow any non-English UTF8 character in email address' local part.
|
|
17
|
+
*
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
utf8LocalPart?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* If set to `true`, the validator will not check for the standard max length of an email.
|
|
23
|
+
*
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
ignoreMaxLength?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* If set to `true`, the validator will allow IP addresses in the host part.
|
|
29
|
+
*
|
|
30
|
+
* @default false
|
|
31
|
+
*/
|
|
32
|
+
allowIpDomain?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* If set to `true`, some additional validation will be enabled,
|
|
35
|
+
* e.g. disallowing certain syntactically valid email addresses that are rejected by GMail.
|
|
36
|
+
*
|
|
37
|
+
* @default false
|
|
38
|
+
*/
|
|
39
|
+
domainSpecificValidation?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* If set to an array of strings and the part of the email after
|
|
42
|
+
* the @ symbol matches one of the strings defined in it,
|
|
43
|
+
* the validation fails.
|
|
44
|
+
*/
|
|
45
|
+
hostBlacklist?: string[];
|
|
46
|
+
/**
|
|
47
|
+
* If set to an array of strings and the part of the email after
|
|
48
|
+
* the @ symbol matches none of the strings defined in it,
|
|
49
|
+
* the validation fails.
|
|
50
|
+
*/
|
|
51
|
+
hostWhitelist?: string[];
|
|
52
|
+
/**
|
|
53
|
+
* If set to a string, then the validator will reject emails that include
|
|
54
|
+
* any of the characters in the string, in the name part.
|
|
55
|
+
*/
|
|
56
|
+
blacklistedChars?: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Validates if value is a valid Email
|
|
60
|
+
* @validator isEmail
|
|
61
|
+
*/
|
|
62
|
+
export declare function isEmail(options?: IsEmailOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is a ETH (Ethereum) address.
|
|
4
|
+
* @validator isETHAddress
|
|
5
|
+
*/
|
|
6
|
+
export declare function isETHAddress(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
export interface IsFQDNOptions extends ValidationOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).
|
|
5
|
+
* @default false
|
|
6
|
+
*/
|
|
7
|
+
allowWildcard?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Validates if value is an FQDN
|
|
11
|
+
* @validator isFQDN
|
|
12
|
+
*/
|
|
13
|
+
export declare function isFQDN(options?: IsFQDNOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
3
|
+
export type HashAlgorithm = validatorJS.HashAlgorithm;
|
|
4
|
+
/**
|
|
5
|
+
* Validates if value a hash of type algorithm
|
|
6
|
+
* @validator isHash
|
|
7
|
+
*/
|
|
8
|
+
export declare function isHash(algorithm: HashAlgorithm, options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is a Hex Color
|
|
4
|
+
* @validator isHexColor
|
|
5
|
+
*/
|
|
6
|
+
export declare function isHexColor(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Check if the string is a hexadecimal number.
|
|
4
|
+
* @validator isHex
|
|
5
|
+
*/
|
|
6
|
+
export declare function isHex(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is an IBAN (International Bank Account Number)
|
|
4
|
+
* @validator isIBAN
|
|
5
|
+
*/
|
|
6
|
+
export declare function isIBAN(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
7
|
+
/**
|
|
8
|
+
* Validates if value is a BIC (Bank Identification Code) or SWIFT code
|
|
9
|
+
* @validator isSWIFT
|
|
10
|
+
*/
|
|
11
|
+
export declare function isSWIFT(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value is an IP
|
|
4
|
+
* @validator isIP
|
|
5
|
+
*/
|
|
6
|
+
export declare function isIP(version?: 4 | 6, options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
7
|
+
/**
|
|
8
|
+
* Validates if value is an IP
|
|
9
|
+
* @validator isIPRange
|
|
10
|
+
*/
|
|
11
|
+
export declare function isIPRange(version?: 4 | 6, options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
export interface IsISSNOptions extends ValidationOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If set to `true`, ISSNs with a lowercase `x` as the check digit are rejected.
|
|
5
|
+
*
|
|
6
|
+
* @default false
|
|
7
|
+
*/
|
|
8
|
+
caseSensitive?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Validates if value is an ISSN
|
|
12
|
+
* @validator isISSN
|
|
13
|
+
*/
|
|
14
|
+
export declare function isISSN(options?: IsISSNOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value a valid JWT token
|
|
4
|
+
* @validator isJWT
|
|
5
|
+
*/
|
|
6
|
+
export declare function isJWT(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates if value a Lowercase string
|
|
4
|
+
* @validator isLowercase
|
|
5
|
+
*/
|
|
6
|
+
export declare function isLowercase(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
7
|
+
/**
|
|
8
|
+
* Validates if value a Uppercase string
|
|
9
|
+
* @validator isUppercase
|
|
10
|
+
*/
|
|
11
|
+
export declare function isUppercase(options?: ValidationOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ValidationOptions } from '../../core/index.js';
|
|
2
|
+
export interface IsMACAddressOptions extends ValidationOptions {
|
|
3
|
+
/**
|
|
4
|
+
* If set to `true`, the validator will allow MAC addresses without the colons.
|
|
5
|
+
* Also, it allows the use of hyphens or spaces.
|
|
6
|
+
*
|
|
7
|
+
* e.g. `01 02 03 04 05 ab` or `01-02-03-04-05-ab`.
|
|
8
|
+
*
|
|
9
|
+
* @default false
|
|
10
|
+
*/
|
|
11
|
+
noSeparators?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Setting `eui` allows for validation against EUI-48 or EUI-64 instead of both.
|
|
14
|
+
*/
|
|
15
|
+
eui?: '48' | '64';
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Validates if value is an MACAddress
|
|
19
|
+
* @validator isMACAddress
|
|
20
|
+
*/
|
|
21
|
+
export declare function isMACAddress(options?: IsMACAddressOptions): import("../../core/validator.js").Validator<string, string, import("../../core/types.js").ExecutionOptions>;
|