validno 0.1.7 → 0.1.9
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/Schema.js +3 -1
- package/dist/checkRules.js +12 -0
- package/dist/checkType.js +29 -14
- package/dist/constants/details.js +6 -0
- package/dist/validate.js +18 -6
- package/package.json +1 -1
- package/src/Schema.ts +8 -2
- package/src/checkRules.ts +15 -0
- package/src/checkType.ts +35 -14
- package/src/constants/details.ts +5 -0
- package/src/validate.ts +26 -9
package/dist/Schema.js
CHANGED
package/dist/checkRules.js
CHANGED
|
@@ -121,6 +121,7 @@ function checkRules(key, value, requirements, inputObj) {
|
|
|
121
121
|
return result;
|
|
122
122
|
}
|
|
123
123
|
const rules = requirements.rules;
|
|
124
|
+
const title = requirements.title || key;
|
|
124
125
|
const allResults = [];
|
|
125
126
|
const allRulesKeys = Object.keys(rules);
|
|
126
127
|
let i = 0;
|
|
@@ -133,6 +134,17 @@ function checkRules(key, value, requirements, inputObj) {
|
|
|
133
134
|
const func = rulesFunctions[ruleName];
|
|
134
135
|
const args = rules[ruleName];
|
|
135
136
|
const result = func(key, value, args, { schema: this.schema, input: inputObj });
|
|
137
|
+
if (requirements.customMessage && typeof requirements.customMessage === 'function') {
|
|
138
|
+
result.details = requirements.customMessage({
|
|
139
|
+
keyword: ruleName,
|
|
140
|
+
value: value,
|
|
141
|
+
key: key,
|
|
142
|
+
title: title,
|
|
143
|
+
reqs: requirements,
|
|
144
|
+
schema: this.schema,
|
|
145
|
+
rules: rules,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
136
148
|
allResults.push(result);
|
|
137
149
|
i++;
|
|
138
150
|
}
|
package/dist/checkType.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ErrorKeywords } from "./constants/details.js";
|
|
1
2
|
import _validations from "./utils/validations.js";
|
|
2
3
|
const getErrorDetails = (key, expectedType, receivedValue) => {
|
|
3
4
|
let receivedType = '';
|
|
@@ -32,7 +33,7 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
|
|
|
32
33
|
const check = checkType(key, value, requirementsRe);
|
|
33
34
|
if (check[0].passed === true) {
|
|
34
35
|
result.passed = true;
|
|
35
|
-
result.details = '
|
|
36
|
+
result.details = 'OK';
|
|
36
37
|
return result;
|
|
37
38
|
}
|
|
38
39
|
i++;
|
|
@@ -41,8 +42,10 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
|
|
|
41
42
|
};
|
|
42
43
|
const checkType = (key, value, requirements, keyName = key) => {
|
|
43
44
|
const isNotNull = value !== null;
|
|
45
|
+
const keyTitle = 'title' in requirements ? requirements.title : keyName;
|
|
46
|
+
const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
|
|
44
47
|
if (value === undefined && requirements.required) {
|
|
45
|
-
return [{ key: keyName, passed: false, details:
|
|
48
|
+
return [{ key: keyName, passed: false, details: `Значение "${keyName}" отсутствует` }];
|
|
46
49
|
}
|
|
47
50
|
let result = [];
|
|
48
51
|
if (Array.isArray(requirements.type)) {
|
|
@@ -52,16 +55,28 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
52
55
|
result.push({
|
|
53
56
|
key: keyName,
|
|
54
57
|
passed: true,
|
|
55
|
-
details: '
|
|
58
|
+
details: 'OK'
|
|
56
59
|
});
|
|
57
60
|
return result;
|
|
58
61
|
}
|
|
62
|
+
const customErrDetails = hasCustomMessage ?
|
|
63
|
+
requirements.customMessage({
|
|
64
|
+
keyword: ErrorKeywords.Type,
|
|
65
|
+
value: value,
|
|
66
|
+
key: keyName,
|
|
67
|
+
title: keyTitle,
|
|
68
|
+
reqs: requirements,
|
|
69
|
+
schema: null
|
|
70
|
+
}) :
|
|
71
|
+
null;
|
|
72
|
+
const baseErrDetails = getErrorDetails(keyName, requirements.type, value);
|
|
73
|
+
const getDetails = (isOK) => isOK ? 'OK' : customErrDetails || baseErrDetails;
|
|
59
74
|
switch (requirements.type) {
|
|
60
75
|
case 'any':
|
|
61
76
|
result.push({
|
|
62
77
|
key: keyName,
|
|
63
78
|
passed: true,
|
|
64
|
-
details: '
|
|
79
|
+
details: 'OK'
|
|
65
80
|
});
|
|
66
81
|
break;
|
|
67
82
|
case Number:
|
|
@@ -69,7 +84,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
69
84
|
result.push({
|
|
70
85
|
key: keyName,
|
|
71
86
|
passed: isNumber,
|
|
72
|
-
details: isNumber
|
|
87
|
+
details: getDetails(isNumber)
|
|
73
88
|
});
|
|
74
89
|
break;
|
|
75
90
|
case String:
|
|
@@ -77,17 +92,17 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
77
92
|
result.push({
|
|
78
93
|
key: keyName,
|
|
79
94
|
passed: isString,
|
|
80
|
-
details: isString
|
|
95
|
+
details: getDetails(isString)
|
|
81
96
|
});
|
|
82
97
|
break;
|
|
83
98
|
case Date:
|
|
84
99
|
const isDate = isNotNull && value.constructor === Date;
|
|
85
100
|
const isValid = isDate && !isNaN(value.getTime());
|
|
86
|
-
const errorMsg = isValid ?
|
|
101
|
+
const errorMsg = isValid ? getDetails(isDate) : 'Дата невалидна';
|
|
87
102
|
result.push({
|
|
88
103
|
key: keyName,
|
|
89
104
|
passed: isDate && isValid,
|
|
90
|
-
details: isDate && isValid ? '
|
|
105
|
+
details: isDate && isValid ? 'OK' : errorMsg
|
|
91
106
|
});
|
|
92
107
|
break;
|
|
93
108
|
case Boolean:
|
|
@@ -95,7 +110,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
95
110
|
result.push({
|
|
96
111
|
key: keyName,
|
|
97
112
|
passed: isBoolean,
|
|
98
|
-
details: isBoolean ? '
|
|
113
|
+
details: isBoolean ? 'OK' : getDetails(isBoolean)
|
|
99
114
|
});
|
|
100
115
|
break;
|
|
101
116
|
case Array:
|
|
@@ -104,7 +119,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
104
119
|
result.push({
|
|
105
120
|
key: keyName,
|
|
106
121
|
passed: false,
|
|
107
|
-
details:
|
|
122
|
+
details: getDetails(isArray)
|
|
108
123
|
});
|
|
109
124
|
break;
|
|
110
125
|
}
|
|
@@ -123,7 +138,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
123
138
|
result.push({
|
|
124
139
|
key: keyName,
|
|
125
140
|
passed: isOk,
|
|
126
|
-
details: isOk ? '
|
|
141
|
+
details: isOk ? 'OK' : !isEachChecked.passed ? isEachChecked.details : getDetails(isOk)
|
|
127
142
|
});
|
|
128
143
|
break;
|
|
129
144
|
case Object:
|
|
@@ -131,7 +146,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
131
146
|
result.push({
|
|
132
147
|
key: keyName,
|
|
133
148
|
passed: isObject,
|
|
134
|
-
details: isObject ? '
|
|
149
|
+
details: isObject ? 'OK' : getDetails(isObject)
|
|
135
150
|
});
|
|
136
151
|
break;
|
|
137
152
|
case RegExp:
|
|
@@ -139,7 +154,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
139
154
|
result.push({
|
|
140
155
|
key: keyName,
|
|
141
156
|
passed: isRegex,
|
|
142
|
-
details: isRegex ? '
|
|
157
|
+
details: isRegex ? 'OK' : getDetails(isRegex)
|
|
143
158
|
});
|
|
144
159
|
break;
|
|
145
160
|
case null:
|
|
@@ -147,7 +162,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
147
162
|
result.push({
|
|
148
163
|
key: keyName,
|
|
149
164
|
passed: isNull,
|
|
150
|
-
details: isNull ? '
|
|
165
|
+
details: isNull ? 'OK' : getDetails(isNull)
|
|
151
166
|
});
|
|
152
167
|
break;
|
|
153
168
|
default:
|
package/dist/validate.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import checkRules from "./checkRules.js";
|
|
2
2
|
import checkType from "./checkType.js";
|
|
3
|
-
import { defaultSchemaKeys } from "./Schema.js";
|
|
4
3
|
import _errors from "./utils/errors.js";
|
|
5
4
|
import _validations from "./utils/validations.js";
|
|
5
|
+
import { defaultSchemaKeys } from "./Schema.js";
|
|
6
|
+
import { ErrorKeywords } from "./constants/details.js";
|
|
6
7
|
export const getResultDefaults = () => {
|
|
7
8
|
return {
|
|
8
9
|
ok: null,
|
|
@@ -11,7 +12,7 @@ export const getResultDefaults = () => {
|
|
|
11
12
|
passed: [],
|
|
12
13
|
errors: [],
|
|
13
14
|
byKeys: {},
|
|
14
|
-
errorsByKeys: {}
|
|
15
|
+
errorsByKeys: {},
|
|
15
16
|
};
|
|
16
17
|
};
|
|
17
18
|
const checkIsNested = (obj) => {
|
|
@@ -38,6 +39,7 @@ export const mergeResults = (resultsOld, resultsNew) => {
|
|
|
38
39
|
export function handleReqKey(key, data, reqs, deepKey = key) {
|
|
39
40
|
let results = getResultDefaults();
|
|
40
41
|
const hasNested = checkIsNested(reqs);
|
|
42
|
+
const keyTitle = 'title' in reqs ? reqs.title : deepKey;
|
|
41
43
|
const missedCheck = [];
|
|
42
44
|
const typeChecked = [];
|
|
43
45
|
const rulesChecked = [];
|
|
@@ -61,13 +63,22 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
|
|
|
61
63
|
return results;
|
|
62
64
|
}
|
|
63
65
|
if (reqs.required === true && key in data === false || data === undefined) {
|
|
64
|
-
|
|
66
|
+
let errMsg = _errors.getMissingError(deepKey);
|
|
67
|
+
if (reqs.customMessage && typeof reqs.customMessage === 'function') {
|
|
68
|
+
errMsg = reqs.customMessage({
|
|
69
|
+
keyword: ErrorKeywords.Missing,
|
|
70
|
+
value: data[key],
|
|
71
|
+
key: deepKey,
|
|
72
|
+
title: keyTitle,
|
|
73
|
+
reqs: reqs,
|
|
74
|
+
schema: this.schema
|
|
75
|
+
});
|
|
76
|
+
}
|
|
65
77
|
missedCheck.push(false);
|
|
66
78
|
results.missed.push(deepKey);
|
|
67
79
|
results.failed.push(deepKey);
|
|
68
80
|
results.errors.push(errMsg);
|
|
69
81
|
results.byKeys[deepKey] = false;
|
|
70
|
-
results.errorsByKeys[deepKey] = [errMsg];
|
|
71
82
|
return results;
|
|
72
83
|
}
|
|
73
84
|
const typeCheck = checkType(key, data[key], reqs, deepKey);
|
|
@@ -101,14 +112,15 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
|
|
|
101
112
|
results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
|
|
102
113
|
return results;
|
|
103
114
|
}
|
|
104
|
-
const
|
|
115
|
+
const checkIfValidationIsNeeded = (key, hasLimits, onlyKeys) => {
|
|
105
116
|
return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
|
|
106
117
|
};
|
|
107
118
|
function validate(schema, data, onlyKeys) {
|
|
108
119
|
let results = getResultDefaults();
|
|
109
120
|
const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0);
|
|
110
121
|
for (const [key, reqs] of Object.entries(schema.schema)) {
|
|
111
|
-
|
|
122
|
+
const isValidationRequired = checkIfValidationIsNeeded(key, areKeysLimited, onlyKeys);
|
|
123
|
+
if (isValidationRequired) {
|
|
112
124
|
const keyResult = handleReqKey.call(this, key, data, reqs);
|
|
113
125
|
results = mergeResults(results, keyResult);
|
|
114
126
|
}
|
package/package.json
CHANGED
package/src/Schema.ts
CHANGED
|
@@ -5,6 +5,8 @@ export type TSchemaItem = {
|
|
|
5
5
|
type: any,
|
|
6
6
|
eachType?: any,
|
|
7
7
|
rules?: {},
|
|
8
|
+
title?: string,
|
|
9
|
+
customMessage?: Function
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
export type TSchemaInput = {
|
|
@@ -15,14 +17,18 @@ export const enum ESchemaFields {
|
|
|
15
17
|
Required = 'required',
|
|
16
18
|
Type = 'type',
|
|
17
19
|
EachType = 'eachType',
|
|
18
|
-
Rules = 'rules'
|
|
20
|
+
Rules = 'rules',
|
|
21
|
+
Title = 'title',
|
|
22
|
+
CustomMessage = 'customMessage'
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
export const defaultSchemaKeys = [
|
|
22
26
|
ESchemaFields.Required,
|
|
23
27
|
ESchemaFields.Type,
|
|
24
28
|
ESchemaFields.EachType,
|
|
25
|
-
ESchemaFields.Rules
|
|
29
|
+
ESchemaFields.Rules,
|
|
30
|
+
ESchemaFields.Title,
|
|
31
|
+
ESchemaFields.CustomMessage
|
|
26
32
|
]
|
|
27
33
|
|
|
28
34
|
export type TSchema = TSchemaInput
|
package/src/checkRules.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ErrorKeywords } from "./constants/details.js";
|
|
1
2
|
import { TSchema, TSchemaItem } from "./Schema.js";
|
|
2
3
|
import _validations from "./utils/validations.js"
|
|
3
4
|
|
|
@@ -144,6 +145,7 @@ function checkRules (this: any, key: string, value: any, requirements: TSchemaIt
|
|
|
144
145
|
}
|
|
145
146
|
|
|
146
147
|
const rules: TRule = requirements.rules
|
|
148
|
+
const title = requirements.title || key
|
|
147
149
|
|
|
148
150
|
const allResults = []
|
|
149
151
|
const allRulesKeys = Object.keys(rules)
|
|
@@ -162,6 +164,19 @@ function checkRules (this: any, key: string, value: any, requirements: TSchemaIt
|
|
|
162
164
|
const args = rules[ruleName]
|
|
163
165
|
|
|
164
166
|
const result = func(key, value, args, {schema: this.schema, input: inputObj})
|
|
167
|
+
|
|
168
|
+
if (requirements.customMessage && typeof requirements.customMessage === 'function') {
|
|
169
|
+
result.details = requirements.customMessage({
|
|
170
|
+
keyword: ruleName,
|
|
171
|
+
value: value,
|
|
172
|
+
key: key,
|
|
173
|
+
title: title,
|
|
174
|
+
reqs: requirements,
|
|
175
|
+
schema: this.schema,
|
|
176
|
+
rules: rules,
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
|
|
165
180
|
allResults.push(result)
|
|
166
181
|
|
|
167
182
|
i++;
|
package/src/checkType.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ErrorKeywords } from "./constants/details.js";
|
|
1
2
|
import { TSchemaInput, TSchemaItem } from "./Schema.js";
|
|
2
3
|
import _validations from "./utils/validations.js";
|
|
3
4
|
|
|
@@ -32,7 +33,7 @@ const checkTypeMultiple = (key: string, value: any, requirements: TSchemaItem |
|
|
|
32
33
|
|
|
33
34
|
if (check[0].passed === true) {
|
|
34
35
|
result.passed = true
|
|
35
|
-
result.details = '
|
|
36
|
+
result.details = 'OK'
|
|
36
37
|
return result
|
|
37
38
|
}
|
|
38
39
|
|
|
@@ -46,9 +47,11 @@ type TCheckTypeResult = {key: string, passed: boolean, details: string}
|
|
|
46
47
|
|
|
47
48
|
const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaInput, keyName = key): TCheckTypeResult[] => {
|
|
48
49
|
const isNotNull = value !== null
|
|
50
|
+
const keyTitle = 'title' in requirements ? requirements.title : keyName
|
|
51
|
+
const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function'
|
|
49
52
|
|
|
50
53
|
if (value === undefined && requirements.required) {
|
|
51
|
-
return [{key: keyName, passed: false, details:
|
|
54
|
+
return [{key: keyName, passed: false, details: `Значение "${keyName}" отсутствует`}]
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
let result: TCheckTypeResult[] = []
|
|
@@ -61,17 +64,34 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
61
64
|
result.push({
|
|
62
65
|
key: keyName,
|
|
63
66
|
passed: true,
|
|
64
|
-
details: '
|
|
67
|
+
details: 'OK'
|
|
65
68
|
})
|
|
66
69
|
return result
|
|
67
70
|
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
const customErrDetails = hasCustomMessage ?
|
|
74
|
+
//@ts-ignore
|
|
75
|
+
requirements.customMessage({
|
|
76
|
+
keyword: ErrorKeywords.Type,
|
|
77
|
+
value: value,
|
|
78
|
+
key: keyName,
|
|
79
|
+
title: keyTitle,
|
|
80
|
+
reqs: requirements,
|
|
81
|
+
schema: null
|
|
82
|
+
}) :
|
|
83
|
+
null;
|
|
84
|
+
|
|
85
|
+
const baseErrDetails = getErrorDetails(keyName, requirements.type, value)
|
|
68
86
|
|
|
87
|
+
const getDetails = (isOK: boolean) => isOK ? 'OK' : customErrDetails || baseErrDetails
|
|
88
|
+
|
|
69
89
|
switch (requirements.type) {
|
|
70
90
|
case 'any':
|
|
71
91
|
result.push({
|
|
72
92
|
key: keyName,
|
|
73
93
|
passed: true,
|
|
74
|
-
details: '
|
|
94
|
+
details: 'OK'
|
|
75
95
|
})
|
|
76
96
|
break;
|
|
77
97
|
case Number:
|
|
@@ -80,7 +100,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
80
100
|
result.push({
|
|
81
101
|
key: keyName,
|
|
82
102
|
passed: isNumber,
|
|
83
|
-
details: isNumber
|
|
103
|
+
details: getDetails(isNumber)
|
|
84
104
|
})
|
|
85
105
|
|
|
86
106
|
break;
|
|
@@ -90,18 +110,18 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
90
110
|
result.push({
|
|
91
111
|
key: keyName,
|
|
92
112
|
passed: isString,
|
|
93
|
-
details: isString
|
|
113
|
+
details: getDetails(isString)
|
|
94
114
|
})
|
|
95
115
|
break;
|
|
96
116
|
case Date:
|
|
97
117
|
const isDate = isNotNull && value.constructor === Date
|
|
98
118
|
const isValid = isDate && !isNaN(value.getTime())
|
|
99
|
-
const errorMsg = isValid ?
|
|
119
|
+
const errorMsg = isValid ? getDetails(isDate) : 'Дата невалидна'
|
|
100
120
|
|
|
101
121
|
result.push({
|
|
102
122
|
key: keyName,
|
|
103
123
|
passed: isDate && isValid,
|
|
104
|
-
details: isDate && isValid ? '
|
|
124
|
+
details: isDate && isValid ? 'OK' : errorMsg
|
|
105
125
|
})
|
|
106
126
|
break;
|
|
107
127
|
case Boolean:
|
|
@@ -110,7 +130,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
110
130
|
result.push({
|
|
111
131
|
key: keyName,
|
|
112
132
|
passed: isBoolean,
|
|
113
|
-
details: isBoolean ? '
|
|
133
|
+
details: isBoolean ? 'OK' : getDetails(isBoolean)
|
|
114
134
|
})
|
|
115
135
|
break;
|
|
116
136
|
case Array:
|
|
@@ -120,7 +140,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
120
140
|
result.push({
|
|
121
141
|
key: keyName,
|
|
122
142
|
passed: false,
|
|
123
|
-
details:
|
|
143
|
+
details: getDetails(isArray)
|
|
124
144
|
});
|
|
125
145
|
|
|
126
146
|
break;
|
|
@@ -146,16 +166,17 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
146
166
|
result.push({
|
|
147
167
|
key: keyName,
|
|
148
168
|
passed: isOk,
|
|
149
|
-
details: isOk ? '
|
|
169
|
+
details: isOk ? 'OK' : !isEachChecked.passed ? isEachChecked.details : getDetails(isOk)
|
|
150
170
|
})
|
|
151
171
|
|
|
152
172
|
break;
|
|
153
173
|
case Object:
|
|
154
174
|
const isObject = _validations.isObject(value) && value.constructor === Object
|
|
175
|
+
|
|
155
176
|
result.push({
|
|
156
177
|
key: keyName,
|
|
157
178
|
passed: isObject,
|
|
158
|
-
details: isObject ? '
|
|
179
|
+
details: isObject ? 'OK' : getDetails(isObject)
|
|
159
180
|
})
|
|
160
181
|
|
|
161
182
|
break;
|
|
@@ -164,7 +185,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
164
185
|
result.push({
|
|
165
186
|
key: keyName,
|
|
166
187
|
passed: isRegex,
|
|
167
|
-
details: isRegex ? '
|
|
188
|
+
details: isRegex ? 'OK' : getDetails(isRegex)
|
|
168
189
|
})
|
|
169
190
|
|
|
170
191
|
break;
|
|
@@ -174,7 +195,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
174
195
|
result.push({
|
|
175
196
|
key: keyName,
|
|
176
197
|
passed: isNull,
|
|
177
|
-
details: isNull ? '
|
|
198
|
+
details: isNull ? 'OK' : getDetails(isNull)
|
|
178
199
|
})
|
|
179
200
|
|
|
180
201
|
break;
|
package/src/validate.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import checkRules from "./checkRules.js";
|
|
2
2
|
import checkType from "./checkType.js";
|
|
3
|
-
import { defaultSchemaKeys, Schema, TSchemaInput } from "./Schema.js";
|
|
4
3
|
import _errors from "./utils/errors.js";
|
|
5
4
|
import _validations from "./utils/validations.js";
|
|
5
|
+
import { defaultSchemaKeys, Schema, TSchemaInput } from "./Schema.js";
|
|
6
|
+
import { ErrorKeywords } from "./constants/details.js";
|
|
6
7
|
|
|
7
8
|
export type TResult = {
|
|
8
9
|
ok: null | boolean,
|
|
@@ -10,8 +11,8 @@ export type TResult = {
|
|
|
10
11
|
failed: string[],
|
|
11
12
|
passed: string[],
|
|
12
13
|
errors: string[],
|
|
13
|
-
byKeys: {[key: string]: boolean}
|
|
14
|
-
errorsByKeys: {[key: string]: string[]}
|
|
14
|
+
byKeys: {[key: string]: boolean},
|
|
15
|
+
errorsByKeys: {[key: string]: string[]},
|
|
15
16
|
};
|
|
16
17
|
|
|
17
18
|
export const getResultDefaults = (): TResult => {
|
|
@@ -22,7 +23,7 @@ export const getResultDefaults = (): TResult => {
|
|
|
22
23
|
passed: [],
|
|
23
24
|
errors: [],
|
|
24
25
|
byKeys: {},
|
|
25
|
-
errorsByKeys: {}
|
|
26
|
+
errorsByKeys: {},
|
|
26
27
|
};
|
|
27
28
|
}
|
|
28
29
|
|
|
@@ -54,6 +55,7 @@ export const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
|
54
55
|
export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInput, deepKey = key) {
|
|
55
56
|
let results = getResultDefaults()
|
|
56
57
|
const hasNested = checkIsNested(reqs)
|
|
58
|
+
const keyTitle = 'title' in reqs ? reqs.title : deepKey
|
|
57
59
|
|
|
58
60
|
const missedCheck: boolean[] = [];
|
|
59
61
|
const typeChecked: boolean[] = [];
|
|
@@ -68,6 +70,7 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
|
|
|
68
70
|
results.missed.push(deepKey)
|
|
69
71
|
results.failed.push(deepKey)
|
|
70
72
|
results.byKeys[deepKey] = false
|
|
73
|
+
|
|
71
74
|
return results
|
|
72
75
|
}
|
|
73
76
|
|
|
@@ -100,14 +103,26 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
|
|
|
100
103
|
// Check missing keys
|
|
101
104
|
// @ts-ignore
|
|
102
105
|
if (reqs.required === true && key in data === false || data === undefined) {
|
|
103
|
-
|
|
106
|
+
let errMsg = _errors.getMissingError(deepKey)
|
|
107
|
+
|
|
108
|
+
if (reqs.customMessage && typeof reqs.customMessage === 'function') {
|
|
109
|
+
// @ts-ignore
|
|
110
|
+
errMsg = reqs.customMessage({
|
|
111
|
+
keyword: ErrorKeywords.Missing,
|
|
112
|
+
value: data[key],
|
|
113
|
+
key: deepKey,
|
|
114
|
+
title: keyTitle,
|
|
115
|
+
reqs: reqs,
|
|
116
|
+
schema: this.schema
|
|
117
|
+
})
|
|
118
|
+
}
|
|
104
119
|
|
|
105
120
|
missedCheck.push(false)
|
|
106
121
|
results.missed.push(deepKey)
|
|
107
122
|
results.failed.push(deepKey)
|
|
108
123
|
results.errors.push(errMsg)
|
|
109
124
|
results.byKeys[deepKey] = false
|
|
110
|
-
|
|
125
|
+
|
|
111
126
|
return results
|
|
112
127
|
}
|
|
113
128
|
|
|
@@ -129,8 +144,8 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
|
|
|
129
144
|
rulesChecked.push(false)
|
|
130
145
|
ruleCheck.details.forEach((el) => {
|
|
131
146
|
if (deepKey in results.errorsByKeys) results.errorsByKeys[deepKey] = []
|
|
132
|
-
results.errors.push(el)
|
|
133
147
|
|
|
148
|
+
results.errors.push(el)
|
|
134
149
|
results.errorsByKeys[deepKey] = ['1']
|
|
135
150
|
})
|
|
136
151
|
}
|
|
@@ -155,7 +170,7 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
|
|
|
155
170
|
return results
|
|
156
171
|
}
|
|
157
172
|
|
|
158
|
-
const
|
|
173
|
+
const checkIfValidationIsNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | string[]) => {
|
|
159
174
|
return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && onlyKeys?.includes(key))
|
|
160
175
|
}
|
|
161
176
|
|
|
@@ -164,7 +179,9 @@ function validate(schema: Schema, data: any, onlyKeys?: string | string[]): TRes
|
|
|
164
179
|
const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0)
|
|
165
180
|
|
|
166
181
|
for (const [key, reqs] of Object.entries(schema.schema)) {
|
|
167
|
-
|
|
182
|
+
const isValidationRequired = checkIfValidationIsNeeded(key, areKeysLimited, onlyKeys)
|
|
183
|
+
|
|
184
|
+
if (isValidationRequired) {
|
|
168
185
|
// @ts-ignore
|
|
169
186
|
const keyResult = handleReqKey.call(this, key, data, reqs)
|
|
170
187
|
|