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
package/dist/dev.js
CHANGED
|
@@ -1,79 +1,10 @@
|
|
|
1
1
|
import { Schema } from "./Schema.js";
|
|
2
2
|
const test = () => {
|
|
3
3
|
console.log('--- Custom Test ---');
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
Project: 'workspace',
|
|
7
|
-
User: 'user'
|
|
8
|
-
};
|
|
9
|
-
const collectionSettingsSchema = new Schema({
|
|
10
|
-
auth: { type: null },
|
|
11
|
-
scope: {
|
|
12
|
-
type: String,
|
|
13
|
-
rules: {
|
|
14
|
-
enum: Object.values(AccessScopes)
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
methods: {
|
|
18
|
-
get: {
|
|
19
|
-
isActive: { type: Boolean },
|
|
20
|
-
scope: {
|
|
21
|
-
type: String,
|
|
22
|
-
rules: { enum: Object.values(AccessScopes) },
|
|
23
|
-
required: false
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
getAll: {
|
|
27
|
-
isActive: { type: Boolean },
|
|
28
|
-
scope: {
|
|
29
|
-
type: String,
|
|
30
|
-
rules: {
|
|
31
|
-
enum: Object.values(AccessScopes)
|
|
32
|
-
},
|
|
33
|
-
required: false
|
|
34
|
-
},
|
|
35
|
-
sort: { default: { type: String } },
|
|
36
|
-
filter: { fields: { type: Array, eachType: String } },
|
|
37
|
-
search: { fields: { type: Array, eachType: String } }
|
|
38
|
-
},
|
|
39
|
-
create: {
|
|
40
|
-
isActive: { type: Boolean },
|
|
41
|
-
scope: {
|
|
42
|
-
type: String,
|
|
43
|
-
rules: { enum: Object.values(AccessScopes) },
|
|
44
|
-
required: false
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
update: {
|
|
48
|
-
isActive: { type: Boolean },
|
|
49
|
-
scope: {
|
|
50
|
-
type: String,
|
|
51
|
-
rules: { enum: Object.values(AccessScopes) },
|
|
52
|
-
required: false
|
|
53
|
-
},
|
|
54
|
-
allowedFields: { type: Array, eachType: String }
|
|
55
|
-
},
|
|
56
|
-
delete: {
|
|
57
|
-
isActive: { type: Boolean },
|
|
58
|
-
scope: {
|
|
59
|
-
type: String,
|
|
60
|
-
rules: { enum: Object.values(AccessScopes) },
|
|
61
|
-
required: false
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
distinct: {
|
|
65
|
-
isActive: { type: Boolean },
|
|
66
|
-
scope: {
|
|
67
|
-
type: String,
|
|
68
|
-
rules: { enum: Object.values(AccessScopes) },
|
|
69
|
-
required: false
|
|
70
|
-
},
|
|
71
|
-
fields: { type: Array, eachType: String }
|
|
72
|
-
}
|
|
73
|
-
}
|
|
4
|
+
const schema = new Schema({
|
|
5
|
+
anyKey: { type: String }
|
|
74
6
|
});
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
console.log(res);
|
|
7
|
+
const result = schema.validate({ anyKey: null });
|
|
8
|
+
console.log(result);
|
|
78
9
|
};
|
|
79
10
|
test();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"corruptedData.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/corruptedData.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../../Schema';
|
|
3
|
+
const AccessScopes = {
|
|
4
|
+
Public: 'public',
|
|
5
|
+
Project: 'workspace',
|
|
6
|
+
User: 'user'
|
|
7
|
+
};
|
|
8
|
+
const collectionSettingsSchema = new Schema({
|
|
9
|
+
auth: { type: null },
|
|
10
|
+
scope: {
|
|
11
|
+
type: String,
|
|
12
|
+
rules: {
|
|
13
|
+
enum: Object.values(AccessScopes)
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
methods: {
|
|
17
|
+
get: {
|
|
18
|
+
isActive: { type: Boolean },
|
|
19
|
+
scope: {
|
|
20
|
+
type: String,
|
|
21
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
22
|
+
required: false
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
getAll: {
|
|
26
|
+
isActive: { type: Boolean },
|
|
27
|
+
scope: {
|
|
28
|
+
type: String,
|
|
29
|
+
rules: {
|
|
30
|
+
enum: Object.values(AccessScopes)
|
|
31
|
+
},
|
|
32
|
+
required: false
|
|
33
|
+
},
|
|
34
|
+
sort: { default: { type: String } },
|
|
35
|
+
filter: { fields: { type: Array, eachType: String } },
|
|
36
|
+
search: { fields: { type: Array, eachType: String } }
|
|
37
|
+
},
|
|
38
|
+
create: {
|
|
39
|
+
isActive: { type: Boolean },
|
|
40
|
+
scope: {
|
|
41
|
+
type: String,
|
|
42
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
43
|
+
required: false
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
update: {
|
|
47
|
+
isActive: { type: Boolean },
|
|
48
|
+
scope: {
|
|
49
|
+
type: String,
|
|
50
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
51
|
+
required: false
|
|
52
|
+
},
|
|
53
|
+
allowedFields: { type: Array, eachType: String }
|
|
54
|
+
},
|
|
55
|
+
delete: {
|
|
56
|
+
isActive: { type: Boolean },
|
|
57
|
+
scope: {
|
|
58
|
+
type: String,
|
|
59
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
60
|
+
required: false
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
distinct: {
|
|
64
|
+
isActive: { type: Boolean },
|
|
65
|
+
scope: {
|
|
66
|
+
type: String,
|
|
67
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
68
|
+
required: false
|
|
69
|
+
},
|
|
70
|
+
fields: { type: Array, eachType: String }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
const settingsData = { test: 1 };
|
|
75
|
+
describe('Corrupted data in nested keys', () => {
|
|
76
|
+
test('Corrupted data in nested keys should not pass the validation', () => {
|
|
77
|
+
const result = collectionSettingsSchema.validate(settingsData);
|
|
78
|
+
expect(result.ok).toBe(false);
|
|
79
|
+
expect(result.errors.length).toBeGreaterThan(1);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataInputIsNotAnObject.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/dataInputIsNotAnObject.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../../Schema';
|
|
3
|
+
describe('Data input is not an object → returns failed validation result', () => {
|
|
4
|
+
const schema = new Schema({
|
|
5
|
+
name: {
|
|
6
|
+
type: String
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
test('null input fails validation but not throws', () => {
|
|
10
|
+
const result = schema.validate(null);
|
|
11
|
+
expect(result).toBeDefined();
|
|
12
|
+
expect(result.ok).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
test('array input fails validation but not throws', () => {
|
|
15
|
+
const result = schema.validate([]);
|
|
16
|
+
expect(result).toBeDefined();
|
|
17
|
+
expect(result.ok).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
test('undefined input fails validation but not throws', () => {
|
|
20
|
+
const result = schema.validate(undefined);
|
|
21
|
+
expect(result).toBeDefined();
|
|
22
|
+
expect(result.ok).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notRequiredNestedKey.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/notRequiredNestedKey.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../../Schema';
|
|
3
|
+
describe('Not required nested key', () => {
|
|
4
|
+
test('[1] Nested key that is not required should pass the validation', () => {
|
|
5
|
+
const schema = new Schema({
|
|
6
|
+
query: { type: Object },
|
|
7
|
+
data: { type: Object },
|
|
8
|
+
params: {
|
|
9
|
+
upsert: { type: Boolean, required: false },
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
const data = {
|
|
13
|
+
query: {},
|
|
14
|
+
data: {}
|
|
15
|
+
};
|
|
16
|
+
const result = schema.validate(data);
|
|
17
|
+
expect(result.ok).toBe(true);
|
|
18
|
+
expect(result.missed).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
test('[2] Nested key that is required should not pass the validation', () => {
|
|
21
|
+
const schema = new Schema({
|
|
22
|
+
query: { type: Object },
|
|
23
|
+
data: { type: Object },
|
|
24
|
+
params: {
|
|
25
|
+
upsert: { type: Boolean, required: true },
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
const data = {
|
|
29
|
+
query: {},
|
|
30
|
+
data: {}
|
|
31
|
+
};
|
|
32
|
+
const result = schema.validate(data);
|
|
33
|
+
expect(result.ok).toBe(false);
|
|
34
|
+
expect(result.missed).toEqual(['params.upsert']);
|
|
35
|
+
expect(result.failed).toEqual(['params.upsert', 'params']);
|
|
36
|
+
expect(result.passed).toEqual(['query', 'data']);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secondLevelDeepValidates.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/secondLevelDeepValidates.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../../Schema';
|
|
3
|
+
const configSchema = new Schema({
|
|
4
|
+
app: {
|
|
5
|
+
name: {
|
|
6
|
+
type: String,
|
|
7
|
+
required: true,
|
|
8
|
+
rules: { lengthMin: 1, lengthMax: 50 }
|
|
9
|
+
},
|
|
10
|
+
version: {
|
|
11
|
+
type: String,
|
|
12
|
+
required: true,
|
|
13
|
+
rules: {
|
|
14
|
+
regex: /^\d+\.\d+\.\d+$/
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
environment: {
|
|
18
|
+
type: String,
|
|
19
|
+
required: true,
|
|
20
|
+
rules: {
|
|
21
|
+
enum: ['development', 'testing', 'staging', 'production']
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
database: {
|
|
26
|
+
host: {
|
|
27
|
+
type: String,
|
|
28
|
+
required: true,
|
|
29
|
+
rules: { lengthMin: 1 }
|
|
30
|
+
},
|
|
31
|
+
port: {
|
|
32
|
+
type: Number,
|
|
33
|
+
required: true,
|
|
34
|
+
rules: { min: 1, max: 65535 }
|
|
35
|
+
},
|
|
36
|
+
name: {
|
|
37
|
+
type: String,
|
|
38
|
+
required: true,
|
|
39
|
+
rules: { lengthMin: 1, lengthMax: 64 }
|
|
40
|
+
},
|
|
41
|
+
ssl: {
|
|
42
|
+
type: Boolean,
|
|
43
|
+
required: false
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
api: {
|
|
47
|
+
port: {
|
|
48
|
+
type: Number,
|
|
49
|
+
required: true,
|
|
50
|
+
rules: { min: 1000, max: 9999 }
|
|
51
|
+
},
|
|
52
|
+
cors: {
|
|
53
|
+
enabled: { type: Boolean, required: true },
|
|
54
|
+
origins: {
|
|
55
|
+
type: Array,
|
|
56
|
+
eachType: String,
|
|
57
|
+
required: false
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
rateLimit: {
|
|
61
|
+
enabled: { type: Boolean, required: true },
|
|
62
|
+
maxRequests: {
|
|
63
|
+
type: Number,
|
|
64
|
+
required: false,
|
|
65
|
+
rules: { min: 1, max: 10000 }
|
|
66
|
+
},
|
|
67
|
+
windowMs: {
|
|
68
|
+
type: Number,
|
|
69
|
+
required: false,
|
|
70
|
+
rules: { min: 1000, max: 86400000 }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
const exampleConfigFile = {
|
|
76
|
+
"app": {
|
|
77
|
+
"name": "MyApp",
|
|
78
|
+
"version": "1.0.0",
|
|
79
|
+
"environment": "development"
|
|
80
|
+
},
|
|
81
|
+
"database": {
|
|
82
|
+
"host": "localhost",
|
|
83
|
+
"port": 5432,
|
|
84
|
+
"name": "myappdb",
|
|
85
|
+
"ssl": false
|
|
86
|
+
},
|
|
87
|
+
"api": {
|
|
88
|
+
"port": 3000,
|
|
89
|
+
"cors": {
|
|
90
|
+
"enabled": true,
|
|
91
|
+
"origins": ["http://localhost:3000"]
|
|
92
|
+
},
|
|
93
|
+
"rateLimit": {
|
|
94
|
+
"enabled": true,
|
|
95
|
+
"maxRequests": 1000,
|
|
96
|
+
"windowMs": 60000
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
describe('Second level deep validates as expected', () => {
|
|
101
|
+
test('should validate the api key', () => {
|
|
102
|
+
const res = configSchema.validate(exampleConfigFile);
|
|
103
|
+
expect(res.ok).toBe(true);
|
|
104
|
+
expect(res.failed).toEqual([]);
|
|
105
|
+
expect(res.missed).toEqual([]);
|
|
106
|
+
});
|
|
107
|
+
test('api key shouldn\'t validate with wrong port type', () => {
|
|
108
|
+
const res2 = configSchema.validate(Object.assign(Object.assign({}, exampleConfigFile), { api: Object.assign(Object.assign({}, exampleConfigFile.api), { port: '99999' }) }));
|
|
109
|
+
expect(res2.ok).toBe(false);
|
|
110
|
+
expect(res2.failed).toContain('api');
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customMessage.test.d.ts","sourceRoot":"","sources":["../../src/tests/customMessage.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { describe, expect, test } from '@jest/globals';
|
|
2
|
+
import { Schema } from '../Schema';
|
|
3
|
+
describe('Тестирование кастомного сообщения об ошибке', () => {
|
|
4
|
+
test('Кастомные сообщения корректно отображаются в результате валидации', () => {
|
|
5
|
+
const wrongTypeKey = 'wrongType';
|
|
6
|
+
const missingKey = 'missingKey';
|
|
7
|
+
const wrongRule1 = 'wrongRule1';
|
|
8
|
+
const wrongRule2 = 'wrongRule2';
|
|
9
|
+
const wrongRule3 = 'wrongRule3';
|
|
10
|
+
const allKeys = [
|
|
11
|
+
wrongTypeKey,
|
|
12
|
+
missingKey,
|
|
13
|
+
wrongRule1,
|
|
14
|
+
wrongRule2,
|
|
15
|
+
wrongRule3
|
|
16
|
+
];
|
|
17
|
+
const getCustomMsg = (key, variation = '') => key + ' custom' + variation;
|
|
18
|
+
const schema = new Schema({
|
|
19
|
+
[wrongTypeKey]: {
|
|
20
|
+
type: Number,
|
|
21
|
+
required: true,
|
|
22
|
+
title: 'Не по типу',
|
|
23
|
+
customMessage: (input) => {
|
|
24
|
+
return getCustomMsg(wrongTypeKey);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
[missingKey]: {
|
|
28
|
+
type: Date,
|
|
29
|
+
required: true,
|
|
30
|
+
title: 'Отсутствующий',
|
|
31
|
+
customMessage: (input) => {
|
|
32
|
+
return getCustomMsg(missingKey);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
[wrongRule1]: {
|
|
36
|
+
type: String,
|
|
37
|
+
required: true,
|
|
38
|
+
title: 'Wrong Rule #1',
|
|
39
|
+
rules: {
|
|
40
|
+
is: 'xxx'
|
|
41
|
+
},
|
|
42
|
+
customMessage: (input) => {
|
|
43
|
+
return getCustomMsg(wrongRule1);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
[wrongRule2]: {
|
|
47
|
+
type: String,
|
|
48
|
+
required: true,
|
|
49
|
+
title: 'Wrong Rule #2',
|
|
50
|
+
rules: {
|
|
51
|
+
lengthMin: 999
|
|
52
|
+
},
|
|
53
|
+
customMessage: (input) => {
|
|
54
|
+
return getCustomMsg(wrongRule2);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
[wrongRule3]: {
|
|
58
|
+
type: String,
|
|
59
|
+
required: true,
|
|
60
|
+
title: 'Wrong Rule #3',
|
|
61
|
+
rules: {
|
|
62
|
+
lengthMax: 1
|
|
63
|
+
},
|
|
64
|
+
customMessage: (input) => {
|
|
65
|
+
const { keyword, value, key, title, reqs, schema, rules } = input;
|
|
66
|
+
if (keyword === 'lengthMax') {
|
|
67
|
+
return getCustomMsg(wrongRule3);
|
|
68
|
+
}
|
|
69
|
+
return wrongRule3 + ' should reach here';
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const obj = {
|
|
74
|
+
[wrongTypeKey]: 'abc',
|
|
75
|
+
[wrongRule1]: 'def',
|
|
76
|
+
[wrongRule2]: 'ghi',
|
|
77
|
+
[wrongRule3]: 'jkl'
|
|
78
|
+
};
|
|
79
|
+
const result = schema.validate(obj);
|
|
80
|
+
expect(result).toEqual({
|
|
81
|
+
ok: false,
|
|
82
|
+
missed: [missingKey],
|
|
83
|
+
failed: [...allKeys],
|
|
84
|
+
passed: [],
|
|
85
|
+
errors: allKeys.map(el => getCustomMsg(el)),
|
|
86
|
+
byKeys: {
|
|
87
|
+
wrongType: false,
|
|
88
|
+
missingKey: false,
|
|
89
|
+
wrongRule1: false,
|
|
90
|
+
wrongRule2: false,
|
|
91
|
+
wrongRule3: false
|
|
92
|
+
},
|
|
93
|
+
errorsByKeys: {
|
|
94
|
+
wrongType: [getCustomMsg(allKeys[0])],
|
|
95
|
+
missingKey: [getCustomMsg(allKeys[1])],
|
|
96
|
+
wrongRule1: [getCustomMsg(allKeys[2])],
|
|
97
|
+
wrongRule2: [getCustomMsg(allKeys[3])],
|
|
98
|
+
wrongRule3: [getCustomMsg(allKeys[4])],
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customType.test.d.ts","sourceRoot":"","sources":["../../src/tests/customType.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"joinErrors.test.d.ts","sourceRoot":"","sources":["../../src/tests/joinErrors.test.ts"],"names":[],"mappings":""}
|