valgen 5.4.1 → 5.5.0
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 +10 -0
- package/cjs/core/context.js +13 -10
- package/cjs/core/index.js +2 -2
- package/cjs/core/utilities.js +4 -5
- package/cjs/core/validator.js +10 -7
- package/cjs/helpers/object.utils.js +2 -3
- package/cjs/helpers/omit-undefined.js +1 -2
- package/cjs/helpers/string.utils.js +2 -5
- package/cjs/index.js +2 -2
- package/cjs/rules/format-rules/is-alpha.js +6 -6
- package/cjs/rules/format-rules/is-ascii.js +2 -3
- package/cjs/rules/format-rules/is-base64.js +4 -4
- package/cjs/rules/format-rules/is-btc-address.js +4 -4
- package/cjs/rules/format-rules/is-credit-card.js +5 -4
- package/cjs/rules/format-rules/is-decimal.js +4 -4
- package/cjs/rules/format-rules/is-ean.js +2 -3
- package/cjs/rules/format-rules/is-email.js +4 -4
- package/cjs/rules/format-rules/is-eth-address.js +4 -4
- package/cjs/rules/format-rules/is-fqdn.js +4 -4
- package/cjs/rules/format-rules/is-hash.js +4 -4
- package/cjs/rules/format-rules/is-hex-color.js +4 -4
- package/cjs/rules/format-rules/is-hex.js +4 -4
- package/cjs/rules/format-rules/is-iban.js +4 -5
- package/cjs/rules/format-rules/is-ip.js +12 -7
- package/cjs/rules/format-rules/is-issn.js +4 -4
- package/cjs/rules/format-rules/is-jwt.js +2 -3
- package/cjs/rules/format-rules/is-lowercase.js +8 -7
- package/cjs/rules/format-rules/is-mac-address.js +6 -4
- package/cjs/rules/format-rules/is-mobile-phone.js +5 -4
- package/cjs/rules/format-rules/is-object-id.js +7 -5
- package/cjs/rules/format-rules/is-passport-number.js +5 -4
- package/cjs/rules/format-rules/is-port.js +6 -4
- package/cjs/rules/format-rules/is-url.js +6 -4
- package/cjs/rules/format-rules/is-uuid.js +6 -4
- package/cjs/rules/format-rules/is-vat-number.js +4 -4
- package/cjs/rules/format-rules/matches.js +2 -3
- package/cjs/rules/index.js +14 -13
- package/cjs/rules/logical-rules/is-defined.js +2 -17
- package/cjs/rules/logical-rules/is-empty.js +4 -5
- package/cjs/rules/logical-rules/range.js +57 -26
- package/cjs/rules/type-rules/is-any.js +1 -3
- package/cjs/rules/type-rules/is-array.js +5 -4
- package/cjs/rules/type-rules/is-bigint.js +5 -4
- package/cjs/rules/type-rules/is-boolean.js +2 -3
- package/cjs/rules/type-rules/is-date.js +13 -8
- package/cjs/rules/type-rules/is-enum.js +7 -4
- package/cjs/rules/type-rules/is-integer.js +11 -5
- package/cjs/rules/type-rules/is-null.js +4 -11
- package/cjs/rules/type-rules/is-number.js +7 -4
- package/cjs/rules/type-rules/is-object.js +20 -12
- package/cjs/rules/type-rules/is-record.js +2 -3
- package/cjs/rules/type-rules/is-string.js +11 -11
- package/cjs/rules/type-rules/is-tuple.js +7 -5
- package/cjs/rules/type-rules/is-undefined.js +17 -0
- package/cjs/rules/utility-rules/exists.js +6 -4
- package/cjs/rules/utility-rules/get-length.js +10 -5
- package/cjs/rules/utility-rules/optional.js +2 -3
- package/cjs/rules/utility-rules/pipe.js +5 -6
- package/cjs/rules/utility-rules/required.js +2 -3
- package/cjs/rules/utility-rules/union.js +2 -3
- package/esm/core/context.js +13 -10
- package/esm/core/index.js +2 -2
- package/esm/core/utilities.js +3 -3
- package/esm/core/validator.js +8 -4
- package/esm/helpers/string.utils.js +1 -3
- package/esm/index.js +1 -1
- package/esm/rules/format-rules/is-alpha.js +4 -3
- package/esm/rules/format-rules/is-ascii.js +1 -1
- package/esm/rules/format-rules/is-base64.js +3 -2
- package/esm/rules/format-rules/is-btc-address.js +3 -2
- package/esm/rules/format-rules/is-credit-card.js +4 -2
- package/esm/rules/format-rules/is-decimal.js +3 -2
- package/esm/rules/format-rules/is-ean.js +1 -1
- package/esm/rules/format-rules/is-email.js +3 -2
- package/esm/rules/format-rules/is-eth-address.js +3 -2
- package/esm/rules/format-rules/is-fqdn.js +3 -2
- package/esm/rules/format-rules/is-hash.js +3 -2
- package/esm/rules/format-rules/is-hex-color.js +3 -2
- package/esm/rules/format-rules/is-hex.js +3 -2
- package/esm/rules/format-rules/is-iban.js +2 -2
- package/esm/rules/format-rules/is-ip.js +10 -4
- package/esm/rules/format-rules/is-issn.js +3 -2
- package/esm/rules/format-rules/is-jwt.js +1 -1
- package/esm/rules/format-rules/is-lowercase.js +6 -4
- package/esm/rules/format-rules/is-mac-address.js +5 -2
- package/esm/rules/format-rules/is-mobile-phone.js +4 -2
- package/esm/rules/format-rules/is-object-id.js +7 -4
- package/esm/rules/format-rules/is-passport-number.js +4 -2
- package/esm/rules/format-rules/is-port.js +5 -2
- package/esm/rules/format-rules/is-url.js +5 -2
- package/esm/rules/format-rules/is-uuid.js +5 -2
- package/esm/rules/format-rules/is-vat-number.js +3 -2
- package/esm/rules/format-rules/matches.js +2 -2
- package/esm/rules/index.js +14 -13
- package/esm/rules/logical-rules/is-defined.js +1 -14
- package/esm/rules/logical-rules/is-empty.js +2 -2
- package/esm/rules/logical-rules/range.js +52 -21
- package/esm/rules/type-rules/is-any.js +1 -3
- package/esm/rules/type-rules/is-array.js +5 -3
- package/esm/rules/type-rules/is-bigint.js +4 -2
- package/esm/rules/type-rules/is-boolean.js +2 -2
- package/esm/rules/type-rules/is-date.js +11 -5
- package/esm/rules/type-rules/is-enum.js +7 -3
- package/esm/rules/type-rules/is-integer.js +11 -4
- package/esm/rules/type-rules/is-null.js +2 -8
- package/esm/rules/type-rules/is-number.js +6 -2
- package/esm/rules/type-rules/is-object.js +19 -10
- package/esm/rules/type-rules/is-record.js +2 -2
- package/esm/rules/type-rules/is-string.js +7 -7
- package/esm/rules/type-rules/is-tuple.js +7 -4
- package/esm/rules/type-rules/is-undefined.js +14 -0
- package/esm/rules/utility-rules/exists.js +5 -2
- package/esm/rules/utility-rules/get-length.js +9 -3
- package/esm/rules/utility-rules/optional.js +2 -2
- package/esm/rules/utility-rules/pipe.js +4 -4
- package/esm/rules/utility-rules/required.js +2 -2
- package/esm/rules/utility-rules/union.js +1 -1
- package/package.json +2 -2
- package/typings/core/index.d.ts +3 -3
- package/typings/core/types.d.ts +1 -1
- package/typings/index.d.ts +1 -1
- package/typings/rules/index.d.ts +14 -13
- package/typings/rules/logical-rules/is-defined.d.ts +0 -5
- package/typings/rules/type-rules/is-tuple.d.ts +36 -4
- package/typings/rules/type-rules/is-undefined.d.ts +6 -0
package/esm/core/context.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { omitUndefined } from '../helpers/omit-undefined.js';
|
|
2
|
-
import { kOptions } from './constants.js';
|
|
3
2
|
import { ValidationError } from './validation-error.js';
|
|
4
3
|
const VARIABLE_REPLACE_PATTERN = /{{([^}]*)}}/g;
|
|
5
4
|
const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
|
|
@@ -22,11 +21,8 @@ export class Context {
|
|
|
22
21
|
...details,
|
|
23
22
|
});
|
|
24
23
|
issue.value = value;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const proto = Object.getPrototypeOf(this);
|
|
28
|
-
const superOnFail = proto.onFail !== onFail ? proto.onFail : undefined;
|
|
29
|
-
const x = onFail(issue, this, superOnFail);
|
|
24
|
+
if (this.onFail) {
|
|
25
|
+
const x = this.onFail(issue, this);
|
|
30
26
|
if (!x)
|
|
31
27
|
return;
|
|
32
28
|
if (typeof x === 'object')
|
|
@@ -44,8 +40,9 @@ export class Context {
|
|
|
44
40
|
const s = String(issue.value);
|
|
45
41
|
return s.length < 30 ? s : s.substring(0, 30) + '..';
|
|
46
42
|
}
|
|
47
|
-
if (!v && k === 'label' && (this.location || this.property))
|
|
43
|
+
if (!v && k === 'label' && (this.location || this.property)) {
|
|
48
44
|
v = '`' + (this.location || this.property) + '`';
|
|
45
|
+
}
|
|
49
46
|
if (v != null)
|
|
50
47
|
return v;
|
|
51
48
|
if (m[2])
|
|
@@ -53,13 +50,19 @@ export class Context {
|
|
|
53
50
|
return m[1] === 'label' ? 'Value' : x;
|
|
54
51
|
});
|
|
55
52
|
this.errors.push(issue);
|
|
56
|
-
if (this.errors.length >= (this.maxErrors ?? Infinity))
|
|
53
|
+
if (this.errors.length >= (this.maxErrors ?? Infinity)) {
|
|
57
54
|
throw new ValidationError(this.errors);
|
|
55
|
+
}
|
|
58
56
|
}
|
|
59
57
|
extend(options) {
|
|
60
|
-
const extended =
|
|
58
|
+
const extended = {};
|
|
59
|
+
if (options) {
|
|
60
|
+
for (const [k, v] of Object.entries(options)) {
|
|
61
|
+
if (v !== undefined)
|
|
62
|
+
extended[k] = v;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
61
65
|
Object.setPrototypeOf(extended, this);
|
|
62
|
-
delete extended.errors;
|
|
63
66
|
return extended;
|
|
64
67
|
}
|
|
65
68
|
}
|
package/esm/core/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './context.js';
|
|
2
1
|
export * from './constants.js';
|
|
2
|
+
export * from './context.js';
|
|
3
3
|
export * from './types.js';
|
|
4
|
+
export * from './utilities.js';
|
|
4
5
|
export * from './validation-error.js';
|
|
5
6
|
export * from './validator.js';
|
|
6
|
-
export * from './utilities.js';
|
package/esm/core/utilities.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { isValidator, validator } from './index.js';
|
|
1
|
+
import { isValidator, validator, } from './index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Forwards codec process to a sub codec. Useful for circular checks
|
|
4
4
|
* @validator forwardRef
|
|
5
5
|
*/
|
|
6
6
|
export function forwardRef(fn) {
|
|
7
|
-
return validator('forwardRef',
|
|
7
|
+
return validator('forwardRef', (input, context) => {
|
|
8
8
|
const nested = fn(context);
|
|
9
9
|
return nested(input, context);
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
12
|
export function iif(check, _then, _else) {
|
|
13
|
-
return validator('iif',
|
|
13
|
+
return validator('iif', (input, context) => {
|
|
14
14
|
let c = _else;
|
|
15
15
|
try {
|
|
16
16
|
if (check(input) !== undefined)
|
package/esm/core/validator.js
CHANGED
|
@@ -2,6 +2,7 @@ import { camelCase } from '../helpers/string.utils.js';
|
|
|
2
2
|
import { kOptions, kValidatorFn } from './constants.js';
|
|
3
3
|
import { Context } from './context.js';
|
|
4
4
|
import { ValidationError } from './validation-error.js';
|
|
5
|
+
let unnamedValidatorIndex = 0;
|
|
5
6
|
export function validator(arg0, arg1, arg2) {
|
|
6
7
|
let id = '';
|
|
7
8
|
let fn;
|
|
@@ -15,15 +16,17 @@ export function validator(arg0, arg1, arg2) {
|
|
|
15
16
|
fn = arg0;
|
|
16
17
|
validatorOptions = arg1;
|
|
17
18
|
}
|
|
18
|
-
if (typeof fn !== 'function')
|
|
19
|
+
if (typeof fn !== 'function') {
|
|
19
20
|
throw new TypeError('You must provide a rule function argument');
|
|
20
|
-
|
|
21
|
+
}
|
|
22
|
+
id = id || fn.name || 'validator' + ++unnamedValidatorIndex;
|
|
21
23
|
const name = fn.name || camelCase(id);
|
|
22
24
|
const _rule = {
|
|
23
25
|
[name](input, options) {
|
|
24
26
|
const ctx = options instanceof Context ? options : undefined;
|
|
25
27
|
const opts = ctx ? undefined : options;
|
|
26
|
-
const context = ctx
|
|
28
|
+
const context = ctx?.extend({ ...validatorOptions, ...opts }) ||
|
|
29
|
+
new Context({ ...validatorOptions, ...opts });
|
|
27
30
|
let value;
|
|
28
31
|
try {
|
|
29
32
|
value = fn(input, context, _rule);
|
|
@@ -33,8 +36,9 @@ export function validator(arg0, arg1, arg2) {
|
|
|
33
36
|
throw e;
|
|
34
37
|
context.fail(_rule, e, input);
|
|
35
38
|
}
|
|
36
|
-
if (!ctx && context.errors.length)
|
|
39
|
+
if (!ctx && context.errors.length) {
|
|
37
40
|
throw new ValidationError(context.errors);
|
|
41
|
+
}
|
|
38
42
|
return value;
|
|
39
43
|
},
|
|
40
44
|
}[name];
|
package/esm/index.js
CHANGED
|
@@ -57,4 +57,4 @@ const toDateString = vg.isDateString({ coerce: true });
|
|
|
57
57
|
const toInteger = vg.isInteger({ coerce: true });
|
|
58
58
|
const toNumber = vg.isNumber({ coerce: true });
|
|
59
59
|
const toString = vg.isString({ coerce: true });
|
|
60
|
-
export {
|
|
60
|
+
export { isAlpha, isAlphanumeric, isAny, isArray, isAscii, isBase64, isBigint, isBoolean, isBtcAddress, isCreditCard, isDate, isDateString, isDecimal, isDefined, isEAN, isEmail, isEmpty, isETHAddress, isFQDN, isHex, isHexColor, isIBAN, isInteger, isIP, isIPRange, isISSN, isJWT, isLowercase, isMACAddress, isMobilePhone, isNotEmpty, isNull, isNullish, isNumber, isObject, isObjectId, isPort, isString, isSWIFT, isUndefined, isUppercase, isURL, isUUID, isUUID1, isUUID2, isUUID3, isUUID4, isUUID5, toArray, toBigint, toBoolean, toDate, toDateString, toInteger, toNumber, toString, vg, };
|
|
@@ -5,7 +5,7 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isAlpha
|
|
6
6
|
*/
|
|
7
7
|
export function isAlpha(options) {
|
|
8
|
-
return validator('isLowercase',
|
|
8
|
+
return validator('isLowercase', (input, context, _this) => {
|
|
9
9
|
if (typeof input === 'string' && validatorJS.isAlpha(input))
|
|
10
10
|
return input;
|
|
11
11
|
context.fail(_this, `"{{value}}" is not an alpha string`, input);
|
|
@@ -16,9 +16,10 @@ export function isAlpha(options) {
|
|
|
16
16
|
* @validator isAlphanumeric
|
|
17
17
|
*/
|
|
18
18
|
export function isAlphanumeric(options) {
|
|
19
|
-
return validator('isAlphanumeric',
|
|
20
|
-
if (typeof input === 'string' && validatorJS.isAlphanumeric(input))
|
|
19
|
+
return validator('isAlphanumeric', (input, context, _this) => {
|
|
20
|
+
if (typeof input === 'string' && validatorJS.isAlphanumeric(input)) {
|
|
21
21
|
return input;
|
|
22
|
+
}
|
|
22
23
|
context.fail(_this, `"{{value}}" is not an alphanumeric string`, input);
|
|
23
24
|
}, options);
|
|
24
25
|
}
|
|
@@ -5,7 +5,7 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isAscii
|
|
6
6
|
*/
|
|
7
7
|
export function isAscii(options) {
|
|
8
|
-
return validator('isAscii',
|
|
8
|
+
return validator('isAscii', (input, context, _this) => {
|
|
9
9
|
if (typeof input === 'string' && validatorJS.isAscii(input))
|
|
10
10
|
return input;
|
|
11
11
|
context.fail(_this, `Value is not an ascii string`, input);
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isBase64
|
|
6
6
|
*/
|
|
7
7
|
export function isBase64(options) {
|
|
8
|
-
return validator('isBase64',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isBase64(input, options))
|
|
8
|
+
return validator('isBase64', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isBase64(input, options)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `"{{value}}" is not a valid a Base64 string`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isBtcAddress
|
|
6
6
|
*/
|
|
7
7
|
export function isBtcAddress(options) {
|
|
8
|
-
return validator('isBtcAddress',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isBtcAddress(input))
|
|
8
|
+
return validator('isBtcAddress', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isBtcAddress(input)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `Value is not a valid a BTC address`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -5,9 +5,11 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isCreditCard
|
|
6
6
|
*/
|
|
7
7
|
export function isCreditCard(options) {
|
|
8
|
-
return validator('isCreditCard',
|
|
9
|
-
if (typeof input === 'string' &&
|
|
8
|
+
return validator('isCreditCard', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' &&
|
|
10
|
+
validatorJS.isCreditCard(input, options)) {
|
|
10
11
|
return input;
|
|
12
|
+
}
|
|
11
13
|
context.fail(_this, `"{{value}}" is not a valid Credit Card number`, input);
|
|
12
14
|
}, options);
|
|
13
15
|
}
|
|
@@ -6,9 +6,10 @@ import { validator } from '../../core/index.js';
|
|
|
6
6
|
* @validator isDecimal
|
|
7
7
|
*/
|
|
8
8
|
export function isDecimal(options) {
|
|
9
|
-
return validator('isDecimal',
|
|
10
|
-
if (typeof input === 'string' && validatorJS.isDecimal(input))
|
|
9
|
+
return validator('isDecimal', (input, context, _this) => {
|
|
10
|
+
if (typeof input === 'string' && validatorJS.isDecimal(input)) {
|
|
11
11
|
return input;
|
|
12
|
+
}
|
|
12
13
|
context.fail(_this, `"{{value}}" is not an decimal number string`, input);
|
|
13
14
|
}, options);
|
|
14
15
|
}
|
|
@@ -5,7 +5,7 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isEAN
|
|
6
6
|
*/
|
|
7
7
|
export function isEAN(options) {
|
|
8
|
-
return validator('isEAN',
|
|
8
|
+
return validator('isEAN', (input, context, _this) => {
|
|
9
9
|
if (typeof input === 'string' && validatorJS.isEAN(input))
|
|
10
10
|
return input;
|
|
11
11
|
context.fail(_this, `"{{value}}" is not a valid EAN (European Article Number)`, input);
|
|
@@ -15,8 +15,9 @@ export function isEmail(options) {
|
|
|
15
15
|
// eslint-disable-next-line camelcase
|
|
16
16
|
allow_ip_domain: options?.allowIpDomain,
|
|
17
17
|
};
|
|
18
|
-
return validator('isEmail',
|
|
19
|
-
if (typeof input === 'string' &&
|
|
18
|
+
return validator('isEmail', (input, context, _this) => {
|
|
19
|
+
if (typeof input === 'string' &&
|
|
20
|
+
validatorJS.isEmail(input, emailOptions)) {
|
|
20
21
|
if (options?.requireDisplayName) {
|
|
21
22
|
if (!validatorJS.isEmail(input, {
|
|
22
23
|
...emailOptions,
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isETHAddress
|
|
6
6
|
*/
|
|
7
7
|
export function isETHAddress(options) {
|
|
8
|
-
return validator('isETHAddress',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isEthereumAddress(input))
|
|
8
|
+
return validator('isETHAddress', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isEthereumAddress(input)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `Value is not a valid a ETH (Ethereum) address`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -9,9 +9,10 @@ export function isFQDN(options) {
|
|
|
9
9
|
// eslint-disable-next-line camelcase
|
|
10
10
|
allow_wildcard: options?.allowWildcard,
|
|
11
11
|
};
|
|
12
|
-
return validator('isFQDN',
|
|
13
|
-
if (typeof input === 'string' && validatorJS.isFQDN(input, opts))
|
|
12
|
+
return validator('isFQDN', (input, context, _this) => {
|
|
13
|
+
if (typeof input === 'string' && validatorJS.isFQDN(input, opts)) {
|
|
14
14
|
return input;
|
|
15
|
+
}
|
|
15
16
|
context.fail(_this, `"{{value}}" is not valid FQDN`, input);
|
|
16
17
|
}, options);
|
|
17
18
|
}
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isHash
|
|
6
6
|
*/
|
|
7
7
|
export function isHash(algorithm, options) {
|
|
8
|
-
return validator('isHash',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isHash(input, algorithm))
|
|
8
|
+
return validator('isHash', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isHash(input, algorithm)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `Value is not a valid ${algorithm} hash`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isHexColor
|
|
6
6
|
*/
|
|
7
7
|
export function isHexColor(options) {
|
|
8
|
-
return validator('isHexColor',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isHexColor(input))
|
|
8
|
+
return validator('isHexColor', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isHexColor(input)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `{{label}} is not a valid Hex Color`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isHex
|
|
6
6
|
*/
|
|
7
7
|
export function isHex(options) {
|
|
8
|
-
return validator('isHex',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isHexadecimal(input))
|
|
8
|
+
return validator('isHex', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isHexadecimal(input)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `{{label}} is not an hexadecimal string`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -5,7 +5,7 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isIBAN
|
|
6
6
|
*/
|
|
7
7
|
export function isIBAN(options) {
|
|
8
|
-
return validator('isIBAN',
|
|
8
|
+
return validator('isIBAN', (input, context, _this) => {
|
|
9
9
|
if (typeof input === 'string' && validatorJS.isIBAN(input))
|
|
10
10
|
return input;
|
|
11
11
|
context.fail(_this, `{{label}} is not a valid IBAN (International Bank Account Number)`, input);
|
|
@@ -16,7 +16,7 @@ export function isIBAN(options) {
|
|
|
16
16
|
* @validator isSWIFT
|
|
17
17
|
*/
|
|
18
18
|
export function isSWIFT(options) {
|
|
19
|
-
return validator('isSWIFT',
|
|
19
|
+
return validator('isSWIFT', (input, context, _this) => {
|
|
20
20
|
if (typeof input === 'string' && validatorJS.isBIC(input))
|
|
21
21
|
return input;
|
|
22
22
|
context.fail(_this, `{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`, input);
|
|
@@ -5,9 +5,12 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isIP
|
|
6
6
|
*/
|
|
7
7
|
export function isIP(version, options) {
|
|
8
|
-
return validator('isIP',
|
|
9
|
-
if (input != null &&
|
|
8
|
+
return validator('isIP', (input, context, _this) => {
|
|
9
|
+
if (input != null &&
|
|
10
|
+
typeof input === 'string' &&
|
|
11
|
+
validatorJS.isIP(input, version)) {
|
|
10
12
|
return input;
|
|
13
|
+
}
|
|
11
14
|
context.fail(_this, `"{{value}}" is not a valid IP${version ? ' v' + version : ''}`, input);
|
|
12
15
|
}, options);
|
|
13
16
|
}
|
|
@@ -16,9 +19,12 @@ export function isIP(version, options) {
|
|
|
16
19
|
* @validator isIPRange
|
|
17
20
|
*/
|
|
18
21
|
export function isIPRange(version, options) {
|
|
19
|
-
return validator('isIPRange',
|
|
20
|
-
if (input != null &&
|
|
22
|
+
return validator('isIPRange', (input, context, _this) => {
|
|
23
|
+
if (input != null &&
|
|
24
|
+
typeof input === 'string' &&
|
|
25
|
+
validatorJS.isIPRange(input, version)) {
|
|
21
26
|
return input;
|
|
27
|
+
}
|
|
22
28
|
context.fail(_this, `"{{value}}" is not a valid IP${version ? ' v' + version : ''} range`, input);
|
|
23
29
|
}, options);
|
|
24
30
|
}
|
|
@@ -9,9 +9,10 @@ export function isISSN(options) {
|
|
|
9
9
|
// eslint-disable-next-line camelcase
|
|
10
10
|
case_sensitive: options?.caseSensitive,
|
|
11
11
|
};
|
|
12
|
-
return validator('isISSN',
|
|
13
|
-
if (typeof input === 'string' && validatorJS.isISSN(input, opts))
|
|
12
|
+
return validator('isISSN', (input, context, _this) => {
|
|
13
|
+
if (typeof input === 'string' && validatorJS.isISSN(input, opts)) {
|
|
14
14
|
return input;
|
|
15
|
+
}
|
|
15
16
|
context.fail(_this, `"{{value}}" is not a valid ISSN`, input);
|
|
16
17
|
}, options);
|
|
17
18
|
}
|
|
@@ -5,7 +5,7 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isJWT
|
|
6
6
|
*/
|
|
7
7
|
export function isJWT(options) {
|
|
8
|
-
return validator('isJWT',
|
|
8
|
+
return validator('isJWT', (input, context, _this) => {
|
|
9
9
|
if (typeof input === 'string' && validatorJS.isJWT(input))
|
|
10
10
|
return input;
|
|
11
11
|
context.fail(_this, `Value is not a valid JWT token`, input);
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isLowercase
|
|
6
6
|
*/
|
|
7
7
|
export function isLowercase(options) {
|
|
8
|
-
return validator('isLowercase',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isLowercase(input))
|
|
8
|
+
return validator('isLowercase', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isLowercase(input)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `"{{value}}" is not a lowercase string`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -16,9 +17,10 @@ export function isLowercase(options) {
|
|
|
16
17
|
* @validator isUppercase
|
|
17
18
|
*/
|
|
18
19
|
export function isUppercase(options) {
|
|
19
|
-
return validator('isUppercase',
|
|
20
|
-
if (typeof input === 'string' && validatorJS.isUppercase(input))
|
|
20
|
+
return validator('isUppercase', (input, context, _this) => {
|
|
21
|
+
if (typeof input === 'string' && validatorJS.isUppercase(input)) {
|
|
21
22
|
return input;
|
|
23
|
+
}
|
|
22
24
|
context.fail(_this, `"{{value}}" is not an uppercase string`, input);
|
|
23
25
|
}, options);
|
|
24
26
|
}
|
|
@@ -10,9 +10,12 @@ export function isMACAddress(options) {
|
|
|
10
10
|
no_separators: options?.noSeparators,
|
|
11
11
|
eui: options?.eui,
|
|
12
12
|
};
|
|
13
|
-
return validator('isMACAddress',
|
|
14
|
-
if (input != null &&
|
|
13
|
+
return validator('isMACAddress', (input, context, _this) => {
|
|
14
|
+
if (input != null &&
|
|
15
|
+
typeof input === 'string' &&
|
|
16
|
+
validatorJS.isMACAddress(input, opts)) {
|
|
15
17
|
return input;
|
|
18
|
+
}
|
|
16
19
|
context.fail(_this, `{{label}} is not a valid MAC address`, input);
|
|
17
20
|
}, options);
|
|
18
21
|
}
|
|
@@ -8,9 +8,11 @@ export function isMobilePhone(options) {
|
|
|
8
8
|
const opts = {
|
|
9
9
|
strictMode: options?.strictMode,
|
|
10
10
|
};
|
|
11
|
-
return validator('isMobilePhone',
|
|
12
|
-
if (typeof input === 'string' &&
|
|
11
|
+
return validator('isMobilePhone', (input, context, _this) => {
|
|
12
|
+
if (typeof input === 'string' &&
|
|
13
|
+
validatorJS.isMobilePhone(input, options?.locale, opts)) {
|
|
13
14
|
return input;
|
|
15
|
+
}
|
|
14
16
|
context.fail(_this, `"{{value}}" is not a valid a Mobile Phone Number`, input);
|
|
15
17
|
}, options);
|
|
16
18
|
}
|
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import validatorJS from 'validator';
|
|
2
|
-
import { validator } from '../../core/index.js';
|
|
2
|
+
import { validator, } from '../../core/index.js';
|
|
3
3
|
/**
|
|
4
4
|
* Validates if value is an "ObjectId".
|
|
5
5
|
* @validator isObjectId
|
|
6
6
|
*/
|
|
7
7
|
export function isObjectId(options) {
|
|
8
|
-
return validator('isObjectId',
|
|
8
|
+
return validator('isObjectId', (input, context, _this) => {
|
|
9
9
|
if (input != null &&
|
|
10
10
|
(_isObjectIdValue(input) ||
|
|
11
11
|
(typeof input === 'object' &&
|
|
12
12
|
typeof input.toHexString === 'function' &&
|
|
13
|
-
_isObjectIdValue(input.toHexString()))))
|
|
13
|
+
_isObjectIdValue(input.toHexString())))) {
|
|
14
14
|
return input;
|
|
15
|
+
}
|
|
15
16
|
context.fail(_this, `"{{value}}" is not a valid ObjectId`, input);
|
|
16
17
|
}, options);
|
|
17
18
|
}
|
|
18
19
|
function _isObjectIdValue(input) {
|
|
19
20
|
return ((input instanceof Uint8Array && input.length === 12) ||
|
|
20
|
-
(typeof input === 'string' &&
|
|
21
|
+
(typeof input === 'string' &&
|
|
22
|
+
input.length === 24 &&
|
|
23
|
+
validatorJS.isHexadecimal(input)));
|
|
21
24
|
}
|
|
@@ -5,9 +5,11 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isPassportNumber
|
|
6
6
|
*/
|
|
7
7
|
export function isPassportNumber(countryCode, options) {
|
|
8
|
-
return validator('isPassportNumber',
|
|
9
|
-
if (typeof input === 'string' &&
|
|
8
|
+
return validator('isPassportNumber', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' &&
|
|
10
|
+
validatorJS.isPassportNumber(input, countryCode)) {
|
|
10
11
|
return input;
|
|
12
|
+
}
|
|
11
13
|
context.fail(_this, `"{{value}}" is not a valid ${countryCode} PassportNumber)`, input);
|
|
12
14
|
}, options);
|
|
13
15
|
}
|
|
@@ -5,9 +5,12 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isPort
|
|
6
6
|
*/
|
|
7
7
|
export function isPort(options) {
|
|
8
|
-
return validator('isPort',
|
|
9
|
-
if (input != null &&
|
|
8
|
+
return validator('isPort', (input, context, _this) => {
|
|
9
|
+
if (input != null &&
|
|
10
|
+
typeof input === 'string' &&
|
|
11
|
+
validatorJS.isPort(input)) {
|
|
10
12
|
return input;
|
|
13
|
+
}
|
|
11
14
|
context.fail(_this, `{{label}} is not a valid port number`, input);
|
|
12
15
|
}, options);
|
|
13
16
|
}
|
|
@@ -5,9 +5,12 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isURL
|
|
6
6
|
*/
|
|
7
7
|
export function isURL(options) {
|
|
8
|
-
return validator('isURL',
|
|
9
|
-
if (input != null &&
|
|
8
|
+
return validator('isURL', (input, context, _this) => {
|
|
9
|
+
if (input != null &&
|
|
10
|
+
typeof input === 'string' &&
|
|
11
|
+
validatorJS.isURL(input, options)) {
|
|
10
12
|
return input;
|
|
13
|
+
}
|
|
11
14
|
context.fail(_this, `{{value}} is not a valid URL`, input);
|
|
12
15
|
}, options);
|
|
13
16
|
}
|
|
@@ -5,9 +5,12 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isUUID
|
|
6
6
|
*/
|
|
7
7
|
export function isUUID(version, options) {
|
|
8
|
-
return validator('isUUID',
|
|
9
|
-
if (input != null &&
|
|
8
|
+
return validator('isUUID', (input, context, _this) => {
|
|
9
|
+
if (input != null &&
|
|
10
|
+
typeof input === 'string' &&
|
|
11
|
+
validatorJS.isUUID(input, version)) {
|
|
10
12
|
return input;
|
|
13
|
+
}
|
|
11
14
|
context.fail(_this, `{{label}} is not a valid UUID${version ? ' v' + version : ''}`, input);
|
|
12
15
|
}, options);
|
|
13
16
|
}
|
|
@@ -5,9 +5,10 @@ import { validator } from '../../core/index.js';
|
|
|
5
5
|
* @validator isVAT
|
|
6
6
|
*/
|
|
7
7
|
export function isVATNumber(countryCode, options) {
|
|
8
|
-
return validator('isVATNumber',
|
|
9
|
-
if (typeof input === 'string' && validatorJS.isVAT(input, countryCode))
|
|
8
|
+
return validator('isVATNumber', (input, context, _this) => {
|
|
9
|
+
if (typeof input === 'string' && validatorJS.isVAT(input, countryCode)) {
|
|
10
10
|
return input;
|
|
11
|
+
}
|
|
11
12
|
context.fail(_this, `"{{value}}" is not a valid VAT number`, input);
|
|
12
13
|
}, options);
|
|
13
14
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { validator } from '../../core/index.js';
|
|
1
|
+
import { validator, } from '../../core/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Coerces given value to "UUID" format or returns undefined if nullish
|
|
4
4
|
* @validator uuid
|
|
@@ -6,7 +6,7 @@ import { validator } from '../../core/index.js';
|
|
|
6
6
|
export function matches(format, options) {
|
|
7
7
|
const regExp = format instanceof RegExp ? format : new RegExp(format);
|
|
8
8
|
const formatName = options?.formatName;
|
|
9
|
-
return validator('matches',
|
|
9
|
+
return validator('matches', (input, context, _this) => {
|
|
10
10
|
if (input == null)
|
|
11
11
|
return;
|
|
12
12
|
if (typeof input === 'string' && regExp.test(input))
|
package/esm/rules/index.js
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
export * from './type-rules/is-any.js';
|
|
2
|
-
export * from './type-rules/is-array.js';
|
|
3
|
-
export * from './type-rules/is-bigint.js';
|
|
4
|
-
export * from './type-rules/is-boolean.js';
|
|
5
|
-
export * from './type-rules/is-date.js';
|
|
6
|
-
export * from './type-rules/is-enum.js';
|
|
7
|
-
export * from './type-rules/is-integer.js';
|
|
8
|
-
export * from './type-rules/is-null.js';
|
|
9
|
-
export * from './type-rules/is-number.js';
|
|
10
|
-
export * from './type-rules/is-object.js';
|
|
11
|
-
export * from './type-rules/is-record.js';
|
|
12
|
-
export * from './type-rules/is-string.js';
|
|
13
|
-
export * from './type-rules/is-tuple.js';
|
|
14
1
|
export * from './format-rules/is-alpha.js';
|
|
15
2
|
export * from './format-rules/is-ascii.js';
|
|
16
3
|
export * from './format-rules/is-base64.js';
|
|
@@ -41,6 +28,20 @@ export * from './format-rules/matches.js';
|
|
|
41
28
|
export * from './logical-rules/is-defined.js';
|
|
42
29
|
export * from './logical-rules/is-empty.js';
|
|
43
30
|
export * from './logical-rules/range.js';
|
|
31
|
+
export * from './type-rules/is-any.js';
|
|
32
|
+
export * from './type-rules/is-array.js';
|
|
33
|
+
export * from './type-rules/is-bigint.js';
|
|
34
|
+
export * from './type-rules/is-boolean.js';
|
|
35
|
+
export * from './type-rules/is-date.js';
|
|
36
|
+
export * from './type-rules/is-enum.js';
|
|
37
|
+
export * from './type-rules/is-integer.js';
|
|
38
|
+
export * from './type-rules/is-null.js';
|
|
39
|
+
export * from './type-rules/is-number.js';
|
|
40
|
+
export * from './type-rules/is-object.js';
|
|
41
|
+
export * from './type-rules/is-record.js';
|
|
42
|
+
export * from './type-rules/is-string.js';
|
|
43
|
+
export * from './type-rules/is-tuple.js';
|
|
44
|
+
export * from './type-rules/is-undefined.js';
|
|
44
45
|
export * from './utility-rules/exists.js';
|
|
45
46
|
export * from './utility-rules/get-length.js';
|
|
46
47
|
export * from './utility-rules/optional.js';
|