validno 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Schema.js +4 -0
- package/dist/app.js +22 -0
- package/dist/checkRules.js +13 -0
- package/dist/sss.js +1 -0
- package/dist/utils/errors.js +3 -0
- package/dist/validate.js +10 -4
- package/dist/validateKey.js +15 -0
- package/package.json +1 -1
- package/src/Schema.ts +5 -0
- package/src/app.ts +43 -183
- package/src/checkRules.ts +28 -8
- package/src/utils/errors.ts +6 -0
- package/src/validate.ts +12 -5
- package/src/validateKey.ts +21 -0
- package/tests/missing.test.js +131 -0
- package/tests/rules.test.js +0 -0
- package/tests/types.test.js +1 -1
package/dist/Schema.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import validate from "./validate.js";
|
|
2
|
+
import validateKey from "./validateKey.js";
|
|
2
3
|
export const defaultSchemaKeys = [
|
|
3
4
|
"required",
|
|
4
5
|
"type",
|
|
@@ -12,4 +13,7 @@ export class Schema {
|
|
|
12
13
|
validate(data) {
|
|
13
14
|
return validate.call(this, this, data);
|
|
14
15
|
}
|
|
16
|
+
validateKey(key, data) {
|
|
17
|
+
return validateKey.call(this, this, data, key);
|
|
18
|
+
}
|
|
15
19
|
}
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Schema from "./index.js";
|
|
2
|
+
const schema = new Schema({
|
|
3
|
+
val: {
|
|
4
|
+
type: String,
|
|
5
|
+
required: true,
|
|
6
|
+
},
|
|
7
|
+
val2: {
|
|
8
|
+
type: Number,
|
|
9
|
+
required: true,
|
|
10
|
+
rules: {
|
|
11
|
+
min: 3
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const testObj = {
|
|
16
|
+
val: 'string',
|
|
17
|
+
val2: 1
|
|
18
|
+
};
|
|
19
|
+
const res = schema.validate(testObj);
|
|
20
|
+
const res2 = schema.validateKey('val2', testObj);
|
|
21
|
+
console.log(res);
|
|
22
|
+
console.log(res2);
|
package/dist/checkRules.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
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
|
+
};
|
|
2
14
|
const rulesFunctions = {
|
|
3
15
|
custom: (key, val, func) => {
|
|
4
16
|
return func(val);
|
|
@@ -60,6 +72,7 @@ const rulesFunctions = {
|
|
|
60
72
|
};
|
|
61
73
|
},
|
|
62
74
|
lengthMin: (key, val, min) => {
|
|
75
|
+
ensureRuleHasCorrectType(val, rulesParams['lengthMin'].allowedTypes);
|
|
63
76
|
return {
|
|
64
77
|
result: _validations.lengthMin(val, min),
|
|
65
78
|
details: `Длина не может быть меньше ${min} символов`
|
package/dist/sss.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/dist/validate.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import checkRules from "./checkRules.js";
|
|
2
2
|
import checkType from "./checkType.js";
|
|
3
3
|
import { defaultSchemaKeys } from "./Schema.js";
|
|
4
|
+
import _errors from "./utils/errors.js";
|
|
4
5
|
import _validations from "./utils/validations.js";
|
|
5
|
-
const getResultDefaults = () => {
|
|
6
|
+
export const getResultDefaults = () => {
|
|
6
7
|
return {
|
|
7
8
|
ok: null,
|
|
8
9
|
missed: [],
|
|
@@ -24,7 +25,7 @@ const checkIsNested = (obj) => {
|
|
|
24
25
|
return true;
|
|
25
26
|
}
|
|
26
27
|
};
|
|
27
|
-
const mergeResults = (resultsOld, resultsNew) => {
|
|
28
|
+
export const mergeResults = (resultsOld, resultsNew) => {
|
|
28
29
|
const output = getResultDefaults();
|
|
29
30
|
output.failed = [...resultsOld.failed, ...resultsNew.failed];
|
|
30
31
|
output.errors = [...resultsOld.errors, ...resultsNew.errors];
|
|
@@ -34,7 +35,7 @@ const mergeResults = (resultsOld, resultsNew) => {
|
|
|
34
35
|
output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
|
|
35
36
|
return output;
|
|
36
37
|
};
|
|
37
|
-
const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
38
|
+
export const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
38
39
|
let results = getResultDefaults();
|
|
39
40
|
const hasNested = checkIsNested(reqs);
|
|
40
41
|
const missedCheck = [];
|
|
@@ -60,8 +61,13 @@ const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
|
60
61
|
return results;
|
|
61
62
|
}
|
|
62
63
|
if (reqs.required === true && key in data === false || data === undefined) {
|
|
64
|
+
const errMsg = _errors.getMissingError(deepKey);
|
|
63
65
|
missedCheck.push(false);
|
|
64
|
-
results.
|
|
66
|
+
results.missed.push(deepKey);
|
|
67
|
+
results.failed.push(deepKey);
|
|
68
|
+
results.errors.push(errMsg);
|
|
69
|
+
results.byKeys[deepKey] = false;
|
|
70
|
+
results.errorsByKeys[deepKey] = [errMsg];
|
|
65
71
|
return results;
|
|
66
72
|
}
|
|
67
73
|
const typeCheck = checkType(key, data[key], reqs, deepKey);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getResultDefaults, handleReqKey, mergeResults } from "./validate.js";
|
|
2
|
+
const validateKey = (schema, data, key) => {
|
|
3
|
+
if (key in schema.schema === false) {
|
|
4
|
+
throw new Error(`Ключ ${key} отсутствует в схеме`);
|
|
5
|
+
}
|
|
6
|
+
let results = getResultDefaults();
|
|
7
|
+
const keyResult = handleReqKey(key, data, schema.schema[key]);
|
|
8
|
+
results = mergeResults(results, keyResult);
|
|
9
|
+
if (results.failed.length)
|
|
10
|
+
results.ok = false;
|
|
11
|
+
else
|
|
12
|
+
results.ok = true;
|
|
13
|
+
return results;
|
|
14
|
+
};
|
|
15
|
+
export default validateKey;
|
package/package.json
CHANGED
package/src/Schema.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import validate from "./validate.js"
|
|
2
|
+
import validateKey from "./validateKey.js"
|
|
2
3
|
|
|
3
4
|
export type TSchemaItem = {
|
|
4
5
|
required: boolean,
|
|
@@ -37,4 +38,8 @@ export class Schema {
|
|
|
37
38
|
validate(data: any) {
|
|
38
39
|
return validate.call(this, this, data)
|
|
39
40
|
}
|
|
41
|
+
|
|
42
|
+
validateKey(key: string, data: any) {
|
|
43
|
+
return validateKey.call(this, this, data, key)
|
|
44
|
+
}
|
|
40
45
|
}
|
package/src/app.ts
CHANGED
|
@@ -1,199 +1,59 @@
|
|
|
1
1
|
import Schema from "./index.js";
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
dateFrom: {
|
|
20
|
-
required: true,
|
|
21
|
-
type: Date,
|
|
22
|
-
|
|
23
|
-
},
|
|
24
|
-
isActive: {
|
|
25
|
-
required: true,
|
|
26
|
-
type: Boolean,
|
|
27
|
-
},
|
|
28
|
-
list: {
|
|
29
|
-
required: true,
|
|
30
|
-
type: Array,
|
|
31
|
-
},
|
|
32
|
-
listTyped: {
|
|
33
|
-
required: true,
|
|
34
|
-
type: Array,
|
|
35
|
-
eachType: Number
|
|
36
|
-
},
|
|
37
|
-
xNull: {
|
|
38
|
-
required: true,
|
|
39
|
-
type: [null, Number]
|
|
40
|
-
},
|
|
41
|
-
reg: {
|
|
42
|
-
required: true,
|
|
43
|
-
type: RegExp
|
|
44
|
-
},
|
|
45
|
-
oneOfStr: {
|
|
46
|
-
required: false,
|
|
47
|
-
type: String,
|
|
48
|
-
rules: {
|
|
49
|
-
enum: ['done', 'new', 'cancel']
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
email: {
|
|
53
|
-
required: true,
|
|
54
|
-
type: String,
|
|
55
|
-
rules: {
|
|
56
|
-
isEmail: true,
|
|
57
|
-
lengthMin: 55
|
|
58
|
-
},
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
const okObj1 = {
|
|
64
|
-
unknown: 1,
|
|
65
|
-
category: 'xx',
|
|
66
|
-
isMissing: '...',
|
|
67
|
-
isActive: true,
|
|
68
|
-
costPrice: 100,
|
|
69
|
-
dateFrom: new Date(),
|
|
70
|
-
list: [],
|
|
71
|
-
listTyped: [1, 2, 3, 0.00001],
|
|
72
|
-
xNull: null,
|
|
73
|
-
reg: new RegExp('.*'),
|
|
74
|
-
oneOfStr: 'done',
|
|
75
|
-
email: 'xxx@gmail.com'
|
|
76
|
-
}
|
|
3
|
+
// const testSchema = new Schema({
|
|
4
|
+
// field: {
|
|
5
|
+
// type: Number,
|
|
6
|
+
// required: true,
|
|
7
|
+
// rules: {
|
|
8
|
+
// lengthMin: 99
|
|
9
|
+
// }
|
|
10
|
+
// },
|
|
11
|
+
// field2: {
|
|
12
|
+
// type: "xxx",
|
|
13
|
+
// required: true,
|
|
14
|
+
// rules: {
|
|
15
|
+
// lengthMin: 10
|
|
16
|
+
// }
|
|
17
|
+
// }
|
|
18
|
+
// });
|
|
77
19
|
|
|
78
|
-
|
|
79
|
-
|
|
20
|
+
// class StringOwn {
|
|
21
|
+
// constructor(v: any) {
|
|
22
|
+
// v = v
|
|
23
|
+
// }
|
|
24
|
+
// }
|
|
80
25
|
|
|
81
|
-
// const
|
|
82
|
-
// xMultiple: {
|
|
83
|
-
// required: true,
|
|
84
|
-
// type: [String, null]
|
|
85
|
-
// },
|
|
86
|
-
// xNumb: {
|
|
87
|
-
// required: true,
|
|
88
|
-
// type: 'any',
|
|
89
|
-
// rules: {
|
|
90
|
-
// custom: (v: any) => v === 3,
|
|
91
|
-
// is: 3,
|
|
92
|
-
// isNot: 2,
|
|
93
|
-
// min: 3,
|
|
94
|
-
// max: 3,
|
|
95
|
-
// regex: /3|4/
|
|
96
|
-
// }
|
|
97
|
-
// },
|
|
98
|
-
// xStr: {
|
|
99
|
-
// required: true,
|
|
100
|
-
// type: String,
|
|
101
|
-
// rules: {
|
|
102
|
-
// custom: (v: any) => typeof v === 'string',
|
|
103
|
-
// is: 'lol',
|
|
104
|
-
// isNot: 'rofl',
|
|
105
|
-
// length: 3,
|
|
106
|
-
// lengthNot: 999,
|
|
107
|
-
// lengthMin: 3,
|
|
108
|
-
// lengthMax: 3,
|
|
109
|
-
// regex: /\D{3}/
|
|
110
|
-
// }
|
|
111
|
-
// },
|
|
112
|
-
// xArrEach: {
|
|
113
|
-
// required: true,
|
|
114
|
-
// type: Array,
|
|
115
|
-
// eachType: [Number, String]
|
|
116
|
-
// },
|
|
117
|
-
// str: {
|
|
118
|
-
// required: false,
|
|
119
|
-
// type: String
|
|
120
|
-
// }
|
|
121
|
-
// })
|
|
26
|
+
// const strOwn = new StringOwn('xxxxuis')
|
|
122
27
|
|
|
123
|
-
// const
|
|
124
|
-
//
|
|
125
|
-
//
|
|
126
|
-
// xStr: 'lol',
|
|
127
|
-
// xArrEach: [3,2,1,null],
|
|
128
|
-
// str: '233'
|
|
28
|
+
// const body = {
|
|
29
|
+
// "field": 33,
|
|
30
|
+
// "field2": "Ssdasjk"
|
|
129
31
|
// }
|
|
130
32
|
|
|
131
|
-
// const
|
|
132
|
-
// console.log(rulesObjResult)
|
|
33
|
+
// const validationResult = testSchema.validate(body);
|
|
133
34
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
35
|
+
|
|
36
|
+
const schema = new Schema({
|
|
37
|
+
val: {
|
|
137
38
|
type: String,
|
|
138
|
-
|
|
139
|
-
lengthMin: 5
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
address: {
|
|
143
|
-
city: {
|
|
144
|
-
required: true,
|
|
145
|
-
type: String,
|
|
146
|
-
},
|
|
147
|
-
street: {
|
|
148
|
-
required: true,
|
|
149
|
-
type: String
|
|
150
|
-
},
|
|
151
|
-
coords: {
|
|
152
|
-
x: {
|
|
153
|
-
required: true,
|
|
154
|
-
type: Number,
|
|
155
|
-
},
|
|
156
|
-
y: {
|
|
157
|
-
required: true,
|
|
158
|
-
type: Number,
|
|
159
|
-
rules: {
|
|
160
|
-
min: 1
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
},
|
|
39
|
+
required: true,
|
|
164
40
|
},
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
type: String,
|
|
171
|
-
rules: {
|
|
172
|
-
lengthMinMax: [10,15]
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
41
|
+
val2: {
|
|
42
|
+
type: Number,
|
|
43
|
+
required: true,
|
|
44
|
+
rules: {
|
|
45
|
+
min: 3
|
|
176
46
|
}
|
|
177
47
|
}
|
|
178
48
|
})
|
|
179
49
|
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
city: 'Moscow',
|
|
184
|
-
street: "41, Komsomolskiy",
|
|
185
|
-
coords: {
|
|
186
|
-
x: 1,
|
|
187
|
-
y: 2
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
deep1: {
|
|
191
|
-
deep2: {
|
|
192
|
-
deep3: {
|
|
193
|
-
deep4: 'ssss'
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
50
|
+
const testObj = {
|
|
51
|
+
val: 'string',
|
|
52
|
+
val2: 1
|
|
197
53
|
}
|
|
198
54
|
|
|
199
|
-
|
|
55
|
+
const res = schema.validate(testObj)
|
|
56
|
+
const res2 = schema.validateKey('val2', testObj)
|
|
57
|
+
|
|
58
|
+
console.log(res)
|
|
59
|
+
console.log(res2)
|
package/src/checkRules.ts
CHANGED
|
@@ -3,6 +3,23 @@ import _validations from "./utils/validations.js"
|
|
|
3
3
|
|
|
4
4
|
export type TRule = {[key: string]: any}
|
|
5
5
|
|
|
6
|
+
type TLengths = string | Array<any>
|
|
7
|
+
|
|
8
|
+
export const rulesParams = {
|
|
9
|
+
lengthMin: {
|
|
10
|
+
allowedTypes: [String]
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const ensureRuleHasCorrectType = (value: any, allowedTypes: any[]) => {
|
|
15
|
+
const isInAllowedList = allowedTypes.some(TYPE => {
|
|
16
|
+
const getType = (el: any) => Object.prototype.toString.call(el)
|
|
17
|
+
return getType(new TYPE()) == getType(value)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
return isInAllowedList
|
|
21
|
+
}
|
|
22
|
+
|
|
6
23
|
const rulesFunctions: any = {
|
|
7
24
|
custom: (key: string, val: any, func: Function) => {
|
|
8
25
|
return func(val)
|
|
@@ -25,38 +42,38 @@ const rulesFunctions: any = {
|
|
|
25
42
|
details: `Значение не должно быть "${notEqualTo}"`
|
|
26
43
|
}
|
|
27
44
|
},
|
|
28
|
-
min: (key: string, val:
|
|
45
|
+
min: (key: string, val: number, min: number) => {
|
|
29
46
|
return {
|
|
30
47
|
result: _validations.isNumberGte(val, min),
|
|
31
48
|
details: "Значение не может быть меньше " + min
|
|
32
49
|
}
|
|
33
50
|
},
|
|
34
|
-
max: (key: string, val:
|
|
51
|
+
max: (key: string, val: number, max: number) => {
|
|
35
52
|
return {
|
|
36
53
|
result: _validations.isNumberLte(val, max),
|
|
37
54
|
details: "Значение не может быть больше " + max
|
|
38
55
|
}
|
|
39
56
|
},
|
|
40
|
-
minMax: (key: string, val:
|
|
57
|
+
minMax: (key: string, val: number, minMax: [min: number, max: number]) => {
|
|
41
58
|
const [min, max] = minMax
|
|
42
59
|
return {
|
|
43
60
|
result: _validations.isNumberGte(val, min) && _validations.isNumberLte(val, max),
|
|
44
61
|
details: `Значение должно быть в пределах ${min}-${max}`
|
|
45
62
|
}
|
|
46
63
|
},
|
|
47
|
-
length: (key: string, val:
|
|
64
|
+
length: (key: string, val: TLengths, length: number) => {
|
|
48
65
|
return {
|
|
49
66
|
result: _validations.lengthIs(val, length),
|
|
50
67
|
details: "Количество символов должно быть равным " + length
|
|
51
68
|
}
|
|
52
69
|
},
|
|
53
|
-
lengthNot: (key: string, val:
|
|
70
|
+
lengthNot: (key: string, val: TLengths, lengthNot: number) => {
|
|
54
71
|
return {
|
|
55
72
|
result: _validations.lengthNot(val, lengthNot),
|
|
56
73
|
details: "Количество символов не должно быть равным " + lengthNot
|
|
57
74
|
}
|
|
58
75
|
},
|
|
59
|
-
lengthMinMax: (key: string, val:
|
|
76
|
+
lengthMinMax: (key: string, val: TLengths, minMax: [min: number, max: number]) => {
|
|
60
77
|
const [min, max] = minMax
|
|
61
78
|
|
|
62
79
|
return {
|
|
@@ -64,13 +81,15 @@ const rulesFunctions: any = {
|
|
|
64
81
|
details: `Длина должна быть от ${min} до ${max} символов`
|
|
65
82
|
}
|
|
66
83
|
},
|
|
67
|
-
lengthMin: (key: string, val:
|
|
84
|
+
lengthMin: (key: string, val: TLengths, min: number) => {
|
|
85
|
+
ensureRuleHasCorrectType(val, rulesParams['lengthMin'].allowedTypes)
|
|
86
|
+
|
|
68
87
|
return {
|
|
69
88
|
result: _validations.lengthMin(val, min),
|
|
70
89
|
details: `Длина не может быть меньше ${min} символов`
|
|
71
90
|
}
|
|
72
91
|
},
|
|
73
|
-
lengthMax: (key: string, val:
|
|
92
|
+
lengthMax: (key: string, val: TLengths, max: number) => {
|
|
74
93
|
return {
|
|
75
94
|
result: _validations.lengthMax(val, max),
|
|
76
95
|
details: `Длина не может быть больше ${max} символов`
|
|
@@ -135,6 +154,7 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
|
|
|
135
154
|
|
|
136
155
|
// If key has failed rules checks
|
|
137
156
|
const failedResults = allResults.filter(el => el.result === false)
|
|
157
|
+
|
|
138
158
|
if (failedResults.length) {
|
|
139
159
|
result.ok = false
|
|
140
160
|
result.details = failedResults.map(el => el.details)
|
package/src/validate.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import checkRules from "./checkRules.js";
|
|
2
2
|
import checkType from "./checkType.js";
|
|
3
3
|
import { defaultSchemaKeys, Schema, TSchemaInput } from "./Schema.js";
|
|
4
|
+
import _errors from "./utils/errors.js";
|
|
4
5
|
import _validations from "./utils/validations.js";
|
|
5
6
|
|
|
6
|
-
type TResult = {
|
|
7
|
+
export type TResult = {
|
|
7
8
|
ok: null | boolean,
|
|
8
9
|
missed: string[],
|
|
9
10
|
failed: string[],
|
|
@@ -13,7 +14,7 @@ type TResult = {
|
|
|
13
14
|
errorsByKeys: {[key: string]: string[]}
|
|
14
15
|
};
|
|
15
16
|
|
|
16
|
-
const getResultDefaults = (): TResult => {
|
|
17
|
+
export const getResultDefaults = (): TResult => {
|
|
17
18
|
return {
|
|
18
19
|
ok: null,
|
|
19
20
|
missed: [],
|
|
@@ -37,7 +38,7 @@ const checkIsNested = (obj: {[key: string]: any}) => {
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
41
|
+
export const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
41
42
|
const output = getResultDefaults()
|
|
42
43
|
|
|
43
44
|
output.failed = [...resultsOld.failed, ...resultsNew.failed]
|
|
@@ -50,7 +51,7 @@ const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
|
50
51
|
return output
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key) => {
|
|
54
|
+
export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key) => {
|
|
54
55
|
let results = getResultDefaults()
|
|
55
56
|
const hasNested = checkIsNested(reqs)
|
|
56
57
|
|
|
@@ -98,8 +99,14 @@ const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key)
|
|
|
98
99
|
// Check missing keys
|
|
99
100
|
// @ts-ignore
|
|
100
101
|
if (reqs.required === true && key in data === false || data === undefined) {
|
|
102
|
+
const errMsg = _errors.getMissingError(deepKey)
|
|
103
|
+
|
|
101
104
|
missedCheck.push(false)
|
|
102
|
-
results.
|
|
105
|
+
results.missed.push(deepKey)
|
|
106
|
+
results.failed.push(deepKey)
|
|
107
|
+
results.errors.push(errMsg)
|
|
108
|
+
results.byKeys[deepKey] = false
|
|
109
|
+
results.errorsByKeys[deepKey] = [errMsg]
|
|
103
110
|
return results
|
|
104
111
|
}
|
|
105
112
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Schema } from "./Schema.js";
|
|
2
|
+
import { getResultDefaults, handleReqKey, mergeResults, TResult } from "./validate.js";
|
|
3
|
+
|
|
4
|
+
const validateKey = (schema: Schema, data: any, key: string): TResult => {
|
|
5
|
+
if (key in schema.schema === false) {
|
|
6
|
+
throw new Error(`Ключ ${key} отсутствует в схеме`)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let results: TResult = getResultDefaults()
|
|
10
|
+
//@ts-ignore
|
|
11
|
+
const keyResult = handleReqKey(key, data, schema.schema[key])
|
|
12
|
+
|
|
13
|
+
results = mergeResults(results, keyResult)
|
|
14
|
+
|
|
15
|
+
if (results.failed.length) results.ok = false
|
|
16
|
+
else results.ok = true
|
|
17
|
+
|
|
18
|
+
return results;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default validateKey;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {describe, expect, test} from '@jest/globals';
|
|
2
|
+
import { Schema } from '../dist/Schema.js';
|
|
3
|
+
import _errors from '../dist/utils/errors.js';
|
|
4
|
+
|
|
5
|
+
describe('Тестирование обработки пропущенных свойста', () => {
|
|
6
|
+
test('Отсутствующий ключ правильно отображается в результате', () => {
|
|
7
|
+
const missedKey = 'testKey'
|
|
8
|
+
const missedKey2 = 'testKey2'
|
|
9
|
+
|
|
10
|
+
const okKey = 'okKey'
|
|
11
|
+
|
|
12
|
+
const scheme = new Schema({
|
|
13
|
+
[missedKey]: {
|
|
14
|
+
required: true,
|
|
15
|
+
type: String
|
|
16
|
+
},
|
|
17
|
+
[missedKey2]: {
|
|
18
|
+
required: true,
|
|
19
|
+
type: String
|
|
20
|
+
},
|
|
21
|
+
[okKey]: {
|
|
22
|
+
required: true,
|
|
23
|
+
type: String
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const obj = {
|
|
28
|
+
[okKey]: 'string'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const result = scheme.validate(obj)
|
|
32
|
+
|
|
33
|
+
expect(result).toEqual({
|
|
34
|
+
ok: false,
|
|
35
|
+
failed: [missedKey, missedKey2],
|
|
36
|
+
missed: [missedKey, missedKey2],
|
|
37
|
+
errors: [
|
|
38
|
+
_errors.getMissingError(missedKey),
|
|
39
|
+
_errors.getMissingError(missedKey2)
|
|
40
|
+
],
|
|
41
|
+
passed: [okKey],
|
|
42
|
+
byKeys: {
|
|
43
|
+
[missedKey]: false,
|
|
44
|
+
[missedKey2]: false,
|
|
45
|
+
[okKey]: true
|
|
46
|
+
},
|
|
47
|
+
errorsByKeys: {
|
|
48
|
+
[okKey]: [],
|
|
49
|
+
[missedKey]: [_errors.getMissingError(missedKey)],
|
|
50
|
+
[missedKey2]: [_errors.getMissingError(missedKey2)]
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('Присутствующий ключ правильно отображается в результате', () => {
|
|
56
|
+
const key = 'testKey'
|
|
57
|
+
const key2 = 'testKey2'
|
|
58
|
+
|
|
59
|
+
const scheme = new Schema({
|
|
60
|
+
[key]: {
|
|
61
|
+
required: true,
|
|
62
|
+
type: String
|
|
63
|
+
},
|
|
64
|
+
[key2]: {
|
|
65
|
+
required: true,
|
|
66
|
+
type: String
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const obj = {
|
|
71
|
+
[key]: 'string',
|
|
72
|
+
[key2]: 'string'
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const result = scheme.validate(obj)
|
|
76
|
+
|
|
77
|
+
expect(result).toEqual({
|
|
78
|
+
ok: true,
|
|
79
|
+
failed: [],
|
|
80
|
+
missed: [],
|
|
81
|
+
errors: [],
|
|
82
|
+
passed: [key, key2],
|
|
83
|
+
byKeys: {
|
|
84
|
+
[key]: true,
|
|
85
|
+
[key2]: true
|
|
86
|
+
},
|
|
87
|
+
errorsByKeys: {
|
|
88
|
+
[key]: [],
|
|
89
|
+
[key2]: []
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('Отсутствующий необязательный ключ правильно отображается в результах', () => {
|
|
95
|
+
const key = 'testKey'
|
|
96
|
+
const key2 = 'testKey2'
|
|
97
|
+
|
|
98
|
+
const scheme = new Schema({
|
|
99
|
+
[key]: {
|
|
100
|
+
required: false,
|
|
101
|
+
type: String
|
|
102
|
+
},
|
|
103
|
+
[key2]: {
|
|
104
|
+
required: true,
|
|
105
|
+
type: String
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
const obj = {
|
|
110
|
+
[key2]: 'string'
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const result = scheme.validate(obj)
|
|
114
|
+
|
|
115
|
+
expect(result).toEqual({
|
|
116
|
+
ok: true,
|
|
117
|
+
failed: [],
|
|
118
|
+
missed: [],
|
|
119
|
+
errors: [],
|
|
120
|
+
passed: [key, key2],
|
|
121
|
+
byKeys: {
|
|
122
|
+
[key]: true,
|
|
123
|
+
[key2]: true
|
|
124
|
+
},
|
|
125
|
+
errorsByKeys: {
|
|
126
|
+
[key]: [],
|
|
127
|
+
[key2]: []
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
})
|
|
File without changes
|
package/tests/types.test.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {describe, expect, test} from '@jest/globals';
|
|
2
2
|
import { Schema } from '../dist/Schema.js';
|
|
3
3
|
|
|
4
|
-
describe(
|
|
4
|
+
describe('Проверка каждого типа по отдельности', () => {
|
|
5
5
|
test('Тест String', () => {
|
|
6
6
|
const key = 'testKey'
|
|
7
7
|
const scheme = new Schema({
|