valgen 5.3.1 → 5.4.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 +16 -0
- 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/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 +3 -3
- package/typings/rules/utility-rules/pipe.d.ts +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
# v5.4.0
|
|
2
|
+
[2024-05-18]
|
|
3
|
+
|
|
4
|
+
### Changes
|
|
5
|
+
|
|
6
|
+
* Added returnIndex to pipe() ([`87458ad`](https://github.com/panates/valgen/commit/87458adb3eb6a4465c8198f942df1fbb333738ff))
|
|
7
|
+
* Updated circleci config ([`101361c`](https://github.com/panates/valgen/commit/101361c20f1b78a75392b9e796bc9075b492cb3e))
|
|
8
|
+
|
|
9
|
+
# v5.3.2
|
|
10
|
+
[2024-05-18]
|
|
11
|
+
|
|
12
|
+
### Changes
|
|
13
|
+
|
|
14
|
+
* Updated dependencies ([`9e618ab`](https://github.com/panates/valgen/commit/9e618ab5062b106a7321ef50beac0cd0ccf7e56c))
|
|
15
|
+
* Fixed coveralls token ([`ede2734`](https://github.com/panates/valgen/commit/ede27343a163ef6649866fedb141598518d6e1c3))
|
|
16
|
+
|
|
1
17
|
# v5.3.1
|
|
2
18
|
[2024-05-18]
|
|
3
19
|
|
|
@@ -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;
|
|
@@ -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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "valgen",
|
|
3
3
|
"description": "Fast runtime type validator, converter and io (encoding/decoding) library",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.4.0",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"url": "https://github.com/panates/valgen.git"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@types/validator": "^13.11.
|
|
15
|
+
"@types/validator": "^13.11.10",
|
|
16
16
|
"date-fns": "^3.6.0",
|
|
17
17
|
"ts-gems": "^3.4.0",
|
|
18
|
-
"validator": "^13.
|
|
18
|
+
"validator": "^13.12.0"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
|
21
21
|
"types": "typings/index.d.ts",
|
|
@@ -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>;
|