validno 0.2.4 → 0.2.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.
- package/dev.js +0 -0
- package/dist/Schema.js +9 -13
- package/dist/checkType.js +15 -11
- package/dist/constants/schema.js +10 -0
- package/dist/index.js +2 -0
- package/dist/validate.js +81 -59
- package/package.json +1 -1
package/dev.js
ADDED
|
File without changes
|
package/dist/Schema.js
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
+
import { ESchemaFields } from "./constants/schema.js";
|
|
1
2
|
import validate from "./validate.js";
|
|
2
|
-
export const defaultSchemaKeys =
|
|
3
|
-
"required",
|
|
4
|
-
"type",
|
|
5
|
-
"eachType",
|
|
6
|
-
"rules",
|
|
7
|
-
"title",
|
|
8
|
-
"customMessage",
|
|
9
|
-
"joinErrors"
|
|
10
|
-
];
|
|
3
|
+
export const defaultSchemaKeys = Object.values(ESchemaFields);
|
|
11
4
|
export class Schema {
|
|
12
|
-
constructor(
|
|
13
|
-
|
|
5
|
+
constructor(inputSchemaDefinition) {
|
|
6
|
+
if (!inputSchemaDefinition || typeof inputSchemaDefinition !== 'object') {
|
|
7
|
+
throw new Error("Invalid schema input");
|
|
8
|
+
}
|
|
9
|
+
this.schema = inputSchemaDefinition;
|
|
14
10
|
}
|
|
15
|
-
validate(
|
|
16
|
-
return validate.call(this,
|
|
11
|
+
validate(inputData, validationKeys) {
|
|
12
|
+
return validate.call(this, inputData, validationKeys);
|
|
17
13
|
}
|
|
18
14
|
}
|
package/dist/checkType.js
CHANGED
|
@@ -2,22 +2,26 @@ import { ErrorKeywords } from "./constants/details.js";
|
|
|
2
2
|
import _validations from "./utils/validations.js";
|
|
3
3
|
import _errors from "./utils/errors.js";
|
|
4
4
|
const checkTypeMultiple = (key, value, requirements, keyName = key) => {
|
|
5
|
-
const constructorNames = requirements.type
|
|
5
|
+
const constructorNames = Array.isArray(requirements.type)
|
|
6
|
+
? requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el))
|
|
7
|
+
: [];
|
|
6
8
|
const result = {
|
|
7
9
|
key: keyName,
|
|
8
10
|
passed: false,
|
|
9
11
|
details: _errors.getErrorDetails(keyName, constructorNames.join('/'), value)
|
|
10
12
|
};
|
|
11
13
|
let i = 0;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
if (Array.isArray(requirements.type)) {
|
|
15
|
+
while (i < requirements.type.length) {
|
|
16
|
+
const requirementsRe = Object.assign(Object.assign({}, requirements), { type: requirements.type[i] });
|
|
17
|
+
const check = checkType(key, value, requirementsRe);
|
|
18
|
+
if (check[0].passed === true) {
|
|
19
|
+
result.passed = true;
|
|
20
|
+
result.details = 'OK';
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
i++;
|
|
19
24
|
}
|
|
20
|
-
i++;
|
|
21
25
|
}
|
|
22
26
|
return result;
|
|
23
27
|
};
|
|
@@ -149,8 +153,8 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
149
153
|
});
|
|
150
154
|
break;
|
|
151
155
|
default:
|
|
152
|
-
const isInstanceOf = value instanceof typeBySchema;
|
|
153
|
-
const isConstructorSame = ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
|
|
156
|
+
const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
|
|
157
|
+
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);
|
|
154
158
|
const checked = isInstanceOf && isConstructorSame;
|
|
155
159
|
result.push({
|
|
156
160
|
key: keyName,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export var ESchemaFields;
|
|
2
|
+
(function (ESchemaFields) {
|
|
3
|
+
ESchemaFields["Required"] = "required";
|
|
4
|
+
ESchemaFields["Type"] = "type";
|
|
5
|
+
ESchemaFields["EachType"] = "eachType";
|
|
6
|
+
ESchemaFields["Rules"] = "rules";
|
|
7
|
+
ESchemaFields["Title"] = "title";
|
|
8
|
+
ESchemaFields["CustomMessage"] = "customMessage";
|
|
9
|
+
ESchemaFields["JoinErrors"] = "joinErrors";
|
|
10
|
+
})(ESchemaFields || (ESchemaFields = {}));
|
package/dist/index.js
CHANGED
package/dist/validate.js
CHANGED
|
@@ -4,111 +4,133 @@ import checkRules from "./checkRules.js";
|
|
|
4
4
|
import _helpers from "./utils/helpers.js";
|
|
5
5
|
import { ErrorKeywords } from "./constants/details.js";
|
|
6
6
|
import ValidnoResult from "./ValidnoResult.js";
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
if (!reqs.customMessage)
|
|
12
|
-
return _errors.getMissingError(
|
|
13
|
-
|
|
7
|
+
function handleMissingKey(schema, input) {
|
|
8
|
+
const { key, nestedKey, data, reqs } = input;
|
|
9
|
+
const messageKey = nestedKey || key;
|
|
10
|
+
const messageTitle = reqs.title || messageKey;
|
|
11
|
+
if (!reqs.customMessage) {
|
|
12
|
+
return _errors.getMissingError(messageKey);
|
|
13
|
+
}
|
|
14
|
+
const errorMessage = reqs.customMessage({
|
|
14
15
|
keyword: ErrorKeywords.Missing,
|
|
15
16
|
value: data[key],
|
|
16
|
-
key:
|
|
17
|
-
title:
|
|
18
|
-
reqs
|
|
19
|
-
schema
|
|
17
|
+
key: messageKey,
|
|
18
|
+
title: messageTitle,
|
|
19
|
+
reqs,
|
|
20
|
+
schema,
|
|
20
21
|
});
|
|
21
|
-
return
|
|
22
|
+
return errorMessage;
|
|
22
23
|
}
|
|
23
|
-
function
|
|
24
|
-
const { results, key,
|
|
25
|
-
const
|
|
24
|
+
function validateNestedKey(input) {
|
|
25
|
+
const { results, key, nestedKey, data, reqs } = input;
|
|
26
|
+
const nestedKeys = Object.keys(reqs);
|
|
26
27
|
const nestedResults = [];
|
|
27
|
-
|
|
28
|
-
while (i < nesctedKeys.length) {
|
|
29
|
-
const nestedKey = nesctedKeys[i];
|
|
28
|
+
for (const itemKey of nestedKeys) {
|
|
30
29
|
const deepParams = {
|
|
31
|
-
key:
|
|
30
|
+
key: itemKey,
|
|
32
31
|
data: data[key],
|
|
33
|
-
reqs: reqs[
|
|
34
|
-
|
|
32
|
+
reqs: reqs[itemKey],
|
|
33
|
+
nestedKey: `${nestedKey}.${itemKey}`
|
|
35
34
|
};
|
|
36
|
-
const deepResults =
|
|
35
|
+
const deepResults = validateKey.call(this, deepParams);
|
|
37
36
|
nestedResults.push(deepResults.ok);
|
|
38
37
|
results.merge(deepResults);
|
|
39
|
-
i++;
|
|
40
38
|
}
|
|
41
|
-
results.fixParentByChilds(
|
|
39
|
+
results.fixParentByChilds(nestedKey, nestedResults);
|
|
42
40
|
return results;
|
|
43
41
|
}
|
|
44
|
-
|
|
45
|
-
let { results, key,
|
|
42
|
+
function validateKey(input) {
|
|
43
|
+
let { results, key, nestedKey, data, reqs } = input;
|
|
46
44
|
if (!results)
|
|
47
45
|
results = new ValidnoResult();
|
|
48
|
-
if (!
|
|
49
|
-
|
|
50
|
-
const hasNested = _helpers.checkIsNested(reqs);
|
|
46
|
+
if (!nestedKey)
|
|
47
|
+
nestedKey = key;
|
|
51
48
|
const hasMissing = _helpers.hasMissing(input);
|
|
52
|
-
const missedCheck = [];
|
|
53
|
-
const typeChecked = [];
|
|
54
|
-
const rulesChecked = [];
|
|
55
49
|
if (_helpers.checkNestedIsMissing(reqs, data)) {
|
|
56
|
-
results
|
|
57
|
-
return results;
|
|
50
|
+
return handleMissingNestedKey(results, nestedKey);
|
|
58
51
|
}
|
|
59
|
-
if (
|
|
60
|
-
return
|
|
52
|
+
if (_helpers.checkIsNested(reqs)) {
|
|
53
|
+
return validateNestedKey.call(this, { results, key, data, reqs, nestedKey });
|
|
61
54
|
}
|
|
55
|
+
return validateKeyDetails.call(this, {
|
|
56
|
+
results,
|
|
57
|
+
key,
|
|
58
|
+
nestedKey,
|
|
59
|
+
data,
|
|
60
|
+
reqs,
|
|
61
|
+
hasMissing,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function handleMissingNestedKey(results, nestedKey) {
|
|
65
|
+
results.setMissing(nestedKey);
|
|
66
|
+
return results;
|
|
67
|
+
}
|
|
68
|
+
function validateKeyDetails(params) {
|
|
69
|
+
const { results, key, nestedKey, data, reqs, hasMissing } = params;
|
|
70
|
+
const missedCheck = [];
|
|
71
|
+
const typeChecked = [];
|
|
72
|
+
const rulesChecked = [];
|
|
62
73
|
if (hasMissing) {
|
|
63
|
-
|
|
64
|
-
missedCheck.push(false);
|
|
65
|
-
results.setMissing(deepKey, errMsg);
|
|
66
|
-
return results;
|
|
74
|
+
return handleMissingKeyValidation(this.schema, { results, key, nestedKey, data, reqs }, missedCheck);
|
|
67
75
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
|
|
77
|
+
checkRulesForKey.call(this, results, nestedKey, data[key], reqs, data, rulesChecked);
|
|
78
|
+
return finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked);
|
|
79
|
+
}
|
|
80
|
+
function handleMissingKeyValidation(schema, params, missedCheck) {
|
|
81
|
+
const { results, key, nestedKey, data, reqs } = params;
|
|
82
|
+
const errMsg = handleMissingKey(schema, { key, nestedKey, data, reqs });
|
|
83
|
+
missedCheck.push(false);
|
|
84
|
+
results.setMissing(nestedKey, errMsg);
|
|
85
|
+
return results;
|
|
86
|
+
}
|
|
87
|
+
function checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
|
|
88
|
+
const typeCheck = checkType(key, value, reqs, nestedKey);
|
|
89
|
+
typeCheck.forEach((res) => {
|
|
90
|
+
if (!res.passed) {
|
|
91
|
+
typeChecked.push(false);
|
|
72
92
|
results.errors.push(res.details);
|
|
73
93
|
}
|
|
74
94
|
});
|
|
75
|
-
|
|
95
|
+
}
|
|
96
|
+
function checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
|
|
97
|
+
const ruleCheck = checkRules.call(this, nestedKey, value, reqs, data);
|
|
76
98
|
if (!ruleCheck.ok) {
|
|
77
99
|
rulesChecked.push(false);
|
|
78
100
|
ruleCheck.details.forEach((el) => {
|
|
79
|
-
if (
|
|
80
|
-
results.errorsByKeys[
|
|
101
|
+
if (!(nestedKey in results.errorsByKeys))
|
|
102
|
+
results.errorsByKeys[nestedKey] = [];
|
|
81
103
|
results.errors.push(el);
|
|
82
|
-
results.errorsByKeys[
|
|
104
|
+
results.errorsByKeys[nestedKey] = ['1'];
|
|
83
105
|
});
|
|
84
106
|
}
|
|
107
|
+
}
|
|
108
|
+
function finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked) {
|
|
85
109
|
if (missedCheck.length)
|
|
86
|
-
results.setMissing(
|
|
87
|
-
const isPassed =
|
|
110
|
+
results.setMissing(nestedKey);
|
|
111
|
+
const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
|
|
88
112
|
if (!isPassed) {
|
|
89
|
-
results.setFailed(
|
|
90
|
-
results.errorsByKeys[
|
|
91
|
-
...results.errors
|
|
92
|
-
];
|
|
113
|
+
results.setFailed(nestedKey);
|
|
114
|
+
results.errorsByKeys[nestedKey] = [...results.errors];
|
|
93
115
|
}
|
|
94
116
|
else {
|
|
95
|
-
results.setPassed(
|
|
117
|
+
results.setPassed(nestedKey);
|
|
96
118
|
}
|
|
97
119
|
return results.finish();
|
|
98
120
|
}
|
|
99
|
-
function
|
|
121
|
+
function validateSchema(data, keysToCheck) {
|
|
100
122
|
const output = new ValidnoResult();
|
|
101
123
|
const hasKeysToCheck = _helpers.areKeysLimited(keysToCheck);
|
|
102
|
-
const schemaKeys = Object.entries(
|
|
124
|
+
const schemaKeys = Object.entries(this.schema);
|
|
103
125
|
for (const [key, reqs] of schemaKeys) {
|
|
104
126
|
const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, keysToCheck);
|
|
105
127
|
if (!toBeValidated)
|
|
106
128
|
continue;
|
|
107
|
-
const keyResult =
|
|
129
|
+
const keyResult = validateKey.call(this, { key, data, reqs });
|
|
108
130
|
output.merge(keyResult);
|
|
109
131
|
}
|
|
110
132
|
output.finish();
|
|
111
133
|
return output;
|
|
112
134
|
}
|
|
113
135
|
;
|
|
114
|
-
export default
|
|
136
|
+
export default validateSchema;
|