validno 0.2.6 → 0.3.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/dist/Schema.js +6 -5
- package/dist/ValidnoResult.js +12 -7
- package/dist/checkType.js +1 -0
- package/dist/constants/details.js +11 -11
- package/dist/constants/schema.js +10 -10
- package/dist/dev.js +67 -27
- package/dist/engine/ValidateEngine.js +44 -0
- package/dist/engine/ValidnoResult.js +102 -0
- package/dist/engine/index.js +2 -0
- package/dist/engine/methods/finishValidation.js +15 -0
- package/dist/engine/methods/handleKey.js +41 -0
- package/dist/engine/methods/handleMissingKey.js +19 -0
- package/dist/engine/methods/handleMissingKeyValidation.js +9 -0
- package/dist/engine/methods/handleNestedKey.js +19 -0
- package/dist/engine/methods/validate.js +14 -0
- package/dist/engine/methods/validateRules.js +172 -0
- package/dist/engine/methods/validateType.js +134 -0
- package/dist/types/common.js +1 -0
- package/dist/utils/errors.js +30 -21
- package/dist/utils/helpers.js +55 -57
- package/dist/utils/validateType.js +9 -8
- package/dist/utils/validations.js +157 -153
- package/dist/validate/index.js +1 -0
- package/dist/validate/validate.js +151 -0
- package/dist/validate.js +136 -127
- package/dist/validateEngine/ValidateEngine.js +44 -0
- package/dist/validateEngine/index.js +2 -0
- package/dist/validateEngine/methods/ValidateEngine.js +139 -0
- package/dist/validateEngine/methods/checkRulesForKey.js +15 -0
- package/dist/validateEngine/methods/checkValueType.js +134 -0
- package/dist/validateEngine/methods/finalizeValidation.js +15 -0
- package/dist/validateEngine/methods/finishValidation.js +15 -0
- package/dist/validateEngine/methods/handleKey.js +43 -0
- package/dist/validateEngine/methods/handleMissingKey.js +19 -0
- package/dist/validateEngine/methods/handleMissingKeyValidation.js +9 -0
- package/dist/validateEngine/methods/handleNestedKey.js +19 -0
- package/dist/validateEngine/methods/validate.js +14 -0
- package/dist/validateEngine/methods/validateKey.js +31 -0
- package/dist/validateEngine/methods/validateKeyDetails.js +13 -0
- package/dist/validateEngine/methods/validateKeyValue.js +13 -0
- package/dist/validateEngine/methods/validateNestedKey.js +19 -0
- package/dist/validateEngine/methods/validateType.js +134 -0
- package/dist/validateEngine/validate.js +14 -0
- package/dist/validateSchema/ValidateEngine.js +147 -0
- package/dist/validateSchema/index.js +6 -0
- package/dist/validateSchema/validate.js +151 -0
- package/dist/validateSchema.js +6 -0
- package/dist/validateType.js +4 -4
- package/package.json +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { ValidationDetails, ValidationIds } from "../../constants/details.js";
|
|
2
|
+
import _validations from "../../utils/validations.js";
|
|
3
|
+
import _errors from "../../utils/errors.js";
|
|
4
|
+
import _validateType from "../../utils/validateType.js";
|
|
5
|
+
const validateUnionType = (key, value, requirements, keyName = key) => {
|
|
6
|
+
const typeList = Array.isArray(requirements.type)
|
|
7
|
+
? requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el))
|
|
8
|
+
: [];
|
|
9
|
+
const results = [];
|
|
10
|
+
for (let i = 0; i < typeList.length; i++) {
|
|
11
|
+
const requirementsRe = Object.assign(Object.assign({}, requirements), { type: requirements.type[i] });
|
|
12
|
+
const result = handleTypeValidation(key, value, requirementsRe);
|
|
13
|
+
results.push(result[0].passed);
|
|
14
|
+
if (results[i] === true)
|
|
15
|
+
return _validateType.getResult(keyName, true);
|
|
16
|
+
}
|
|
17
|
+
const isPassed = results.some((r) => r === true);
|
|
18
|
+
const result = _validateType.getResult(keyName, isPassed, isPassed ? undefined : _errors.getErrorDetails(keyName, typeList.join('/'), value));
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
const handleTypeValidation = (key, value, requirements, keyName = key) => {
|
|
22
|
+
var _a;
|
|
23
|
+
const isNotNull = value !== null;
|
|
24
|
+
const keyTitle = 'title' in requirements ? requirements.title : keyName;
|
|
25
|
+
const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
|
|
26
|
+
if (value === undefined && requirements.required) {
|
|
27
|
+
return [_validateType.getResult(keyName, false, _errors.getMissingError(keyName))];
|
|
28
|
+
}
|
|
29
|
+
if (Array.isArray(requirements.type)) {
|
|
30
|
+
return [validateUnionType(key, value, requirements)];
|
|
31
|
+
}
|
|
32
|
+
if (value === undefined && requirements.required !== true) {
|
|
33
|
+
return [_validateType.getResult(keyName, true)];
|
|
34
|
+
}
|
|
35
|
+
const customErrDetails = hasCustomMessage ?
|
|
36
|
+
requirements.customMessage({
|
|
37
|
+
keyword: ValidationIds.Type,
|
|
38
|
+
value: value,
|
|
39
|
+
key: keyName,
|
|
40
|
+
title: keyTitle,
|
|
41
|
+
reqs: requirements,
|
|
42
|
+
schema: null
|
|
43
|
+
}) :
|
|
44
|
+
null;
|
|
45
|
+
const baseErrDetails = _errors.getErrorDetails(keyName, requirements.type, value);
|
|
46
|
+
const getDetails = (isOK, errorText) => isOK ?
|
|
47
|
+
ValidationDetails.OK :
|
|
48
|
+
errorText || customErrDetails || baseErrDetails;
|
|
49
|
+
const typeBySchema = requirements.type;
|
|
50
|
+
const result = [];
|
|
51
|
+
switch (typeBySchema) {
|
|
52
|
+
case 'any': {
|
|
53
|
+
result.push(_validateType.getResult(keyName, true, getDetails(true)));
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case Number: {
|
|
57
|
+
const isNumber = isNotNull && value.constructor === Number;
|
|
58
|
+
result.push(_validateType.getResult(keyName, isNumber, getDetails(isNumber)));
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
case String: {
|
|
62
|
+
const isString = isNotNull && value.constructor === String;
|
|
63
|
+
result.push(_validateType.getResult(keyName, isString, getDetails(isString)));
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
case Date: {
|
|
67
|
+
const isDate = isNotNull && value.constructor === Date;
|
|
68
|
+
const isValid = isDate && !isNaN(value.getTime());
|
|
69
|
+
const isValidDate = isDate && isValid;
|
|
70
|
+
result.push(_validateType.getResult(keyName, isValidDate, getDetails(isValidDate, ValidationDetails.INVALID_DATE)));
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case Boolean: {
|
|
74
|
+
const isBoolean = isNotNull && value.constructor === Boolean;
|
|
75
|
+
result.push(_validateType.getResult(keyName, isBoolean, getDetails(isBoolean)));
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case Array: {
|
|
79
|
+
const isArray = isNotNull && value.constructor === Array;
|
|
80
|
+
if (!isArray) {
|
|
81
|
+
result.push(_validateType.getResult(keyName, false, getDetails(isArray)));
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
let isEachChecked = { passed: true, details: "" };
|
|
85
|
+
if ('eachType' in requirements) {
|
|
86
|
+
for (const el of value) {
|
|
87
|
+
const result = handleTypeValidation(`each of ${key}`, el, { type: requirements.eachType, required: true });
|
|
88
|
+
if (!result[0].passed) {
|
|
89
|
+
isEachChecked.passed = false;
|
|
90
|
+
isEachChecked.details = result[0].details || '';
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const isOk = isArray && isEachChecked.passed;
|
|
96
|
+
const details = !isEachChecked.passed ? isEachChecked.details : getDetails(isOk);
|
|
97
|
+
result.push(_validateType.getResult(keyName, isOk, details));
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case Object: {
|
|
101
|
+
const isObject = _validations.isObject(value) && value.constructor === Object;
|
|
102
|
+
result.push(_validateType.getResult(keyName, isObject, getDetails(isObject)));
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case RegExp: {
|
|
106
|
+
const isRegex = _validations.isRegex(value);
|
|
107
|
+
result.push(_validateType.getResult(keyName, isRegex, getDetails(isRegex)));
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case null: {
|
|
111
|
+
const isNull = value === null;
|
|
112
|
+
result.push(_validateType.getResult(keyName, isNull, getDetails(isNull)));
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
default: {
|
|
116
|
+
const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
|
|
117
|
+
const isConstructorSame = typeof typeBySchema === 'function' && ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
|
|
118
|
+
const isOK = isInstanceOf && isConstructorSame;
|
|
119
|
+
result.push(_validateType.getResult(keyName, isOK, getDetails(isOK)));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
};
|
|
124
|
+
function validateType(input) {
|
|
125
|
+
const { results, key, value, reqs, nestedKey, typeChecked } = input;
|
|
126
|
+
const typeCheck = handleTypeValidation(key, value, reqs, nestedKey);
|
|
127
|
+
typeCheck.forEach((res) => {
|
|
128
|
+
if (!res.passed) {
|
|
129
|
+
typeChecked.push(false);
|
|
130
|
+
results.errors.push(res.details || '');
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
export default validateType;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils/errors.js
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
receivedType
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return '';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
class ErrorUtility {
|
|
2
|
+
getMissingError(key = 'na') {
|
|
3
|
+
return `Missing value for '${key}'`;
|
|
4
|
+
}
|
|
5
|
+
getErrorDetails(key, expectedType, receivedValue) {
|
|
6
|
+
const receivedType = this.getTypeString(receivedValue);
|
|
7
|
+
const expectedOutput = this.getExpectedTypeString(expectedType);
|
|
8
|
+
|
|
9
|
+
if (expectedOutput === receivedType) return '';
|
|
10
|
+
|
|
11
|
+
return `Check the type of '${key}': expected ${expectedOutput}, received ${receivedType}`;
|
|
12
|
+
}
|
|
13
|
+
joinErrors(errorsArr, separator = '; ') {
|
|
14
|
+
let _a;
|
|
15
|
+
return ((_a = errorsArr === null || errorsArr === void 0 ? void 0 : errorsArr.filter(error => error === null || error === void 0 ? void 0 : error.trim())) === null || _a === void 0 ? void 0 : _a.join(separator)) || '';
|
|
16
|
+
}
|
|
17
|
+
getTypeString(value) {
|
|
18
|
+
let _a;
|
|
19
|
+
if (value === undefined)
|
|
20
|
+
return 'undefined';
|
|
21
|
+
if (value === null)
|
|
22
|
+
return 'null';
|
|
23
|
+
return ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) || typeof value || 'unknown';
|
|
24
|
+
}
|
|
25
|
+
getExpectedTypeString(expectedType) {
|
|
26
|
+
return (expectedType === null || expectedType === void 0 ? void 0 : expectedType.name) || String(expectedType) || 'unknown';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const errors = new ErrorUtility();
|
|
30
|
+
export default errors;
|
package/dist/utils/helpers.js
CHANGED
|
@@ -1,69 +1,67 @@
|
|
|
1
|
-
import { defaultSchemaKeys } from "../Schema.js";
|
|
2
1
|
import _validations from "./validations.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
import { defaultSchemaKeys } from "../Schema.js";
|
|
3
|
+
class HelperUtility {
|
|
4
|
+
checkIsNested(obj) {
|
|
5
|
+
if (!_validations.isObject(obj)) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
const objKeys = Object.keys(obj);
|
|
9
|
+
return !objKeys.every(key => defaultSchemaKeys.includes(key));
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
checkNestedIsMissing(reqs, data) {
|
|
12
|
+
const isRequired = reqs.required;
|
|
13
|
+
const isUndefined = data === undefined;
|
|
14
|
+
const isEmpty = _validations.isObject(data) && Object.keys(data).length === 0;
|
|
15
|
+
return isRequired && (isUndefined || isEmpty);
|
|
13
16
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const hasArrayOfKeys = (Array.isArray(onlyKeys) && onlyKeys.length > 0);
|
|
23
|
-
const hasStringKey = (typeof onlyKeys === 'string' && onlyKeys.length > 0);
|
|
24
|
-
return hasArrayOfKeys || hasStringKey;
|
|
25
|
-
};
|
|
26
|
-
_helpers.needValidation = (key, hasLimits, onlyKeys) => {
|
|
27
|
-
const noLimits = !hasLimits;
|
|
28
|
-
const keyIsInList = (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
|
|
29
|
-
return noLimits || keyIsInList;
|
|
30
|
-
};
|
|
31
|
-
_helpers.hasMissing = (input) => {
|
|
32
|
-
const { reqs, data, key } = input;
|
|
33
|
-
const isRequired = reqs.required === true;
|
|
34
|
-
const missingData = (data === undefined || key in data === false || data[key] === undefined);
|
|
35
|
-
return isRequired && missingData;
|
|
36
|
-
};
|
|
37
|
-
_helpers.compareArrs = (v1, v2) => {
|
|
38
|
-
if (v1.length !== v2.length)
|
|
39
|
-
return false;
|
|
40
|
-
return v1.every((el, i) => {
|
|
41
|
-
if (_validations.isObject(el)) {
|
|
42
|
-
return JSON.stringify(el) === JSON.stringify(v2[i]);
|
|
17
|
+
areKeysLimited(onlyKeys) {
|
|
18
|
+
const hasArrayOfKeys = Array.isArray(onlyKeys) && onlyKeys.length > 0;
|
|
19
|
+
const hasStringKey = typeof onlyKeys === 'string' && onlyKeys.length > 0;
|
|
20
|
+
return hasArrayOfKeys || hasStringKey;
|
|
21
|
+
}
|
|
22
|
+
needValidation(key, hasLimits, onlyKeys) {
|
|
23
|
+
if (!hasLimits) {
|
|
24
|
+
return true;
|
|
43
25
|
}
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
26
|
+
return key === onlyKeys || (Array.isArray(onlyKeys) && onlyKeys.includes(key));
|
|
27
|
+
}
|
|
28
|
+
hasMissing(input) {
|
|
29
|
+
const { reqs, data, key } = input;
|
|
30
|
+
const isRequired = !!reqs.required;
|
|
31
|
+
const missingData = (data === undefined ||
|
|
32
|
+
!(key in data) ||
|
|
33
|
+
data[key] === undefined);
|
|
34
|
+
return isRequired && missingData;
|
|
35
|
+
}
|
|
36
|
+
compareArrs(v1, v2) {
|
|
37
|
+
if (v1.length !== v2.length) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return v1.every((element, index) => {
|
|
41
|
+
if (_validations.isObject(element)) {
|
|
42
|
+
return JSON.stringify(element) === JSON.stringify(v2[index]);
|
|
43
|
+
}
|
|
44
|
+
return v2[index] === element;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
compareObjs(obj1, obj2) {
|
|
48
|
+
return this.deepEqual(obj1, obj2);
|
|
49
|
+
}
|
|
50
|
+
deepEqual(value1, value2) {
|
|
51
|
+
if (value1 === value2) {
|
|
50
52
|
return true;
|
|
51
53
|
}
|
|
52
|
-
if (typeof
|
|
54
|
+
if (typeof value1 !== 'object' || value1 === null ||
|
|
55
|
+
typeof value2 !== 'object' || value2 === null) {
|
|
53
56
|
return false;
|
|
54
57
|
}
|
|
55
|
-
const keys1 = Object.keys(
|
|
56
|
-
const keys2 = Object.keys(
|
|
58
|
+
const keys1 = Object.keys(value1);
|
|
59
|
+
const keys2 = Object.keys(value2);
|
|
57
60
|
if (keys1.length !== keys2.length) {
|
|
58
61
|
return false;
|
|
59
62
|
}
|
|
60
|
-
|
|
61
|
-
if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return true;
|
|
63
|
+
return keys1.every(key => keys2.includes(key) && this.deepEqual(value1[key], value2[key]));
|
|
66
64
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
export default
|
|
65
|
+
}
|
|
66
|
+
const helpers = new HelperUtility();
|
|
67
|
+
export default helpers;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
getResult
|
|
1
|
+
import { ValidationDetails } from "../constants/details.js";
|
|
2
|
+
class TypeValidationUtility {
|
|
3
|
+
getResult(key, passed, details = ValidationDetails.OK) {
|
|
4
4
|
return {
|
|
5
|
-
key
|
|
6
|
-
passed
|
|
7
|
-
details
|
|
5
|
+
key,
|
|
6
|
+
passed,
|
|
7
|
+
details
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
-
}
|
|
11
|
-
|
|
10
|
+
}
|
|
11
|
+
const validateType = new TypeValidationUtility();
|
|
12
|
+
export default validateType;
|
|
@@ -1,154 +1,158 @@
|
|
|
1
1
|
import _helpers from "./helpers.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
2
|
+
class ValidationUtility {
|
|
3
|
+
isString(value) {
|
|
4
|
+
return typeof value === 'string';
|
|
5
|
+
}
|
|
6
|
+
isNumber(value) {
|
|
7
|
+
return typeof value === 'number';
|
|
8
|
+
}
|
|
9
|
+
isArray(value) {
|
|
10
|
+
return Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
isObject(value) {
|
|
13
|
+
let _a;
|
|
14
|
+
return value !== null &&
|
|
15
|
+
typeof value === 'object' &&
|
|
16
|
+
((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Object' &&
|
|
17
|
+
!Array.isArray(value);
|
|
18
|
+
}
|
|
19
|
+
isDate(value) {
|
|
20
|
+
return value instanceof Date && String(value) !== 'Invalid Date';
|
|
21
|
+
}
|
|
22
|
+
isRegex(value) {
|
|
23
|
+
return value instanceof RegExp;
|
|
24
|
+
}
|
|
25
|
+
isBoolean(value) {
|
|
26
|
+
return typeof value === 'boolean';
|
|
27
|
+
}
|
|
28
|
+
isNull(value) {
|
|
29
|
+
return value === null;
|
|
30
|
+
}
|
|
31
|
+
isUndefined(value) {
|
|
32
|
+
return value === undefined;
|
|
33
|
+
}
|
|
34
|
+
isNullOrUndefined(value) {
|
|
35
|
+
return value === undefined || value === null;
|
|
36
|
+
}
|
|
37
|
+
isEmail(value) {
|
|
38
|
+
const emailRegex = /^(?!.*\.\.)[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
39
|
+
return emailRegex.test(value);
|
|
40
|
+
}
|
|
41
|
+
isDateYYYYMMDD(value) {
|
|
42
|
+
const regex = /^\d{4}-\d{2}-\d{2}$/;
|
|
43
|
+
return regex.test(value);
|
|
44
|
+
}
|
|
45
|
+
isHex(value) {
|
|
46
|
+
const regex = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
|
|
47
|
+
return regex.test(value);
|
|
48
|
+
}
|
|
49
|
+
lengthIs(value, length) {
|
|
50
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
51
|
+
return false;
|
|
52
|
+
if (typeof length !== 'number')
|
|
53
|
+
return false;
|
|
54
|
+
return value.length === length;
|
|
55
|
+
}
|
|
56
|
+
lengthNot(value, length) {
|
|
57
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
58
|
+
return false;
|
|
59
|
+
if (typeof length !== 'number')
|
|
60
|
+
return false;
|
|
61
|
+
return value.length !== length;
|
|
62
|
+
}
|
|
63
|
+
lengthMin(value, min) {
|
|
64
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
65
|
+
return false;
|
|
66
|
+
if (typeof min !== 'number')
|
|
67
|
+
return false;
|
|
68
|
+
return value.length >= min;
|
|
69
|
+
}
|
|
70
|
+
lengthMax(value, max) {
|
|
71
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
72
|
+
return false;
|
|
73
|
+
if (typeof max !== 'number')
|
|
74
|
+
return false;
|
|
75
|
+
return value.length <= max;
|
|
76
|
+
}
|
|
77
|
+
isNumberGte(value, gte) {
|
|
78
|
+
return typeof value === 'number' && value >= gte;
|
|
79
|
+
}
|
|
80
|
+
isNumberGt(value, gt) {
|
|
81
|
+
return typeof value === 'number' && value > gt;
|
|
82
|
+
}
|
|
83
|
+
isNumberLte(value, lte) {
|
|
84
|
+
return typeof value === 'number' && value <= lte;
|
|
85
|
+
}
|
|
86
|
+
isNumberLt(value, lt) {
|
|
87
|
+
return typeof value === 'number' && value < lt;
|
|
88
|
+
}
|
|
89
|
+
isDateGte(date1, date2) {
|
|
90
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return date1 >= date2;
|
|
94
|
+
}
|
|
95
|
+
isDateGt(date1, date2) {
|
|
96
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return date1 > date2;
|
|
100
|
+
}
|
|
101
|
+
isDateLte(date1, date2) {
|
|
102
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return date1 <= date2;
|
|
106
|
+
}
|
|
107
|
+
isDateLt(date1, date2) {
|
|
108
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return date1 < date2;
|
|
112
|
+
}
|
|
113
|
+
hasKey(obj, key) {
|
|
114
|
+
if (!this.isObject(obj))
|
|
115
|
+
return false;
|
|
116
|
+
return key in obj;
|
|
117
|
+
}
|
|
118
|
+
is(value, compareTo) {
|
|
119
|
+
if (value === compareTo) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
const bothArrays = this.isArray(value) && this.isArray(compareTo);
|
|
123
|
+
if (bothArrays) {
|
|
124
|
+
return _helpers.compareArrs(value, compareTo);
|
|
125
|
+
}
|
|
126
|
+
const bothObjects = this.isObject(value) && this.isObject(compareTo);
|
|
127
|
+
if (bothObjects) {
|
|
128
|
+
return _helpers.compareObjs(value, compareTo);
|
|
129
|
+
}
|
|
130
|
+
const bothDates = this.isDate(value) && this.isDate(compareTo);
|
|
131
|
+
if (bothDates) {
|
|
132
|
+
return value.getTime() === compareTo.getTime();
|
|
133
|
+
}
|
|
134
|
+
return value === compareTo;
|
|
135
|
+
}
|
|
136
|
+
not(value, not) {
|
|
137
|
+
return !this.is(value, not);
|
|
138
|
+
}
|
|
139
|
+
regexTested(value, regexp) {
|
|
140
|
+
if (!regexp || !(regexp instanceof RegExp)) {
|
|
141
|
+
throw new Error('regexp argument is incorrect');
|
|
142
|
+
}
|
|
143
|
+
return regexp.test(value);
|
|
144
|
+
}
|
|
145
|
+
get gt() { return this.isNumberGt; }
|
|
146
|
+
get gte() { return this.isNumberGte; }
|
|
147
|
+
get lte() { return this.isNumberLte; }
|
|
148
|
+
get lt() { return this.isNumberLt; }
|
|
149
|
+
get eq() { return this.is; }
|
|
150
|
+
get isNot() { return this.not; }
|
|
151
|
+
get ne() { return this.not; }
|
|
152
|
+
get neq() { return this.not; }
|
|
153
|
+
get regex() { return this.regexTested; }
|
|
154
|
+
get regexp() { return this.regexTested; }
|
|
155
|
+
get test() { return this.regexTested; }
|
|
156
|
+
}
|
|
157
|
+
const validations = new ValidationUtility();
|
|
158
|
+
export default validations;
|