validno 0.4.3 → 0.4.4
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/dev.js +4 -73
- package/dist/tests/cases/corruptedData.test.d.ts +2 -0
- package/dist/tests/cases/corruptedData.test.d.ts.map +1 -0
- package/dist/tests/cases/corruptedData.test.js +81 -0
- package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts +2 -0
- package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts.map +1 -0
- package/dist/tests/cases/dataInputIsNotAnObject.test.js +24 -0
- package/dist/tests/cases/notRequiredNestedKey.test.d.ts +2 -0
- package/dist/tests/cases/notRequiredNestedKey.test.d.ts.map +1 -0
- package/dist/tests/cases/notRequiredNestedKey.test.js +38 -0
- package/dist/tests/cases/secondLevelDeepValidates.test.d.ts +2 -0
- package/dist/tests/cases/secondLevelDeepValidates.test.d.ts.map +1 -0
- package/dist/tests/cases/secondLevelDeepValidates.test.js +112 -0
- package/dist/tests/customMessage.test.d.ts +2 -0
- package/dist/tests/customMessage.test.d.ts.map +1 -0
- package/dist/tests/customMessage.test.js +102 -0
- package/dist/tests/customType.test.d.ts +2 -0
- package/dist/tests/customType.test.d.ts.map +1 -0
- package/dist/tests/customType.test.js +146 -0
- package/dist/tests/joinErrors.test.d.ts +2 -0
- package/dist/tests/joinErrors.test.d.ts.map +1 -0
- package/dist/tests/joinErrors.test.js +62 -0
- package/dist/tests/missing.test.d.ts +2 -0
- package/dist/tests/missing.test.d.ts.map +1 -0
- package/dist/tests/missing.test.js +224 -0
- package/dist/tests/onlyKeys.test.d.ts +2 -0
- package/dist/tests/onlyKeys.test.d.ts.map +1 -0
- package/dist/tests/onlyKeys.test.js +74 -0
- package/dist/tests/types.test.d.ts +2 -0
- package/dist/tests/types.test.d.ts.map +1 -0
- package/dist/tests/types.test.js +273 -0
- package/dist/tests/utils/_errors.test.d.ts +2 -0
- package/dist/tests/utils/_errors.test.d.ts.map +1 -0
- package/dist/tests/utils/_errors.test.js +47 -0
- package/dist/tests/utils/_validations.test.d.ts +2 -0
- package/dist/tests/utils/_validations.test.d.ts.map +1 -0
- package/dist/tests/utils/_validations.test.js +773 -0
- package/package.json +4 -2
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../Schema';
|
|
3
|
+
describe('Тестирование функции joinErrors', () => {
|
|
4
|
+
const keys = ['ops1', 'ops2', 'miss'];
|
|
5
|
+
const schema = new Schema({
|
|
6
|
+
[keys[0]]: {
|
|
7
|
+
type: Number,
|
|
8
|
+
required: true,
|
|
9
|
+
title: 'titl',
|
|
10
|
+
},
|
|
11
|
+
[keys[1]]: {
|
|
12
|
+
type: String,
|
|
13
|
+
required: true,
|
|
14
|
+
title: 'titl',
|
|
15
|
+
},
|
|
16
|
+
[keys[2]]: {
|
|
17
|
+
type: String,
|
|
18
|
+
required: true,
|
|
19
|
+
title: 'titl',
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const schema2 = new Schema({});
|
|
23
|
+
test('Пустая строка при отсутствии ошибок', () => {
|
|
24
|
+
const objOk = {
|
|
25
|
+
[keys[0]]: 222,
|
|
26
|
+
[keys[1]]: 'str',
|
|
27
|
+
[keys[2]]: 'here'
|
|
28
|
+
};
|
|
29
|
+
const res = schema.validate(objOk);
|
|
30
|
+
expect(res.ok).toBe(true);
|
|
31
|
+
expect(res.joinErrors()).toBe("");
|
|
32
|
+
});
|
|
33
|
+
test('Пустая строка при отсутствии ошибок и пустой схеме', () => {
|
|
34
|
+
const objOk = {
|
|
35
|
+
[keys[0]]: 222,
|
|
36
|
+
[keys[1]]: 'str',
|
|
37
|
+
[keys[2]]: 'here'
|
|
38
|
+
};
|
|
39
|
+
const res = schema2.validate(objOk);
|
|
40
|
+
expect(res.ok).toBe(true);
|
|
41
|
+
expect(res.joinErrors()).toBe("");
|
|
42
|
+
});
|
|
43
|
+
test('Корректная строка при наличии ошибок', () => {
|
|
44
|
+
const unexpectedKey = 'unexpectedKey';
|
|
45
|
+
const objOk = {
|
|
46
|
+
[keys[0]]: '222',
|
|
47
|
+
[keys[1]]: 1,
|
|
48
|
+
[unexpectedKey]: 'not supposed to be here)'
|
|
49
|
+
};
|
|
50
|
+
const res = schema.validate(objOk);
|
|
51
|
+
const errorsStr = res.joinErrors();
|
|
52
|
+
const stringHasAllKeys = [
|
|
53
|
+
errorsStr.includes(keys[0]),
|
|
54
|
+
errorsStr.includes(keys[1]),
|
|
55
|
+
errorsStr.includes(keys[2]),
|
|
56
|
+
!errorsStr.includes(unexpectedKey)
|
|
57
|
+
].every(k => k === true);
|
|
58
|
+
expect(res.ok).toBe(false);
|
|
59
|
+
expect(errorsStr.length).toBeGreaterThan(5);
|
|
60
|
+
expect(stringHasAllKeys).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"missing.test.d.ts","sourceRoot":"","sources":["../../src/tests/missing.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../Schema';
|
|
3
|
+
import _errors from '../utils/errors';
|
|
4
|
+
describe('Тестирование обработки пропущенных свойста', () => {
|
|
5
|
+
test('Отсутствующий ключ правильно отображается в результате', () => {
|
|
6
|
+
const missedKey = 'testKey';
|
|
7
|
+
const missedKey2 = 'testKey2';
|
|
8
|
+
const okKey = 'okKey';
|
|
9
|
+
const scheme = new Schema({
|
|
10
|
+
[missedKey]: {
|
|
11
|
+
required: true,
|
|
12
|
+
type: String
|
|
13
|
+
},
|
|
14
|
+
[missedKey2]: {
|
|
15
|
+
required: true,
|
|
16
|
+
type: String
|
|
17
|
+
},
|
|
18
|
+
[okKey]: {
|
|
19
|
+
required: true,
|
|
20
|
+
type: String
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
const obj = {
|
|
24
|
+
[okKey]: 'string'
|
|
25
|
+
};
|
|
26
|
+
const result = scheme.validate(obj);
|
|
27
|
+
expect(result).toEqual({
|
|
28
|
+
ok: false,
|
|
29
|
+
failed: [missedKey, missedKey2],
|
|
30
|
+
missed: [missedKey, missedKey2],
|
|
31
|
+
errors: [
|
|
32
|
+
_errors.getMissingError(missedKey),
|
|
33
|
+
_errors.getMissingError(missedKey2)
|
|
34
|
+
],
|
|
35
|
+
passed: [okKey],
|
|
36
|
+
byKeys: {
|
|
37
|
+
[missedKey]: false,
|
|
38
|
+
[missedKey2]: false,
|
|
39
|
+
[okKey]: true
|
|
40
|
+
},
|
|
41
|
+
errorsByKeys: {
|
|
42
|
+
[missedKey]: [_errors.getMissingError(missedKey)],
|
|
43
|
+
[missedKey2]: [_errors.getMissingError(missedKey2)]
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
test('Присутствующий ключ правильно отображается в результате', () => {
|
|
48
|
+
const key = 'testKey';
|
|
49
|
+
const key2 = 'testKey2';
|
|
50
|
+
const scheme = new Schema({
|
|
51
|
+
[key]: {
|
|
52
|
+
required: true,
|
|
53
|
+
type: String
|
|
54
|
+
},
|
|
55
|
+
[key2]: {
|
|
56
|
+
required: true,
|
|
57
|
+
type: String
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const obj = {
|
|
61
|
+
[key]: 'string',
|
|
62
|
+
[key2]: 'string'
|
|
63
|
+
};
|
|
64
|
+
const result = scheme.validate(obj);
|
|
65
|
+
expect(result).toEqual({
|
|
66
|
+
ok: true,
|
|
67
|
+
failed: [],
|
|
68
|
+
missed: [],
|
|
69
|
+
errors: [],
|
|
70
|
+
passed: [key, key2],
|
|
71
|
+
byKeys: {
|
|
72
|
+
[key]: true,
|
|
73
|
+
[key2]: true
|
|
74
|
+
},
|
|
75
|
+
errorsByKeys: {}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
test('Отсутствующий необязательный ключ правильно отображается в результах', () => {
|
|
79
|
+
const key = 'testKey';
|
|
80
|
+
const key2 = 'testKey2';
|
|
81
|
+
const scheme = new Schema({
|
|
82
|
+
[key]: {
|
|
83
|
+
required: false,
|
|
84
|
+
type: String
|
|
85
|
+
},
|
|
86
|
+
[key2]: {
|
|
87
|
+
required: true,
|
|
88
|
+
type: String
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
const obj = {
|
|
92
|
+
[key2]: 'string'
|
|
93
|
+
};
|
|
94
|
+
const result = scheme.validate(obj);
|
|
95
|
+
expect(result).toEqual({
|
|
96
|
+
ok: true,
|
|
97
|
+
failed: [],
|
|
98
|
+
missed: [],
|
|
99
|
+
errors: [],
|
|
100
|
+
passed: [key, key2],
|
|
101
|
+
byKeys: {
|
|
102
|
+
[key]: true,
|
|
103
|
+
[key2]: true
|
|
104
|
+
},
|
|
105
|
+
errorsByKeys: {}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
test('Отсутствующие ключи в deep объекте корректно отображаются в результате', () => {
|
|
109
|
+
const scheme = new Schema({
|
|
110
|
+
parent: {
|
|
111
|
+
childA: {
|
|
112
|
+
childA1: {
|
|
113
|
+
type: String,
|
|
114
|
+
required: true
|
|
115
|
+
},
|
|
116
|
+
childA2: {
|
|
117
|
+
type: String,
|
|
118
|
+
required: true
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
childB: {
|
|
122
|
+
type: String,
|
|
123
|
+
required: true
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
parentBMissing: {
|
|
127
|
+
type: String,
|
|
128
|
+
required: true
|
|
129
|
+
},
|
|
130
|
+
keyOk: {
|
|
131
|
+
type: String,
|
|
132
|
+
required: true
|
|
133
|
+
},
|
|
134
|
+
notRequired: {
|
|
135
|
+
type: String,
|
|
136
|
+
required: false
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
const obj = {
|
|
140
|
+
parent: {
|
|
141
|
+
childB: 'str'
|
|
142
|
+
},
|
|
143
|
+
keyOk: 'x'
|
|
144
|
+
};
|
|
145
|
+
const result = scheme.validate(obj);
|
|
146
|
+
result.failed.sort();
|
|
147
|
+
result.missed.sort();
|
|
148
|
+
result.errors.sort();
|
|
149
|
+
result.passed.sort();
|
|
150
|
+
expect(result).toEqual({
|
|
151
|
+
ok: false,
|
|
152
|
+
failed: ['parent', 'parent.childA', 'parent.childA.childA1', 'parent.childA.childA2', 'parentBMissing'].sort(),
|
|
153
|
+
missed: ['parent.childA.childA1', 'parent.childA.childA2', 'parentBMissing'].sort(),
|
|
154
|
+
errors: [
|
|
155
|
+
"Missing value for 'parent.childA.childA1'",
|
|
156
|
+
"Missing value for 'parent.childA.childA2'",
|
|
157
|
+
"Missing value for 'parentBMissing'",
|
|
158
|
+
].sort(),
|
|
159
|
+
passed: ['parent.childB', 'keyOk', 'notRequired'].sort(),
|
|
160
|
+
byKeys: {
|
|
161
|
+
parent: false,
|
|
162
|
+
'parent.childA': false,
|
|
163
|
+
'parent.childA.childA1': false,
|
|
164
|
+
'parent.childA.childA2': false,
|
|
165
|
+
'parent.childB': true,
|
|
166
|
+
parentBMissing: false,
|
|
167
|
+
keyOk: true,
|
|
168
|
+
notRequired: true
|
|
169
|
+
},
|
|
170
|
+
errorsByKeys: {
|
|
171
|
+
'parent.childA.childA1': ["Missing value for 'parent.childA.childA1'"],
|
|
172
|
+
'parent.childA.childA2': ["Missing value for 'parent.childA.childA2'"],
|
|
173
|
+
parentBMissing: ["Missing value for 'parentBMissing'"]
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
describe('Свойство required по умолчанию равно true', () => {
|
|
179
|
+
test('Свойство required по умолчанию равно true', () => {
|
|
180
|
+
const scheme = new Schema({
|
|
181
|
+
testKey: {
|
|
182
|
+
type: String
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
const obj = {};
|
|
186
|
+
const result = scheme.validate(obj);
|
|
187
|
+
expect(result).toEqual({
|
|
188
|
+
ok: false,
|
|
189
|
+
failed: ['testKey'],
|
|
190
|
+
missed: ['testKey'],
|
|
191
|
+
errors: ["Missing value for 'testKey'"],
|
|
192
|
+
passed: [],
|
|
193
|
+
byKeys: {
|
|
194
|
+
testKey: false
|
|
195
|
+
},
|
|
196
|
+
errorsByKeys: {
|
|
197
|
+
testKey: ["Missing value for 'testKey'"]
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
test('Свойство required=true корректно обрабатывается', () => {
|
|
202
|
+
const scheme = new Schema({
|
|
203
|
+
testKey: {
|
|
204
|
+
type: String,
|
|
205
|
+
required: true
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
const obj = {};
|
|
209
|
+
const result = scheme.validate(obj);
|
|
210
|
+
expect(result).toEqual({
|
|
211
|
+
ok: false,
|
|
212
|
+
failed: ['testKey'],
|
|
213
|
+
missed: ['testKey'],
|
|
214
|
+
errors: ["Missing value for 'testKey'"],
|
|
215
|
+
passed: [],
|
|
216
|
+
byKeys: {
|
|
217
|
+
testKey: false
|
|
218
|
+
},
|
|
219
|
+
errorsByKeys: {
|
|
220
|
+
testKey: ["Missing value for 'testKey'"]
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onlyKeys.test.d.ts","sourceRoot":"","sources":["../../src/tests/onlyKeys.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../Schema';
|
|
3
|
+
describe('Параметр onlyKeys возвращает корректный результат валидации объекта', () => {
|
|
4
|
+
const schema = new Schema({
|
|
5
|
+
str: {
|
|
6
|
+
type: String,
|
|
7
|
+
required: true,
|
|
8
|
+
},
|
|
9
|
+
numb: {
|
|
10
|
+
type: Number,
|
|
11
|
+
required: true
|
|
12
|
+
},
|
|
13
|
+
obj: {
|
|
14
|
+
deep1: {
|
|
15
|
+
type: Boolean,
|
|
16
|
+
required: true
|
|
17
|
+
},
|
|
18
|
+
deep2: {
|
|
19
|
+
type: Boolean,
|
|
20
|
+
required: false
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
test('Проверяются все ключи, если !onlyKeys', () => {
|
|
25
|
+
const obj = {
|
|
26
|
+
str: 'string',
|
|
27
|
+
numb: 1,
|
|
28
|
+
obj: {
|
|
29
|
+
deep1: true,
|
|
30
|
+
deep2: true
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const result = schema.validate(obj);
|
|
34
|
+
expect(result.ok).toBe(true);
|
|
35
|
+
expect(result.passed.length).toBe(5);
|
|
36
|
+
});
|
|
37
|
+
test('Проверяются только необходимые ключи, если представлен onlyKeys (string)', () => {
|
|
38
|
+
const obj = {
|
|
39
|
+
str: 'string',
|
|
40
|
+
numb: 1,
|
|
41
|
+
obj: {
|
|
42
|
+
deep1: true,
|
|
43
|
+
deep2: true
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const keyToCheck = 'str';
|
|
47
|
+
const result = schema.validate(obj, keyToCheck);
|
|
48
|
+
expect(result.ok).toBe(true);
|
|
49
|
+
expect(result.passed.length).toBe(1);
|
|
50
|
+
expect(result.passed.includes(keyToCheck)).toBe(true);
|
|
51
|
+
expect(result.missed.length).toBe(0);
|
|
52
|
+
expect(result.failed.length).toBe(0);
|
|
53
|
+
expect(result.errors.length).toBe(0);
|
|
54
|
+
});
|
|
55
|
+
test('Проверяются только необходимые ключи, если представлен onlyKeys (string[])', () => {
|
|
56
|
+
const obj = {
|
|
57
|
+
str: 'string',
|
|
58
|
+
numb: 1,
|
|
59
|
+
obj: {
|
|
60
|
+
deep1: true,
|
|
61
|
+
deep2: true
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const keyToCheck = ['str', 'obj'];
|
|
65
|
+
const result = schema.validate(obj, keyToCheck);
|
|
66
|
+
expect(result.ok).toBe(true);
|
|
67
|
+
expect(result.passed.length).toBe(4);
|
|
68
|
+
expect(result.passed.includes(keyToCheck[0])).toBe(true);
|
|
69
|
+
expect(result.passed.includes(keyToCheck[1])).toBe(true);
|
|
70
|
+
expect(result.missed.length).toBe(0);
|
|
71
|
+
expect(result.failed.length).toBe(0);
|
|
72
|
+
expect(result.errors.length).toBe(0);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.test.d.ts","sourceRoot":"","sources":["../../src/tests/types.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { Schema } from '../Schema';
|
|
2
|
+
import { describe, expect, test } from '@jest/globals';
|
|
3
|
+
describe('Проверка каждого типа по отдельности', () => {
|
|
4
|
+
test('Тест String', () => {
|
|
5
|
+
const key = 'testKey';
|
|
6
|
+
const scheme = new Schema({
|
|
7
|
+
[key]: {
|
|
8
|
+
required: true,
|
|
9
|
+
type: String
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
const objOK = {
|
|
13
|
+
[key]: 'string'
|
|
14
|
+
};
|
|
15
|
+
const resOK = scheme.validate(objOK);
|
|
16
|
+
expect(resOK.ok).toBe(true);
|
|
17
|
+
expect(resOK.passed.includes(key)).toBe(true);
|
|
18
|
+
expect(resOK.byKeys[key]).toBe(true);
|
|
19
|
+
const objBad = {
|
|
20
|
+
[key]: 1
|
|
21
|
+
};
|
|
22
|
+
const resBad = scheme.validate(objBad);
|
|
23
|
+
expect(resBad.ok).toBe(false);
|
|
24
|
+
expect(resBad.failed.includes(key)).toBe(true);
|
|
25
|
+
expect(resBad.passed.includes(key)).toBe(false);
|
|
26
|
+
expect(resBad.byKeys[key]).toBe(false);
|
|
27
|
+
});
|
|
28
|
+
test('Тест Number', () => {
|
|
29
|
+
const key = 'testKey';
|
|
30
|
+
const scheme = new Schema({
|
|
31
|
+
[key]: {
|
|
32
|
+
required: true,
|
|
33
|
+
type: Number
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const objOK = {
|
|
37
|
+
[key]: 111
|
|
38
|
+
};
|
|
39
|
+
const resOK = scheme.validate(objOK);
|
|
40
|
+
expect(resOK.ok).toBe(true);
|
|
41
|
+
expect(resOK.passed.includes(key)).toBe(true);
|
|
42
|
+
expect(resOK.byKeys[key]).toBe(true);
|
|
43
|
+
const objBad = {
|
|
44
|
+
[key]: 'string'
|
|
45
|
+
};
|
|
46
|
+
const resBad = scheme.validate(objBad);
|
|
47
|
+
expect(resBad.ok).toBe(false);
|
|
48
|
+
expect(resBad.failed.includes(key)).toBe(true);
|
|
49
|
+
expect(resBad.passed.includes(key)).toBe(false);
|
|
50
|
+
expect(resBad.byKeys[key]).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
test('Тест Boolean', () => {
|
|
53
|
+
const key = 'testKey';
|
|
54
|
+
const scheme = new Schema({
|
|
55
|
+
[key]: {
|
|
56
|
+
required: true,
|
|
57
|
+
type: Boolean
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const objOK = {
|
|
61
|
+
[key]: true
|
|
62
|
+
};
|
|
63
|
+
const resOK = scheme.validate(objOK);
|
|
64
|
+
expect(resOK.ok).toBe(true);
|
|
65
|
+
expect(resOK.passed.includes(key)).toBe(true);
|
|
66
|
+
expect(resOK.byKeys[key]).toBe(true);
|
|
67
|
+
const objBad = {
|
|
68
|
+
[key]: 'false'
|
|
69
|
+
};
|
|
70
|
+
const resBad = scheme.validate(objBad);
|
|
71
|
+
expect(resBad.ok).toBe(false);
|
|
72
|
+
expect(resBad.failed.includes(key)).toBe(true);
|
|
73
|
+
expect(resBad.passed.includes(key)).toBe(false);
|
|
74
|
+
expect(resBad.byKeys[key]).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
test('Тест Array', () => {
|
|
77
|
+
const key = 'testKey';
|
|
78
|
+
const scheme = new Schema({
|
|
79
|
+
[key]: {
|
|
80
|
+
required: true,
|
|
81
|
+
type: Array
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
const objOK = {
|
|
85
|
+
[key]: []
|
|
86
|
+
};
|
|
87
|
+
const objOK2 = {
|
|
88
|
+
[key]: [1, 2, 3]
|
|
89
|
+
};
|
|
90
|
+
const resOK = scheme.validate(objOK);
|
|
91
|
+
expect(resOK.ok).toBe(true);
|
|
92
|
+
expect(resOK.passed.includes(key)).toBe(true);
|
|
93
|
+
expect(resOK.byKeys[key]).toBe(true);
|
|
94
|
+
const resOK2 = scheme.validate(objOK2);
|
|
95
|
+
expect(resOK2.ok).toBe(true);
|
|
96
|
+
expect(resOK2.passed.includes(key)).toBe(true);
|
|
97
|
+
expect(resOK2.byKeys[key]).toBe(true);
|
|
98
|
+
const objBad = {
|
|
99
|
+
[key]: 'not array'
|
|
100
|
+
};
|
|
101
|
+
const resBad = scheme.validate(objBad);
|
|
102
|
+
expect(resBad.ok).toBe(false);
|
|
103
|
+
expect(resBad.failed.includes(key)).toBe(true);
|
|
104
|
+
expect(resBad.passed.includes(key)).toBe(false);
|
|
105
|
+
expect(resBad.byKeys[key]).toBe(false);
|
|
106
|
+
});
|
|
107
|
+
test('Тест null', () => {
|
|
108
|
+
const key = 'testKey';
|
|
109
|
+
const scheme = new Schema({
|
|
110
|
+
[key]: {
|
|
111
|
+
required: true,
|
|
112
|
+
type: null
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
const objOK = {
|
|
116
|
+
[key]: null
|
|
117
|
+
};
|
|
118
|
+
const resOK = scheme.validate(objOK);
|
|
119
|
+
expect(resOK.ok).toBe(true);
|
|
120
|
+
expect(resOK.passed.includes(key)).toBe(true);
|
|
121
|
+
expect(resOK.byKeys[key]).toBe(true);
|
|
122
|
+
const objBad = {
|
|
123
|
+
[key]: 'not null'
|
|
124
|
+
};
|
|
125
|
+
const resBad = scheme.validate(objBad);
|
|
126
|
+
expect(resBad.ok).toBe(false);
|
|
127
|
+
expect(resBad.failed.includes(key)).toBe(true);
|
|
128
|
+
expect(resBad.passed.includes(key)).toBe(false);
|
|
129
|
+
expect(resBad.byKeys[key]).toBe(false);
|
|
130
|
+
});
|
|
131
|
+
test('Тест Date', () => {
|
|
132
|
+
const key = 'testKey';
|
|
133
|
+
const scheme = new Schema({
|
|
134
|
+
[key]: {
|
|
135
|
+
required: true,
|
|
136
|
+
type: Date
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
const objOK = {
|
|
140
|
+
[key]: new Date()
|
|
141
|
+
};
|
|
142
|
+
const resOK = scheme.validate(objOK);
|
|
143
|
+
expect(resOK.ok).toBe(true);
|
|
144
|
+
expect(resOK.passed.includes(key)).toBe(true);
|
|
145
|
+
expect(resOK.byKeys[key]).toBe(true);
|
|
146
|
+
const objBad = {
|
|
147
|
+
[key]: 'not date'
|
|
148
|
+
};
|
|
149
|
+
const resBad = scheme.validate(objBad);
|
|
150
|
+
expect(resBad.ok).toBe(false);
|
|
151
|
+
expect(resBad.failed.includes(key)).toBe(true);
|
|
152
|
+
expect(resBad.passed.includes(key)).toBe(false);
|
|
153
|
+
expect(resBad.byKeys[key]).toBe(false);
|
|
154
|
+
});
|
|
155
|
+
test('Тест Object', () => {
|
|
156
|
+
const key = 'testKey';
|
|
157
|
+
const scheme = new Schema({
|
|
158
|
+
[key]: {
|
|
159
|
+
required: true,
|
|
160
|
+
type: Object
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
const objOK = {
|
|
164
|
+
[key]: { test: 1 }
|
|
165
|
+
};
|
|
166
|
+
const resOK = scheme.validate(objOK);
|
|
167
|
+
expect(resOK.ok).toBe(true);
|
|
168
|
+
expect(resOK.passed.includes(key)).toBe(true);
|
|
169
|
+
expect(resOK.byKeys[key]).toBe(true);
|
|
170
|
+
const objBad = {
|
|
171
|
+
[key]: 'not obj'
|
|
172
|
+
};
|
|
173
|
+
const resBad = scheme.validate(objBad);
|
|
174
|
+
expect(resBad.ok).toBe(false);
|
|
175
|
+
expect(resBad.failed.includes(key)).toBe(true);
|
|
176
|
+
expect(resBad.passed.includes(key)).toBe(false);
|
|
177
|
+
expect(resBad.byKeys[key]).toBe(false);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
describe("Проверка всех типов сразу", () => {
|
|
181
|
+
test('Тест всех типов с корректными данными', () => {
|
|
182
|
+
const schema = new Schema({
|
|
183
|
+
str: {
|
|
184
|
+
type: String,
|
|
185
|
+
},
|
|
186
|
+
obj: {
|
|
187
|
+
type: Object,
|
|
188
|
+
},
|
|
189
|
+
num: {
|
|
190
|
+
type: Number,
|
|
191
|
+
},
|
|
192
|
+
arr: {
|
|
193
|
+
type: Array,
|
|
194
|
+
},
|
|
195
|
+
null: {
|
|
196
|
+
type: null,
|
|
197
|
+
},
|
|
198
|
+
bool: {
|
|
199
|
+
type: Boolean,
|
|
200
|
+
},
|
|
201
|
+
date: {
|
|
202
|
+
type: Date
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
const obj = {
|
|
206
|
+
str: 'String',
|
|
207
|
+
obj: {},
|
|
208
|
+
num: 1,
|
|
209
|
+
arr: [1, 2, 3],
|
|
210
|
+
null: null,
|
|
211
|
+
bool: true,
|
|
212
|
+
date: new Date()
|
|
213
|
+
};
|
|
214
|
+
const res = schema.validate(obj);
|
|
215
|
+
const arePassed = [];
|
|
216
|
+
for (const key in obj) {
|
|
217
|
+
arePassed.push(res.byKeys[key]);
|
|
218
|
+
}
|
|
219
|
+
expect(res.ok).toBe(true);
|
|
220
|
+
expect(arePassed.every(el => el === true)).toBe(true);
|
|
221
|
+
expect(res.errors.length).toBe(0);
|
|
222
|
+
});
|
|
223
|
+
test('Тест всех типов с некорректными данными', () => {
|
|
224
|
+
const schema = new Schema({
|
|
225
|
+
str: {
|
|
226
|
+
type: String,
|
|
227
|
+
},
|
|
228
|
+
obj: {
|
|
229
|
+
type: Object,
|
|
230
|
+
},
|
|
231
|
+
num: {
|
|
232
|
+
type: Number,
|
|
233
|
+
},
|
|
234
|
+
arr: {
|
|
235
|
+
type: Array,
|
|
236
|
+
},
|
|
237
|
+
null: {
|
|
238
|
+
type: null,
|
|
239
|
+
},
|
|
240
|
+
bool: {
|
|
241
|
+
type: Boolean,
|
|
242
|
+
},
|
|
243
|
+
date: {
|
|
244
|
+
type: Date
|
|
245
|
+
},
|
|
246
|
+
test: {
|
|
247
|
+
type: Object,
|
|
248
|
+
required: true
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
const obj = {
|
|
252
|
+
str: 123,
|
|
253
|
+
obj: [],
|
|
254
|
+
num: 'str',
|
|
255
|
+
arr: null,
|
|
256
|
+
null: {},
|
|
257
|
+
bool: new Date(),
|
|
258
|
+
date: true,
|
|
259
|
+
test: undefined
|
|
260
|
+
};
|
|
261
|
+
const res = schema.validate(obj);
|
|
262
|
+
expect(res.ok).toBe(false);
|
|
263
|
+
const areFailed = [];
|
|
264
|
+
const hasErrorMsg = [];
|
|
265
|
+
for (const key in obj) {
|
|
266
|
+
areFailed.push(res.byKeys[key]);
|
|
267
|
+
hasErrorMsg.push(key in res.errorsByKeys);
|
|
268
|
+
}
|
|
269
|
+
expect(areFailed.every(el => el === false)).toBe(true);
|
|
270
|
+
expect(hasErrorMsg.every(el => el === true)).toBe(true);
|
|
271
|
+
expect(res.errors.length).toBe(Object.keys(obj).length);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_errors.test.d.ts","sourceRoot":"","sources":["../../../src/tests/utils/_errors.test.ts"],"names":[],"mappings":""}
|