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,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isObject = void 0;
|
|
4
|
-
const constants_js_1 = require("
|
|
5
|
-
const index_js_1 = require("
|
|
4
|
+
const constants_js_1 = require("../../constants.js");
|
|
5
|
+
const index_js_1 = require("../../core/index.js");
|
|
6
6
|
/**
|
|
7
7
|
* Validates object according to schema
|
|
8
8
|
* Converts properties according to schema rules if coerce option is set to 'true'.
|
|
@@ -37,7 +37,8 @@ function isObject(schema, options) {
|
|
|
37
37
|
const _rule = (0, index_js_1.validator)('isObject', function (input, context, _this) {
|
|
38
38
|
if (ctor && ctor[constants_js_1.preValidation])
|
|
39
39
|
input = ctor[constants_js_1.preValidation](input, context, _this);
|
|
40
|
-
|
|
40
|
+
const coerce = options?.coerce || context.coerce;
|
|
41
|
+
if (typeof input === 'string' && coerce)
|
|
41
42
|
input = JSON.parse(input);
|
|
42
43
|
if (!(input && typeof input === 'object')) {
|
|
43
44
|
context.fail(_this, `{{label}} must be an object`, input);
|
|
@@ -68,8 +69,7 @@ function isObject(schema, options) {
|
|
|
68
69
|
inputKey = keys[i];
|
|
69
70
|
schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
|
|
70
71
|
v = input[inputKey];
|
|
71
|
-
_propRule = propertyRules[schemaKey] ||
|
|
72
|
-
((0, index_js_1.isValidator)(additionalFields) ? additionalFields : undefined);
|
|
72
|
+
_propRule = propertyRules[schemaKey] || ((0, index_js_1.isValidator)(additionalFields) ? additionalFields : undefined);
|
|
73
73
|
if (_propRule) {
|
|
74
74
|
if (processedSchemaKeys[schemaKey])
|
|
75
75
|
continue;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isRecord = void 0;
|
|
4
|
-
const index_js_1 = require("
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
5
|
/**
|
|
6
6
|
* Validates record object according to given "key" and "value" rules
|
|
7
7
|
* Converts properties according to rules if coerce option is set to 'true'.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.trimStart = exports.trimEnd = exports.trim = exports.stringSplit = exports.stringReplace = exports.isString = void 0;
|
|
4
|
-
const index_js_1 = require("
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
5
|
/**
|
|
6
6
|
* Validates if value is "string".
|
|
7
7
|
* Converts input value to string if coerce option is set to 'true'.
|
|
@@ -9,7 +9,8 @@ const index_js_1 = require("../core/index.js");
|
|
|
9
9
|
*/
|
|
10
10
|
function isString(options) {
|
|
11
11
|
return (0, index_js_1.validator)('isString', function (input, context, _this) {
|
|
12
|
-
|
|
12
|
+
const coerce = options?.coerce || context.coerce;
|
|
13
|
+
if (input != null && typeof input !== 'string' && coerce) {
|
|
13
14
|
if (typeof input === 'object') {
|
|
14
15
|
if (typeof input.toJSON === 'function')
|
|
15
16
|
input = input.toJSON();
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isTuple = void 0;
|
|
4
|
-
const index_js_1 = require("
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
5
|
function isTuple(items, options) {
|
|
6
6
|
return (0, index_js_1.validator)('isTuple', function (input, context, _this) {
|
|
7
|
-
|
|
7
|
+
const coerce = options?.coerce || context.coerce;
|
|
8
|
+
if (input != null && coerce && !Array.isArray(input))
|
|
8
9
|
input = [input];
|
|
9
10
|
if (!Array.isArray(input)) {
|
|
10
|
-
context.fail(_this, `{{
|
|
11
|
+
context.fail(_this, `"{{value}}" is not a valid tuple`, input);
|
|
11
12
|
return;
|
|
12
13
|
}
|
|
13
14
|
const location = context.location || '';
|
|
@@ -24,8 +25,7 @@ function isTuple(items, options) {
|
|
|
24
25
|
itemContext.label = context.label ? context.label + `[${i}]` : undefined;
|
|
25
26
|
itemContext.index = i;
|
|
26
27
|
itemContext.location = location + '[' + i + ']';
|
|
27
|
-
itemContext.label = (itemContext.label || itemContext.property || 'Value at ') +
|
|
28
|
-
`[${i}]`;
|
|
28
|
+
itemContext.label = (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
|
|
29
29
|
v = itemRule(input[i], itemContext);
|
|
30
30
|
out.push(v);
|
|
31
31
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exists = void 0;
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Validates if property exists
|
|
7
|
+
* @validator exists
|
|
8
|
+
*/
|
|
9
|
+
function exists(options) {
|
|
10
|
+
return (0, index_js_1.validator)('exists', function (input, context, _this) {
|
|
11
|
+
if (input !== undefined || (context.scope && Object.getOwnPropertyDescriptor(context.scope, input)))
|
|
12
|
+
return input;
|
|
13
|
+
context.fail(_this, `{{label}} must exist`, input);
|
|
14
|
+
}, options);
|
|
15
|
+
}
|
|
16
|
+
exports.exists = exists;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLength = void 0;
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Returns length of an Array, String, ArrayBuffer, Buffer or any object with the "length" property.
|
|
7
|
+
* @validator getLength
|
|
8
|
+
*/
|
|
9
|
+
function getLength() {
|
|
10
|
+
return (0, index_js_1.validator)('getLength', function (input, context, _this) {
|
|
11
|
+
if (typeof input === 'string')
|
|
12
|
+
return input.length;
|
|
13
|
+
if (Array.isArray(input))
|
|
14
|
+
return input.length;
|
|
15
|
+
if (input instanceof ArrayBuffer)
|
|
16
|
+
return input.byteLength;
|
|
17
|
+
if (input && typeof input === 'object' && typeof input.length === 'number')
|
|
18
|
+
return input.length;
|
|
19
|
+
if (input && typeof input === 'object' && typeof input.size === 'number')
|
|
20
|
+
return input.size;
|
|
21
|
+
context.fail(_this, `Unable to get length of {{label}}`, input);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
exports.getLength = getLength;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.optional = void 0;
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Makes sub-rule optional
|
|
7
|
+
* @validator optional
|
|
8
|
+
*/
|
|
9
|
+
function optional(nested, options) {
|
|
10
|
+
return (0, index_js_1.validator)('optional', function (input, context) {
|
|
11
|
+
if (input == null)
|
|
12
|
+
return input;
|
|
13
|
+
return nested(input, context);
|
|
14
|
+
}, options);
|
|
15
|
+
}
|
|
16
|
+
exports.optional = optional;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.required = void 0;
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Makes sub-rule required
|
|
7
|
+
* @validator required
|
|
8
|
+
*/
|
|
9
|
+
function required(nested, options) {
|
|
10
|
+
return (0, index_js_1.validator)('required', function (input, context, _this) {
|
|
11
|
+
if (input == null) {
|
|
12
|
+
context.fail(_this, `{{label}} is required`, input);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
return nested(input, context);
|
|
16
|
+
}, options);
|
|
17
|
+
}
|
|
18
|
+
exports.required = required;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.union = void 0;
|
|
4
|
-
const index_js_1 = require("
|
|
4
|
+
const index_js_1 = require("../../core/index.js");
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
@@ -13,7 +13,7 @@ function union(codecs) {
|
|
|
13
13
|
let v;
|
|
14
14
|
let passed = false;
|
|
15
15
|
// Mock fail method to prevent errors
|
|
16
|
-
context.fail = () => passed = false;
|
|
16
|
+
context.fail = () => (passed = false);
|
|
17
17
|
for (i = 0; i < l; i++) {
|
|
18
18
|
c = codecs[i];
|
|
19
19
|
try {
|
|
@@ -30,7 +30,7 @@ function union(codecs) {
|
|
|
30
30
|
delete context.fail;
|
|
31
31
|
if (passed)
|
|
32
32
|
return v;
|
|
33
|
-
context.fail(_this, `
|
|
33
|
+
context.fail(_this, `Value didn't match any on union rules`, input);
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
36
|
exports.union = union;
|
package/esm/core/context.js
CHANGED
|
@@ -19,7 +19,7 @@ export class Context {
|
|
|
19
19
|
index: this.index,
|
|
20
20
|
label: this.label,
|
|
21
21
|
value,
|
|
22
|
-
...details
|
|
22
|
+
...details,
|
|
23
23
|
});
|
|
24
24
|
issue.value = value;
|
|
25
25
|
const onFail = this.onFail || rule[kOptions].onFail;
|
|
@@ -34,14 +34,17 @@ export class Context {
|
|
|
34
34
|
else
|
|
35
35
|
issue.message = String(x);
|
|
36
36
|
}
|
|
37
|
-
issue.message = ('' + issue.message)
|
|
38
|
-
.replace(VARIABLE_REPLACE_PATTERN, (x, g) => {
|
|
37
|
+
issue.message = ('' + issue.message).replace(VARIABLE_REPLACE_PATTERN, (x, g) => {
|
|
39
38
|
const m = OPTIONAL_VAR_PATTERN.exec(g);
|
|
40
39
|
if (!m)
|
|
41
40
|
return x;
|
|
42
41
|
const k = m[1];
|
|
43
42
|
let v = issue[k];
|
|
44
|
-
if (
|
|
43
|
+
if (k === 'value') {
|
|
44
|
+
const s = String(issue.value);
|
|
45
|
+
return s.length < 30 ? s : s.substring(0, 30) + '..';
|
|
46
|
+
}
|
|
47
|
+
if (!v && k === 'label' && (this.location || this.property))
|
|
45
48
|
v = '`' + (this.location || this.property) + '`';
|
|
46
49
|
if (v != null)
|
|
47
50
|
return v;
|
package/esm/core/validator.js
CHANGED
|
@@ -19,10 +19,10 @@ export function validator(arg0, arg1, arg2) {
|
|
|
19
19
|
throw new TypeError('You must provide a rule function argument');
|
|
20
20
|
id = id || fn.name || 'unnamed-rule';
|
|
21
21
|
const name = fn.name || camelCase(id);
|
|
22
|
-
const _rule =
|
|
22
|
+
const _rule = {
|
|
23
23
|
[name](input, options) {
|
|
24
24
|
const ctx = options instanceof Context ? options : undefined;
|
|
25
|
-
const opts =
|
|
25
|
+
const opts = ctx ? undefined : options;
|
|
26
26
|
const context = ctx || new Context(opts);
|
|
27
27
|
let value;
|
|
28
28
|
try {
|
|
@@ -36,8 +36,8 @@ export function validator(arg0, arg1, arg2) {
|
|
|
36
36
|
if (!ctx && context.errors.length)
|
|
37
37
|
throw new ValidationError(context.errors);
|
|
38
38
|
return value;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
39
|
+
},
|
|
40
|
+
}[name];
|
|
41
41
|
_rule.id = id;
|
|
42
42
|
_rule[kValidatorFn] = fn;
|
|
43
43
|
_rule[kOptions] = validatorOptions || {};
|
package/esm/index.js
CHANGED
|
@@ -32,7 +32,7 @@ const isMACAddress = vg.isMACAddress();
|
|
|
32
32
|
const isPort = vg.isPort();
|
|
33
33
|
const isURL = vg.isURL();
|
|
34
34
|
const isBase64 = vg.isBase64();
|
|
35
|
-
const
|
|
35
|
+
const isSWIFT = vg.isSWIFT();
|
|
36
36
|
const isCreditCard = vg.isCreditCard();
|
|
37
37
|
const isIBAN = vg.isIBAN();
|
|
38
38
|
const isEAN = vg.isEAN();
|
|
@@ -48,5 +48,12 @@ const isAlpha = vg.isAlpha();
|
|
|
48
48
|
const isAlphanumeric = vg.isAlphanumeric();
|
|
49
49
|
const isAscii = vg.isAscii();
|
|
50
50
|
const isDecimal = vg.isDecimal();
|
|
51
|
-
const
|
|
52
|
-
|
|
51
|
+
const isHex = vg.isHex();
|
|
52
|
+
const toArray = vg.isArray(isAny, { coerce: true });
|
|
53
|
+
const toBoolean = vg.isBoolean({ coerce: true });
|
|
54
|
+
const toDate = vg.isDate({ coerce: true });
|
|
55
|
+
const toDateString = vg.isDateString({ coerce: true });
|
|
56
|
+
const toInteger = vg.isInteger({ coerce: true });
|
|
57
|
+
const toNumber = vg.isNumber({ coerce: true });
|
|
58
|
+
const toString = vg.isString({ coerce: true });
|
|
59
|
+
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,24 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Check if the string contains only letters (a-zA-Z).
|
|
5
|
+
* @validator isAlpha
|
|
6
|
+
*/
|
|
7
|
+
export function isAlpha(options) {
|
|
8
|
+
return validator('isLowercase', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isAlpha(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `"{{value}}" is not an alpha string`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Check if the string contains only letters and numbers.
|
|
16
|
+
* @validator isAlphanumeric
|
|
17
|
+
*/
|
|
18
|
+
export function isAlphanumeric(options) {
|
|
19
|
+
return validator('isAlphanumeric', function (input, context, _this) {
|
|
20
|
+
if (typeof input === 'string' && validatorJS.isAlphanumeric(input))
|
|
21
|
+
return input;
|
|
22
|
+
context.fail(_this, `"{{value}}" is not an alphanumeric string`, input);
|
|
23
|
+
}, options);
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Check if the string contains ASCII chars only.
|
|
5
|
+
* @validator isAscii
|
|
6
|
+
*/
|
|
7
|
+
export function isAscii(options) {
|
|
8
|
+
return validator('isAscii', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isAscii(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `Value is not an ascii string`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is a "Base64" string.
|
|
5
|
+
* @validator isBase64
|
|
6
|
+
*/
|
|
7
|
+
export function isBase64(options) {
|
|
8
|
+
return validator('isBase64', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isBase64(input, options))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `"{{value}}" is not a valid a Base64 string`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is a BTC address.
|
|
5
|
+
* @validator isBtcAddress
|
|
6
|
+
*/
|
|
7
|
+
export function isBtcAddress(options) {
|
|
8
|
+
return validator('isBtcAddress', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isBtcAddress(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `Value is not a valid a BTC address`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is a credit card number
|
|
5
|
+
* @validator isCreditCard
|
|
6
|
+
*/
|
|
7
|
+
export function isCreditCard(options) {
|
|
8
|
+
return validator('isCreditCard', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isCreditCard(input, options))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `"{{value}}" is not a valid Credit Card number`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Check if the string represents a decimal number,
|
|
5
|
+
* such as `0.1`, `.3`, `1.1`, `1.00003`, `4.0` etc.
|
|
6
|
+
* @validator isDecimal
|
|
7
|
+
*/
|
|
8
|
+
export function isDecimal(options) {
|
|
9
|
+
return validator('isDecimal', function (input, context, _this) {
|
|
10
|
+
if (typeof input === 'string' && validatorJS.isDecimal(input))
|
|
11
|
+
return input;
|
|
12
|
+
context.fail(_this, `"{{value}}" is not an decimal number string`, input);
|
|
13
|
+
}, options);
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is an EAN (European Article Number)
|
|
5
|
+
* @validator isEAN
|
|
6
|
+
*/
|
|
7
|
+
export function isEAN(options) {
|
|
8
|
+
return validator('isEAN', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isEAN(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `"{{value}}" is not a valid EAN (European Article Number)`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is a valid Email
|
|
5
|
+
* @validator isEmail
|
|
6
|
+
*/
|
|
7
|
+
export function isEmail(options) {
|
|
8
|
+
const emailOptions = {
|
|
9
|
+
// eslint-disable-next-line camelcase
|
|
10
|
+
allow_display_name: true,
|
|
11
|
+
// eslint-disable-next-line camelcase
|
|
12
|
+
allow_utf8_local_part: options?.utf8LocalPart,
|
|
13
|
+
// eslint-disable-next-line camelcase
|
|
14
|
+
ignore_max_length: true,
|
|
15
|
+
// eslint-disable-next-line camelcase
|
|
16
|
+
allow_ip_domain: options?.allowIpDomain,
|
|
17
|
+
};
|
|
18
|
+
return validator('isEmail', function (input, context, _this) {
|
|
19
|
+
if (typeof input === 'string' && validatorJS.isEmail(input, emailOptions)) {
|
|
20
|
+
if (options?.requireDisplayName) {
|
|
21
|
+
if (!validatorJS.isEmail(input, {
|
|
22
|
+
...emailOptions,
|
|
23
|
+
// eslint-disable-next-line camelcase
|
|
24
|
+
require_display_name: true,
|
|
25
|
+
})) {
|
|
26
|
+
context.fail(_this, `Display name for the email is required. Etc. ( Name <me@tempuri.org> ) `, input);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else if (!options?.allowDisplayName &&
|
|
31
|
+
!validatorJS.isEmail(input, {
|
|
32
|
+
...emailOptions,
|
|
33
|
+
// eslint-disable-next-line camelcase
|
|
34
|
+
allow_display_name: false,
|
|
35
|
+
})) {
|
|
36
|
+
context.fail(_this, `Display name in email is not allowed`, input);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (options?.hostBlacklist &&
|
|
40
|
+
!validatorJS.isEmail(input, {
|
|
41
|
+
...emailOptions,
|
|
42
|
+
// eslint-disable-next-line camelcase
|
|
43
|
+
host_blacklist: options.hostBlacklist,
|
|
44
|
+
})) {
|
|
45
|
+
context.fail(_this, `Email "{{value}}" is in black-list`, input);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (options?.hostWhitelist &&
|
|
49
|
+
!validatorJS.isEmail(input, {
|
|
50
|
+
...emailOptions,
|
|
51
|
+
// eslint-disable-next-line camelcase
|
|
52
|
+
host_whitelist: options.hostWhitelist,
|
|
53
|
+
})) {
|
|
54
|
+
context.fail(_this, `Email "{{value}}" is in not white-list`, input);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (options?.blacklistedChars &&
|
|
58
|
+
!validatorJS.isEmail(input, {
|
|
59
|
+
...emailOptions,
|
|
60
|
+
// eslint-disable-next-line camelcase
|
|
61
|
+
blacklisted_chars: options.blacklistedChars,
|
|
62
|
+
})) {
|
|
63
|
+
context.fail(_this, `Black listed characters (${options.blacklistedChars}) found in name part`, input);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
return input;
|
|
67
|
+
}
|
|
68
|
+
context.fail(_this, `"{{value}}" does not match required e-mail format`, input);
|
|
69
|
+
}, options);
|
|
70
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is a ETH (Ethereum) address.
|
|
5
|
+
* @validator isETHAddress
|
|
6
|
+
*/
|
|
7
|
+
export function isETHAddress(options) {
|
|
8
|
+
return validator('isETHAddress', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isEthereumAddress(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `Value is not a valid a ETH (Ethereum) address`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is an FQDN
|
|
5
|
+
* @validator isFQDN
|
|
6
|
+
*/
|
|
7
|
+
export function isFQDN(options) {
|
|
8
|
+
const opts = {
|
|
9
|
+
// eslint-disable-next-line camelcase
|
|
10
|
+
allow_wildcard: options?.allowWildcard,
|
|
11
|
+
};
|
|
12
|
+
return validator('isFQDN', function (input, context, _this) {
|
|
13
|
+
if (typeof input === 'string' && validatorJS.isFQDN(input, opts))
|
|
14
|
+
return input;
|
|
15
|
+
context.fail(_this, `"{{value}}" is not valid FQDN`, input);
|
|
16
|
+
}, options);
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value a hash of type algorithm
|
|
5
|
+
* @validator isHash
|
|
6
|
+
*/
|
|
7
|
+
export function isHash(algorithm, options) {
|
|
8
|
+
return validator('isHash', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isHash(input, algorithm))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `Value is not a valid ${algorithm} hash`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is a Hex Color
|
|
5
|
+
* @validator isHexColor
|
|
6
|
+
*/
|
|
7
|
+
export function isHexColor(options) {
|
|
8
|
+
return validator('isHexColor', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isHexColor(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `{{label}} is not a valid Hex Color`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Check if the string is a hexadecimal number.
|
|
5
|
+
* @validator isHex
|
|
6
|
+
*/
|
|
7
|
+
export function isHex(options) {
|
|
8
|
+
return validator('isHex', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isHexadecimal(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `{{label}} is not an hexadecimal string`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is an IBAN (International Bank Account Number)
|
|
5
|
+
* @validator isIBAN
|
|
6
|
+
*/
|
|
7
|
+
export function isIBAN(options) {
|
|
8
|
+
return validator('isIBAN', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isIBAN(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `{{label}} is not a valid IBAN (International Bank Account Number)`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Validates if value is a BIC (Bank Identification Code) or SWIFT code
|
|
16
|
+
* @validator isSWIFT
|
|
17
|
+
*/
|
|
18
|
+
export function isSWIFT(options) {
|
|
19
|
+
return validator('isSWIFT', function (input, context, _this) {
|
|
20
|
+
if (typeof input === 'string' && validatorJS.isBIC(input))
|
|
21
|
+
return input;
|
|
22
|
+
context.fail(_this, `{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`, input);
|
|
23
|
+
}, options);
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is an IP
|
|
5
|
+
* @validator isIP
|
|
6
|
+
*/
|
|
7
|
+
export function isIP(version, options) {
|
|
8
|
+
return validator('isIP', function (input, context, _this) {
|
|
9
|
+
if (input != null && typeof input === 'string' && validatorJS.isIP(input, version))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `"{{value}}" is not a valid IP${version ? ' v' + version : ''}`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Validates if value is an IP
|
|
16
|
+
* @validator isIPRange
|
|
17
|
+
*/
|
|
18
|
+
export function isIPRange(version, options) {
|
|
19
|
+
return validator('isIPRange', function (input, context, _this) {
|
|
20
|
+
if (input != null && typeof input === 'string' && validatorJS.isIPRange(input, version))
|
|
21
|
+
return input;
|
|
22
|
+
context.fail(_this, `"{{value}}" is not a valid IP${version ? ' v' + version : ''} range`, input);
|
|
23
|
+
}, options);
|
|
24
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value is an ISSN
|
|
5
|
+
* @validator isISSN
|
|
6
|
+
*/
|
|
7
|
+
export function isISSN(options) {
|
|
8
|
+
const opts = {
|
|
9
|
+
// eslint-disable-next-line camelcase
|
|
10
|
+
case_sensitive: options?.caseSensitive,
|
|
11
|
+
};
|
|
12
|
+
return validator('isISSN', function (input, context, _this) {
|
|
13
|
+
if (typeof input === 'string' && validatorJS.isISSN(input, opts))
|
|
14
|
+
return input;
|
|
15
|
+
context.fail(_this, `"{{value}}" is not a valid ISSN`, input);
|
|
16
|
+
}, options);
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as validatorJS from 'validator';
|
|
2
|
+
import { validator } from '../../core/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Validates if value a valid JWT token
|
|
5
|
+
* @validator isJWT
|
|
6
|
+
*/
|
|
7
|
+
export function isJWT(options) {
|
|
8
|
+
return validator('isJWT', function (input, context, _this) {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isJWT(input))
|
|
10
|
+
return input;
|
|
11
|
+
context.fail(_this, `Value is not a valid JWT token`, input);
|
|
12
|
+
}, options);
|
|
13
|
+
}
|