validno 0.2.3 → 0.2.5
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/dev.js +0 -0
- package/dist/Schema.js +9 -13
- package/dist/ValidnoResult.js +11 -1
- package/dist/checkType.js +15 -11
- package/dist/constants/schema.js +10 -0
- package/dist/dev.js +41 -81
- package/dist/index.js +2 -0
- package/dist/utils/errors.js +6 -2
- package/dist/utils/helpers.js +32 -11
- package/dist/utils/validations.js +42 -16
- package/dist/validate.js +87 -66
- package/package.json +1 -1
- package/.eslintrc.cjs +0 -13
- package/.eslintrc.json +0 -16
- package/jest.config.ts +0 -199
package/dev.js
ADDED
|
File without changes
|
package/dist/Schema.js
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
+
import { ESchemaFields } from "./constants/schema.js";
|
|
1
2
|
import validate from "./validate.js";
|
|
2
|
-
export const defaultSchemaKeys =
|
|
3
|
-
"required",
|
|
4
|
-
"type",
|
|
5
|
-
"eachType",
|
|
6
|
-
"rules",
|
|
7
|
-
"title",
|
|
8
|
-
"customMessage",
|
|
9
|
-
"joinErrors"
|
|
10
|
-
];
|
|
3
|
+
export const defaultSchemaKeys = Object.values(ESchemaFields);
|
|
11
4
|
export class Schema {
|
|
12
|
-
constructor(
|
|
13
|
-
|
|
5
|
+
constructor(inputSchemaDefinition) {
|
|
6
|
+
if (!inputSchemaDefinition || typeof inputSchemaDefinition !== 'object') {
|
|
7
|
+
throw new Error("Invalid schema input");
|
|
8
|
+
}
|
|
9
|
+
this.schema = inputSchemaDefinition;
|
|
14
10
|
}
|
|
15
|
-
validate(
|
|
16
|
-
return validate.call(this,
|
|
11
|
+
validate(inputData, validationKeys) {
|
|
12
|
+
return validate.call(this, inputData, validationKeys);
|
|
17
13
|
}
|
|
18
14
|
}
|
package/dist/ValidnoResult.js
CHANGED
|
@@ -8,7 +8,6 @@ class ValidnoResult {
|
|
|
8
8
|
this.errors = (results === null || results === void 0 ? void 0 : results.errors) || [];
|
|
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
|
-
this.byKeys = (results === null || results === void 0 ? void 0 : results.byKeys) || {};
|
|
12
11
|
}
|
|
13
12
|
setKeyStatus(key, result) {
|
|
14
13
|
this.byKeys[key] = result;
|
|
@@ -76,5 +75,16 @@ class ValidnoResult {
|
|
|
76
75
|
this.clearEmptyErrorsByKeys();
|
|
77
76
|
return this;
|
|
78
77
|
}
|
|
78
|
+
data() {
|
|
79
|
+
return {
|
|
80
|
+
ok: this.ok,
|
|
81
|
+
missed: this.missed,
|
|
82
|
+
failed: this.failed,
|
|
83
|
+
passed: this.passed,
|
|
84
|
+
errors: this.errors,
|
|
85
|
+
byKeys: this.byKeys,
|
|
86
|
+
errorsByKeys: this.errorsByKeys,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
79
89
|
}
|
|
80
90
|
export default ValidnoResult;
|
package/dist/checkType.js
CHANGED
|
@@ -2,22 +2,26 @@ import { ErrorKeywords } from "./constants/details.js";
|
|
|
2
2
|
import _validations from "./utils/validations.js";
|
|
3
3
|
import _errors from "./utils/errors.js";
|
|
4
4
|
const checkTypeMultiple = (key, value, requirements, keyName = key) => {
|
|
5
|
-
const constructorNames = requirements.type
|
|
5
|
+
const constructorNames = Array.isArray(requirements.type)
|
|
6
|
+
? requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el))
|
|
7
|
+
: [];
|
|
6
8
|
const result = {
|
|
7
9
|
key: keyName,
|
|
8
10
|
passed: false,
|
|
9
11
|
details: _errors.getErrorDetails(keyName, constructorNames.join('/'), value)
|
|
10
12
|
};
|
|
11
13
|
let i = 0;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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++;
|
|
19
24
|
}
|
|
20
|
-
i++;
|
|
21
25
|
}
|
|
22
26
|
return result;
|
|
23
27
|
};
|
|
@@ -149,8 +153,8 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
149
153
|
});
|
|
150
154
|
break;
|
|
151
155
|
default:
|
|
152
|
-
const isInstanceOf = value instanceof typeBySchema;
|
|
153
|
-
const isConstructorSame = ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
|
|
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);
|
|
154
158
|
const checked = isInstanceOf && isConstructorSame;
|
|
155
159
|
result.push({
|
|
156
160
|
key: keyName,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export var ESchemaFields;
|
|
2
|
+
(function (ESchemaFields) {
|
|
3
|
+
ESchemaFields["Required"] = "required";
|
|
4
|
+
ESchemaFields["Type"] = "type";
|
|
5
|
+
ESchemaFields["EachType"] = "eachType";
|
|
6
|
+
ESchemaFields["Rules"] = "rules";
|
|
7
|
+
ESchemaFields["Title"] = "title";
|
|
8
|
+
ESchemaFields["CustomMessage"] = "customMessage";
|
|
9
|
+
ESchemaFields["JoinErrors"] = "joinErrors";
|
|
10
|
+
})(ESchemaFields || (ESchemaFields = {}));
|
package/dist/dev.js
CHANGED
|
@@ -1,91 +1,51 @@
|
|
|
1
1
|
import { Schema } from "./Schema.js";
|
|
2
2
|
const validateConfig = (cfg) => {
|
|
3
3
|
const cfgSchema = new Schema({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
type:
|
|
11
|
-
required:
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type: Object,
|
|
15
|
-
required: false,
|
|
16
|
-
},
|
|
17
|
-
getAll: {
|
|
18
|
-
type: Object,
|
|
19
|
-
required: false,
|
|
20
|
-
},
|
|
21
|
-
update: {
|
|
22
|
-
type: Object,
|
|
23
|
-
required: false,
|
|
24
|
-
},
|
|
25
|
-
delete: {
|
|
26
|
-
type: Object,
|
|
27
|
-
required: false,
|
|
28
|
-
},
|
|
29
|
-
deleteMany: {
|
|
30
|
-
type: Object,
|
|
31
|
-
required: false,
|
|
32
|
-
},
|
|
33
|
-
export: {
|
|
34
|
-
type: Object,
|
|
35
|
-
required: false,
|
|
36
|
-
},
|
|
37
|
-
distinct: {
|
|
38
|
-
type: Object,
|
|
39
|
-
required: false,
|
|
40
|
-
},
|
|
41
|
-
custom: {
|
|
42
|
-
type: Array,
|
|
43
|
-
required: false,
|
|
44
|
-
rules: {},
|
|
45
|
-
},
|
|
46
|
-
},
|
|
4
|
+
str1: {
|
|
5
|
+
type: String,
|
|
6
|
+
required: true
|
|
7
|
+
},
|
|
8
|
+
str2: {
|
|
9
|
+
deep1: {
|
|
10
|
+
type: String,
|
|
11
|
+
required: true,
|
|
12
|
+
}
|
|
13
|
+
}
|
|
47
14
|
});
|
|
48
15
|
const res = cfgSchema.validate(cfg);
|
|
49
16
|
console.log(res);
|
|
50
|
-
if (res.ok !== true) {
|
|
51
|
-
const errorsMsg = res.errors.join('; ');
|
|
52
|
-
throw new Error(errorsMsg);
|
|
53
|
-
}
|
|
54
17
|
};
|
|
55
18
|
const uniTestService = {
|
|
56
|
-
|
|
57
|
-
getAll: {
|
|
58
|
-
isActive: true,
|
|
59
|
-
sort: {
|
|
60
|
-
default: 'createdAt',
|
|
61
|
-
},
|
|
62
|
-
search: {
|
|
63
|
-
fields: ['title'],
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
export: {
|
|
67
|
-
isActive: false,
|
|
68
|
-
},
|
|
69
|
-
get: {
|
|
70
|
-
isActive: true,
|
|
71
|
-
},
|
|
72
|
-
create: null,
|
|
73
|
-
createMany: {
|
|
74
|
-
isActive: true,
|
|
75
|
-
},
|
|
76
|
-
update: {
|
|
77
|
-
isActive: true,
|
|
78
|
-
},
|
|
79
|
-
delete: {
|
|
80
|
-
isActive: true,
|
|
81
|
-
},
|
|
82
|
-
deleteMany: {
|
|
83
|
-
isActive: true,
|
|
84
|
-
},
|
|
85
|
-
distinct: {
|
|
86
|
-
isActive: true,
|
|
87
|
-
fields: ['title', '_id'],
|
|
88
|
-
},
|
|
89
|
-
},
|
|
19
|
+
str1: 'xxxx'
|
|
90
20
|
};
|
|
91
21
|
validateConfig(uniTestService);
|
|
22
|
+
console.log(uniTestService);
|
|
23
|
+
console.log(uniTestService);
|
|
24
|
+
const obj = {
|
|
25
|
+
top: {
|
|
26
|
+
mid1: {
|
|
27
|
+
low1a: false,
|
|
28
|
+
low1b: false
|
|
29
|
+
},
|
|
30
|
+
mid2: {
|
|
31
|
+
low2a: false
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
top2: false
|
|
35
|
+
};
|
|
36
|
+
const setNestedValue = (linkedObj, keysLevels, curLevel, valueToSet) => {
|
|
37
|
+
const lvKey = keysLevels[curLevel];
|
|
38
|
+
if (curLevel < keysLevels.length - 1) {
|
|
39
|
+
if (!linkedObj[lvKey] || typeof linkedObj[lvKey] !== 'object') {
|
|
40
|
+
linkedObj[lvKey] = {};
|
|
41
|
+
}
|
|
42
|
+
return setNestedValue(linkedObj[lvKey], keysLevels, curLevel + 1, valueToSet);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
linkedObj[lvKey] = valueToSet;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const setDeepValue = (obj, keysChain, valueToSet) => {
|
|
49
|
+
const keysLevels = keysChain.split('.');
|
|
50
|
+
setNestedValue(obj, keysLevels, 0, valueToSet);
|
|
51
|
+
};
|
package/dist/index.js
CHANGED
package/dist/utils/errors.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const _errors = {};
|
|
2
|
-
_errors.getMissingError = (key) =>
|
|
2
|
+
_errors.getMissingError = (key = 'na') => `Отсутствует значение '${key}'`;
|
|
3
3
|
_errors.getErrorDetails = (key, expectedType, receivedValue) => {
|
|
4
4
|
var _a;
|
|
5
5
|
let receivedType = '';
|
|
@@ -9,7 +9,11 @@ _errors.getErrorDetails = (key, expectedType, receivedValue) => {
|
|
|
9
9
|
receivedType = 'null';
|
|
10
10
|
else
|
|
11
11
|
receivedType = ((_a = receivedValue.constructor) === null || _a === void 0 ? void 0 : _a.name) || typeof receivedValue || 'na';
|
|
12
|
-
|
|
12
|
+
const expectedOutput = (expectedType === null || expectedType === void 0 ? void 0 : expectedType.name) || expectedType;
|
|
13
|
+
const receivedOutput = receivedType || 'na';
|
|
14
|
+
if (String(expectedOutput) === String(receivedOutput))
|
|
15
|
+
return '';
|
|
16
|
+
return `Проверьте тип '${key}': ожидался ${expectedOutput}, получен ${receivedOutput}`;
|
|
13
17
|
};
|
|
14
18
|
_errors.joinErrors = (errorsArr, separator = '; ') => {
|
|
15
19
|
return (errorsArr === null || errorsArr === void 0 ? void 0 : errorsArr.join(`${separator}`)) || '';
|
package/dist/utils/helpers.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { defaultSchemaKeys } from "../Schema.js";
|
|
2
|
-
import ValidnoResult from "../ValidnoResult.js";
|
|
3
2
|
import _validations from "./validations.js";
|
|
4
3
|
const _helpers = {};
|
|
5
4
|
_helpers.checkIsNested = (obj) => {
|
|
@@ -13,16 +12,6 @@ _helpers.checkIsNested = (obj) => {
|
|
|
13
12
|
return true;
|
|
14
13
|
}
|
|
15
14
|
};
|
|
16
|
-
_helpers.mergeResults = (resultsOld, resultsNew) => {
|
|
17
|
-
const output = new ValidnoResult();
|
|
18
|
-
output.failed = [...resultsOld.failed, ...resultsNew.failed];
|
|
19
|
-
output.errors = [...resultsOld.errors, ...resultsNew.errors];
|
|
20
|
-
output.missed = [...resultsOld.missed, ...resultsNew.missed];
|
|
21
|
-
output.passed = [...resultsOld.passed, ...resultsNew.passed];
|
|
22
|
-
output.byKeys = Object.assign(Object.assign({}, resultsOld.byKeys), resultsNew.byKeys);
|
|
23
|
-
output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
|
|
24
|
-
return output;
|
|
25
|
-
};
|
|
26
15
|
_helpers.checkNestedIsMissing = (reqs, data) => {
|
|
27
16
|
const isRequired = reqs.required;
|
|
28
17
|
const isUndef = data === undefined;
|
|
@@ -45,4 +34,36 @@ _helpers.hasMissing = (input) => {
|
|
|
45
34
|
const missingData = (data === undefined || key in data === false || data[key] === undefined);
|
|
46
35
|
return isRequired && missingData;
|
|
47
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]);
|
|
43
|
+
}
|
|
44
|
+
return v2[i] === el;
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
_helpers.compareObjs = (obj1, obj2) => {
|
|
48
|
+
function deepEqual(obj1, obj2) {
|
|
49
|
+
if (obj1 === obj2) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const keys1 = Object.keys(obj1);
|
|
56
|
+
const keys2 = Object.keys(obj2);
|
|
57
|
+
if (keys1.length !== keys2.length) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
for (let key of keys1) {
|
|
61
|
+
if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
return deepEqual(obj1, obj2);
|
|
68
|
+
};
|
|
48
69
|
export default _helpers;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import _helpers from "./helpers.js";
|
|
1
2
|
const _validations = {};
|
|
2
3
|
_validations.isString = (value) => {
|
|
3
4
|
return typeof value === 'string';
|
|
@@ -73,37 +74,55 @@ _validations.lengthMax = (value, max) => {
|
|
|
73
74
|
_validations.isNumberGte = (value, gte) => {
|
|
74
75
|
return typeof value === 'number' && value >= gte;
|
|
75
76
|
};
|
|
77
|
+
_validations.isNumberGt = (value, gt) => {
|
|
78
|
+
return typeof value === 'number' && value > gt;
|
|
79
|
+
};
|
|
76
80
|
_validations.isNumberLte = (value, lte) => {
|
|
77
81
|
return typeof value === 'number' && value <= lte;
|
|
78
82
|
};
|
|
83
|
+
_validations.isNumberLt = (value, lt) => {
|
|
84
|
+
return typeof value === 'number' && value < lt;
|
|
85
|
+
};
|
|
86
|
+
_validations.isDateGte = (date1, date2) => {
|
|
87
|
+
if (!_validations.isDate(date1) || !_validations.isDate(date2)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return date1 >= date2;
|
|
91
|
+
};
|
|
92
|
+
_validations.isDateGt = (date1, date2) => {
|
|
93
|
+
if (!_validations.isDate(date1) || !_validations.isDate(date2)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return date1 > date2;
|
|
97
|
+
};
|
|
98
|
+
_validations.isDateLte = (date1, date2) => {
|
|
99
|
+
if (!_validations.isDate(date1) || !_validations.isDate(date2)) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
return date1 <= date2;
|
|
103
|
+
};
|
|
104
|
+
_validations.isDateLt = (date1, date2) => {
|
|
105
|
+
if (!_validations.isDate(date1) || !_validations.isDate(date2)) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return date1 < date2;
|
|
109
|
+
};
|
|
79
110
|
_validations.hasKey = (obj, key) => {
|
|
80
111
|
if (_validations.isObject(obj) === false)
|
|
81
112
|
return false;
|
|
82
113
|
return key in obj;
|
|
83
114
|
};
|
|
84
|
-
const compareArrs = (v1, v2) => {
|
|
85
|
-
if (v1.length !== v2.length)
|
|
86
|
-
return false;
|
|
87
|
-
return v1.every((el, i) => {
|
|
88
|
-
if (_validations.isObject(el)) {
|
|
89
|
-
return JSON.stringify(el) === JSON.stringify(v2[i]);
|
|
90
|
-
}
|
|
91
|
-
return v2[i] === el;
|
|
92
|
-
});
|
|
93
|
-
};
|
|
94
115
|
_validations.is = (value, compareTo) => {
|
|
95
116
|
if (value === compareTo) {
|
|
96
117
|
return true;
|
|
97
118
|
}
|
|
98
119
|
const bothArrays = _validations.isArray(value) && _validations.isArray(compareTo);
|
|
99
120
|
if (bothArrays) {
|
|
100
|
-
return compareArrs(value, compareTo);
|
|
121
|
+
return _helpers.compareArrs(value, compareTo);
|
|
101
122
|
}
|
|
102
123
|
const bothObjects = _validations.isObject(value) && _validations.isObject(compareTo);
|
|
103
124
|
if (bothObjects) {
|
|
104
|
-
|
|
105
|
-
const compareToStr = JSON.stringify(compareTo);
|
|
106
|
-
return valueStr === compareToStr;
|
|
125
|
+
return _helpers.compareObjs(value, compareTo);
|
|
107
126
|
}
|
|
108
127
|
const bothDates = _validations.isDate(value) && _validations.isDate(compareTo);
|
|
109
128
|
if (bothDates) {
|
|
@@ -114,14 +133,21 @@ _validations.is = (value, compareTo) => {
|
|
|
114
133
|
_validations.not = (value, not) => {
|
|
115
134
|
return !_validations.is(value, not);
|
|
116
135
|
};
|
|
117
|
-
_validations.isNot = _validations.not;
|
|
118
|
-
_validations.ne = _validations.not;
|
|
119
136
|
_validations.regexTested = (value, regexp) => {
|
|
120
137
|
if (!regexp || regexp instanceof RegExp !== true) {
|
|
121
138
|
throw new Error('regexp argument is incorrect');
|
|
122
139
|
}
|
|
123
140
|
return regexp.test(value);
|
|
124
141
|
};
|
|
142
|
+
_validations.gt = _validations.isNumberGt;
|
|
143
|
+
_validations.gte = _validations.isNumberGte;
|
|
144
|
+
_validations.lte = _validations.isNumberLte;
|
|
145
|
+
_validations.lt = _validations.isNumberLt;
|
|
146
|
+
_validations.eq = _validations.is;
|
|
147
|
+
_validations.isNot = _validations.not;
|
|
148
|
+
_validations.ne = _validations.not;
|
|
149
|
+
_validations.neq = _validations.not;
|
|
150
|
+
_validations.regexpTested = _validations.regexpTested;
|
|
125
151
|
_validations.regex = _validations.regexTested;
|
|
126
152
|
_validations.regexp = _validations.regexTested;
|
|
127
153
|
_validations.test = _validations.regexTested;
|
package/dist/validate.js
CHANGED
|
@@ -4,112 +4,133 @@ import checkRules from "./checkRules.js";
|
|
|
4
4
|
import _helpers from "./utils/helpers.js";
|
|
5
5
|
import { ErrorKeywords } from "./constants/details.js";
|
|
6
6
|
import ValidnoResult from "./ValidnoResult.js";
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
if (reqs.customMessage
|
|
12
|
-
|
|
13
|
-
keyword: ErrorKeywords.Missing,
|
|
14
|
-
value: data[key],
|
|
15
|
-
key: keyForMsg,
|
|
16
|
-
title: keyTitle,
|
|
17
|
-
reqs: reqs,
|
|
18
|
-
schema: this.schema
|
|
19
|
-
});
|
|
20
|
-
return errMsg;
|
|
7
|
+
function handleMissingKey(schema, input) {
|
|
8
|
+
const { key, nestedKey, data, reqs } = input;
|
|
9
|
+
const messageKey = nestedKey || key;
|
|
10
|
+
const messageTitle = reqs.title || messageKey;
|
|
11
|
+
if (!reqs.customMessage) {
|
|
12
|
+
return _errors.getMissingError(messageKey);
|
|
21
13
|
}
|
|
22
|
-
|
|
14
|
+
const errorMessage = reqs.customMessage({
|
|
15
|
+
keyword: ErrorKeywords.Missing,
|
|
16
|
+
value: data[key],
|
|
17
|
+
key: messageKey,
|
|
18
|
+
title: messageTitle,
|
|
19
|
+
reqs,
|
|
20
|
+
schema,
|
|
21
|
+
});
|
|
22
|
+
return errorMessage;
|
|
23
23
|
}
|
|
24
|
-
function
|
|
25
|
-
const { results, key,
|
|
26
|
-
const
|
|
24
|
+
function validateNestedKey(input) {
|
|
25
|
+
const { results, key, nestedKey, data, reqs } = input;
|
|
26
|
+
const nestedKeys = Object.keys(reqs);
|
|
27
27
|
const nestedResults = [];
|
|
28
|
-
|
|
29
|
-
while (i < nesctedKeys.length) {
|
|
30
|
-
const nestedKey = nesctedKeys[i];
|
|
28
|
+
for (const itemKey of nestedKeys) {
|
|
31
29
|
const deepParams = {
|
|
32
|
-
key:
|
|
30
|
+
key: itemKey,
|
|
33
31
|
data: data[key],
|
|
34
|
-
reqs: reqs[
|
|
35
|
-
|
|
32
|
+
reqs: reqs[itemKey],
|
|
33
|
+
nestedKey: `${nestedKey}.${itemKey}`
|
|
36
34
|
};
|
|
37
|
-
const deepResults =
|
|
35
|
+
const deepResults = validateKey.call(this, deepParams);
|
|
38
36
|
nestedResults.push(deepResults.ok);
|
|
39
37
|
results.merge(deepResults);
|
|
40
|
-
i++;
|
|
41
38
|
}
|
|
42
|
-
results.fixParentByChilds(
|
|
39
|
+
results.fixParentByChilds(nestedKey, nestedResults);
|
|
43
40
|
return results;
|
|
44
41
|
}
|
|
45
|
-
|
|
46
|
-
let { results, key,
|
|
42
|
+
function validateKey(input) {
|
|
43
|
+
let { results, key, nestedKey, data, reqs } = input;
|
|
47
44
|
if (!results)
|
|
48
45
|
results = new ValidnoResult();
|
|
49
|
-
if (!
|
|
50
|
-
|
|
51
|
-
const hasNested = _helpers.checkIsNested(reqs);
|
|
46
|
+
if (!nestedKey)
|
|
47
|
+
nestedKey = key;
|
|
52
48
|
const hasMissing = _helpers.hasMissing(input);
|
|
53
|
-
const missedCheck = [];
|
|
54
|
-
const typeChecked = [];
|
|
55
|
-
const rulesChecked = [];
|
|
56
49
|
if (_helpers.checkNestedIsMissing(reqs, data)) {
|
|
57
|
-
results
|
|
58
|
-
return results;
|
|
50
|
+
return handleMissingNestedKey(results, nestedKey);
|
|
59
51
|
}
|
|
60
|
-
if (
|
|
61
|
-
return
|
|
52
|
+
if (_helpers.checkIsNested(reqs)) {
|
|
53
|
+
return validateNestedKey.call(this, { results, key, data, reqs, nestedKey });
|
|
62
54
|
}
|
|
55
|
+
return validateKeyDetails.call(this, {
|
|
56
|
+
results,
|
|
57
|
+
key,
|
|
58
|
+
nestedKey,
|
|
59
|
+
data,
|
|
60
|
+
reqs,
|
|
61
|
+
hasMissing,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function handleMissingNestedKey(results, nestedKey) {
|
|
65
|
+
results.setMissing(nestedKey);
|
|
66
|
+
return results;
|
|
67
|
+
}
|
|
68
|
+
function validateKeyDetails(params) {
|
|
69
|
+
const { results, key, nestedKey, data, reqs, hasMissing } = params;
|
|
70
|
+
const missedCheck = [];
|
|
71
|
+
const typeChecked = [];
|
|
72
|
+
const rulesChecked = [];
|
|
63
73
|
if (hasMissing) {
|
|
64
|
-
|
|
65
|
-
missedCheck.push(false);
|
|
66
|
-
results.setMissing(deepKey, errMsg);
|
|
67
|
-
return results;
|
|
74
|
+
return handleMissingKeyValidation(this.schema, { results, key, nestedKey, data, reqs }, missedCheck);
|
|
68
75
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
76
|
+
checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
|
|
77
|
+
checkRulesForKey.call(this, results, nestedKey, data[key], reqs, data, rulesChecked);
|
|
78
|
+
return finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked);
|
|
79
|
+
}
|
|
80
|
+
function handleMissingKeyValidation(schema, params, missedCheck) {
|
|
81
|
+
const { results, key, nestedKey, data, reqs } = params;
|
|
82
|
+
const errMsg = handleMissingKey(schema, { key, nestedKey, data, reqs });
|
|
83
|
+
missedCheck.push(false);
|
|
84
|
+
results.setMissing(nestedKey, errMsg);
|
|
85
|
+
return results;
|
|
86
|
+
}
|
|
87
|
+
function checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
|
|
88
|
+
const typeCheck = checkType(key, value, reqs, nestedKey);
|
|
89
|
+
typeCheck.forEach((res) => {
|
|
90
|
+
if (!res.passed) {
|
|
91
|
+
typeChecked.push(false);
|
|
73
92
|
results.errors.push(res.details);
|
|
74
93
|
}
|
|
75
94
|
});
|
|
76
|
-
|
|
95
|
+
}
|
|
96
|
+
function checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
|
|
97
|
+
const ruleCheck = checkRules.call(this, nestedKey, value, reqs, data);
|
|
77
98
|
if (!ruleCheck.ok) {
|
|
78
99
|
rulesChecked.push(false);
|
|
79
100
|
ruleCheck.details.forEach((el) => {
|
|
80
|
-
if (
|
|
81
|
-
results.errorsByKeys[
|
|
101
|
+
if (!(nestedKey in results.errorsByKeys))
|
|
102
|
+
results.errorsByKeys[nestedKey] = [];
|
|
82
103
|
results.errors.push(el);
|
|
83
|
-
results.errorsByKeys[
|
|
104
|
+
results.errorsByKeys[nestedKey] = ['1'];
|
|
84
105
|
});
|
|
85
106
|
}
|
|
107
|
+
}
|
|
108
|
+
function finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked) {
|
|
86
109
|
if (missedCheck.length)
|
|
87
|
-
results.setMissing(
|
|
88
|
-
const isPassed =
|
|
110
|
+
results.setMissing(nestedKey);
|
|
111
|
+
const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
|
|
89
112
|
if (!isPassed) {
|
|
90
|
-
results.setFailed(
|
|
91
|
-
results.errorsByKeys[
|
|
92
|
-
...results.errors
|
|
93
|
-
];
|
|
113
|
+
results.setFailed(nestedKey);
|
|
114
|
+
results.errorsByKeys[nestedKey] = [...results.errors];
|
|
94
115
|
}
|
|
95
116
|
else {
|
|
96
|
-
results.setPassed(
|
|
117
|
+
results.setPassed(nestedKey);
|
|
97
118
|
}
|
|
98
119
|
return results.finish();
|
|
99
120
|
}
|
|
100
|
-
function
|
|
101
|
-
const
|
|
121
|
+
function validateSchema(data, keysToCheck) {
|
|
122
|
+
const output = new ValidnoResult();
|
|
102
123
|
const hasKeysToCheck = _helpers.areKeysLimited(keysToCheck);
|
|
103
|
-
const schemaKeys = Object.entries(
|
|
124
|
+
const schemaKeys = Object.entries(this.schema);
|
|
104
125
|
for (const [key, reqs] of schemaKeys) {
|
|
105
126
|
const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, keysToCheck);
|
|
106
127
|
if (!toBeValidated)
|
|
107
128
|
continue;
|
|
108
|
-
const keyResult =
|
|
109
|
-
|
|
129
|
+
const keyResult = validateKey.call(this, { key, data, reqs });
|
|
130
|
+
output.merge(keyResult);
|
|
110
131
|
}
|
|
111
|
-
|
|
112
|
-
return
|
|
132
|
+
output.finish();
|
|
133
|
+
return output;
|
|
113
134
|
}
|
|
114
135
|
;
|
|
115
|
-
export default
|
|
136
|
+
export default validateSchema;
|
package/package.json
CHANGED
package/.eslintrc.cjs
DELETED
package/.eslintrc.json
DELETED
package/jest.config.ts
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* For a detailed explanation regarding each configuration property, visit:
|
|
3
|
-
* https://jestjs.io/docs/configuration
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type {Config} from 'jest';
|
|
7
|
-
|
|
8
|
-
const config: Config = {
|
|
9
|
-
// All imported modules in your tests should be mocked automatically
|
|
10
|
-
// automock: false,
|
|
11
|
-
|
|
12
|
-
// Stop running tests after `n` failures
|
|
13
|
-
// bail: 0,
|
|
14
|
-
|
|
15
|
-
// The directory where Jest should store its cached dependency information
|
|
16
|
-
// cacheDirectory: "C:\\Users\\lesha\\AppData\\Local\\Temp\\jest",
|
|
17
|
-
|
|
18
|
-
// Automatically clear mock calls, instances, contexts and results before every test
|
|
19
|
-
clearMocks: true,
|
|
20
|
-
|
|
21
|
-
// Indicates whether the coverage information should be collected while executing the test
|
|
22
|
-
collectCoverage: false,
|
|
23
|
-
|
|
24
|
-
// An array of glob patterns indicating a set of files for which coverage information should be collected
|
|
25
|
-
// collectCoverageFrom: undefined,
|
|
26
|
-
|
|
27
|
-
// The directory where Jest should output its coverage files
|
|
28
|
-
coverageDirectory: "coverage",
|
|
29
|
-
|
|
30
|
-
// An array of regexp pattern strings used to skip coverage collection
|
|
31
|
-
// coveragePathIgnorePatterns: [
|
|
32
|
-
// "\\\\node_modules\\\\"
|
|
33
|
-
// ],
|
|
34
|
-
|
|
35
|
-
// Indicates which provider should be used to instrument code for coverage
|
|
36
|
-
coverageProvider: "v8",
|
|
37
|
-
|
|
38
|
-
// A list of reporter names that Jest uses when writing coverage reports
|
|
39
|
-
// coverageReporters: [
|
|
40
|
-
// "json",
|
|
41
|
-
// "text",
|
|
42
|
-
// "lcov",
|
|
43
|
-
// "clover"
|
|
44
|
-
// ],
|
|
45
|
-
|
|
46
|
-
// An object that configures minimum threshold enforcement for coverage results
|
|
47
|
-
// coverageThreshold: undefined,
|
|
48
|
-
|
|
49
|
-
// A path to a custom dependency extractor
|
|
50
|
-
// dependencyExtractor: undefined,
|
|
51
|
-
|
|
52
|
-
// Make calling deprecated APIs throw helpful error messages
|
|
53
|
-
// errorOnDeprecated: false,
|
|
54
|
-
|
|
55
|
-
// The default configuration for fake timers
|
|
56
|
-
// fakeTimers: {
|
|
57
|
-
// "enableGlobally": false
|
|
58
|
-
// },
|
|
59
|
-
|
|
60
|
-
// Force coverage collection from ignored files using an array of glob patterns
|
|
61
|
-
// forceCoverageMatch: [],
|
|
62
|
-
|
|
63
|
-
// A path to a module which exports an async function that is triggered once before all test suites
|
|
64
|
-
// globalSetup: undefined,
|
|
65
|
-
|
|
66
|
-
// A path to a module which exports an async function that is triggered once after all test suites
|
|
67
|
-
// globalTeardown: undefined,
|
|
68
|
-
|
|
69
|
-
// A set of global variables that need to be available in all test environments
|
|
70
|
-
// globals: {},
|
|
71
|
-
|
|
72
|
-
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
|
|
73
|
-
// maxWorkers: "50%",
|
|
74
|
-
|
|
75
|
-
// An array of directory names to be searched recursively up from the requiring module's location
|
|
76
|
-
// moduleDirectories: [
|
|
77
|
-
// "node_modules"
|
|
78
|
-
// ],
|
|
79
|
-
|
|
80
|
-
// An array of file extensions your modules use
|
|
81
|
-
// moduleFileExtensions: [
|
|
82
|
-
// "js",
|
|
83
|
-
// "mjs",
|
|
84
|
-
// "cjs",
|
|
85
|
-
// "jsx",
|
|
86
|
-
// "ts",
|
|
87
|
-
// "tsx",
|
|
88
|
-
// "json",
|
|
89
|
-
// "node"
|
|
90
|
-
// ],
|
|
91
|
-
|
|
92
|
-
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
|
|
93
|
-
// moduleNameMapper: {},
|
|
94
|
-
|
|
95
|
-
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
|
|
96
|
-
// modulePathIgnorePatterns: [],
|
|
97
|
-
|
|
98
|
-
// Activates notifications for test results
|
|
99
|
-
// notify: false,
|
|
100
|
-
|
|
101
|
-
// An enum that specifies notification mode. Requires { notify: true }
|
|
102
|
-
// notifyMode: "failure-change",
|
|
103
|
-
|
|
104
|
-
// A preset that is used as a base for Jest's configuration
|
|
105
|
-
// preset: undefined,
|
|
106
|
-
|
|
107
|
-
// Run tests from one or more projects
|
|
108
|
-
// projects: undefined,
|
|
109
|
-
|
|
110
|
-
// Use this configuration option to add custom reporters to Jest
|
|
111
|
-
// reporters: undefined,
|
|
112
|
-
|
|
113
|
-
// Automatically reset mock state before every test
|
|
114
|
-
// resetMocks: false,
|
|
115
|
-
|
|
116
|
-
// Reset the module registry before running each individual test
|
|
117
|
-
// resetModules: false,
|
|
118
|
-
|
|
119
|
-
// A path to a custom resolver
|
|
120
|
-
// resolver: undefined,
|
|
121
|
-
|
|
122
|
-
// Automatically restore mock state and implementation before every test
|
|
123
|
-
// restoreMocks: false,
|
|
124
|
-
|
|
125
|
-
// The root directory that Jest should scan for tests and modules within
|
|
126
|
-
// rootDir: undefined,
|
|
127
|
-
|
|
128
|
-
// A list of paths to directories that Jest should use to search for files in
|
|
129
|
-
// roots: [
|
|
130
|
-
// "<rootDir>"
|
|
131
|
-
// ],
|
|
132
|
-
|
|
133
|
-
// Allows you to use a custom runner instead of Jest's default test runner
|
|
134
|
-
// runner: "jest-runner",
|
|
135
|
-
|
|
136
|
-
// The paths to modules that run some code to configure or set up the testing environment before each test
|
|
137
|
-
// setupFiles: [],
|
|
138
|
-
|
|
139
|
-
// A list of paths to modules that run some code to configure or set up the testing framework before each test
|
|
140
|
-
// setupFilesAfterEnv: [],
|
|
141
|
-
|
|
142
|
-
// The number of seconds after which a test is considered as slow and reported as such in the results.
|
|
143
|
-
// slowTestThreshold: 5,
|
|
144
|
-
|
|
145
|
-
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
|
|
146
|
-
// snapshotSerializers: [],
|
|
147
|
-
|
|
148
|
-
// The test environment that will be used for testing
|
|
149
|
-
// testEnvironment: "jest-environment-node",
|
|
150
|
-
|
|
151
|
-
// Options that will be passed to the testEnvironment
|
|
152
|
-
// testEnvironmentOptions: {},
|
|
153
|
-
|
|
154
|
-
// Adds a location field to test results
|
|
155
|
-
// testLocationInResults: false,
|
|
156
|
-
|
|
157
|
-
// The glob patterns Jest uses to detect test files
|
|
158
|
-
// testMatch: [
|
|
159
|
-
// "**/__tests__/**/*.[jt]s?(x)",
|
|
160
|
-
// "**/?(*.)+(spec|test).[tj]s?(x)"
|
|
161
|
-
// ],
|
|
162
|
-
|
|
163
|
-
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
|
|
164
|
-
// testPathIgnorePatterns: [
|
|
165
|
-
// "\\\\node_modules\\\\"
|
|
166
|
-
// ],
|
|
167
|
-
|
|
168
|
-
// The regexp pattern or array of patterns that Jest uses to detect test files
|
|
169
|
-
// testRegex: [],
|
|
170
|
-
|
|
171
|
-
// This option allows the use of a custom results processor
|
|
172
|
-
// testResultsProcessor: undefined,
|
|
173
|
-
|
|
174
|
-
// This option allows use of a custom test runner
|
|
175
|
-
// testRunner: "jest-circus/runner",
|
|
176
|
-
|
|
177
|
-
// A map from regular expressions to paths to transformers
|
|
178
|
-
// transform: undefined,
|
|
179
|
-
|
|
180
|
-
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
|
|
181
|
-
// transformIgnorePatterns: [
|
|
182
|
-
// "\\\\node_modules\\\\",
|
|
183
|
-
// "\\.pnp\\.[^\\\\]+$"
|
|
184
|
-
// ],
|
|
185
|
-
|
|
186
|
-
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
|
|
187
|
-
// unmockedModulePathPatterns: undefined,
|
|
188
|
-
|
|
189
|
-
// Indicates whether each individual test should be reported during the run
|
|
190
|
-
// verbose: undefined,
|
|
191
|
-
|
|
192
|
-
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
|
|
193
|
-
// watchPathIgnorePatterns: [],
|
|
194
|
-
|
|
195
|
-
// Whether to use watchman for file crawling
|
|
196
|
-
// watchman: true,
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
export default config;
|