validno 0.4.4 → 0.4.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.
Files changed (42) hide show
  1. package/dist/dev.d.ts +6 -1
  2. package/dist/dev.d.ts.map +1 -1
  3. package/dist/dev.js +116 -5
  4. package/dist/engine/methods/validateType.d.ts.map +1 -1
  5. package/dist/engine/methods/validateType.js +4 -0
  6. package/package.json +1 -1
  7. package/dist/tests/cases/corruptedData.test.d.ts +0 -2
  8. package/dist/tests/cases/corruptedData.test.d.ts.map +0 -1
  9. package/dist/tests/cases/corruptedData.test.js +0 -81
  10. package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts +0 -2
  11. package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts.map +0 -1
  12. package/dist/tests/cases/dataInputIsNotAnObject.test.js +0 -24
  13. package/dist/tests/cases/notRequiredNestedKey.test.d.ts +0 -2
  14. package/dist/tests/cases/notRequiredNestedKey.test.d.ts.map +0 -1
  15. package/dist/tests/cases/notRequiredNestedKey.test.js +0 -38
  16. package/dist/tests/cases/secondLevelDeepValidates.test.d.ts +0 -2
  17. package/dist/tests/cases/secondLevelDeepValidates.test.d.ts.map +0 -1
  18. package/dist/tests/cases/secondLevelDeepValidates.test.js +0 -112
  19. package/dist/tests/customMessage.test.d.ts +0 -2
  20. package/dist/tests/customMessage.test.d.ts.map +0 -1
  21. package/dist/tests/customMessage.test.js +0 -102
  22. package/dist/tests/customType.test.d.ts +0 -2
  23. package/dist/tests/customType.test.d.ts.map +0 -1
  24. package/dist/tests/customType.test.js +0 -146
  25. package/dist/tests/joinErrors.test.d.ts +0 -2
  26. package/dist/tests/joinErrors.test.d.ts.map +0 -1
  27. package/dist/tests/joinErrors.test.js +0 -62
  28. package/dist/tests/missing.test.d.ts +0 -2
  29. package/dist/tests/missing.test.d.ts.map +0 -1
  30. package/dist/tests/missing.test.js +0 -224
  31. package/dist/tests/onlyKeys.test.d.ts +0 -2
  32. package/dist/tests/onlyKeys.test.d.ts.map +0 -1
  33. package/dist/tests/onlyKeys.test.js +0 -74
  34. package/dist/tests/types.test.d.ts +0 -2
  35. package/dist/tests/types.test.d.ts.map +0 -1
  36. package/dist/tests/types.test.js +0 -273
  37. package/dist/tests/utils/_errors.test.d.ts +0 -2
  38. package/dist/tests/utils/_errors.test.d.ts.map +0 -1
  39. package/dist/tests/utils/_errors.test.js +0 -47
  40. package/dist/tests/utils/_validations.test.d.ts +0 -2
  41. package/dist/tests/utils/_validations.test.d.ts.map +0 -1
  42. package/dist/tests/utils/_validations.test.js +0 -773
@@ -1,146 +0,0 @@
1
- import { describe, expect, test } from '@jest/globals';
2
- import { Schema } from '../Schema';
3
- class Parent {
4
- constructor(value) {
5
- this.value = value;
6
- this.version = 1;
7
- }
8
- getV() {
9
- return this.version;
10
- }
11
- }
12
- class Child extends Parent {
13
- constructor(value) {
14
- super(value);
15
- this.isDeep = true;
16
- }
17
- }
18
- class CustomString extends String {
19
- constructor(value) {
20
- super(value);
21
- }
22
- isEmpty() {
23
- return this !== undefined && this.length === 0;
24
- }
25
- }
26
- describe('Тестирование кастомного типа', () => {
27
- test('Значения, созданные из кастомных типов, корректно проходят проверку', () => {
28
- const keys = ['cust1', 'cust2', 'cust3', 'badKey'];
29
- const err = new Error('ops');
30
- const parent = new Parent(1);
31
- const child = new Child(2);
32
- const child2 = new Child(3);
33
- const schema = new Schema({
34
- [keys[0]]: {
35
- type: Error,
36
- required: true
37
- },
38
- [keys[1]]: {
39
- type: Parent,
40
- required: true
41
- },
42
- [keys[2]]: {
43
- type: Child,
44
- required: true
45
- },
46
- });
47
- const obj = {
48
- [keys[0]]: err,
49
- [keys[1]]: parent,
50
- [keys[2]]: child,
51
- };
52
- const res = schema.validate(obj);
53
- const allKeysArePassed = [
54
- res.byKeys[keys[0]] === true,
55
- res.byKeys[keys[1]] === true,
56
- res.byKeys[keys[2]] === true,
57
- ].every(k => k === true);
58
- expect(res.ok).toBe(true);
59
- expect(allKeysArePassed).toBe(true);
60
- });
61
- test('Дочерний класс не проходит проверку по родительскому классу', () => {
62
- const keys = ['cust1', 'cust2', 'cust3'];
63
- const parent = new Parent(1);
64
- const child = new Child(2);
65
- const schema = new Schema({
66
- [keys[0]]: {
67
- type: Parent,
68
- required: true
69
- },
70
- [keys[1]]: {
71
- type: Child,
72
- required: true
73
- },
74
- [keys[2]]: {
75
- type: Parent,
76
- required: true
77
- },
78
- });
79
- const obj = {
80
- [keys[0]]: parent,
81
- [keys[1]]: child,
82
- [keys[2]]: child,
83
- };
84
- const res = schema.validate(obj);
85
- const allKeysArePassed = [
86
- res.byKeys[keys[0]] === true,
87
- res.byKeys[keys[1]] === true,
88
- res.byKeys[keys[2]] === false,
89
- ].every(k => k === true);
90
- expect(res.ok).toBe(false);
91
- expect(allKeysArePassed).toBe(true);
92
- expect(res.failed.includes(keys[2])).toBe(true);
93
- });
94
- test('Унаследованный класс от String не проходит проверку как String', () => {
95
- const keys = ['cust0', 'cust1', 'cust2', 'cust3', 'undef'];
96
- const origin = new String('str1');
97
- const origin2 = 'str2';
98
- const custom = new CustomString('str3');
99
- const schema = new Schema({
100
- [keys[0]]: {
101
- type: String,
102
- required: true,
103
- title: "test String to String"
104
- },
105
- [keys[1]]: {
106
- type: CustomString,
107
- required: true,
108
- title: "test CustomString to CustomString"
109
- },
110
- [keys[2]]: {
111
- type: CustomString,
112
- required: true,
113
- title: "test String to CustomString"
114
- },
115
- [keys[3]]: {
116
- type: String,
117
- required: true,
118
- title: "test CustomString to String"
119
- },
120
- [keys[4]]: {
121
- type: String,
122
- required: true,
123
- title: "test undefined to String"
124
- },
125
- });
126
- const obj = {
127
- [keys[0]]: origin,
128
- [keys[1]]: custom,
129
- [keys[2]]: origin2,
130
- [keys[3]]: custom,
131
- };
132
- const res = schema.validate(obj);
133
- const allKeysArePassed = [
134
- res.byKeys[keys[0]] === true,
135
- res.byKeys[keys[1]] === true,
136
- res.byKeys[keys[2]] === false,
137
- res.byKeys[keys[3]] === false,
138
- res.byKeys[keys[4]] === false,
139
- ].every(k => k === true);
140
- expect(res.ok).toBe(false);
141
- expect(allKeysArePassed).toBe(true);
142
- expect(res.failed.includes(keys[2])).toBe(true);
143
- expect(res.failed.includes(keys[3])).toBe(true);
144
- expect(res.failed.includes(keys[4])).toBe(true);
145
- });
146
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=joinErrors.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"joinErrors.test.d.ts","sourceRoot":"","sources":["../../src/tests/joinErrors.test.ts"],"names":[],"mappings":""}
@@ -1,62 +0,0 @@
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
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=missing.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"missing.test.d.ts","sourceRoot":"","sources":["../../src/tests/missing.test.ts"],"names":[],"mappings":""}
@@ -1,224 +0,0 @@
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
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=onlyKeys.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"onlyKeys.test.d.ts","sourceRoot":"","sources":["../../src/tests/onlyKeys.test.ts"],"names":[],"mappings":""}
@@ -1,74 +0,0 @@
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
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.test.d.ts","sourceRoot":"","sources":["../../src/tests/types.test.ts"],"names":[],"mappings":""}