valgen 5.3.2 → 5.4.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 +15 -0
- package/cjs/index.js +3 -1
- package/cjs/rules/logical-rules/range.js +16 -6
- package/cjs/rules/type-rules/is-array.js +8 -7
- package/cjs/rules/type-rules/is-boolean.js +5 -4
- package/cjs/rules/type-rules/is-integer.js +9 -8
- package/cjs/rules/type-rules/is-number.js +9 -8
- package/cjs/rules/type-rules/is-object.js +11 -10
- package/cjs/rules/type-rules/is-string.js +10 -9
- package/cjs/rules/type-rules/is-tuple.js +6 -5
- package/cjs/rules/utility-rules/pipe.js +8 -4
- package/esm/index.js +2 -1
- package/esm/rules/logical-rules/range.js +16 -6
- package/esm/rules/type-rules/is-array.js +8 -7
- package/esm/rules/type-rules/is-boolean.js +5 -4
- package/esm/rules/type-rules/is-integer.js +9 -8
- package/esm/rules/type-rules/is-number.js +9 -8
- package/esm/rules/type-rules/is-object.js +11 -10
- package/esm/rules/type-rules/is-string.js +10 -9
- package/esm/rules/type-rules/is-tuple.js +6 -5
- package/esm/rules/utility-rules/pipe.js +8 -4
- package/package.json +1 -1
- package/typings/index.d.ts +2 -1
- package/typings/rules/utility-rules/pipe.d.ts +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
# v5.4.1
|
|
2
|
+
[2024-05-18]
|
|
3
|
+
|
|
4
|
+
### Changes
|
|
5
|
+
|
|
6
|
+
* Exposed toBigint ([`50ad810`](https://github.com/panates/valgen/commit/50ad8101d58619c92d134059564a4cbaa575f5de))
|
|
7
|
+
|
|
8
|
+
# v5.4.0
|
|
9
|
+
[2024-05-18]
|
|
10
|
+
|
|
11
|
+
### Changes
|
|
12
|
+
|
|
13
|
+
* Added returnIndex to pipe() ([`87458ad`](https://github.com/panates/valgen/commit/87458adb3eb6a4465c8198f942df1fbb333738ff))
|
|
14
|
+
* Updated circleci config ([`101361c`](https://github.com/panates/valgen/commit/101361c20f1b78a75392b9e796bc9075b492cb3e))
|
|
15
|
+
|
|
1
16
|
# v5.3.2
|
|
2
17
|
[2024-05-18]
|
|
3
18
|
|
package/cjs/index.js
CHANGED
|
@@ -27,7 +27,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.toArray = exports.isHex = exports.isDecimal = exports.isAscii = exports.isAlphanumeric = exports.isAlpha = exports.isUppercase = exports.isLowercase = exports.isJWT = exports.isHexColor = exports.isETHAddress = exports.isBtcAddress = exports.isISSN = exports.isFQDN = exports.isEAN = exports.isIBAN = exports.isCreditCard = exports.isSWIFT = exports.isBase64 = exports.isURL = exports.isPort = exports.isMACAddress = exports.isIPRange = exports.isIP = exports.isMobilePhone = exports.isEmail = exports.isUUID5 = exports.isUUID4 = exports.isUUID3 = exports.isUUID2 = exports.isUUID1 = exports.isUUID = exports.isString = exports.isObjectId = exports.isObject = exports.isNumber = exports.isUndefined = exports.isDefined = exports.isNullish = exports.isNull = exports.isInteger = exports.isNotEmpty = exports.isEmpty = exports.isDateString = exports.isDate = exports.isBoolean = exports.isBigint = exports.isArray = exports.isAny = exports.vg = void 0;
|
|
30
|
-
exports.toString = exports.toNumber = exports.toInteger = exports.toDateString = exports.toDate = exports.toBoolean = void 0;
|
|
30
|
+
exports.toString = exports.toNumber = exports.toInteger = exports.toDateString = exports.toDate = exports.toBigint = exports.toBoolean = void 0;
|
|
31
31
|
const vg = __importStar(require("./rules/index.js"));
|
|
32
32
|
exports.vg = vg;
|
|
33
33
|
__exportStar(require("./constants.js"), exports);
|
|
@@ -134,6 +134,8 @@ const toBoolean = vg.isBoolean({ coerce: true });
|
|
|
134
134
|
exports.toBoolean = toBoolean;
|
|
135
135
|
const toDate = vg.isDate({ coerce: true });
|
|
136
136
|
exports.toDate = toDate;
|
|
137
|
+
const toBigint = vg.isBigint({ coerce: true });
|
|
138
|
+
exports.toBigint = toBigint;
|
|
137
139
|
const toDateString = vg.isDateString({ coerce: true });
|
|
138
140
|
exports.toDateString = toDateString;
|
|
139
141
|
const toInteger = vg.isInteger({ coerce: true });
|
|
@@ -100,15 +100,25 @@ exports.isLte = isLte;
|
|
|
100
100
|
* Checks the length is at least "minValue"
|
|
101
101
|
* @validator lengthMin
|
|
102
102
|
*/
|
|
103
|
-
const lengthMin = (minValue) => (0, pipe_js_1.allOf)(
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
const lengthMin = (minValue) => (0, pipe_js_1.allOf)([
|
|
104
|
+
(0, pipe_js_1.pipe)([
|
|
105
|
+
(0, get_length_js_1.getLength)(),
|
|
106
|
+
isGte(minValue, {
|
|
107
|
+
onFail: () => `The length of {{label}} must be at least ${minValue}`,
|
|
108
|
+
}),
|
|
109
|
+
]),
|
|
110
|
+
]);
|
|
106
111
|
exports.lengthMin = lengthMin;
|
|
107
112
|
/**
|
|
108
113
|
* Checks if the length is at most "maxValue"
|
|
109
114
|
* @validator lengthMax
|
|
110
115
|
*/
|
|
111
|
-
const lengthMax = (maxValue) => (0, pipe_js_1.allOf)(
|
|
112
|
-
|
|
113
|
-
|
|
116
|
+
const lengthMax = (maxValue) => (0, pipe_js_1.allOf)([
|
|
117
|
+
(0, pipe_js_1.pipe)([
|
|
118
|
+
(0, get_length_js_1.getLength)(),
|
|
119
|
+
isLte(maxValue, {
|
|
120
|
+
onFail: () => `The length of {{label}} must be at most ${maxValue}`,
|
|
121
|
+
}),
|
|
122
|
+
]),
|
|
123
|
+
]);
|
|
114
124
|
exports.lengthMax = lengthMax;
|
|
@@ -10,23 +10,24 @@ const index_js_1 = require("../../core/index.js");
|
|
|
10
10
|
function isArray(itemValidator, options) {
|
|
11
11
|
return (0, index_js_1.validator)('isArray', function (input, context, _this) {
|
|
12
12
|
const coerce = options?.coerce || context.coerce;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
let output = input;
|
|
14
|
+
if (output != null && coerce && !Array.isArray(output))
|
|
15
|
+
output = [output];
|
|
16
|
+
if (!Array.isArray(output)) {
|
|
16
17
|
context.fail(_this, `"{{value}}" is not an array value`, input);
|
|
17
18
|
return;
|
|
18
19
|
}
|
|
19
20
|
if (!itemValidator)
|
|
20
|
-
return
|
|
21
|
+
return output;
|
|
21
22
|
// const location = context.location || '';
|
|
22
23
|
const itemContext = context.extend();
|
|
23
24
|
let i;
|
|
24
25
|
let v;
|
|
25
|
-
const l =
|
|
26
|
+
const l = output.length;
|
|
26
27
|
const out = [];
|
|
27
28
|
for (i = 0; i < l; i++) {
|
|
28
|
-
v =
|
|
29
|
-
itemContext.scope =
|
|
29
|
+
v = output[i];
|
|
30
|
+
itemContext.scope = output;
|
|
30
31
|
// itemContext.location = location + '[' + i + ']';
|
|
31
32
|
itemContext.location = context.location ? context.location + `[${i}]` : `<Array>[${i}]`;
|
|
32
33
|
itemContext.index = i;
|
|
@@ -12,7 +12,8 @@ const FALSE_PATTERN = /^false|f|0|no|n$/i;
|
|
|
12
12
|
function isBoolean(options) {
|
|
13
13
|
return (0, index_js_1.validator)('isBoolean', function (input, context, _this) {
|
|
14
14
|
const coerce = options?.coerce || context.coerce;
|
|
15
|
-
|
|
15
|
+
let output = input;
|
|
16
|
+
if (output != null && typeof output !== 'boolean' && coerce) {
|
|
16
17
|
if (typeof input === 'string') {
|
|
17
18
|
if (TRUE_PATTERN.test(input))
|
|
18
19
|
return true;
|
|
@@ -21,10 +22,10 @@ function isBoolean(options) {
|
|
|
21
22
|
throw new TypeError(`Invalid boolean string`);
|
|
22
23
|
}
|
|
23
24
|
if (input === 1 || input === 0)
|
|
24
|
-
|
|
25
|
+
output = !!input;
|
|
25
26
|
}
|
|
26
|
-
if (typeof
|
|
27
|
-
return
|
|
27
|
+
if (typeof output === 'boolean')
|
|
28
|
+
return output;
|
|
28
29
|
const t = typeof input === 'string' ? 'String ' : '';
|
|
29
30
|
context.fail(_this, `${t}"{{value}}" is not a valid boolean value`, input);
|
|
30
31
|
}, options);
|
|
@@ -10,17 +10,18 @@ const index_js_1 = require("../../core/index.js");
|
|
|
10
10
|
function isInteger(options) {
|
|
11
11
|
return (0, index_js_1.validator)('isInteger', function (input, context, _this) {
|
|
12
12
|
const coerce = options?.coerce || context.coerce;
|
|
13
|
-
|
|
13
|
+
let output = input;
|
|
14
|
+
if (output != null && typeof output !== 'number' && coerce) {
|
|
14
15
|
if (typeof input === 'string')
|
|
15
|
-
|
|
16
|
-
if (typeof input === 'bigint') {
|
|
17
|
-
|
|
18
|
-
if (input === BigInt(
|
|
19
|
-
input =
|
|
16
|
+
output = parseFloat(input);
|
|
17
|
+
else if (typeof input === 'bigint') {
|
|
18
|
+
output = Number(input);
|
|
19
|
+
if (input === BigInt(output))
|
|
20
|
+
input = output;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
if (typeof
|
|
23
|
-
return
|
|
23
|
+
if (typeof output === 'number' && !isNaN(output) && Number.isInteger(output))
|
|
24
|
+
return output;
|
|
24
25
|
const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
|
|
25
26
|
context.fail(_this, `${t}"{{value}}" is not a valid integer value`, input);
|
|
26
27
|
}, options);
|
|
@@ -10,17 +10,18 @@ const index_js_1 = require("../../core/index.js");
|
|
|
10
10
|
function isNumber(options) {
|
|
11
11
|
return (0, index_js_1.validator)('isNumber', function (input, context, _this) {
|
|
12
12
|
const coerce = options?.coerce || context.coerce;
|
|
13
|
-
|
|
13
|
+
let output = input;
|
|
14
|
+
if (output != null && typeof output !== 'number' && coerce) {
|
|
14
15
|
if (typeof input === 'string')
|
|
15
|
-
|
|
16
|
-
if (typeof input === 'bigint') {
|
|
17
|
-
|
|
18
|
-
if (input === BigInt(
|
|
19
|
-
return
|
|
16
|
+
output = parseFloat(input);
|
|
17
|
+
else if (typeof input === 'bigint') {
|
|
18
|
+
output = Number(input);
|
|
19
|
+
if (input === BigInt(output))
|
|
20
|
+
return output;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
if (typeof
|
|
23
|
-
return
|
|
23
|
+
if (typeof output === 'number' && !isNaN(output))
|
|
24
|
+
return output;
|
|
24
25
|
const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
|
|
25
26
|
context.fail(_this, `${t}"{{value}}" is not a valid number value`, input);
|
|
26
27
|
}, options);
|
|
@@ -35,16 +35,17 @@ function isObject(schema, options) {
|
|
|
35
35
|
throw new TypeError(`Invalid definition in validation schema (${k})`);
|
|
36
36
|
}
|
|
37
37
|
const _rule = (0, index_js_1.validator)('isObject', function (input, context, _this) {
|
|
38
|
+
let output = input;
|
|
38
39
|
if (ctor && ctor[constants_js_1.preValidation])
|
|
39
|
-
|
|
40
|
+
output = ctor[constants_js_1.preValidation](output, context, _this);
|
|
40
41
|
const coerce = options?.coerce || context.coerce;
|
|
41
|
-
if (typeof
|
|
42
|
-
|
|
43
|
-
if (!(
|
|
42
|
+
if (typeof output === 'string' && coerce)
|
|
43
|
+
output = JSON.parse(output);
|
|
44
|
+
if (!(output && typeof output === 'object')) {
|
|
44
45
|
context.fail(_this, `{{label}} must be an object`, input);
|
|
45
46
|
return;
|
|
46
47
|
}
|
|
47
|
-
const keys = [...Object.keys(
|
|
48
|
+
const keys = [...Object.keys(output), ...schemaKeys];
|
|
48
49
|
const l = keys.length;
|
|
49
50
|
let i = 0;
|
|
50
51
|
let inputKey;
|
|
@@ -56,9 +57,9 @@ function isObject(schema, options) {
|
|
|
56
57
|
Object.setPrototypeOf(out, ctor.prototype);
|
|
57
58
|
if (detectCircular) {
|
|
58
59
|
context.circMap = context.circMap || new Map();
|
|
59
|
-
if (context.circMap.has(
|
|
60
|
-
return context.circMap.get(
|
|
61
|
-
context.circMap.set(
|
|
60
|
+
if (context.circMap.has(output))
|
|
61
|
+
return context.circMap.get(output);
|
|
62
|
+
context.circMap.set(output, out);
|
|
62
63
|
}
|
|
63
64
|
if (context.root == null)
|
|
64
65
|
context.root = context.root || ctorName || '';
|
|
@@ -68,14 +69,14 @@ function isObject(schema, options) {
|
|
|
68
69
|
for (i = 0; i < l; i++) {
|
|
69
70
|
inputKey = keys[i];
|
|
70
71
|
schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
|
|
71
|
-
v =
|
|
72
|
+
v = output[inputKey];
|
|
72
73
|
_propRule = propertyRules[schemaKey] || ((0, index_js_1.isValidator)(additionalFields) ? additionalFields : undefined);
|
|
73
74
|
if (_propRule) {
|
|
74
75
|
if (processedSchemaKeys[schemaKey])
|
|
75
76
|
continue;
|
|
76
77
|
processedSchemaKeys[schemaKey] = true;
|
|
77
78
|
const subCtx = context.extend();
|
|
78
|
-
subCtx.scope =
|
|
79
|
+
subCtx.scope = output;
|
|
79
80
|
subCtx.context = ctorName;
|
|
80
81
|
subCtx.location = location + (location ? '.' : '') + schemaKey;
|
|
81
82
|
subCtx.property = schemaKey;
|
|
@@ -10,19 +10,20 @@ const index_js_1 = require("../../core/index.js");
|
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
let output = input;
|
|
14
|
+
if (output != null && typeof output !== 'string' && coerce) {
|
|
15
|
+
if (typeof output === 'object') {
|
|
16
|
+
if (typeof output.toJSON === 'function')
|
|
17
|
+
output = output.toJSON();
|
|
17
18
|
else
|
|
18
|
-
|
|
19
|
+
output = JSON.stringify(output);
|
|
19
20
|
}
|
|
20
21
|
else
|
|
21
|
-
|
|
22
|
+
output = String(output);
|
|
22
23
|
}
|
|
23
|
-
if (typeof
|
|
24
|
-
return
|
|
25
|
-
context.fail(_this, `{{
|
|
24
|
+
if (typeof output === 'string')
|
|
25
|
+
return output;
|
|
26
|
+
context.fail(_this, `"{{value}}" is not a string`, input);
|
|
26
27
|
}, options);
|
|
27
28
|
}
|
|
28
29
|
exports.isString = isString;
|
|
@@ -5,8 +5,9 @@ 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
|
-
|
|
9
|
-
|
|
8
|
+
let output = input;
|
|
9
|
+
if (output != null && coerce && !Array.isArray(output))
|
|
10
|
+
output = [output];
|
|
10
11
|
if (!Array.isArray(input)) {
|
|
11
12
|
context.fail(_this, `"{{value}}" is not a valid tuple`, input);
|
|
12
13
|
return;
|
|
@@ -15,18 +16,18 @@ function isTuple(items, options) {
|
|
|
15
16
|
let itemRule;
|
|
16
17
|
let i;
|
|
17
18
|
let v;
|
|
18
|
-
const l =
|
|
19
|
+
const l = output.length;
|
|
19
20
|
const out = [];
|
|
20
21
|
const nl = items.length;
|
|
21
22
|
const itemContext = context.extend();
|
|
22
23
|
for (i = 0; i < l && i < nl; i++) {
|
|
23
24
|
itemRule = items[i];
|
|
24
|
-
itemContext.scope =
|
|
25
|
+
itemContext.scope = output;
|
|
25
26
|
itemContext.label = context.label ? context.label + `[${i}]` : undefined;
|
|
26
27
|
itemContext.index = i;
|
|
27
28
|
itemContext.location = location + '[' + i + ']';
|
|
28
29
|
itemContext.label = (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
|
|
29
|
-
v = itemRule(
|
|
30
|
+
v = itemRule(output[i], itemContext);
|
|
30
31
|
out.push(v);
|
|
31
32
|
}
|
|
32
33
|
return context.errors.length ? undefined : out;
|
|
@@ -6,27 +6,31 @@ const index_js_1 = require("../../core/index.js");
|
|
|
6
6
|
*
|
|
7
7
|
* @validator pipe
|
|
8
8
|
*/
|
|
9
|
-
function pipe(
|
|
9
|
+
function pipe(rules, options) {
|
|
10
10
|
const l = rules.length;
|
|
11
|
+
const returnIndex = options?.returnIndex;
|
|
11
12
|
return (0, index_js_1.validator)('pipe', function (input, context) {
|
|
12
13
|
let i;
|
|
13
14
|
let c;
|
|
14
15
|
let v = input;
|
|
16
|
+
let returnValue;
|
|
15
17
|
for (i = 0; i < l; i++) {
|
|
16
18
|
c = rules[i];
|
|
17
19
|
v = c(v, context);
|
|
20
|
+
if (returnIndex == null || i <= returnIndex)
|
|
21
|
+
returnValue = v;
|
|
18
22
|
if (context.errors.length)
|
|
19
23
|
return;
|
|
20
24
|
}
|
|
21
|
-
return
|
|
22
|
-
});
|
|
25
|
+
return returnValue;
|
|
26
|
+
}, options);
|
|
23
27
|
}
|
|
24
28
|
exports.pipe = pipe;
|
|
25
29
|
/**
|
|
26
30
|
* Test given value against to all codecs and returns original input
|
|
27
31
|
* @validator allOf
|
|
28
32
|
*/
|
|
29
|
-
function allOf(
|
|
33
|
+
function allOf(rules) {
|
|
30
34
|
return (0, index_js_1.validator)('allOf', function (input, context) {
|
|
31
35
|
let i;
|
|
32
36
|
let c;
|
package/esm/index.js
CHANGED
|
@@ -52,8 +52,9 @@ const isHex = vg.isHex();
|
|
|
52
52
|
const toArray = vg.isArray(isAny, { coerce: true });
|
|
53
53
|
const toBoolean = vg.isBoolean({ coerce: true });
|
|
54
54
|
const toDate = vg.isDate({ coerce: true });
|
|
55
|
+
const toBigint = vg.isBigint({ coerce: true });
|
|
55
56
|
const toDateString = vg.isDateString({ coerce: true });
|
|
56
57
|
const toInteger = vg.isInteger({ coerce: true });
|
|
57
58
|
const toNumber = vg.isNumber({ coerce: true });
|
|
58
59
|
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, };
|
|
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, toBigint, toDate, toDateString, toInteger, toNumber, toString, };
|
|
@@ -92,13 +92,23 @@ export function isLte(maxValue, options) {
|
|
|
92
92
|
* Checks the length is at least "minValue"
|
|
93
93
|
* @validator lengthMin
|
|
94
94
|
*/
|
|
95
|
-
export const lengthMin = (minValue) => allOf(
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
export const lengthMin = (minValue) => allOf([
|
|
96
|
+
pipe([
|
|
97
|
+
getLength(),
|
|
98
|
+
isGte(minValue, {
|
|
99
|
+
onFail: () => `The length of {{label}} must be at least ${minValue}`,
|
|
100
|
+
}),
|
|
101
|
+
]),
|
|
102
|
+
]);
|
|
98
103
|
/**
|
|
99
104
|
* Checks if the length is at most "maxValue"
|
|
100
105
|
* @validator lengthMax
|
|
101
106
|
*/
|
|
102
|
-
export const lengthMax = (maxValue) => allOf(
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
export const lengthMax = (maxValue) => allOf([
|
|
108
|
+
pipe([
|
|
109
|
+
getLength(),
|
|
110
|
+
isLte(maxValue, {
|
|
111
|
+
onFail: () => `The length of {{label}} must be at most ${maxValue}`,
|
|
112
|
+
}),
|
|
113
|
+
]),
|
|
114
|
+
]);
|
|
@@ -7,23 +7,24 @@ import { validator } from '../../core/index.js';
|
|
|
7
7
|
export function isArray(itemValidator, options) {
|
|
8
8
|
return validator('isArray', function (input, context, _this) {
|
|
9
9
|
const coerce = options?.coerce || context.coerce;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
let output = input;
|
|
11
|
+
if (output != null && coerce && !Array.isArray(output))
|
|
12
|
+
output = [output];
|
|
13
|
+
if (!Array.isArray(output)) {
|
|
13
14
|
context.fail(_this, `"{{value}}" is not an array value`, input);
|
|
14
15
|
return;
|
|
15
16
|
}
|
|
16
17
|
if (!itemValidator)
|
|
17
|
-
return
|
|
18
|
+
return output;
|
|
18
19
|
// const location = context.location || '';
|
|
19
20
|
const itemContext = context.extend();
|
|
20
21
|
let i;
|
|
21
22
|
let v;
|
|
22
|
-
const l =
|
|
23
|
+
const l = output.length;
|
|
23
24
|
const out = [];
|
|
24
25
|
for (i = 0; i < l; i++) {
|
|
25
|
-
v =
|
|
26
|
-
itemContext.scope =
|
|
26
|
+
v = output[i];
|
|
27
|
+
itemContext.scope = output;
|
|
27
28
|
// itemContext.location = location + '[' + i + ']';
|
|
28
29
|
itemContext.location = context.location ? context.location + `[${i}]` : `<Array>[${i}]`;
|
|
29
30
|
itemContext.index = i;
|
|
@@ -9,7 +9,8 @@ const FALSE_PATTERN = /^false|f|0|no|n$/i;
|
|
|
9
9
|
export function isBoolean(options) {
|
|
10
10
|
return validator('isBoolean', function (input, context, _this) {
|
|
11
11
|
const coerce = options?.coerce || context.coerce;
|
|
12
|
-
|
|
12
|
+
let output = input;
|
|
13
|
+
if (output != null && typeof output !== 'boolean' && coerce) {
|
|
13
14
|
if (typeof input === 'string') {
|
|
14
15
|
if (TRUE_PATTERN.test(input))
|
|
15
16
|
return true;
|
|
@@ -18,10 +19,10 @@ export function isBoolean(options) {
|
|
|
18
19
|
throw new TypeError(`Invalid boolean string`);
|
|
19
20
|
}
|
|
20
21
|
if (input === 1 || input === 0)
|
|
21
|
-
|
|
22
|
+
output = !!input;
|
|
22
23
|
}
|
|
23
|
-
if (typeof
|
|
24
|
-
return
|
|
24
|
+
if (typeof output === 'boolean')
|
|
25
|
+
return output;
|
|
25
26
|
const t = typeof input === 'string' ? 'String ' : '';
|
|
26
27
|
context.fail(_this, `${t}"{{value}}" is not a valid boolean value`, input);
|
|
27
28
|
}, options);
|
|
@@ -7,17 +7,18 @@ import { validator } from '../../core/index.js';
|
|
|
7
7
|
export function isInteger(options) {
|
|
8
8
|
return validator('isInteger', function (input, context, _this) {
|
|
9
9
|
const coerce = options?.coerce || context.coerce;
|
|
10
|
-
|
|
10
|
+
let output = input;
|
|
11
|
+
if (output != null && typeof output !== 'number' && coerce) {
|
|
11
12
|
if (typeof input === 'string')
|
|
12
|
-
|
|
13
|
-
if (typeof input === 'bigint') {
|
|
14
|
-
|
|
15
|
-
if (input === BigInt(
|
|
16
|
-
input =
|
|
13
|
+
output = parseFloat(input);
|
|
14
|
+
else if (typeof input === 'bigint') {
|
|
15
|
+
output = Number(input);
|
|
16
|
+
if (input === BigInt(output))
|
|
17
|
+
input = output;
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
|
-
if (typeof
|
|
20
|
-
return
|
|
20
|
+
if (typeof output === 'number' && !isNaN(output) && Number.isInteger(output))
|
|
21
|
+
return output;
|
|
21
22
|
const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
|
|
22
23
|
context.fail(_this, `${t}"{{value}}" is not a valid integer value`, input);
|
|
23
24
|
}, options);
|
|
@@ -7,17 +7,18 @@ import { validator } from '../../core/index.js';
|
|
|
7
7
|
export function isNumber(options) {
|
|
8
8
|
return validator('isNumber', function (input, context, _this) {
|
|
9
9
|
const coerce = options?.coerce || context.coerce;
|
|
10
|
-
|
|
10
|
+
let output = input;
|
|
11
|
+
if (output != null && typeof output !== 'number' && coerce) {
|
|
11
12
|
if (typeof input === 'string')
|
|
12
|
-
|
|
13
|
-
if (typeof input === 'bigint') {
|
|
14
|
-
|
|
15
|
-
if (input === BigInt(
|
|
16
|
-
return
|
|
13
|
+
output = parseFloat(input);
|
|
14
|
+
else if (typeof input === 'bigint') {
|
|
15
|
+
output = Number(input);
|
|
16
|
+
if (input === BigInt(output))
|
|
17
|
+
return output;
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
|
-
if (typeof
|
|
20
|
-
return
|
|
20
|
+
if (typeof output === 'number' && !isNaN(output))
|
|
21
|
+
return output;
|
|
21
22
|
const t = typeof input === 'bigint' ? 'BigInt ' : typeof input === 'string' ? 'String ' : '';
|
|
22
23
|
context.fail(_this, `${t}"{{value}}" is not a valid number value`, input);
|
|
23
24
|
}, options);
|
|
@@ -32,16 +32,17 @@ export function isObject(schema, options) {
|
|
|
32
32
|
throw new TypeError(`Invalid definition in validation schema (${k})`);
|
|
33
33
|
}
|
|
34
34
|
const _rule = validator('isObject', function (input, context, _this) {
|
|
35
|
+
let output = input;
|
|
35
36
|
if (ctor && ctor[preValidation])
|
|
36
|
-
|
|
37
|
+
output = ctor[preValidation](output, context, _this);
|
|
37
38
|
const coerce = options?.coerce || context.coerce;
|
|
38
|
-
if (typeof
|
|
39
|
-
|
|
40
|
-
if (!(
|
|
39
|
+
if (typeof output === 'string' && coerce)
|
|
40
|
+
output = JSON.parse(output);
|
|
41
|
+
if (!(output && typeof output === 'object')) {
|
|
41
42
|
context.fail(_this, `{{label}} must be an object`, input);
|
|
42
43
|
return;
|
|
43
44
|
}
|
|
44
|
-
const keys = [...Object.keys(
|
|
45
|
+
const keys = [...Object.keys(output), ...schemaKeys];
|
|
45
46
|
const l = keys.length;
|
|
46
47
|
let i = 0;
|
|
47
48
|
let inputKey;
|
|
@@ -53,9 +54,9 @@ export function isObject(schema, options) {
|
|
|
53
54
|
Object.setPrototypeOf(out, ctor.prototype);
|
|
54
55
|
if (detectCircular) {
|
|
55
56
|
context.circMap = context.circMap || new Map();
|
|
56
|
-
if (context.circMap.has(
|
|
57
|
-
return context.circMap.get(
|
|
58
|
-
context.circMap.set(
|
|
57
|
+
if (context.circMap.has(output))
|
|
58
|
+
return context.circMap.get(output);
|
|
59
|
+
context.circMap.set(output, out);
|
|
59
60
|
}
|
|
60
61
|
if (context.root == null)
|
|
61
62
|
context.root = context.root || ctorName || '';
|
|
@@ -65,14 +66,14 @@ export function isObject(schema, options) {
|
|
|
65
66
|
for (i = 0; i < l; i++) {
|
|
66
67
|
inputKey = keys[i];
|
|
67
68
|
schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
|
|
68
|
-
v =
|
|
69
|
+
v = output[inputKey];
|
|
69
70
|
_propRule = propertyRules[schemaKey] || (isValidator(additionalFields) ? additionalFields : undefined);
|
|
70
71
|
if (_propRule) {
|
|
71
72
|
if (processedSchemaKeys[schemaKey])
|
|
72
73
|
continue;
|
|
73
74
|
processedSchemaKeys[schemaKey] = true;
|
|
74
75
|
const subCtx = context.extend();
|
|
75
|
-
subCtx.scope =
|
|
76
|
+
subCtx.scope = output;
|
|
76
77
|
subCtx.context = ctorName;
|
|
77
78
|
subCtx.location = location + (location ? '.' : '') + schemaKey;
|
|
78
79
|
subCtx.property = schemaKey;
|
|
@@ -7,19 +7,20 @@ import { validator } from '../../core/index.js';
|
|
|
7
7
|
export function isString(options) {
|
|
8
8
|
return validator('isString', function (input, context, _this) {
|
|
9
9
|
const coerce = options?.coerce || context.coerce;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
let output = input;
|
|
11
|
+
if (output != null && typeof output !== 'string' && coerce) {
|
|
12
|
+
if (typeof output === 'object') {
|
|
13
|
+
if (typeof output.toJSON === 'function')
|
|
14
|
+
output = output.toJSON();
|
|
14
15
|
else
|
|
15
|
-
|
|
16
|
+
output = JSON.stringify(output);
|
|
16
17
|
}
|
|
17
18
|
else
|
|
18
|
-
|
|
19
|
+
output = String(output);
|
|
19
20
|
}
|
|
20
|
-
if (typeof
|
|
21
|
-
return
|
|
22
|
-
context.fail(_this, `{{
|
|
21
|
+
if (typeof output === 'string')
|
|
22
|
+
return output;
|
|
23
|
+
context.fail(_this, `"{{value}}" is not a string`, input);
|
|
23
24
|
}, options);
|
|
24
25
|
}
|
|
25
26
|
export function stringReplace(searchValue, replacer) {
|
|
@@ -2,8 +2,9 @@ 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
|
-
|
|
6
|
-
|
|
5
|
+
let output = input;
|
|
6
|
+
if (output != null && coerce && !Array.isArray(output))
|
|
7
|
+
output = [output];
|
|
7
8
|
if (!Array.isArray(input)) {
|
|
8
9
|
context.fail(_this, `"{{value}}" is not a valid tuple`, input);
|
|
9
10
|
return;
|
|
@@ -12,18 +13,18 @@ export function isTuple(items, options) {
|
|
|
12
13
|
let itemRule;
|
|
13
14
|
let i;
|
|
14
15
|
let v;
|
|
15
|
-
const l =
|
|
16
|
+
const l = output.length;
|
|
16
17
|
const out = [];
|
|
17
18
|
const nl = items.length;
|
|
18
19
|
const itemContext = context.extend();
|
|
19
20
|
for (i = 0; i < l && i < nl; i++) {
|
|
20
21
|
itemRule = items[i];
|
|
21
|
-
itemContext.scope =
|
|
22
|
+
itemContext.scope = output;
|
|
22
23
|
itemContext.label = context.label ? context.label + `[${i}]` : undefined;
|
|
23
24
|
itemContext.index = i;
|
|
24
25
|
itemContext.location = location + '[' + i + ']';
|
|
25
26
|
itemContext.label = (itemContext.label || itemContext.property || 'Value at ') + `[${i}]`;
|
|
26
|
-
v = itemRule(
|
|
27
|
+
v = itemRule(output[i], itemContext);
|
|
27
28
|
out.push(v);
|
|
28
29
|
}
|
|
29
30
|
return context.errors.length ? undefined : out;
|
|
@@ -3,26 +3,30 @@ import { validator } from '../../core/index.js';
|
|
|
3
3
|
*
|
|
4
4
|
* @validator pipe
|
|
5
5
|
*/
|
|
6
|
-
export function pipe(
|
|
6
|
+
export function pipe(rules, options) {
|
|
7
7
|
const l = rules.length;
|
|
8
|
+
const returnIndex = options?.returnIndex;
|
|
8
9
|
return validator('pipe', function (input, context) {
|
|
9
10
|
let i;
|
|
10
11
|
let c;
|
|
11
12
|
let v = input;
|
|
13
|
+
let returnValue;
|
|
12
14
|
for (i = 0; i < l; i++) {
|
|
13
15
|
c = rules[i];
|
|
14
16
|
v = c(v, context);
|
|
17
|
+
if (returnIndex == null || i <= returnIndex)
|
|
18
|
+
returnValue = v;
|
|
15
19
|
if (context.errors.length)
|
|
16
20
|
return;
|
|
17
21
|
}
|
|
18
|
-
return
|
|
19
|
-
});
|
|
22
|
+
return returnValue;
|
|
23
|
+
}, options);
|
|
20
24
|
}
|
|
21
25
|
/**
|
|
22
26
|
* Test given value against to all codecs and returns original input
|
|
23
27
|
* @validator allOf
|
|
24
28
|
*/
|
|
25
|
-
export function allOf(
|
|
29
|
+
export function allOf(rules) {
|
|
26
30
|
return validator('allOf', function (input, context) {
|
|
27
31
|
let i;
|
|
28
32
|
let c;
|
package/package.json
CHANGED
package/typings/index.d.ts
CHANGED
|
@@ -53,8 +53,9 @@ declare const isHex: import("./core/validator.js").Validator<string, string, imp
|
|
|
53
53
|
declare const toArray: import("./core/validator.js").Validator<any[], any, import("./core/types.js").ExecutionOptions>;
|
|
54
54
|
declare const toBoolean: import("./core/validator.js").Validator<boolean | undefined, unknown, import("./core/types.js").ExecutionOptions>;
|
|
55
55
|
declare const toDate: import("./core/validator.js").Validator<Date, string | number | Date, import("./core/types.js").ExecutionOptions>;
|
|
56
|
+
declare const toBigint: import("./core/validator.js").Validator<bigint, unknown, import("./core/types.js").ExecutionOptions>;
|
|
56
57
|
declare const toDateString: import("./core/validator.js").Validator<string, string | number | Date, import("./core/types.js").ExecutionOptions>;
|
|
57
58
|
declare const toInteger: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
|
|
58
59
|
declare const toNumber: import("./core/validator.js").Validator<number, unknown, import("./core/types.js").ExecutionOptions>;
|
|
59
60
|
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, };
|
|
61
|
+
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, toBigint, toDate, toDateString, toInteger, toNumber, toString, };
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { Validator } from '../../core/index.js';
|
|
1
|
+
import { ValidationOptions, Validator } from '../../core/index.js';
|
|
2
|
+
export interface PipeOptions extends ValidationOptions {
|
|
3
|
+
returnIndex?: number;
|
|
4
|
+
}
|
|
2
5
|
/**
|
|
3
6
|
*
|
|
4
7
|
* @validator pipe
|
|
5
8
|
*/
|
|
6
|
-
export declare function pipe<T>(
|
|
9
|
+
export declare function pipe<T>(rules: Validator[], options?: PipeOptions): Validator<T>;
|
|
7
10
|
/**
|
|
8
11
|
* Test given value against to all codecs and returns original input
|
|
9
12
|
* @validator allOf
|
|
10
13
|
*/
|
|
11
|
-
export declare function allOf<T = any>(
|
|
14
|
+
export declare function allOf<T = any>(rules: Validator[]): Validator<T>;
|