validno 0.4.4 → 0.4.6
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.d.ts +6 -1
- package/dist/dev.d.ts.map +1 -1
- package/dist/dev.js +130 -5
- package/dist/engine/methods/validateRules.d.ts.map +1 -1
- package/dist/engine/methods/validateRules.js +6 -5
- package/dist/engine/methods/validateType.d.ts.map +1 -1
- package/dist/engine/methods/validateType.js +4 -0
- package/package.json +1 -1
- package/dist/tests/cases/corruptedData.test.d.ts +0 -2
- package/dist/tests/cases/corruptedData.test.d.ts.map +0 -1
- package/dist/tests/cases/corruptedData.test.js +0 -81
- package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts +0 -2
- package/dist/tests/cases/dataInputIsNotAnObject.test.d.ts.map +0 -1
- package/dist/tests/cases/dataInputIsNotAnObject.test.js +0 -24
- package/dist/tests/cases/notRequiredNestedKey.test.d.ts +0 -2
- package/dist/tests/cases/notRequiredNestedKey.test.d.ts.map +0 -1
- package/dist/tests/cases/notRequiredNestedKey.test.js +0 -38
- package/dist/tests/cases/secondLevelDeepValidates.test.d.ts +0 -2
- package/dist/tests/cases/secondLevelDeepValidates.test.d.ts.map +0 -1
- package/dist/tests/cases/secondLevelDeepValidates.test.js +0 -112
- package/dist/tests/customMessage.test.d.ts +0 -2
- package/dist/tests/customMessage.test.d.ts.map +0 -1
- package/dist/tests/customMessage.test.js +0 -102
- package/dist/tests/customType.test.d.ts +0 -2
- package/dist/tests/customType.test.d.ts.map +0 -1
- package/dist/tests/customType.test.js +0 -146
- package/dist/tests/joinErrors.test.d.ts +0 -2
- package/dist/tests/joinErrors.test.d.ts.map +0 -1
- package/dist/tests/joinErrors.test.js +0 -62
- package/dist/tests/missing.test.d.ts +0 -2
- package/dist/tests/missing.test.d.ts.map +0 -1
- package/dist/tests/missing.test.js +0 -224
- package/dist/tests/onlyKeys.test.d.ts +0 -2
- package/dist/tests/onlyKeys.test.d.ts.map +0 -1
- package/dist/tests/onlyKeys.test.js +0 -74
- package/dist/tests/types.test.d.ts +0 -2
- package/dist/tests/types.test.d.ts.map +0 -1
- package/dist/tests/types.test.js +0 -273
- package/dist/tests/utils/_errors.test.d.ts +0 -2
- package/dist/tests/utils/_errors.test.d.ts.map +0 -1
- package/dist/tests/utils/_errors.test.js +0 -47
- package/dist/tests/utils/_validations.test.d.ts +0 -2
- package/dist/tests/utils/_validations.test.d.ts.map +0 -1
- package/dist/tests/utils/_validations.test.js +0 -773
package/dist/dev.d.ts
CHANGED
package/dist/dev.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../src/dev.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../src/dev.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,YAAY;;;;;CAKf,CAAC"}
|
package/dist/dev.js
CHANGED
|
@@ -1,10 +1,135 @@
|
|
|
1
1
|
import { Schema } from "./Schema.js";
|
|
2
|
+
import isObjectId from "./utils/isObjectId.js";
|
|
3
|
+
import { ObjectId } from "mongodb";
|
|
4
|
+
export const AccessScopes = {
|
|
5
|
+
Inherit: 'inherit',
|
|
6
|
+
Public: 'public',
|
|
7
|
+
Project: 'workspace',
|
|
8
|
+
User: 'user'
|
|
9
|
+
};
|
|
10
|
+
const checkMethodScope = (value, context) => {
|
|
11
|
+
const authCollection = context.input.authCollection;
|
|
12
|
+
if (isObjectId(authCollection, ObjectId))
|
|
13
|
+
return true;
|
|
14
|
+
const mainScope = context.input.scope;
|
|
15
|
+
const methods = context.input.methods;
|
|
16
|
+
const allMethodsScopes = [
|
|
17
|
+
mainScope,
|
|
18
|
+
...Object.values(methods).filter(method => method.isActive).map(method => method.scope)
|
|
19
|
+
];
|
|
20
|
+
if (!allMethodsScopes.includes(AccessScopes.User) && !allMethodsScopes.includes(AccessScopes.Project)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
result: false,
|
|
25
|
+
details: 'Invalid scope. To use User or Project scope, the collection must have authCollection to be set.'
|
|
26
|
+
};
|
|
27
|
+
};
|
|
2
28
|
const test = () => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
29
|
+
const data = {
|
|
30
|
+
scope: 'public',
|
|
31
|
+
methods: {
|
|
32
|
+
get: { isActive: false, scope: 'public' },
|
|
33
|
+
getAll: {
|
|
34
|
+
isActive: true,
|
|
35
|
+
scope: 'public',
|
|
36
|
+
sort: [Object],
|
|
37
|
+
filter: [Object],
|
|
38
|
+
search: [Object]
|
|
39
|
+
},
|
|
40
|
+
create: { isActive: true, scope: 'public' },
|
|
41
|
+
update: { isActive: false, scope: 'public', allowedFields: [] },
|
|
42
|
+
delete: { isActive: false, scope: 'public' },
|
|
43
|
+
distinct: { isActive: false, scope: 'public', fields: [] }
|
|
44
|
+
},
|
|
45
|
+
authSettings: null,
|
|
46
|
+
authCollection: null
|
|
47
|
+
};
|
|
48
|
+
const collectionSettingsSchema = new Schema({
|
|
49
|
+
authCollection: {
|
|
50
|
+
type: [ObjectId, null],
|
|
51
|
+
required: false,
|
|
52
|
+
},
|
|
53
|
+
scope: {
|
|
54
|
+
type: String,
|
|
55
|
+
rules: {
|
|
56
|
+
enum: Object.values(AccessScopes).filter(scope => scope !== AccessScopes.Inherit),
|
|
57
|
+
custom: checkMethodScope
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
methods: {
|
|
61
|
+
get: {
|
|
62
|
+
isActive: { type: Boolean },
|
|
63
|
+
scope: {
|
|
64
|
+
type: String,
|
|
65
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
66
|
+
required: false
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
getAll: {
|
|
70
|
+
isActive: { type: Boolean },
|
|
71
|
+
scope: {
|
|
72
|
+
type: String,
|
|
73
|
+
rules: {
|
|
74
|
+
enum: Object.values(AccessScopes)
|
|
75
|
+
},
|
|
76
|
+
required: false
|
|
77
|
+
},
|
|
78
|
+
sort: { default: { type: String, required: false } },
|
|
79
|
+
filter: { fields: { type: Array, eachType: String, required: false } },
|
|
80
|
+
search: { fields: { type: Array, eachType: String, required: false } }
|
|
81
|
+
},
|
|
82
|
+
create: {
|
|
83
|
+
isActive: { type: Boolean },
|
|
84
|
+
scope: {
|
|
85
|
+
type: String,
|
|
86
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
87
|
+
required: false
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
update: {
|
|
91
|
+
isActive: { type: Boolean },
|
|
92
|
+
scope: {
|
|
93
|
+
type: String,
|
|
94
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
95
|
+
required: false
|
|
96
|
+
},
|
|
97
|
+
allowedFields: { type: Array, eachType: String, required: false }
|
|
98
|
+
},
|
|
99
|
+
delete: {
|
|
100
|
+
isActive: { type: Boolean },
|
|
101
|
+
scope: {
|
|
102
|
+
type: String,
|
|
103
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
104
|
+
required: false
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
distinct: {
|
|
108
|
+
isActive: { type: Boolean },
|
|
109
|
+
scope: {
|
|
110
|
+
type: String,
|
|
111
|
+
rules: { enum: Object.values(AccessScopes) },
|
|
112
|
+
required: false
|
|
113
|
+
},
|
|
114
|
+
fields: { type: Array, eachType: String }
|
|
115
|
+
}
|
|
116
|
+
}
|
|
6
117
|
});
|
|
7
|
-
const
|
|
8
|
-
console.log(
|
|
118
|
+
const validateResult = collectionSettingsSchema.validate(data);
|
|
119
|
+
console.log(validateResult);
|
|
9
120
|
};
|
|
10
121
|
test();
|
|
122
|
+
const schema = new Schema({
|
|
123
|
+
select: {
|
|
124
|
+
type: Array,
|
|
125
|
+
eachType: String,
|
|
126
|
+
required: true,
|
|
127
|
+
rules: {
|
|
128
|
+
lengthMin: 1
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
const result1 = schema.validate({ select: ['field1', 'field2'] });
|
|
133
|
+
console.log(result1);
|
|
134
|
+
const result2 = schema.validate({ select: [] });
|
|
135
|
+
console.log(result2);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateRules.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validateRules.ts"],"names":[],"mappings":"AAEA,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAGhD,OAAO,EAAe,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEtE,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,aAAa,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,gBAAgB,CAAC;IACvB,IAAI,EAAE,GAAG,CAAC;IACV,YAAY,EAAE,OAAO,EAAE,CAAA;CAC1B;AAED,oBAAY,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAItC,eAAO,MAAM,WAAW;;;;CAIvB,CAAA;
|
|
1
|
+
{"version":3,"file":"validateRules.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validateRules.ts"],"names":[],"mappings":"AAEA,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAGhD,OAAO,EAAe,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEtE,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,aAAa,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,gBAAgB,CAAC;IACvB,IAAI,EAAE,GAAG,CAAC;IACV,YAAY,EAAE,OAAO,EAAE,CAAA;CAC1B;AAED,oBAAY,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAItC,eAAO,MAAM,WAAW;;;;CAIvB,CAAA;AAmND,iBAAS,aAAa,CAAC,KAAK,EAAE,kBAAkB,QAe/C;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -70,33 +70,34 @@ const rulesFunctions = {
|
|
|
70
70
|
length: (key, val, length) => {
|
|
71
71
|
return {
|
|
72
72
|
result: _validations.lengthIs(val, length),
|
|
73
|
-
details: `Value must be equal to ${length}`
|
|
73
|
+
details: `Value length must be equal to ${length} ${Array.isArray(val) ? 'items' : 'characters'}`
|
|
74
74
|
};
|
|
75
75
|
},
|
|
76
76
|
lengthNot: (key, val, lengthNot) => {
|
|
77
77
|
return {
|
|
78
78
|
result: _validations.lengthNot(val, lengthNot),
|
|
79
|
-
details: `Value must not be equal to ${lengthNot}`
|
|
79
|
+
details: `Value must not be equal to ${lengthNot} ${Array.isArray(val) ? 'items' : 'characters'}`
|
|
80
80
|
};
|
|
81
81
|
},
|
|
82
82
|
lengthMinMax: (key, val, minMax) => {
|
|
83
83
|
const [min, max] = minMax;
|
|
84
84
|
return {
|
|
85
85
|
result: _validations.lengthMin(val, min) && _validations.lengthMax(val, max),
|
|
86
|
-
details: `Value must be between ${min} and ${max} characters`
|
|
86
|
+
details: `Value must be between ${min} and ${max} ${Array.isArray(val) ? 'items' : 'characters'}`
|
|
87
87
|
};
|
|
88
88
|
},
|
|
89
89
|
lengthMin: (key, val, min) => {
|
|
90
90
|
ensureRuleHasCorrectType(val, rulesParams['lengthMin'].allowedTypes);
|
|
91
91
|
return {
|
|
92
92
|
result: _validations.lengthMin(val, min),
|
|
93
|
-
details: `Value must
|
|
93
|
+
details: `Value must have at least ${min} ${Array.isArray(val) ? 'items' : 'characters'}`
|
|
94
94
|
};
|
|
95
95
|
},
|
|
96
96
|
lengthMax: (key, val, max) => {
|
|
97
|
+
ensureRuleHasCorrectType(val, rulesParams['lengthMin'].allowedTypes);
|
|
97
98
|
return {
|
|
98
99
|
result: _validations.lengthMax(val, max),
|
|
99
|
-
details: `Value must not
|
|
100
|
+
details: `Value must not exceed ${max} ${Array.isArray(val) ? 'items' : 'characters'}`
|
|
100
101
|
};
|
|
101
102
|
},
|
|
102
103
|
regex: (key, val, regex) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateType.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validateType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAe,MAAM,uBAAuB,CAAC;AACtE,OAAO,aAAa,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"validateType.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validateType.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAe,MAAM,uBAAuB,CAAC;AACtE,OAAO,aAAa,MAAM,qBAAqB,CAAC;AAqLhD,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,aAAa,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,GAAG,CAAC;IACX,IAAI,EAAE,gBAAgB,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,OAAO,EAAE,CAAA;CACzB;AAED,iBAAS,YAAY,CAAC,KAAK,EAAE,kBAAkB,QAU9C;AAED,eAAe,YAAY,CAAC"}
|
|
@@ -115,6 +115,10 @@ const handleTypeValidation = (key, value, requirements, keyName = key) => {
|
|
|
115
115
|
break;
|
|
116
116
|
}
|
|
117
117
|
default: {
|
|
118
|
+
if (value === null && typeBySchema !== null) {
|
|
119
|
+
result.push(_validateType.getResult(keyName, false, getDetails(false)));
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
118
122
|
const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
|
|
119
123
|
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);
|
|
120
124
|
const isBothObjectId = isObjectId(value, typeBySchema);
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"corruptedData.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/corruptedData.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,81 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dataInputIsNotAnObject.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/dataInputIsNotAnObject.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"notRequiredNestedKey.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/notRequiredNestedKey.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"secondLevelDeepValidates.test.d.ts","sourceRoot":"","sources":["../../../src/tests/cases/secondLevelDeepValidates.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,112 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"customMessage.test.d.ts","sourceRoot":"","sources":["../../src/tests/customMessage.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,102 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"customType.test.d.ts","sourceRoot":"","sources":["../../src/tests/customType.test.ts"],"names":[],"mappings":""}
|