validno 0.4.1 → 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/constants/details.d.ts +3 -2
- package/dist/constants/details.d.ts.map +1 -1
- package/dist/constants/details.js +2 -1
- package/dist/dev.js +9 -56
- package/dist/engine/methods/handleNestedKey.js +1 -1
- package/dist/engine/methods/validate.d.ts.map +1 -1
- package/dist/engine/methods/validate.js +7 -0
- package/dist/engine/methods/validateType.js +1 -1
- 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/dist/utils/helpers.d.ts.map +1 -1
- package/package.json +4 -2
- package/dist/ValidnoResult.js +0 -102
- package/dist/checkType.js +0 -1
- package/dist/validate/index.js +0 -1
- package/dist/validate/validate.js +0 -151
- package/dist/validate.js +0 -151
- package/dist/validateEngine/ValidateEngine.js +0 -44
- package/dist/validateEngine/index.js +0 -2
- package/dist/validateEngine/methods/ValidateEngine.js +0 -139
- package/dist/validateEngine/methods/checkRulesForKey.js +0 -15
- package/dist/validateEngine/methods/checkValueType.js +0 -134
- package/dist/validateEngine/methods/finalizeValidation.js +0 -15
- package/dist/validateEngine/methods/finishValidation.js +0 -15
- package/dist/validateEngine/methods/handleKey.js +0 -43
- package/dist/validateEngine/methods/handleMissingKey.js +0 -19
- package/dist/validateEngine/methods/handleMissingKeyValidation.js +0 -9
- package/dist/validateEngine/methods/handleNestedKey.js +0 -19
- package/dist/validateEngine/methods/validate.js +0 -14
- package/dist/validateEngine/methods/validateKey.js +0 -31
- package/dist/validateEngine/methods/validateKeyDetails.js +0 -13
- package/dist/validateEngine/methods/validateKeyValue.js +0 -13
- package/dist/validateEngine/methods/validateNestedKey.js +0 -19
- package/dist/validateEngine/methods/validateType.js +0 -134
- package/dist/validateEngine/validate.js +0 -14
- package/dist/validateRules.js +0 -159
- package/dist/validateSchema/ValidateEngine.js +0 -147
- package/dist/validateSchema/index.js +0 -6
- package/dist/validateSchema/validate.js +0 -151
- package/dist/validateSchema.js +0 -6
- package/dist/validateType.js +0 -124
|
@@ -5,7 +5,8 @@ export declare enum ValidationIds {
|
|
|
5
5
|
}
|
|
6
6
|
export declare enum ValidationDetails {
|
|
7
7
|
OK = "OK",
|
|
8
|
-
|
|
9
|
-
CustomRuleFailed = "Custom rule failed"
|
|
8
|
+
InvalidDate = "Invalid date",
|
|
9
|
+
CustomRuleFailed = "Custom rule failed",
|
|
10
|
+
BadInput = "Validation input must be a plain object"
|
|
10
11
|
}
|
|
11
12
|
//# sourceMappingURL=details.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"details.d.ts","sourceRoot":"","sources":["../../src/constants/details.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IACrB,OAAO,YAAY;IACnB,IAAI,SAAS;IACb,IAAI,SAAS;CAChB;AAED,oBAAY,iBAAiB;IACzB,EAAE,OAAO;IACT,
|
|
1
|
+
{"version":3,"file":"details.d.ts","sourceRoot":"","sources":["../../src/constants/details.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IACrB,OAAO,YAAY;IACnB,IAAI,SAAS;IACb,IAAI,SAAS;CAChB;AAED,oBAAY,iBAAiB;IACzB,EAAE,OAAO;IACT,WAAW,iBAAiB;IAC5B,gBAAgB,uBAAuB;IACvC,QAAQ,4CAA4C;CACvD"}
|
|
@@ -7,6 +7,7 @@ export var ValidationIds;
|
|
|
7
7
|
export var ValidationDetails;
|
|
8
8
|
(function (ValidationDetails) {
|
|
9
9
|
ValidationDetails["OK"] = "OK";
|
|
10
|
-
ValidationDetails["
|
|
10
|
+
ValidationDetails["InvalidDate"] = "Invalid date";
|
|
11
11
|
ValidationDetails["CustomRuleFailed"] = "Custom rule failed";
|
|
12
|
+
ValidationDetails["BadInput"] = "Validation input must be a plain object";
|
|
12
13
|
})(ValidationDetails || (ValidationDetails = {}));
|
package/dist/dev.js
CHANGED
|
@@ -1,57 +1,10 @@
|
|
|
1
|
-
import Schema from
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
cors: {
|
|
10
|
-
enabled: { type: Boolean, required: true },
|
|
11
|
-
origins: {
|
|
12
|
-
type: Array,
|
|
13
|
-
eachType: String,
|
|
14
|
-
required: false
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
rateLimit: {
|
|
18
|
-
enabled: { type: Boolean, required: true },
|
|
19
|
-
maxRequests: {
|
|
20
|
-
type: Number,
|
|
21
|
-
required: false,
|
|
22
|
-
rules: { min: 1, max: 10000 }
|
|
23
|
-
},
|
|
24
|
-
windowMs: {
|
|
25
|
-
type: Number,
|
|
26
|
-
required: false,
|
|
27
|
-
rules: { min: 1000, max: 86400000 }
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
const exampleConfigFile = {
|
|
33
|
-
"app": {
|
|
34
|
-
"name": "MyApp",
|
|
35
|
-
"version": "1.0.0",
|
|
36
|
-
"environment": "development"
|
|
37
|
-
},
|
|
38
|
-
"database": {
|
|
39
|
-
"host": "localhost",
|
|
40
|
-
"port": 5432,
|
|
41
|
-
"name": "myappdb",
|
|
42
|
-
"ssl": false
|
|
43
|
-
},
|
|
44
|
-
"api": {
|
|
45
|
-
"port": 3000,
|
|
46
|
-
"cors": {
|
|
47
|
-
"enabled": true,
|
|
48
|
-
"origins": ["http://localhost:3000"]
|
|
49
|
-
},
|
|
50
|
-
"rateLimit": {
|
|
51
|
-
"enabled": true,
|
|
52
|
-
"maxRequests": 1000,
|
|
53
|
-
"windowMs": 60000
|
|
54
|
-
}
|
|
55
|
-
},
|
|
1
|
+
import { Schema } from "./Schema.js";
|
|
2
|
+
const test = () => {
|
|
3
|
+
console.log('--- Custom Test ---');
|
|
4
|
+
const schema = new Schema({
|
|
5
|
+
anyKey: { type: String }
|
|
6
|
+
});
|
|
7
|
+
const result = schema.validate({ anyKey: null });
|
|
8
|
+
console.log(result);
|
|
56
9
|
};
|
|
57
|
-
|
|
10
|
+
test();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validate.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,MAAM,qBAAqB,CAAA;AAC/C,OAAO,cAAwC,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validate.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,MAAM,qBAAqB,CAAA;AAC/C,OAAO,cAAwC,MAAM,sBAAsB,CAAA;AAG3E,iBAAS,QAAQ,CACb,IAAI,EAAE,cAAc,EACpB,IAAI,EAAE,GAAG,EACT,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GACnC,aAAa,CAmBf;AAED,eAAe,QAAQ,CAAC"}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import _helpers from "../../utils/helpers.js";
|
|
2
|
+
import ValidnoResult from "../ValidnoResult.js";
|
|
3
|
+
import { ValidationDetails } from "../../constants/details.js";
|
|
2
4
|
function validate(data, validationKeys) {
|
|
5
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
6
|
+
const result = new ValidnoResult();
|
|
7
|
+
result.setFailed("", ValidationDetails.BadInput);
|
|
8
|
+
return result.finish();
|
|
9
|
+
}
|
|
3
10
|
const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
|
|
4
11
|
const schemaKeys = Object.entries(this.definition);
|
|
5
12
|
for (const [key, reqs] of schemaKeys) {
|
|
@@ -69,7 +69,7 @@ const handleTypeValidation = (key, value, requirements, keyName = key) => {
|
|
|
69
69
|
const isDate = isNotNull && value.constructor === Date;
|
|
70
70
|
const isValid = isDate && !isNaN(value.getTime());
|
|
71
71
|
const isValidDate = isDate && isValid;
|
|
72
|
-
result.push(_validateType.getResult(keyName, isValidDate, getDetails(isValidDate, ValidationDetails.
|
|
72
|
+
result.push(_validateType.getResult(keyName, isValidDate, getDetails(isValidDate, ValidationDetails.InvalidDate)));
|
|
73
73
|
break;
|
|
74
74
|
}
|
|
75
75
|
case Boolean: {
|
|
@@ -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":""}
|