valgen 3.2.0 → 4.0.0-alpha.2
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 +6 -0
- package/README.md +12 -7
- package/cjs/core/constants.js +5 -0
- package/cjs/core/context.js +65 -0
- package/cjs/core/index.js +21 -0
- package/cjs/core/types.js +2 -0
- package/cjs/core/validation-error.js +11 -0
- package/cjs/core/validator.js +61 -0
- package/cjs/helpers/object.utils.js +19 -0
- package/cjs/helpers/string.utils.js +9 -0
- package/cjs/index.js +18 -0
- package/cjs/package.json +3 -0
- package/cjs/rules/index.js +38 -0
- package/cjs/rules/is-any.js +12 -0
- package/cjs/rules/is-array.js +38 -0
- package/cjs/rules/is-boolean.js +30 -0
- package/cjs/rules/is-date.js +63 -0
- package/cjs/rules/is-empty.js +68 -0
- package/cjs/rules/is-enum.js +30 -0
- package/cjs/rules/is-integer.js +26 -0
- package/cjs/rules/is-matches.js +24 -0
- package/cjs/rules/is-null.js +40 -0
- package/cjs/rules/is-number.js +26 -0
- package/cjs/rules/is-object-id.js +28 -0
- package/cjs/rules/is-object.js +103 -0
- package/cjs/rules/is-record.js +41 -0
- package/cjs/rules/is-string.js +79 -0
- package/cjs/rules/is-tuple.js +32 -0
- package/cjs/rules/length.js +41 -0
- package/cjs/rules/optional.js +44 -0
- package/cjs/rules/pipe.js +43 -0
- package/cjs/rules/range.js +89 -0
- package/cjs/rules/string-formats.js +296 -0
- package/cjs/rules/union.js +31 -0
- package/cjs/rules/utilities.js +31 -0
- package/esm/core/constants.js +2 -0
- package/esm/core/context.js +61 -0
- package/esm/core/index.js +5 -0
- package/esm/core/types.js +1 -0
- package/esm/core/validation-error.js +7 -0
- package/esm/core/validator.js +56 -0
- package/esm/helpers/object.utils.js +14 -0
- package/esm/helpers/string.utils.js +5 -0
- package/esm/index.js +2 -0
- package/esm/rules/index.js +22 -0
- package/esm/rules/is-any.js +8 -0
- package/esm/rules/is-array.js +34 -0
- package/esm/rules/is-boolean.js +26 -0
- package/esm/rules/is-date.js +55 -0
- package/esm/rules/is-empty.js +63 -0
- package/esm/rules/is-enum.js +26 -0
- package/esm/rules/is-integer.js +22 -0
- package/esm/rules/is-matches.js +20 -0
- package/esm/rules/is-null.js +34 -0
- package/esm/rules/is-number.js +22 -0
- package/esm/rules/is-object-id.js +21 -0
- package/esm/rules/is-object.js +99 -0
- package/esm/rules/is-record.js +37 -0
- package/esm/rules/is-string.js +70 -0
- package/esm/rules/is-tuple.js +28 -0
- package/esm/rules/length.js +35 -0
- package/esm/rules/optional.js +38 -0
- package/esm/rules/pipe.js +38 -0
- package/esm/rules/range.js +81 -0
- package/esm/rules/string-formats.js +266 -0
- package/esm/rules/union.js +27 -0
- package/esm/rules/utilities.js +26 -0
- package/package.json +33 -37
- package/typings/core/constants.d.ts +2 -0
- package/typings/core/context.d.ts +26 -0
- package/typings/core/index.d.ts +6 -0
- package/typings/core/types.d.ts +18 -0
- package/typings/core/validation-error.d.ts +5 -0
- package/typings/core/validator.d.ts +18 -0
- package/typings/helpers/object.utils.d.ts +2 -0
- package/typings/helpers/string.utils.d.ts +1 -0
- package/typings/index.d.ts +2 -0
- package/typings/rules/index.d.ts +22 -0
- package/typings/rules/is-any.d.ts +5 -0
- package/typings/rules/is-array.d.ts +7 -0
- package/typings/rules/is-boolean.d.ts +7 -0
- package/typings/rules/is-date.d.ts +19 -0
- package/typings/rules/is-empty.d.ts +13 -0
- package/typings/rules/is-enum.d.ts +9 -0
- package/typings/rules/is-integer.d.ts +7 -0
- package/typings/rules/is-matches.d.ts +9 -0
- package/typings/rules/is-null.d.ts +16 -0
- package/typings/rules/is-number.d.ts +7 -0
- package/typings/rules/is-object-id.d.ts +10 -0
- package/typings/rules/is-object.d.ts +22 -0
- package/typings/rules/is-record.d.ts +7 -0
- package/typings/rules/is-string.d.ts +42 -0
- package/typings/rules/is-tuple.d.ts +27 -0
- package/typings/rules/length.d.ts +21 -0
- package/typings/rules/optional.d.ts +17 -0
- package/typings/rules/pipe.d.ts +11 -0
- package/typings/rules/range.d.ts +48 -0
- package/typings/rules/string-formats.d.ts +140 -0
- package/typings/rules/union.d.ts +5 -0
- package/typings/rules/utilities.d.ts +11 -0
- package/lib/ArrayType.d.ts +0 -5
- package/lib/ArrayType.js +0 -43
- package/lib/DataType.d.ts +0 -25
- package/lib/DataType.js +0 -214
- package/lib/ObjectType.d.ts +0 -5
- package/lib/ObjectType.js +0 -78
- package/lib/SchemaError.d.ts +0 -3
- package/lib/SchemaError.js +0 -17
- package/lib/UnionType.d.ts +0 -5
- package/lib/UnionType.js +0 -54
- package/lib/Valgen.d.ts +0 -88
- package/lib/Valgen.js +0 -289
- package/lib/ValidationError.d.ts +0 -2
- package/lib/ValidationError.js +0 -4
- package/lib/factories/AnyFactory.d.ts +0 -28
- package/lib/factories/AnyFactory.js +0 -73
- package/lib/factories/ArrayFactory.d.ts +0 -16
- package/lib/factories/ArrayFactory.js +0 -148
- package/lib/factories/Base64Factory.d.ts +0 -12
- package/lib/factories/Base64Factory.js +0 -31
- package/lib/factories/BooleanFactory.d.ts +0 -12
- package/lib/factories/BooleanFactory.js +0 -49
- package/lib/factories/DateFactory.d.ts +0 -40
- package/lib/factories/DateFactory.js +0 -239
- package/lib/factories/FunctionFactory.d.ts +0 -12
- package/lib/factories/FunctionFactory.js +0 -28
- package/lib/factories/IntegerFactory.d.ts +0 -14
- package/lib/factories/IntegerFactory.js +0 -33
- package/lib/factories/NilFactory.d.ts +0 -4
- package/lib/factories/NilFactory.js +0 -20
- package/lib/factories/NumberFactory.d.ts +0 -36
- package/lib/factories/NumberFactory.js +0 -178
- package/lib/factories/ObjectFactory.d.ts +0 -38
- package/lib/factories/ObjectFactory.js +0 -326
- package/lib/factories/StringFactory.d.ts +0 -16
- package/lib/factories/StringFactory.js +0 -94
- package/lib/factories/UnionFactory.d.ts +0 -13
- package/lib/factories/UnionFactory.js +0 -62
- package/lib/index.d.ts +0 -19
- package/lib/index.js +0 -37
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNumber = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Validates if value is "number".
|
|
7
|
+
* Converts input value to number if coerce option is set to 'true'.
|
|
8
|
+
* @validator isNumber
|
|
9
|
+
*/
|
|
10
|
+
function isNumber(options) {
|
|
11
|
+
return (0, index_js_1.validator)('isNumber', function (input, context) {
|
|
12
|
+
if (input != null && typeof input !== 'number' && context.coerce) {
|
|
13
|
+
if (typeof input === 'string')
|
|
14
|
+
input = parseFloat(input);
|
|
15
|
+
if (typeof input === 'bigint') {
|
|
16
|
+
const v = Number(input);
|
|
17
|
+
if (input === BigInt(v))
|
|
18
|
+
input = v;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (typeof input === 'number' && !isNaN(input))
|
|
22
|
+
return input;
|
|
23
|
+
context.failure(`{{label}} not a valid number`);
|
|
24
|
+
}, options);
|
|
25
|
+
}
|
|
26
|
+
exports.isNumber = isNumber;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isObjectId = void 0;
|
|
7
|
+
const validator_1 = __importDefault(require("validator"));
|
|
8
|
+
const index_js_1 = require("../core/index.js");
|
|
9
|
+
/**
|
|
10
|
+
* Validates if value is an "ObjectId".
|
|
11
|
+
* @validator isObjectId
|
|
12
|
+
*/
|
|
13
|
+
function isObjectId(options) {
|
|
14
|
+
return (0, index_js_1.validator)('isObjectId', function (input, context) {
|
|
15
|
+
if (input != null &&
|
|
16
|
+
(_isObjectIdValue(input) ||
|
|
17
|
+
(typeof input === 'object' &&
|
|
18
|
+
typeof input.toHexString === 'function' &&
|
|
19
|
+
_isObjectIdValue(input.toHexString()))))
|
|
20
|
+
return input;
|
|
21
|
+
context.failure(`{{label}} is not a valid ObjectId`);
|
|
22
|
+
}, options);
|
|
23
|
+
}
|
|
24
|
+
exports.isObjectId = isObjectId;
|
|
25
|
+
function _isObjectIdValue(input) {
|
|
26
|
+
return (input instanceof Uint8Array && input.length === 12) ||
|
|
27
|
+
(typeof input === 'string' && input.length === 24 && validator_1.default.isHexadecimal(input));
|
|
28
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isObject = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Validates object according to schema
|
|
7
|
+
* Converts properties according to schema rules if coerce option is set to 'true'.
|
|
8
|
+
* @validator isObject
|
|
9
|
+
*/
|
|
10
|
+
function isObject(schema, options) {
|
|
11
|
+
const ctor = options?.ctor;
|
|
12
|
+
const ctorName = options?.name || ctor?.name;
|
|
13
|
+
const additionalFields = options?.additionalFields ?? false;
|
|
14
|
+
const caseInSensitive = !!options?.caseInSensitive;
|
|
15
|
+
const detectCircular = !!options?.detectCircular;
|
|
16
|
+
if (options) {
|
|
17
|
+
delete options.name;
|
|
18
|
+
delete options.ctor;
|
|
19
|
+
delete options.additionalFields;
|
|
20
|
+
delete options.caseInSensitive;
|
|
21
|
+
delete options.detectCircular;
|
|
22
|
+
}
|
|
23
|
+
const propertyRules = {};
|
|
24
|
+
const propertyOptions = {};
|
|
25
|
+
Object.keys(schema).forEach(k => {
|
|
26
|
+
const n = schema[k];
|
|
27
|
+
const key = caseInSensitive ? k.toLowerCase() : k;
|
|
28
|
+
if (Array.isArray(n)) {
|
|
29
|
+
if (!(0, index_js_1.isValidator)(n[0]))
|
|
30
|
+
throw new TypeError(`Invalid tuple definition in validation schema (${k})`);
|
|
31
|
+
propertyRules[key] = n[0];
|
|
32
|
+
propertyOptions[key] = { as: k, ...n[1] };
|
|
33
|
+
}
|
|
34
|
+
else if ((0, index_js_1.isValidator)(n)) {
|
|
35
|
+
propertyRules[key] = n;
|
|
36
|
+
propertyOptions[key] = { as: k };
|
|
37
|
+
}
|
|
38
|
+
else
|
|
39
|
+
throw new TypeError(`Invalid definition in validation schema (${k})`);
|
|
40
|
+
});
|
|
41
|
+
const schemaKeys = Object.keys(propertyRules);
|
|
42
|
+
const _rule = (0, index_js_1.validator)('isObject', function (input, context) {
|
|
43
|
+
if (!(input && typeof input === 'object')) {
|
|
44
|
+
context.failure(`{{label}} must be an object`);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const keyMap = [...Object.keys(input), ...schemaKeys]
|
|
48
|
+
.reduce((a, k) => {
|
|
49
|
+
if (caseInSensitive)
|
|
50
|
+
a[k.toLowerCase()] = k;
|
|
51
|
+
else
|
|
52
|
+
a[k] = k;
|
|
53
|
+
return a;
|
|
54
|
+
}, {});
|
|
55
|
+
const keys = Object.keys(keyMap);
|
|
56
|
+
const l = keys.length;
|
|
57
|
+
let i = 0;
|
|
58
|
+
let inputKey;
|
|
59
|
+
let schemaKey;
|
|
60
|
+
let v;
|
|
61
|
+
let _propRule;
|
|
62
|
+
const out = {};
|
|
63
|
+
if (ctor)
|
|
64
|
+
Object.setPrototypeOf(out, ctor.prototype);
|
|
65
|
+
if (detectCircular) {
|
|
66
|
+
context.circMap = context.circMap || new Map();
|
|
67
|
+
if (context.circMap.has(input))
|
|
68
|
+
return context.circMap.get(input);
|
|
69
|
+
context.circMap.set(input, out);
|
|
70
|
+
}
|
|
71
|
+
const rootName = context.root?.input.context;
|
|
72
|
+
const location = context.input.location || '';
|
|
73
|
+
// Iterate object keys and perform rules
|
|
74
|
+
for (i = 0; i < l; i++) {
|
|
75
|
+
inputKey = keys[i];
|
|
76
|
+
schemaKey = keyMap[inputKey];
|
|
77
|
+
v = input[inputKey];
|
|
78
|
+
_propRule = propertyRules[schemaKey] ||
|
|
79
|
+
((0, index_js_1.isValidator)(additionalFields) ? additionalFields : undefined);
|
|
80
|
+
if (_propRule) {
|
|
81
|
+
const subCtx = context.extend(_propRule);
|
|
82
|
+
if (ctorName) {
|
|
83
|
+
if (rootName && rootName !== ctorName)
|
|
84
|
+
subCtx.input.root = rootName;
|
|
85
|
+
subCtx.input.context = ctorName;
|
|
86
|
+
}
|
|
87
|
+
subCtx.input.value = v;
|
|
88
|
+
subCtx.input.label = propertyOptions[schemaKey]?.label || schemaKey;
|
|
89
|
+
subCtx.input.location = location + (location ? '.' : '') + schemaKey;
|
|
90
|
+
subCtx.input.property = schemaKey;
|
|
91
|
+
v = _propRule[index_js_1.kValidatorFn](v, subCtx, _propRule);
|
|
92
|
+
}
|
|
93
|
+
else if (v !== undefined && !additionalFields)
|
|
94
|
+
context.failure(`${ctorName || 'Object'} does not accept additional fields`);
|
|
95
|
+
if (v !== undefined)
|
|
96
|
+
out[propertyOptions[schemaKey]?.as || schemaKey] = v;
|
|
97
|
+
}
|
|
98
|
+
return context.errors.length ? undefined : out;
|
|
99
|
+
}, options);
|
|
100
|
+
_rule.schema = schema;
|
|
101
|
+
return _rule;
|
|
102
|
+
}
|
|
103
|
+
exports.isObject = isObject;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRecord = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Validates record object according to given "key" and "value" rules
|
|
7
|
+
* Converts properties according to rules if coerce option is set to 'true'.
|
|
8
|
+
* @validator isRecord
|
|
9
|
+
*/
|
|
10
|
+
function isRecord(keyRule, valueRule, options) {
|
|
11
|
+
return (0, index_js_1.validator)('isRecord', function (input, context) {
|
|
12
|
+
if (!(input && typeof input === 'object')) {
|
|
13
|
+
context.failure(`{{label}} is not an object`);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const location = context.input.location || '';
|
|
17
|
+
const keyContext = context.extend(keyRule);
|
|
18
|
+
const valueContext = context.extend(valueRule);
|
|
19
|
+
const keys = Object.keys(input);
|
|
20
|
+
const l = keys.length;
|
|
21
|
+
let i;
|
|
22
|
+
let k;
|
|
23
|
+
let v;
|
|
24
|
+
const out = {};
|
|
25
|
+
for (i = 0; i < l; i++) {
|
|
26
|
+
k = keys[i];
|
|
27
|
+
v = input[k];
|
|
28
|
+
keyContext.input.value = k;
|
|
29
|
+
keyContext.input.property = '@' + k;
|
|
30
|
+
keyContext.input.location = location + (location ? '.@' : '@') + k;
|
|
31
|
+
k = keyRule[index_js_1.kValidatorFn](k, keyContext, keyRule);
|
|
32
|
+
valueContext.input.value = v;
|
|
33
|
+
valueContext.input.property = k;
|
|
34
|
+
valueContext.input.location = location + (location ? '.' : '') + k;
|
|
35
|
+
v = valueRule[index_js_1.kValidatorFn](v, valueContext, valueRule);
|
|
36
|
+
out[k] = v;
|
|
37
|
+
}
|
|
38
|
+
return context.errors.length ? undefined : out;
|
|
39
|
+
}, options);
|
|
40
|
+
}
|
|
41
|
+
exports.isRecord = isRecord;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.trimStart = exports.trimEnd = exports.trim = exports.stringSplit = exports.stringReplace = exports.isString = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Validates if value is "string".
|
|
7
|
+
* Converts input value to string if coerce option is set to 'true'.
|
|
8
|
+
* @validator isString
|
|
9
|
+
*/
|
|
10
|
+
function isString(options) {
|
|
11
|
+
return (0, index_js_1.validator)('isString', function (input, context) {
|
|
12
|
+
if (input != null && typeof input !== 'string' && context.coerce) {
|
|
13
|
+
if (typeof input === 'object') {
|
|
14
|
+
if (typeof input.toJSON === 'function')
|
|
15
|
+
input = input.toJSON();
|
|
16
|
+
else
|
|
17
|
+
input = JSON.stringify(input);
|
|
18
|
+
}
|
|
19
|
+
else
|
|
20
|
+
input = String(input);
|
|
21
|
+
}
|
|
22
|
+
if (typeof input === 'string')
|
|
23
|
+
return input;
|
|
24
|
+
context.failure(`{{label}} is not a string`);
|
|
25
|
+
}, options);
|
|
26
|
+
}
|
|
27
|
+
exports.isString = isString;
|
|
28
|
+
function stringReplace(searchValue, replacer) {
|
|
29
|
+
return (0, index_js_1.validator)('stringReplace', function (input) {
|
|
30
|
+
if (input == null)
|
|
31
|
+
return input;
|
|
32
|
+
return String(input).replace(searchValue, replacer);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exports.stringReplace = stringReplace;
|
|
36
|
+
function stringSplit(splitter, limit) {
|
|
37
|
+
return (0, index_js_1.validator)('stringSplit', function (input) {
|
|
38
|
+
if (input == null)
|
|
39
|
+
return input;
|
|
40
|
+
return String(input).split(splitter, limit);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
exports.stringSplit = stringSplit;
|
|
44
|
+
/**
|
|
45
|
+
* Removes whitespace from both ends of a string
|
|
46
|
+
* @validator trim
|
|
47
|
+
*/
|
|
48
|
+
function trim() {
|
|
49
|
+
return (0, index_js_1.validator)('trim', function (input) {
|
|
50
|
+
if (input == null)
|
|
51
|
+
return input;
|
|
52
|
+
return String(input).trim();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
exports.trim = trim;
|
|
56
|
+
// *************************************************************
|
|
57
|
+
/**
|
|
58
|
+
* Removes whitespace from the end of a string
|
|
59
|
+
* @validator trimEnd
|
|
60
|
+
*/
|
|
61
|
+
const trimEnd = () => trimEndRule;
|
|
62
|
+
exports.trimEnd = trimEnd;
|
|
63
|
+
const trimEndRule = (0, index_js_1.validator)('trimEnd', function (input) {
|
|
64
|
+
if (input == null)
|
|
65
|
+
return input;
|
|
66
|
+
return String(input).trimEnd();
|
|
67
|
+
});
|
|
68
|
+
// *************************************************************
|
|
69
|
+
/**
|
|
70
|
+
* Removes whitespace from the beginning of a string
|
|
71
|
+
* @validator trimStart
|
|
72
|
+
*/
|
|
73
|
+
const trimStart = () => trimStartRule;
|
|
74
|
+
exports.trimStart = trimStart;
|
|
75
|
+
const trimStartRule = (0, index_js_1.validator)('trimStart', function (input) {
|
|
76
|
+
if (input == null)
|
|
77
|
+
return input;
|
|
78
|
+
return String(input).trimStart();
|
|
79
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTuple = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
function isTuple(items, options) {
|
|
6
|
+
return (0, index_js_1.validator)('isTuple', function (input, context) {
|
|
7
|
+
if (input != null && context.coerce && !Array.isArray(input))
|
|
8
|
+
input = [input];
|
|
9
|
+
if (!Array.isArray(input)) {
|
|
10
|
+
context.failure(`{{label}} is not a valid tuple`);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const location = context.input.location || '';
|
|
14
|
+
let itemRule;
|
|
15
|
+
let i;
|
|
16
|
+
const l = input.length;
|
|
17
|
+
const out = [];
|
|
18
|
+
const nl = items.length;
|
|
19
|
+
for (i = 0; i < l && i < nl; i++) {
|
|
20
|
+
itemRule = items[i];
|
|
21
|
+
const itemContext = context.extend(itemRule);
|
|
22
|
+
itemContext.input.value = input[i];
|
|
23
|
+
itemContext.input.label = ((context.input.label || context.input.property) || 'Item') +
|
|
24
|
+
`[${i}]`;
|
|
25
|
+
itemContext.input.property = i;
|
|
26
|
+
itemContext.input.location = location + '[' + i + ']';
|
|
27
|
+
out.push(itemRule[index_js_1.kValidatorFn](input[i], itemContext, itemRule) ?? null);
|
|
28
|
+
}
|
|
29
|
+
return context.errors.length ? undefined : out;
|
|
30
|
+
}, options);
|
|
31
|
+
}
|
|
32
|
+
exports.isTuple = isTuple;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.lengthMax = exports.lengthMin = exports.extractLength = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
const pipe_js_1 = require("./pipe.js");
|
|
6
|
+
const range_js_1 = require("./range.js");
|
|
7
|
+
/**
|
|
8
|
+
* Returns length of an Array, String, ArrayBuffer, Buffer or any object with the "length" property.
|
|
9
|
+
* @validator getLength
|
|
10
|
+
*/
|
|
11
|
+
const extractLength = () => extractLengthRule;
|
|
12
|
+
exports.extractLength = extractLength;
|
|
13
|
+
const extractLengthRule = (0, index_js_1.validator)('extractLength', function (input, context) {
|
|
14
|
+
if (typeof input === 'string')
|
|
15
|
+
return input.length;
|
|
16
|
+
if (Array.isArray(input))
|
|
17
|
+
return input.length;
|
|
18
|
+
if (input instanceof ArrayBuffer)
|
|
19
|
+
return input.byteLength;
|
|
20
|
+
if (input && typeof input === 'object' && typeof input.length === 'number')
|
|
21
|
+
return input.length;
|
|
22
|
+
if (input && typeof input === 'object' && typeof input.size === 'number')
|
|
23
|
+
return input.size;
|
|
24
|
+
context.failure(`Unable to extract length of {{label}}`);
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Checks the length is at least "minValue"
|
|
28
|
+
* @validator lengthMin
|
|
29
|
+
*/
|
|
30
|
+
const lengthMin = (minValue) => (0, pipe_js_1.allOf)((0, pipe_js_1.pipe)((0, exports.extractLength)(), (0, range_js_1.isGte)(minValue, {
|
|
31
|
+
onFail: () => `The length of {{label}} must be at least ${minValue}`
|
|
32
|
+
})));
|
|
33
|
+
exports.lengthMin = lengthMin;
|
|
34
|
+
/**
|
|
35
|
+
* Checks if the length is at most "maxValue"
|
|
36
|
+
* @validator charLengthMax
|
|
37
|
+
*/
|
|
38
|
+
const lengthMax = (maxValue) => (0, pipe_js_1.allOf)((0, pipe_js_1.pipe)((0, exports.extractLength)(), (0, range_js_1.isLte)(maxValue, {
|
|
39
|
+
onFail: () => `The length of {{label}} must be at most ${maxValue}`
|
|
40
|
+
})));
|
|
41
|
+
exports.lengthMax = lengthMax;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exists = exports.required = 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[index_js_1.kValidatorFn](input, context, nested);
|
|
14
|
+
}, options);
|
|
15
|
+
}
|
|
16
|
+
exports.optional = optional;
|
|
17
|
+
/**
|
|
18
|
+
* Makes sub-rule required
|
|
19
|
+
* @validator required
|
|
20
|
+
*/
|
|
21
|
+
function required(nested, options) {
|
|
22
|
+
return (0, index_js_1.validator)('required', function (input, context) {
|
|
23
|
+
if (input == null) {
|
|
24
|
+
context.failure(`{{label}} is required`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
return nested[index_js_1.kValidatorFn](input, context, nested);
|
|
28
|
+
}, options);
|
|
29
|
+
}
|
|
30
|
+
exports.required = required;
|
|
31
|
+
/**
|
|
32
|
+
* Validates if property exists
|
|
33
|
+
* @validator exists
|
|
34
|
+
*/
|
|
35
|
+
function exists(options) {
|
|
36
|
+
return (0, index_js_1.validator)('exists', function (input, context) {
|
|
37
|
+
if (input !== undefined ||
|
|
38
|
+
(context.input.value && context.parent &&
|
|
39
|
+
Object.getOwnPropertyDescriptor(context.input.value, context.input.property)))
|
|
40
|
+
return input;
|
|
41
|
+
context.failure(`{{label}} must exist`);
|
|
42
|
+
}, options);
|
|
43
|
+
}
|
|
44
|
+
exports.exists = exists;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.allOf = exports.pipe = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @validator pipe
|
|
8
|
+
*/
|
|
9
|
+
function pipe(...rules) {
|
|
10
|
+
const l = rules.length;
|
|
11
|
+
return (0, index_js_1.validator)('pipe', function (input, context) {
|
|
12
|
+
let i;
|
|
13
|
+
let c;
|
|
14
|
+
let v = input;
|
|
15
|
+
for (i = 0; i < l; i++) {
|
|
16
|
+
c = rules[i];
|
|
17
|
+
const subContext = context.extend(c);
|
|
18
|
+
v = c[index_js_1.kValidatorFn](v, subContext, c);
|
|
19
|
+
if (context.errors.length)
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
return v;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
exports.pipe = pipe;
|
|
26
|
+
/**
|
|
27
|
+
* Test given value against to all codecs and returns original input
|
|
28
|
+
* @validator allOf
|
|
29
|
+
*/
|
|
30
|
+
function allOf(...rules) {
|
|
31
|
+
return (0, index_js_1.validator)('allOf', function (input, context) {
|
|
32
|
+
let i;
|
|
33
|
+
let c;
|
|
34
|
+
const l = rules.length;
|
|
35
|
+
for (i = 0; i < l; i++) {
|
|
36
|
+
c = rules[i];
|
|
37
|
+
const subContext = context.extend(c);
|
|
38
|
+
c[index_js_1.kValidatorFn](input, subContext, c);
|
|
39
|
+
}
|
|
40
|
+
return input;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
exports.allOf = allOf;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.range = exports.isLte = exports.isLt = exports.isGte = exports.isGt = void 0;
|
|
4
|
+
const index_js_1 = require("../core/index.js");
|
|
5
|
+
function isGt(minValue, options) {
|
|
6
|
+
return (0, index_js_1.validator)('isGt', function (input, context) {
|
|
7
|
+
if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
|
|
8
|
+
(typeof input === 'number' || typeof input === 'bigint') &&
|
|
9
|
+
input > minValue)
|
|
10
|
+
return input;
|
|
11
|
+
if (minValue instanceof Date && input instanceof Date && input > minValue)
|
|
12
|
+
return input;
|
|
13
|
+
if (typeof minValue === 'string' && typeof input === 'string' &&
|
|
14
|
+
(input > minValue ||
|
|
15
|
+
(options?.caseInsensitive && input.toLowerCase() > minValue.toLowerCase())))
|
|
16
|
+
return input;
|
|
17
|
+
context.failure(`{{label}} must be greater than ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`);
|
|
18
|
+
}, options);
|
|
19
|
+
}
|
|
20
|
+
exports.isGt = isGt;
|
|
21
|
+
function isGte(minValue, options) {
|
|
22
|
+
return (0, index_js_1.validator)('isGte', function (input, context) {
|
|
23
|
+
if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
|
|
24
|
+
(typeof input === 'number' || typeof input === 'bigint') &&
|
|
25
|
+
input >= minValue)
|
|
26
|
+
return input;
|
|
27
|
+
if (minValue instanceof Date && input instanceof Date && input >= minValue)
|
|
28
|
+
return input;
|
|
29
|
+
if (typeof minValue === 'string' && typeof input === 'string' &&
|
|
30
|
+
(input >= minValue ||
|
|
31
|
+
(options?.caseInsensitive && input.toLowerCase() >= minValue.toLowerCase())))
|
|
32
|
+
return input;
|
|
33
|
+
context.failure(`{{label}} must be greater than or equal to ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`);
|
|
34
|
+
}, options);
|
|
35
|
+
}
|
|
36
|
+
exports.isGte = isGte;
|
|
37
|
+
function isLt(maxValue, options) {
|
|
38
|
+
return (0, index_js_1.validator)('isLt', function (input, context) {
|
|
39
|
+
if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
|
|
40
|
+
(typeof input === 'number' || typeof input === 'bigint') &&
|
|
41
|
+
input < maxValue)
|
|
42
|
+
return input;
|
|
43
|
+
if (maxValue instanceof Date && input instanceof Date && input < maxValue)
|
|
44
|
+
return input;
|
|
45
|
+
if (typeof maxValue === 'string' && typeof input === 'string' &&
|
|
46
|
+
(input < maxValue ||
|
|
47
|
+
(options?.caseInsensitive && input.toLowerCase() < maxValue.toLowerCase())))
|
|
48
|
+
return input;
|
|
49
|
+
context.failure(`{{label}} must be lover than ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`);
|
|
50
|
+
}, options);
|
|
51
|
+
}
|
|
52
|
+
exports.isLt = isLt;
|
|
53
|
+
function isLte(maxValue, options) {
|
|
54
|
+
return (0, index_js_1.validator)('isLt', function (input, context) {
|
|
55
|
+
if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
|
|
56
|
+
(typeof input === 'number' || typeof input === 'bigint') &&
|
|
57
|
+
input <= maxValue)
|
|
58
|
+
return input;
|
|
59
|
+
if (maxValue instanceof Date && input instanceof Date && input <= maxValue)
|
|
60
|
+
return input;
|
|
61
|
+
if (typeof maxValue === 'string' && typeof input === 'string' &&
|
|
62
|
+
(input <= maxValue ||
|
|
63
|
+
(options?.caseInsensitive && input.toLowerCase() <= maxValue.toLowerCase())))
|
|
64
|
+
return input;
|
|
65
|
+
context.failure(`{{label}} must be lover than or equal to ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`);
|
|
66
|
+
}, options);
|
|
67
|
+
}
|
|
68
|
+
exports.isLte = isLte;
|
|
69
|
+
/**
|
|
70
|
+
* Checks if value is between minValue and maxValue
|
|
71
|
+
* @validator range
|
|
72
|
+
*/
|
|
73
|
+
function range(minValue, maxValue, options) {
|
|
74
|
+
return (0, index_js_1.validator)('range', function (input, context) {
|
|
75
|
+
if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
|
|
76
|
+
(typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
|
|
77
|
+
(typeof input === 'number' || typeof input === 'bigint') &&
|
|
78
|
+
input >= minValue && input <= maxValue)
|
|
79
|
+
return input;
|
|
80
|
+
if (minValue instanceof Date && maxValue instanceof Date &&
|
|
81
|
+
input instanceof Date && input >= minValue && input <= maxValue)
|
|
82
|
+
return input;
|
|
83
|
+
if (typeof minValue === 'string' && typeof maxValue === 'string' &&
|
|
84
|
+
typeof input === 'string' && input >= minValue && input <= maxValue)
|
|
85
|
+
return input;
|
|
86
|
+
context.failure(`{{label}} must be between ${minValue} and ${maxValue}`);
|
|
87
|
+
}, options);
|
|
88
|
+
}
|
|
89
|
+
exports.range = range;
|