validno 0.4.0 → 0.4.3
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/constants/details.d.ts +3 -2
- package/dist/constants/details.d.ts.map +1 -1
- package/dist/constants/details.js +2 -1
- package/dist/dev.js +74 -52
- package/dist/engine/methods/handleNestedKey.js +1 -1
- package/dist/engine/methods/validate.d.ts.map +1 -1
- package/dist/engine/methods/validate.js +7 -0
- package/dist/engine/methods/validateType.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/utils/helpers.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/ValidnoResult.js +0 -102
- package/dist/checkType.js +0 -1
- package/dist/validate/index.js +0 -1
- package/dist/validate/validate.js +0 -151
- package/dist/validate.js +0 -151
- package/dist/validateEngine/ValidateEngine.js +0 -44
- package/dist/validateEngine/index.js +0 -2
- package/dist/validateEngine/methods/ValidateEngine.js +0 -139
- package/dist/validateEngine/methods/checkRulesForKey.js +0 -15
- package/dist/validateEngine/methods/checkValueType.js +0 -134
- package/dist/validateEngine/methods/finalizeValidation.js +0 -15
- package/dist/validateEngine/methods/finishValidation.js +0 -15
- package/dist/validateEngine/methods/handleKey.js +0 -43
- package/dist/validateEngine/methods/handleMissingKey.js +0 -19
- package/dist/validateEngine/methods/handleMissingKeyValidation.js +0 -9
- package/dist/validateEngine/methods/handleNestedKey.js +0 -19
- package/dist/validateEngine/methods/validate.js +0 -14
- package/dist/validateEngine/methods/validateKey.js +0 -31
- package/dist/validateEngine/methods/validateKeyDetails.js +0 -13
- package/dist/validateEngine/methods/validateKeyValue.js +0 -13
- package/dist/validateEngine/methods/validateNestedKey.js +0 -19
- package/dist/validateEngine/methods/validateType.js +0 -134
- package/dist/validateEngine/validate.js +0 -14
- package/dist/validateRules.js +0 -159
- package/dist/validateSchema/ValidateEngine.js +0 -147
- package/dist/validateSchema/index.js +0 -6
- package/dist/validateSchema/validate.js +0 -151
- package/dist/validateSchema.js +0 -6
- package/dist/validateType.js +0 -124
|
@@ -1,134 +0,0 @@
|
|
|
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 ? null : _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;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import _helpers from "../utils/helpers.js";
|
|
2
|
-
function validate(data, validationKeys) {
|
|
3
|
-
const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
|
|
4
|
-
const schemaKeys = Object.entries(this.definition);
|
|
5
|
-
for (const [key, reqs] of schemaKeys) {
|
|
6
|
-
const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, validationKeys);
|
|
7
|
-
if (!toBeValidated)
|
|
8
|
-
continue;
|
|
9
|
-
const keyResult = this.validateKey({ key, data, reqs });
|
|
10
|
-
this.result.merge(keyResult);
|
|
11
|
-
}
|
|
12
|
-
return this.result.finish();
|
|
13
|
-
}
|
|
14
|
-
export default validate;
|
package/dist/validateRules.js
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import _validations from "./utils/validations.js";
|
|
2
|
-
export const rulesParams = {
|
|
3
|
-
lengthMin: {
|
|
4
|
-
allowedTypes: [String]
|
|
5
|
-
}
|
|
6
|
-
};
|
|
7
|
-
const ensureRuleHasCorrectType = (value, allowedTypes) => {
|
|
8
|
-
const isInAllowedList = allowedTypes.some(TYPE => {
|
|
9
|
-
const getType = (el) => Object.prototype.toString.call(el);
|
|
10
|
-
return getType(new TYPE()) == getType(value);
|
|
11
|
-
});
|
|
12
|
-
return isInAllowedList;
|
|
13
|
-
};
|
|
14
|
-
const rulesFunctions = {
|
|
15
|
-
custom: (key, val, func, extra) => {
|
|
16
|
-
return func(val, extra);
|
|
17
|
-
},
|
|
18
|
-
isEmail: (key, val) => {
|
|
19
|
-
return {
|
|
20
|
-
result: _validations.isEmail(val),
|
|
21
|
-
details: `Значение должно соответствовать формату name@email.ru`
|
|
22
|
-
};
|
|
23
|
-
},
|
|
24
|
-
is: (key, val, equalTo) => {
|
|
25
|
-
return {
|
|
26
|
-
result: _validations.is(val, equalTo),
|
|
27
|
-
details: `Значение должно быть "${equalTo}"`
|
|
28
|
-
};
|
|
29
|
-
},
|
|
30
|
-
isNot: (key, val, notEqualTo) => {
|
|
31
|
-
return {
|
|
32
|
-
result: _validations.isNot(val, notEqualTo),
|
|
33
|
-
details: `Значение "${notEqualTo}" недопустимо`
|
|
34
|
-
};
|
|
35
|
-
},
|
|
36
|
-
min: (key, val, min) => {
|
|
37
|
-
return {
|
|
38
|
-
result: _validations.isNumberGte(val, min),
|
|
39
|
-
details: "Значение не может быть меньше " + min
|
|
40
|
-
};
|
|
41
|
-
},
|
|
42
|
-
max: (key, val, max) => {
|
|
43
|
-
return {
|
|
44
|
-
result: _validations.isNumberLte(val, max),
|
|
45
|
-
details: "Значение не может быть больше " + max
|
|
46
|
-
};
|
|
47
|
-
},
|
|
48
|
-
minMax: (key, val, minMax) => {
|
|
49
|
-
const [min, max] = minMax;
|
|
50
|
-
return {
|
|
51
|
-
result: _validations.isNumberGte(val, min) && _validations.isNumberLte(val, max),
|
|
52
|
-
details: `Значение должно быть в пределах ${min}-${max}`
|
|
53
|
-
};
|
|
54
|
-
},
|
|
55
|
-
length: (key, val, length) => {
|
|
56
|
-
return {
|
|
57
|
-
result: _validations.lengthIs(val, length),
|
|
58
|
-
details: "Количество символов должно быть равным " + length
|
|
59
|
-
};
|
|
60
|
-
},
|
|
61
|
-
lengthNot: (key, val, lengthNot) => {
|
|
62
|
-
return {
|
|
63
|
-
result: _validations.lengthNot(val, lengthNot),
|
|
64
|
-
details: "Количество символов не должно быть равным " + lengthNot
|
|
65
|
-
};
|
|
66
|
-
},
|
|
67
|
-
lengthMinMax: (key, val, minMax) => {
|
|
68
|
-
const [min, max] = minMax;
|
|
69
|
-
return {
|
|
70
|
-
result: _validations.lengthMin(val, min) && _validations.lengthMax(val, max),
|
|
71
|
-
details: `Длина должна быть от ${min} до ${max} символов`
|
|
72
|
-
};
|
|
73
|
-
},
|
|
74
|
-
lengthMin: (key, val, min) => {
|
|
75
|
-
ensureRuleHasCorrectType(val, rulesParams['lengthMin'].allowedTypes);
|
|
76
|
-
return {
|
|
77
|
-
result: _validations.lengthMin(val, min),
|
|
78
|
-
details: `Длина не может быть меньше ${min} символов`
|
|
79
|
-
};
|
|
80
|
-
},
|
|
81
|
-
lengthMax: (key, val, max) => {
|
|
82
|
-
return {
|
|
83
|
-
result: _validations.lengthMax(val, max),
|
|
84
|
-
details: `Длина не может быть больше ${max} символов`
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
regex: (key, val, regex) => {
|
|
88
|
-
return {
|
|
89
|
-
result: _validations.regexTested(val, regex),
|
|
90
|
-
details: "Значение не соответствует допустимому формату"
|
|
91
|
-
};
|
|
92
|
-
},
|
|
93
|
-
enum: (key, value, allowedList) => {
|
|
94
|
-
const output = {
|
|
95
|
-
result: true,
|
|
96
|
-
details: ''
|
|
97
|
-
};
|
|
98
|
-
if (!Array.isArray(value)) {
|
|
99
|
-
const isCorrect = allowedList.includes(value);
|
|
100
|
-
output.result = isCorrect,
|
|
101
|
-
output.details = isCorrect ? '' : `Значение "${value}" недопустимо`;
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
const incorrectValues = [];
|
|
105
|
-
value.forEach((v) => !allowedList.includes(v) ? incorrectValues.push(v) : {});
|
|
106
|
-
const isCorrect = incorrectValues.length === 0;
|
|
107
|
-
output.result = isCorrect,
|
|
108
|
-
output.details = isCorrect ? '' : `Значения недопустимы: "${incorrectValues.join(', ')}"`;
|
|
109
|
-
}
|
|
110
|
-
return output;
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
function validateRules(key, value, requirements, inputObj) {
|
|
114
|
-
const result = {
|
|
115
|
-
ok: true,
|
|
116
|
-
details: []
|
|
117
|
-
};
|
|
118
|
-
if (requirements.required !== true && value === undefined)
|
|
119
|
-
return result;
|
|
120
|
-
if (!requirements || !requirements.rules || !Object.keys(requirements.rules).length) {
|
|
121
|
-
return result;
|
|
122
|
-
}
|
|
123
|
-
const rules = requirements.rules;
|
|
124
|
-
const title = requirements.title || key;
|
|
125
|
-
const allResults = [];
|
|
126
|
-
const allRulesKeys = Object.keys(rules);
|
|
127
|
-
let i = 0;
|
|
128
|
-
while (i < allRulesKeys.length) {
|
|
129
|
-
const ruleName = allRulesKeys[i];
|
|
130
|
-
if (ruleName in rulesFunctions === false) {
|
|
131
|
-
i++;
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
const func = rulesFunctions[ruleName];
|
|
135
|
-
const args = rules[ruleName];
|
|
136
|
-
const result = func(key, value, args, { schema: this.schema, input: inputObj });
|
|
137
|
-
if (requirements.customMessage && typeof requirements.customMessage === 'function') {
|
|
138
|
-
result.details = requirements.customMessage({
|
|
139
|
-
keyword: ruleName,
|
|
140
|
-
value: value,
|
|
141
|
-
key: key,
|
|
142
|
-
title: title,
|
|
143
|
-
reqs: requirements,
|
|
144
|
-
schema: this.schema,
|
|
145
|
-
rules: rules,
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
allResults.push(result);
|
|
149
|
-
i++;
|
|
150
|
-
}
|
|
151
|
-
const failedResults = allResults.filter(el => el.result === false);
|
|
152
|
-
if (failedResults.length) {
|
|
153
|
-
result.ok = false;
|
|
154
|
-
result.details = failedResults.map(el => el.details);
|
|
155
|
-
}
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
;
|
|
159
|
-
export default validateRules;
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import validateType from "../validateType.js";
|
|
2
|
-
import validateRules from "../validateRules.js";
|
|
3
|
-
import { ValidationIds } from "../constants/details.js";
|
|
4
|
-
import ValidnoResult from "../ValidnoResult.js";
|
|
5
|
-
import _errors from "../utils/errors.js";
|
|
6
|
-
import _helpers from "../utils/helpers.js";
|
|
7
|
-
class ValidateEngine {
|
|
8
|
-
constructor(definition) {
|
|
9
|
-
this.definition = definition;
|
|
10
|
-
this.result = new ValidnoResult();
|
|
11
|
-
}
|
|
12
|
-
validate(data, validationKeys) {
|
|
13
|
-
const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
|
|
14
|
-
const schemaKeys = Object.entries(this.definition);
|
|
15
|
-
for (const [key, reqs] of schemaKeys) {
|
|
16
|
-
const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, validationKeys);
|
|
17
|
-
if (!toBeValidated)
|
|
18
|
-
continue;
|
|
19
|
-
const keyResult = this.validateKey({ key, data, reqs });
|
|
20
|
-
this.result.merge(keyResult);
|
|
21
|
-
}
|
|
22
|
-
return this.result.finish();
|
|
23
|
-
}
|
|
24
|
-
handleMissingKey(schema, input) {
|
|
25
|
-
const { key, nestedKey, data, reqs } = input;
|
|
26
|
-
const messageKey = nestedKey || key;
|
|
27
|
-
const messageTitle = reqs.title || messageKey;
|
|
28
|
-
if (!reqs.customMessage) {
|
|
29
|
-
return _errors.getMissingError(messageKey);
|
|
30
|
-
}
|
|
31
|
-
const errorMessage = reqs.customMessage({
|
|
32
|
-
keyword: ValidationIds.Missing,
|
|
33
|
-
value: data[key],
|
|
34
|
-
key: messageKey,
|
|
35
|
-
title: messageTitle,
|
|
36
|
-
reqs,
|
|
37
|
-
schema,
|
|
38
|
-
});
|
|
39
|
-
return errorMessage;
|
|
40
|
-
}
|
|
41
|
-
validateNestedKey(input) {
|
|
42
|
-
const { results, key, nestedKey, data, reqs } = input;
|
|
43
|
-
const nestedKeys = Object.keys(reqs);
|
|
44
|
-
const nestedResults = [];
|
|
45
|
-
for (const itemKey of nestedKeys) {
|
|
46
|
-
const deepParams = {
|
|
47
|
-
key: itemKey,
|
|
48
|
-
data: data[key],
|
|
49
|
-
reqs: reqs[itemKey],
|
|
50
|
-
nestedKey: `${nestedKey}.${itemKey}`
|
|
51
|
-
};
|
|
52
|
-
const deepResults = this.validateKey(deepParams);
|
|
53
|
-
nestedResults.push(deepResults.ok);
|
|
54
|
-
results.merge(deepResults);
|
|
55
|
-
}
|
|
56
|
-
results.fixParentByChilds(nestedKey, nestedResults);
|
|
57
|
-
return results;
|
|
58
|
-
}
|
|
59
|
-
validateKey(input) {
|
|
60
|
-
let { results, key, nestedKey, data, reqs } = input;
|
|
61
|
-
if (data === undefined) {
|
|
62
|
-
const noDataResult = new ValidnoResult();
|
|
63
|
-
noDataResult.setNoData();
|
|
64
|
-
noDataResult.finish();
|
|
65
|
-
return noDataResult;
|
|
66
|
-
}
|
|
67
|
-
if (!results)
|
|
68
|
-
results = new ValidnoResult();
|
|
69
|
-
if (!nestedKey)
|
|
70
|
-
nestedKey = key;
|
|
71
|
-
const hasMissing = _helpers.hasMissing(input);
|
|
72
|
-
if (_helpers.checkNestedIsMissing(reqs, data)) {
|
|
73
|
-
return this.handleMissingNestedKey(results, nestedKey);
|
|
74
|
-
}
|
|
75
|
-
if (_helpers.checkIsNested(reqs)) {
|
|
76
|
-
return this.validateNestedKey({ results, key, data, reqs, nestedKey });
|
|
77
|
-
}
|
|
78
|
-
return this.validateKeyDetails({
|
|
79
|
-
results,
|
|
80
|
-
key,
|
|
81
|
-
nestedKey,
|
|
82
|
-
data,
|
|
83
|
-
reqs,
|
|
84
|
-
hasMissing,
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
handleMissingNestedKey(results, nestedKey) {
|
|
88
|
-
results.setMissing(nestedKey);
|
|
89
|
-
return results;
|
|
90
|
-
}
|
|
91
|
-
validateKeyDetails(params) {
|
|
92
|
-
const { results, key, nestedKey, data, reqs, hasMissing } = params;
|
|
93
|
-
const missedCheck = [];
|
|
94
|
-
const typeChecked = [];
|
|
95
|
-
const rulesChecked = [];
|
|
96
|
-
if (hasMissing) {
|
|
97
|
-
return this.handleMissingKeyValidation({ results, key, nestedKey, data, reqs }, missedCheck);
|
|
98
|
-
}
|
|
99
|
-
this.checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
|
|
100
|
-
this.checkRulesForKey(results, nestedKey, data[key], reqs, data, rulesChecked);
|
|
101
|
-
return this.finalizeValidation({ results, nestedKey, missedCheck, typeChecked, rulesChecked });
|
|
102
|
-
}
|
|
103
|
-
handleMissingKeyValidation(params, missedCheck) {
|
|
104
|
-
const schema = this.definition;
|
|
105
|
-
const { results, key, nestedKey, data, reqs } = params;
|
|
106
|
-
const errMsg = this.handleMissingKey(schema, { key, nestedKey, data, reqs });
|
|
107
|
-
missedCheck.push(false);
|
|
108
|
-
results.setMissing(nestedKey, errMsg);
|
|
109
|
-
return results;
|
|
110
|
-
}
|
|
111
|
-
checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
|
|
112
|
-
const typeCheck = validateType(key, value, reqs, nestedKey);
|
|
113
|
-
typeCheck.forEach((res) => {
|
|
114
|
-
if (!res.passed) {
|
|
115
|
-
typeChecked.push(false);
|
|
116
|
-
results.errors.push(res.details || '');
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
|
|
121
|
-
const ruleCheck = validateRules.call(this, nestedKey, value, reqs, data);
|
|
122
|
-
if (!ruleCheck.ok) {
|
|
123
|
-
rulesChecked.push(false);
|
|
124
|
-
ruleCheck.details.forEach((el) => {
|
|
125
|
-
if (!(nestedKey in results.errorsByKeys))
|
|
126
|
-
results.errorsByKeys[nestedKey] = [];
|
|
127
|
-
results.errors.push(el);
|
|
128
|
-
results.errorsByKeys[nestedKey] = ['1'];
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
finalizeValidation(checks) {
|
|
133
|
-
const { results, nestedKey, missedCheck, typeChecked, rulesChecked } = checks;
|
|
134
|
-
if (missedCheck.length)
|
|
135
|
-
results.setMissing(nestedKey);
|
|
136
|
-
const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
|
|
137
|
-
if (!isPassed) {
|
|
138
|
-
results.setFailed(nestedKey);
|
|
139
|
-
results.errorsByKeys[nestedKey] = [...results.errors];
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
results.setPassed(nestedKey);
|
|
143
|
-
}
|
|
144
|
-
return results.finish();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
export default ValidateEngine;
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import validateType from "../validateType.js";
|
|
2
|
-
import validateRules from "../validateRules.js";
|
|
3
|
-
import { ValidationIds } from "../constants/details.js";
|
|
4
|
-
import ValidnoResult from "../ValidnoResult.js";
|
|
5
|
-
import _errors from "../utils/errors.js";
|
|
6
|
-
import _helpers from "../utils/helpers.js";
|
|
7
|
-
class ValidateEngine {
|
|
8
|
-
constructor(definition) {
|
|
9
|
-
this.definition = definition;
|
|
10
|
-
this.result = new ValidnoResult();
|
|
11
|
-
}
|
|
12
|
-
validate(data, validationKeys) {
|
|
13
|
-
const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
|
|
14
|
-
const schemaKeys = Object.entries(this.definition);
|
|
15
|
-
for (const [key, reqs] of schemaKeys) {
|
|
16
|
-
const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, validationKeys);
|
|
17
|
-
if (!toBeValidated)
|
|
18
|
-
continue;
|
|
19
|
-
const keyResult = this.validateKey({ key, data, reqs });
|
|
20
|
-
this.result.merge(keyResult);
|
|
21
|
-
}
|
|
22
|
-
return this.result.finish();
|
|
23
|
-
}
|
|
24
|
-
handleMissingKey(schema, input) {
|
|
25
|
-
const { key, nestedKey, data, reqs } = input;
|
|
26
|
-
const messageKey = nestedKey || key;
|
|
27
|
-
const messageTitle = reqs.title || messageKey;
|
|
28
|
-
if (!reqs.customMessage) {
|
|
29
|
-
return _errors.getMissingError(messageKey);
|
|
30
|
-
}
|
|
31
|
-
const errorMessage = reqs.customMessage({
|
|
32
|
-
keyword: ValidationIds.Missing,
|
|
33
|
-
value: data[key],
|
|
34
|
-
key: messageKey,
|
|
35
|
-
title: messageTitle,
|
|
36
|
-
reqs,
|
|
37
|
-
schema,
|
|
38
|
-
});
|
|
39
|
-
return errorMessage;
|
|
40
|
-
}
|
|
41
|
-
validateNestedKey(input) {
|
|
42
|
-
const { results, key, nestedKey, data, reqs } = input;
|
|
43
|
-
const nestedKeys = Object.keys(reqs);
|
|
44
|
-
const nestedResults = [];
|
|
45
|
-
for (const itemKey of nestedKeys) {
|
|
46
|
-
const deepParams = {
|
|
47
|
-
key: itemKey,
|
|
48
|
-
data: data[key],
|
|
49
|
-
reqs: reqs[itemKey],
|
|
50
|
-
nestedKey: `${nestedKey}.${itemKey}`
|
|
51
|
-
};
|
|
52
|
-
const deepResults = this.validateKey(deepParams);
|
|
53
|
-
nestedResults.push(deepResults.ok);
|
|
54
|
-
results.merge(deepResults);
|
|
55
|
-
}
|
|
56
|
-
results.fixParentByChilds(nestedKey, nestedResults);
|
|
57
|
-
return results;
|
|
58
|
-
}
|
|
59
|
-
validateKey(input) {
|
|
60
|
-
let { results, key, nestedKey, data, reqs } = input;
|
|
61
|
-
if (data === undefined) {
|
|
62
|
-
const noDataResult = new ValidnoResult();
|
|
63
|
-
noDataResult.setNoData();
|
|
64
|
-
noDataResult.finish();
|
|
65
|
-
return noDataResult;
|
|
66
|
-
}
|
|
67
|
-
if (!results)
|
|
68
|
-
results = new ValidnoResult();
|
|
69
|
-
if (!nestedKey)
|
|
70
|
-
nestedKey = key;
|
|
71
|
-
const hasMissing = _helpers.hasMissing(input);
|
|
72
|
-
if (_helpers.checkNestedIsMissing(reqs, data)) {
|
|
73
|
-
return this.handleMissingNestedKey(results, nestedKey);
|
|
74
|
-
}
|
|
75
|
-
if (_helpers.checkIsNested(reqs)) {
|
|
76
|
-
return this.validateNestedKey({ results, key, data, reqs, nestedKey });
|
|
77
|
-
}
|
|
78
|
-
return this.validateKeyDetails({
|
|
79
|
-
results,
|
|
80
|
-
key,
|
|
81
|
-
nestedKey,
|
|
82
|
-
data,
|
|
83
|
-
reqs,
|
|
84
|
-
hasMissing,
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
handleMissingNestedKey(results, nestedKey) {
|
|
88
|
-
results.setMissing(nestedKey);
|
|
89
|
-
return results;
|
|
90
|
-
}
|
|
91
|
-
validateKeyDetails(params) {
|
|
92
|
-
const { results, key, nestedKey, data, reqs, hasMissing } = params;
|
|
93
|
-
const missedCheck = [];
|
|
94
|
-
const typeChecked = [];
|
|
95
|
-
const rulesChecked = [];
|
|
96
|
-
if (hasMissing) {
|
|
97
|
-
return this.handleMissingKeyValidation({ results, key, nestedKey, data, reqs }, missedCheck);
|
|
98
|
-
}
|
|
99
|
-
this.checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
|
|
100
|
-
this.checkRulesForKey(results, nestedKey, data[key], reqs, data, rulesChecked);
|
|
101
|
-
return this.finalizeValidation({ results, nestedKey, missedCheck, typeChecked, rulesChecked });
|
|
102
|
-
}
|
|
103
|
-
handleMissingKeyValidation(params, missedCheck) {
|
|
104
|
-
const schema = this.definition;
|
|
105
|
-
const { results, key, nestedKey, data, reqs } = params;
|
|
106
|
-
const errMsg = this.handleMissingKey(schema, { key, nestedKey, data, reqs });
|
|
107
|
-
missedCheck.push(false);
|
|
108
|
-
results.setMissing(nestedKey, errMsg);
|
|
109
|
-
return results;
|
|
110
|
-
}
|
|
111
|
-
checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
|
|
112
|
-
const typeCheck = validateType(key, value, reqs, nestedKey);
|
|
113
|
-
typeCheck.forEach((res) => {
|
|
114
|
-
if (!res.passed) {
|
|
115
|
-
typeChecked.push(false);
|
|
116
|
-
results.errors.push(res.details || '');
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
|
|
121
|
-
const ruleCheck = validateRules.call(this, nestedKey, value, reqs, data);
|
|
122
|
-
if (!ruleCheck.ok) {
|
|
123
|
-
rulesChecked.push(false);
|
|
124
|
-
ruleCheck.details.forEach((el) => {
|
|
125
|
-
if (!(nestedKey in results.errorsByKeys))
|
|
126
|
-
results.errorsByKeys[nestedKey] = [];
|
|
127
|
-
results.errors.push(el);
|
|
128
|
-
results.errorsByKeys[nestedKey] = ['1'];
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
finalizeValidation(checks) {
|
|
133
|
-
const { results, nestedKey, missedCheck, typeChecked, rulesChecked } = checks;
|
|
134
|
-
if (missedCheck.length)
|
|
135
|
-
results.setMissing(nestedKey);
|
|
136
|
-
const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
|
|
137
|
-
if (!isPassed) {
|
|
138
|
-
results.setFailed(nestedKey);
|
|
139
|
-
results.errorsByKeys[nestedKey] = [...results.errors];
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
results.setPassed(nestedKey);
|
|
143
|
-
}
|
|
144
|
-
return results.finish();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
const validateSchema = (schemaDef, data, keysToValidate) => {
|
|
148
|
-
const engine = new ValidateEngine(schemaDef);
|
|
149
|
-
return engine.validate(data, keysToValidate);
|
|
150
|
-
};
|
|
151
|
-
export default validateSchema;
|
package/dist/validateSchema.js
DELETED