validno 0.2.5 → 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 +19 -7
- package/dist/checkType.js +1 -167
- package/dist/constants/details.js +11 -6
- package/dist/constants/schema.js +10 -10
- package/dist/dev.js +65 -46
- 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 +12 -0
- package/dist/utils/validations.js +157 -153
- package/dist/validate/validate.js +151 -0
- package/dist/validate.js +136 -121
- 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/{checkRules.js → validateRules.js} +2 -2
- 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 +124 -0
- package/package.json +1 -1
- package/dev.js +0 -0
- /package/dist/{utils/nested.js → validate/index.js} +0 -0
package/dist/Schema.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
export const defaultSchemaKeys = Object.values(
|
|
1
|
+
import { SchemaFields } from "./constants/schema.js";
|
|
2
|
+
import ValidateEngine from "./engine/ValidateEngine.js";
|
|
3
|
+
export const defaultSchemaKeys = Object.values(SchemaFields);
|
|
4
4
|
export class Schema {
|
|
5
5
|
constructor(inputSchemaDefinition) {
|
|
6
6
|
if (!inputSchemaDefinition || typeof inputSchemaDefinition !== 'object') {
|
|
7
7
|
throw new Error("Invalid schema input");
|
|
8
8
|
}
|
|
9
|
-
this.
|
|
9
|
+
this.definition = inputSchemaDefinition;
|
|
10
10
|
}
|
|
11
11
|
validate(inputData, validationKeys) {
|
|
12
|
-
|
|
12
|
+
const engine = new ValidateEngine(this.definition);
|
|
13
|
+
return engine.validate(inputData, validationKeys);
|
|
13
14
|
}
|
|
14
15
|
}
|
package/dist/ValidnoResult.js
CHANGED
|
@@ -9,16 +9,22 @@ class ValidnoResult {
|
|
|
9
9
|
this.byKeys = (results === null || results === void 0 ? void 0 : results.byKeys) || {};
|
|
10
10
|
this.errorsByKeys = (results === null || results === void 0 ? void 0 : results.errorsByKeys) || {};
|
|
11
11
|
}
|
|
12
|
+
setNoData() {
|
|
13
|
+
this.ok = false;
|
|
14
|
+
this.errors = ['Отсутствует объект для проверки'];
|
|
15
|
+
}
|
|
12
16
|
setKeyStatus(key, result) {
|
|
13
17
|
this.byKeys[key] = result;
|
|
14
18
|
}
|
|
15
19
|
fixParentByChilds(parentKey, childChecks = []) {
|
|
16
|
-
const isEveryOk = childChecks.every(
|
|
20
|
+
const isEveryOk = childChecks.every(check => check === true);
|
|
17
21
|
this.setKeyStatus(parentKey, isEveryOk);
|
|
18
|
-
if (isEveryOk
|
|
22
|
+
if (isEveryOk) {
|
|
19
23
|
this.setPassed(parentKey);
|
|
20
|
-
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
21
26
|
this.setFailed(parentKey);
|
|
27
|
+
}
|
|
22
28
|
}
|
|
23
29
|
setMissing(key, errMsg) {
|
|
24
30
|
const error = errMsg || _errors.getMissingError(key);
|
|
@@ -31,7 +37,7 @@ class ValidnoResult {
|
|
|
31
37
|
this.setKeyStatus(key, true);
|
|
32
38
|
}
|
|
33
39
|
setFailed(key, msg) {
|
|
34
|
-
if (key in this.errorsByKeys
|
|
40
|
+
if (!(key in this.errorsByKeys)) {
|
|
35
41
|
this.errorsByKeys[key] = [];
|
|
36
42
|
}
|
|
37
43
|
this.failed.push(key);
|
|
@@ -51,8 +57,9 @@ class ValidnoResult {
|
|
|
51
57
|
this.passed = [...this.passed, ...resultsNew.passed];
|
|
52
58
|
this.byKeys = Object.assign(Object.assign({}, this.byKeys), resultsNew.byKeys);
|
|
53
59
|
for (const key in resultsNew.errorsByKeys) {
|
|
54
|
-
if (key in this.errorsByKeys
|
|
60
|
+
if (!(key in this.errorsByKeys)) {
|
|
55
61
|
this.errorsByKeys[key] = [];
|
|
62
|
+
}
|
|
56
63
|
this.errorsByKeys[key] = [
|
|
57
64
|
...this.errorsByKeys[key],
|
|
58
65
|
...resultsNew.errorsByKeys[key]
|
|
@@ -68,10 +75,12 @@ class ValidnoResult {
|
|
|
68
75
|
}
|
|
69
76
|
}
|
|
70
77
|
finish() {
|
|
71
|
-
if (this.failed.length)
|
|
78
|
+
if (this.failed.length || this.errors.length) {
|
|
72
79
|
this.ok = false;
|
|
73
|
-
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
74
82
|
this.ok = true;
|
|
83
|
+
}
|
|
75
84
|
this.clearEmptyErrorsByKeys();
|
|
76
85
|
return this;
|
|
77
86
|
}
|
|
@@ -86,5 +95,8 @@ class ValidnoResult {
|
|
|
86
95
|
errorsByKeys: this.errorsByKeys,
|
|
87
96
|
};
|
|
88
97
|
}
|
|
98
|
+
isValid() {
|
|
99
|
+
return this.ok === true;
|
|
100
|
+
}
|
|
89
101
|
}
|
|
90
102
|
export default ValidnoResult;
|
package/dist/checkType.js
CHANGED
|
@@ -1,167 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import _validations from "./utils/validations.js";
|
|
3
|
-
import _errors from "./utils/errors.js";
|
|
4
|
-
const checkTypeMultiple = (key, value, requirements, keyName = key) => {
|
|
5
|
-
const constructorNames = Array.isArray(requirements.type)
|
|
6
|
-
? requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el))
|
|
7
|
-
: [];
|
|
8
|
-
const result = {
|
|
9
|
-
key: keyName,
|
|
10
|
-
passed: false,
|
|
11
|
-
details: _errors.getErrorDetails(keyName, constructorNames.join('/'), value)
|
|
12
|
-
};
|
|
13
|
-
let i = 0;
|
|
14
|
-
if (Array.isArray(requirements.type)) {
|
|
15
|
-
while (i < requirements.type.length) {
|
|
16
|
-
const requirementsRe = Object.assign(Object.assign({}, requirements), { type: requirements.type[i] });
|
|
17
|
-
const check = checkType(key, value, requirementsRe);
|
|
18
|
-
if (check[0].passed === true) {
|
|
19
|
-
result.passed = true;
|
|
20
|
-
result.details = 'OK';
|
|
21
|
-
return result;
|
|
22
|
-
}
|
|
23
|
-
i++;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return result;
|
|
27
|
-
};
|
|
28
|
-
const checkType = (key, value, requirements, keyName = key) => {
|
|
29
|
-
var _a;
|
|
30
|
-
const isNotNull = value !== null;
|
|
31
|
-
const keyTitle = 'title' in requirements ? requirements.title : keyName;
|
|
32
|
-
const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
|
|
33
|
-
if (value === undefined && requirements.required) {
|
|
34
|
-
return [{ key: keyName, passed: false, details: `Значение "${keyName}" отсутствует` }];
|
|
35
|
-
}
|
|
36
|
-
let result = [];
|
|
37
|
-
if (Array.isArray(requirements.type)) {
|
|
38
|
-
return [checkTypeMultiple(key, value, requirements)];
|
|
39
|
-
}
|
|
40
|
-
if (value === undefined && requirements.required !== true) {
|
|
41
|
-
result.push({
|
|
42
|
-
key: keyName,
|
|
43
|
-
passed: true,
|
|
44
|
-
details: 'OK'
|
|
45
|
-
});
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
const customErrDetails = hasCustomMessage ?
|
|
49
|
-
requirements.customMessage({
|
|
50
|
-
keyword: ErrorKeywords.Type,
|
|
51
|
-
value: value,
|
|
52
|
-
key: keyName,
|
|
53
|
-
title: keyTitle,
|
|
54
|
-
reqs: requirements,
|
|
55
|
-
schema: null
|
|
56
|
-
}) :
|
|
57
|
-
null;
|
|
58
|
-
const baseErrDetails = _errors.getErrorDetails(keyName, requirements.type, value);
|
|
59
|
-
const getDetails = (isOK) => isOK ? 'OK' : customErrDetails || baseErrDetails;
|
|
60
|
-
const typeBySchema = requirements.type;
|
|
61
|
-
switch (typeBySchema) {
|
|
62
|
-
case 'any':
|
|
63
|
-
result.push({
|
|
64
|
-
key: keyName,
|
|
65
|
-
passed: true,
|
|
66
|
-
details: 'OK'
|
|
67
|
-
});
|
|
68
|
-
break;
|
|
69
|
-
case Number:
|
|
70
|
-
const isNumber = isNotNull && value.constructor === Number;
|
|
71
|
-
result.push({
|
|
72
|
-
key: keyName,
|
|
73
|
-
passed: isNumber,
|
|
74
|
-
details: getDetails(isNumber)
|
|
75
|
-
});
|
|
76
|
-
break;
|
|
77
|
-
case String:
|
|
78
|
-
const isString = isNotNull && value.constructor === String;
|
|
79
|
-
result.push({
|
|
80
|
-
key: keyName,
|
|
81
|
-
passed: isString,
|
|
82
|
-
details: getDetails(isString)
|
|
83
|
-
});
|
|
84
|
-
break;
|
|
85
|
-
case Date:
|
|
86
|
-
const isDate = isNotNull && value.constructor === Date;
|
|
87
|
-
const isValid = isDate && !isNaN(value.getTime());
|
|
88
|
-
const errorMsg = isValid ? getDetails(isDate) : 'Дата невалидна';
|
|
89
|
-
result.push({
|
|
90
|
-
key: keyName,
|
|
91
|
-
passed: isDate && isValid,
|
|
92
|
-
details: isDate && isValid ? 'OK' : errorMsg
|
|
93
|
-
});
|
|
94
|
-
break;
|
|
95
|
-
case Boolean:
|
|
96
|
-
const isBoolean = isNotNull && value.constructor === Boolean;
|
|
97
|
-
result.push({
|
|
98
|
-
key: keyName,
|
|
99
|
-
passed: isBoolean,
|
|
100
|
-
details: isBoolean ? 'OK' : getDetails(isBoolean)
|
|
101
|
-
});
|
|
102
|
-
break;
|
|
103
|
-
case Array:
|
|
104
|
-
const isArray = isNotNull && value.constructor === Array;
|
|
105
|
-
if (!isArray) {
|
|
106
|
-
result.push({
|
|
107
|
-
key: keyName,
|
|
108
|
-
passed: false,
|
|
109
|
-
details: getDetails(isArray)
|
|
110
|
-
});
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
let isEachChecked = { passed: true, details: "" };
|
|
114
|
-
if ('eachType' in requirements) {
|
|
115
|
-
isEachChecked.passed = value.every((el) => {
|
|
116
|
-
const checkResult = checkType('each of ' + key, el, { type: requirements.eachType, required: true });
|
|
117
|
-
if (!checkResult[0].passed) {
|
|
118
|
-
isEachChecked.details = checkResult[0].details;
|
|
119
|
-
isEachChecked.passed = false;
|
|
120
|
-
}
|
|
121
|
-
return true;
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
const isOk = isArray && isEachChecked.passed;
|
|
125
|
-
result.push({
|
|
126
|
-
key: keyName,
|
|
127
|
-
passed: isOk,
|
|
128
|
-
details: isOk ? 'OK' : !isEachChecked.passed ? isEachChecked.details : getDetails(isOk)
|
|
129
|
-
});
|
|
130
|
-
break;
|
|
131
|
-
case Object:
|
|
132
|
-
const isObject = _validations.isObject(value) && value.constructor === Object;
|
|
133
|
-
result.push({
|
|
134
|
-
key: keyName,
|
|
135
|
-
passed: isObject,
|
|
136
|
-
details: isObject ? 'OK' : getDetails(isObject)
|
|
137
|
-
});
|
|
138
|
-
break;
|
|
139
|
-
case RegExp:
|
|
140
|
-
const isRegex = _validations.isRegex(value);
|
|
141
|
-
result.push({
|
|
142
|
-
key: keyName,
|
|
143
|
-
passed: isRegex,
|
|
144
|
-
details: isRegex ? 'OK' : getDetails(isRegex)
|
|
145
|
-
});
|
|
146
|
-
break;
|
|
147
|
-
case null:
|
|
148
|
-
const isNull = value === null;
|
|
149
|
-
result.push({
|
|
150
|
-
key: keyName,
|
|
151
|
-
passed: isNull,
|
|
152
|
-
details: isNull ? 'OK' : getDetails(isNull)
|
|
153
|
-
});
|
|
154
|
-
break;
|
|
155
|
-
default:
|
|
156
|
-
const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
|
|
157
|
-
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);
|
|
158
|
-
const checked = isInstanceOf && isConstructorSame;
|
|
159
|
-
result.push({
|
|
160
|
-
key: keyName,
|
|
161
|
-
passed: checked,
|
|
162
|
-
details: getDetails(checked)
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
return result;
|
|
166
|
-
};
|
|
167
|
-
export default checkType;
|
|
1
|
+
"use strict";
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
export var
|
|
2
|
-
(function (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
})(
|
|
1
|
+
export var ValidationIds;
|
|
2
|
+
(function (ValidationIds) {
|
|
3
|
+
ValidationIds["Missing"] = "missing";
|
|
4
|
+
ValidationIds["Type"] = "type";
|
|
5
|
+
ValidationIds["Rule"] = "rule";
|
|
6
|
+
})(ValidationIds || (ValidationIds = {}));
|
|
7
|
+
export var ValidationDetails;
|
|
8
|
+
(function (ValidationDetails) {
|
|
9
|
+
ValidationDetails["OK"] = "OK";
|
|
10
|
+
ValidationDetails["INVALID_DATE"] = "Invalid date";
|
|
11
|
+
})(ValidationDetails || (ValidationDetails = {}));
|
package/dist/constants/schema.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export var
|
|
2
|
-
(function (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
})(
|
|
1
|
+
export var SchemaFields;
|
|
2
|
+
(function (SchemaFields) {
|
|
3
|
+
SchemaFields["Required"] = "required";
|
|
4
|
+
SchemaFields["Type"] = "type";
|
|
5
|
+
SchemaFields["EachType"] = "eachType";
|
|
6
|
+
SchemaFields["Rules"] = "rules";
|
|
7
|
+
SchemaFields["Title"] = "title";
|
|
8
|
+
SchemaFields["CustomMessage"] = "customMessage";
|
|
9
|
+
SchemaFields["JoinErrors"] = "joinErrors";
|
|
10
|
+
})(SchemaFields || (SchemaFields = {}));
|
package/dist/dev.js
CHANGED
|
@@ -1,51 +1,70 @@
|
|
|
1
|
-
import
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import Schema from './index.js';
|
|
2
|
+
const schema = new Schema({
|
|
3
|
+
name: {
|
|
4
|
+
type: String,
|
|
5
|
+
required: true
|
|
6
|
+
},
|
|
7
|
+
lastVisit: {
|
|
8
|
+
type: Date,
|
|
9
|
+
required: true,
|
|
10
|
+
},
|
|
11
|
+
age: {
|
|
12
|
+
type: Number,
|
|
13
|
+
required: true,
|
|
14
|
+
},
|
|
15
|
+
email: {
|
|
16
|
+
type: String,
|
|
17
|
+
required: true,
|
|
18
|
+
rules: {
|
|
19
|
+
isEmail: true
|
|
13
20
|
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
},
|
|
22
|
+
isAwesome: {
|
|
23
|
+
type: Boolean,
|
|
24
|
+
required: true
|
|
25
|
+
},
|
|
26
|
+
friends: {
|
|
27
|
+
type: Array,
|
|
28
|
+
required: true,
|
|
29
|
+
eachType: String,
|
|
30
|
+
},
|
|
31
|
+
broRegex: {
|
|
32
|
+
type: RegExp,
|
|
33
|
+
required: true,
|
|
34
|
+
},
|
|
35
|
+
socials: {
|
|
36
|
+
x: {
|
|
37
|
+
type: String,
|
|
38
|
+
required: false
|
|
29
39
|
},
|
|
30
|
-
|
|
31
|
-
|
|
40
|
+
instagram: {
|
|
41
|
+
type: String,
|
|
42
|
+
required: false
|
|
32
43
|
}
|
|
33
44
|
},
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
nullableField: {
|
|
46
|
+
type: null,
|
|
47
|
+
required: false,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
const exampleObj = {
|
|
51
|
+
name: 'Barney Stinson',
|
|
52
|
+
age: 35,
|
|
53
|
+
lastVisit: new Date('2025-05-05'),
|
|
54
|
+
email: 'legggen@dary.com',
|
|
55
|
+
isAwesome: true,
|
|
56
|
+
friends: [
|
|
57
|
+
'Ted Mosby',
|
|
58
|
+
'Marshall Eriksen',
|
|
59
|
+
'Lily Aldrin',
|
|
60
|
+
'Robin Scherbatsky'
|
|
61
|
+
],
|
|
62
|
+
socials: {
|
|
63
|
+
x: '@barney_stinson',
|
|
64
|
+
insstagram: '@barney_stinson'
|
|
65
|
+
},
|
|
66
|
+
broRegex: /bro/i,
|
|
67
|
+
deletedAt: null,
|
|
51
68
|
};
|
|
69
|
+
const validation = schema.validate(exampleObj);
|
|
70
|
+
console.log(validation);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import ValidnoResult from "./ValidnoResult.js";
|
|
2
|
+
import validate from "./methods/validate.js";
|
|
3
|
+
import validateType from "./methods/validateType.js";
|
|
4
|
+
import validateRules from "./methods/validateRules.js";
|
|
5
|
+
import handleMissingKey from "./methods/handleMissingKey.js";
|
|
6
|
+
import handleNestedKey from "./methods/handleNestedKey.js";
|
|
7
|
+
import handleKey from "./methods/handleKey.js";
|
|
8
|
+
import handleMissingKeyValidation from "./methods/handleMissingKeyValidation.js";
|
|
9
|
+
import finishValidation from "./methods/finishValidation.js";
|
|
10
|
+
class ValidateEngine {
|
|
11
|
+
constructor(definition) {
|
|
12
|
+
this.definition = definition;
|
|
13
|
+
this.result = new ValidnoResult();
|
|
14
|
+
}
|
|
15
|
+
validate(data, validationKeys) {
|
|
16
|
+
return validate.call(this, data, validationKeys);
|
|
17
|
+
}
|
|
18
|
+
handleKey(input) {
|
|
19
|
+
return handleKey.call(this, input);
|
|
20
|
+
}
|
|
21
|
+
handleNestedKey(input) {
|
|
22
|
+
return handleNestedKey.call(this, input);
|
|
23
|
+
}
|
|
24
|
+
handleMissingKey(schema, input) {
|
|
25
|
+
return handleMissingKey(schema, input);
|
|
26
|
+
}
|
|
27
|
+
handleMissingNestedKey(nestedKey, results) {
|
|
28
|
+
results.setMissing(nestedKey);
|
|
29
|
+
return results;
|
|
30
|
+
}
|
|
31
|
+
handleMissingKeyValidation(params) {
|
|
32
|
+
return handleMissingKeyValidation.call(this, params);
|
|
33
|
+
}
|
|
34
|
+
validateType(input) {
|
|
35
|
+
return validateType(input);
|
|
36
|
+
}
|
|
37
|
+
validateRules(input) {
|
|
38
|
+
return validateRules.call(this, input);
|
|
39
|
+
}
|
|
40
|
+
finishValidation(checks) {
|
|
41
|
+
return finishValidation(checks);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export default ValidateEngine;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import _errors from "../utils/errors.js";
|
|
2
|
+
class ValidnoResult {
|
|
3
|
+
constructor(results) {
|
|
4
|
+
this.ok = (results === null || results === void 0 ? void 0 : results.ok) !== undefined ? results.ok : null;
|
|
5
|
+
this.missed = (results === null || results === void 0 ? void 0 : results.missed) || [];
|
|
6
|
+
this.failed = (results === null || results === void 0 ? void 0 : results.failed) || [];
|
|
7
|
+
this.passed = (results === null || results === void 0 ? void 0 : results.passed) || [];
|
|
8
|
+
this.errors = (results === null || results === void 0 ? void 0 : results.errors) || [];
|
|
9
|
+
this.byKeys = (results === null || results === void 0 ? void 0 : results.byKeys) || {};
|
|
10
|
+
this.errorsByKeys = (results === null || results === void 0 ? void 0 : results.errorsByKeys) || {};
|
|
11
|
+
}
|
|
12
|
+
setNoData(key) {
|
|
13
|
+
this.ok = false;
|
|
14
|
+
this.errors = [`Missing value for '${key}'`];
|
|
15
|
+
}
|
|
16
|
+
setKeyStatus(key, result) {
|
|
17
|
+
this.byKeys[key] = result;
|
|
18
|
+
}
|
|
19
|
+
fixParentByChilds(parentKey, childChecks = []) {
|
|
20
|
+
const isEveryOk = childChecks.every(check => check === true);
|
|
21
|
+
this.setKeyStatus(parentKey, isEveryOk);
|
|
22
|
+
if (isEveryOk) {
|
|
23
|
+
this.setPassed(parentKey);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
this.setFailed(parentKey);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
setMissing(key, errMsg) {
|
|
30
|
+
const error = errMsg || _errors.getMissingError(key);
|
|
31
|
+
this.missed.push(key);
|
|
32
|
+
this.setFailed(key, error);
|
|
33
|
+
this.setKeyStatus(key, false);
|
|
34
|
+
}
|
|
35
|
+
setPassed(key) {
|
|
36
|
+
this.passed.push(key);
|
|
37
|
+
this.setKeyStatus(key, true);
|
|
38
|
+
}
|
|
39
|
+
setFailed(key, msg) {
|
|
40
|
+
if (!(key in this.errorsByKeys)) {
|
|
41
|
+
this.errorsByKeys[key] = [];
|
|
42
|
+
}
|
|
43
|
+
this.failed.push(key);
|
|
44
|
+
this.setKeyStatus(key, false);
|
|
45
|
+
if (!msg)
|
|
46
|
+
return;
|
|
47
|
+
this.errors.push(msg);
|
|
48
|
+
this.errorsByKeys[key].push(msg);
|
|
49
|
+
}
|
|
50
|
+
joinErrors(separator = '; ') {
|
|
51
|
+
return _errors.joinErrors(this.errors, separator);
|
|
52
|
+
}
|
|
53
|
+
merge(resultsNew) {
|
|
54
|
+
this.failed = [...this.failed, ...resultsNew.failed];
|
|
55
|
+
this.errors = [...this.errors, ...resultsNew.errors];
|
|
56
|
+
this.missed = [...this.missed, ...resultsNew.missed];
|
|
57
|
+
this.passed = [...this.passed, ...resultsNew.passed];
|
|
58
|
+
this.byKeys = Object.assign(Object.assign({}, this.byKeys), resultsNew.byKeys);
|
|
59
|
+
for (const key in resultsNew.errorsByKeys) {
|
|
60
|
+
if (!(key in this.errorsByKeys)) {
|
|
61
|
+
this.errorsByKeys[key] = [];
|
|
62
|
+
}
|
|
63
|
+
this.errorsByKeys[key] = [
|
|
64
|
+
...this.errorsByKeys[key],
|
|
65
|
+
...resultsNew.errorsByKeys[key]
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
clearEmptyErrorsByKeys() {
|
|
71
|
+
for (const key in this.errorsByKeys) {
|
|
72
|
+
if (!this.errorsByKeys[key].length) {
|
|
73
|
+
delete this.errorsByKeys[key];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
finish() {
|
|
78
|
+
if (this.failed.length || this.errors.length) {
|
|
79
|
+
this.ok = false;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.ok = true;
|
|
83
|
+
}
|
|
84
|
+
this.clearEmptyErrorsByKeys();
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
data() {
|
|
88
|
+
return {
|
|
89
|
+
ok: this.ok,
|
|
90
|
+
missed: this.missed,
|
|
91
|
+
failed: this.failed,
|
|
92
|
+
passed: this.passed,
|
|
93
|
+
errors: this.errors,
|
|
94
|
+
byKeys: this.byKeys,
|
|
95
|
+
errorsByKeys: this.errorsByKeys,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
isValid() {
|
|
99
|
+
return this.ok === true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export default ValidnoResult;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function finishValidation(checks) {
|
|
2
|
+
const { results, nestedKey, missedCheck, typeChecked, rulesChecked } = checks;
|
|
3
|
+
if (missedCheck.length)
|
|
4
|
+
results.setMissing(nestedKey);
|
|
5
|
+
const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
|
|
6
|
+
if (!isPassed) {
|
|
7
|
+
results.setFailed(nestedKey);
|
|
8
|
+
results.errorsByKeys[nestedKey] = [...results.errors];
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
results.setPassed(nestedKey);
|
|
12
|
+
}
|
|
13
|
+
return results.finish();
|
|
14
|
+
}
|
|
15
|
+
export default finishValidation;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import _helpers from "../../utils/helpers.js";
|
|
2
|
+
import ValidnoResult from "../ValidnoResult.js";
|
|
3
|
+
function validateKeyValue(params) {
|
|
4
|
+
const { results, key, nestedKey, data, reqs, hasMissing } = params;
|
|
5
|
+
const missedCheck = [];
|
|
6
|
+
const typeChecked = [];
|
|
7
|
+
const rulesChecked = [];
|
|
8
|
+
if (hasMissing) {
|
|
9
|
+
return this.handleMissingKeyValidation({ results, key, nestedKey, data, reqs, missedCheck });
|
|
10
|
+
}
|
|
11
|
+
this.validateType({ results, key, value: data[key], reqs, nestedKey, typeChecked });
|
|
12
|
+
this.validateRules({ results, nestedKey, value: data[key], reqs, data, rulesChecked });
|
|
13
|
+
return this.finishValidation({ results, nestedKey, missedCheck, typeChecked, rulesChecked });
|
|
14
|
+
}
|
|
15
|
+
function validateKey(input) {
|
|
16
|
+
let { results, key, nestedKey, data, reqs } = input;
|
|
17
|
+
if (data === undefined) {
|
|
18
|
+
const noDataResult = new ValidnoResult();
|
|
19
|
+
noDataResult.setNoData(nestedKey);
|
|
20
|
+
}
|
|
21
|
+
if (!results)
|
|
22
|
+
results = new ValidnoResult();
|
|
23
|
+
if (!nestedKey)
|
|
24
|
+
nestedKey = key;
|
|
25
|
+
const hasMissing = _helpers.hasMissing(input);
|
|
26
|
+
if (_helpers.checkNestedIsMissing(reqs, data)) {
|
|
27
|
+
return this.handleMissingNestedKey(nestedKey, results);
|
|
28
|
+
}
|
|
29
|
+
if (_helpers.checkIsNested(reqs)) {
|
|
30
|
+
return this.handleNestedKey({ results, key, data, reqs, nestedKey });
|
|
31
|
+
}
|
|
32
|
+
return validateKeyValue.call(this, {
|
|
33
|
+
results,
|
|
34
|
+
key,
|
|
35
|
+
nestedKey,
|
|
36
|
+
data,
|
|
37
|
+
reqs,
|
|
38
|
+
hasMissing,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export default validateKey;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ValidationIds } from "../../constants/details.js";
|
|
2
|
+
import _errors from "../../utils/errors.js";
|
|
3
|
+
function handleMissingKey(schema, input) {
|
|
4
|
+
const { key, nestedKey, data, reqs } = input;
|
|
5
|
+
const messageKey = nestedKey || key;
|
|
6
|
+
const messageTitle = reqs.title || messageKey;
|
|
7
|
+
if (!reqs.customMessage)
|
|
8
|
+
return _errors.getMissingError(messageKey);
|
|
9
|
+
const errorMessage = reqs.customMessage({
|
|
10
|
+
keyword: ValidationIds.Missing,
|
|
11
|
+
value: data[key],
|
|
12
|
+
key: messageKey,
|
|
13
|
+
title: messageTitle,
|
|
14
|
+
reqs,
|
|
15
|
+
schema,
|
|
16
|
+
});
|
|
17
|
+
return errorMessage;
|
|
18
|
+
}
|
|
19
|
+
export default handleMissingKey;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
function handleMissingKeyValidation(params) {
|
|
2
|
+
const schema = this.definition;
|
|
3
|
+
const { results, key, nestedKey, data, reqs, missedCheck } = params;
|
|
4
|
+
const errMsg = this.handleMissingKey(schema, { key, nestedKey, data, reqs });
|
|
5
|
+
missedCheck.push(false);
|
|
6
|
+
results.setMissing(nestedKey, errMsg);
|
|
7
|
+
return results;
|
|
8
|
+
}
|
|
9
|
+
export default handleMissingKeyValidation;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function handleNestedKey(input) {
|
|
2
|
+
const { results, key, nestedKey, data, reqs } = input;
|
|
3
|
+
const nestedKeys = Object.keys(reqs);
|
|
4
|
+
const nestedResults = [];
|
|
5
|
+
for (const itemKey of nestedKeys) {
|
|
6
|
+
const deepParams = {
|
|
7
|
+
key: itemKey,
|
|
8
|
+
data: data[key],
|
|
9
|
+
reqs: reqs[itemKey],
|
|
10
|
+
nestedKey: `${nestedKey}.${itemKey}`
|
|
11
|
+
};
|
|
12
|
+
const deepResults = this.handleKey(deepParams);
|
|
13
|
+
nestedResults.push(deepResults.ok);
|
|
14
|
+
results.merge(deepResults);
|
|
15
|
+
}
|
|
16
|
+
results.fixParentByChilds(nestedKey, nestedResults);
|
|
17
|
+
return results;
|
|
18
|
+
}
|
|
19
|
+
export default handleNestedKey;
|