validno 0.2.5 → 0.3.0
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 +6 -5
- package/dist/ValidnoResult.js +19 -7
- package/dist/checkType.js +1 -167
- package/dist/constants/details.js +11 -6
- package/dist/constants/schema.js +10 -10
- package/dist/dev.js +65 -46
- package/dist/engine/ValidateEngine.js +44 -0
- package/dist/engine/ValidnoResult.js +102 -0
- package/dist/engine/index.js +2 -0
- package/dist/engine/methods/finishValidation.js +15 -0
- package/dist/engine/methods/handleKey.js +41 -0
- package/dist/engine/methods/handleMissingKey.js +19 -0
- package/dist/engine/methods/handleMissingKeyValidation.js +9 -0
- package/dist/engine/methods/handleNestedKey.js +19 -0
- package/dist/engine/methods/validate.js +14 -0
- package/dist/engine/methods/validateRules.js +172 -0
- package/dist/engine/methods/validateType.js +134 -0
- package/dist/types/common.js +1 -0
- package/dist/utils/errors.js +30 -21
- package/dist/utils/helpers.js +55 -57
- package/dist/utils/validateType.js +12 -0
- package/dist/utils/validations.js +157 -153
- package/dist/validate/validate.js +151 -0
- package/dist/validate.js +136 -121
- package/dist/validateEngine/ValidateEngine.js +44 -0
- package/dist/validateEngine/index.js +2 -0
- package/dist/validateEngine/methods/ValidateEngine.js +139 -0
- package/dist/validateEngine/methods/checkRulesForKey.js +15 -0
- package/dist/validateEngine/methods/checkValueType.js +134 -0
- package/dist/validateEngine/methods/finalizeValidation.js +15 -0
- package/dist/validateEngine/methods/finishValidation.js +15 -0
- package/dist/validateEngine/methods/handleKey.js +43 -0
- package/dist/validateEngine/methods/handleMissingKey.js +19 -0
- package/dist/validateEngine/methods/handleMissingKeyValidation.js +9 -0
- package/dist/validateEngine/methods/handleNestedKey.js +19 -0
- package/dist/validateEngine/methods/validate.js +14 -0
- package/dist/validateEngine/methods/validateKey.js +31 -0
- package/dist/validateEngine/methods/validateKeyDetails.js +13 -0
- package/dist/validateEngine/methods/validateKeyValue.js +13 -0
- package/dist/validateEngine/methods/validateNestedKey.js +19 -0
- package/dist/validateEngine/methods/validateType.js +134 -0
- package/dist/validateEngine/validate.js +14 -0
- package/dist/{checkRules.js → validateRules.js} +2 -2
- package/dist/validateSchema/ValidateEngine.js +147 -0
- package/dist/validateSchema/index.js +6 -0
- package/dist/validateSchema/validate.js +151 -0
- package/dist/validateSchema.js +6 -0
- package/dist/validateType.js +124 -0
- package/package.json +1 -1
- package/dev.js +0 -0
- /package/dist/{utils/nested.js → validate/index.js} +0 -0
|
@@ -1,154 +1,158 @@
|
|
|
1
1
|
import _helpers from "./helpers.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
2
|
+
class ValidationUtility {
|
|
3
|
+
isString(value) {
|
|
4
|
+
return typeof value === 'string';
|
|
5
|
+
}
|
|
6
|
+
isNumber(value) {
|
|
7
|
+
return typeof value === 'number';
|
|
8
|
+
}
|
|
9
|
+
isArray(value) {
|
|
10
|
+
return Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
isObject(value) {
|
|
13
|
+
let _a;
|
|
14
|
+
return value !== null &&
|
|
15
|
+
typeof value === 'object' &&
|
|
16
|
+
((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Object' &&
|
|
17
|
+
!Array.isArray(value);
|
|
18
|
+
}
|
|
19
|
+
isDate(value) {
|
|
20
|
+
return value instanceof Date && String(value) !== 'Invalid Date';
|
|
21
|
+
}
|
|
22
|
+
isRegex(value) {
|
|
23
|
+
return value instanceof RegExp;
|
|
24
|
+
}
|
|
25
|
+
isBoolean(value) {
|
|
26
|
+
return typeof value === 'boolean';
|
|
27
|
+
}
|
|
28
|
+
isNull(value) {
|
|
29
|
+
return value === null;
|
|
30
|
+
}
|
|
31
|
+
isUndefined(value) {
|
|
32
|
+
return value === undefined;
|
|
33
|
+
}
|
|
34
|
+
isNullOrUndefined(value) {
|
|
35
|
+
return value === undefined || value === null;
|
|
36
|
+
}
|
|
37
|
+
isEmail(value) {
|
|
38
|
+
const emailRegex = /^(?!.*\.\.)[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
39
|
+
return emailRegex.test(value);
|
|
40
|
+
}
|
|
41
|
+
isDateYYYYMMDD(value) {
|
|
42
|
+
const regex = /^\d{4}-\d{2}-\d{2}$/;
|
|
43
|
+
return regex.test(value);
|
|
44
|
+
}
|
|
45
|
+
isHex(value) {
|
|
46
|
+
const regex = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
|
|
47
|
+
return regex.test(value);
|
|
48
|
+
}
|
|
49
|
+
lengthIs(value, length) {
|
|
50
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
51
|
+
return false;
|
|
52
|
+
if (typeof length !== 'number')
|
|
53
|
+
return false;
|
|
54
|
+
return value.length === length;
|
|
55
|
+
}
|
|
56
|
+
lengthNot(value, length) {
|
|
57
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
58
|
+
return false;
|
|
59
|
+
if (typeof length !== 'number')
|
|
60
|
+
return false;
|
|
61
|
+
return value.length !== length;
|
|
62
|
+
}
|
|
63
|
+
lengthMin(value, min) {
|
|
64
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
65
|
+
return false;
|
|
66
|
+
if (typeof min !== 'number')
|
|
67
|
+
return false;
|
|
68
|
+
return value.length >= min;
|
|
69
|
+
}
|
|
70
|
+
lengthMax(value, max) {
|
|
71
|
+
if (typeof value !== 'string' && !Array.isArray(value))
|
|
72
|
+
return false;
|
|
73
|
+
if (typeof max !== 'number')
|
|
74
|
+
return false;
|
|
75
|
+
return value.length <= max;
|
|
76
|
+
}
|
|
77
|
+
isNumberGte(value, gte) {
|
|
78
|
+
return typeof value === 'number' && value >= gte;
|
|
79
|
+
}
|
|
80
|
+
isNumberGt(value, gt) {
|
|
81
|
+
return typeof value === 'number' && value > gt;
|
|
82
|
+
}
|
|
83
|
+
isNumberLte(value, lte) {
|
|
84
|
+
return typeof value === 'number' && value <= lte;
|
|
85
|
+
}
|
|
86
|
+
isNumberLt(value, lt) {
|
|
87
|
+
return typeof value === 'number' && value < lt;
|
|
88
|
+
}
|
|
89
|
+
isDateGte(date1, date2) {
|
|
90
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return date1 >= date2;
|
|
94
|
+
}
|
|
95
|
+
isDateGt(date1, date2) {
|
|
96
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return date1 > date2;
|
|
100
|
+
}
|
|
101
|
+
isDateLte(date1, date2) {
|
|
102
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return date1 <= date2;
|
|
106
|
+
}
|
|
107
|
+
isDateLt(date1, date2) {
|
|
108
|
+
if (!this.isDate(date1) || !this.isDate(date2)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return date1 < date2;
|
|
112
|
+
}
|
|
113
|
+
hasKey(obj, key) {
|
|
114
|
+
if (!this.isObject(obj))
|
|
115
|
+
return false;
|
|
116
|
+
return key in obj;
|
|
117
|
+
}
|
|
118
|
+
is(value, compareTo) {
|
|
119
|
+
if (value === compareTo) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
const bothArrays = this.isArray(value) && this.isArray(compareTo);
|
|
123
|
+
if (bothArrays) {
|
|
124
|
+
return _helpers.compareArrs(value, compareTo);
|
|
125
|
+
}
|
|
126
|
+
const bothObjects = this.isObject(value) && this.isObject(compareTo);
|
|
127
|
+
if (bothObjects) {
|
|
128
|
+
return _helpers.compareObjs(value, compareTo);
|
|
129
|
+
}
|
|
130
|
+
const bothDates = this.isDate(value) && this.isDate(compareTo);
|
|
131
|
+
if (bothDates) {
|
|
132
|
+
return value.getTime() === compareTo.getTime();
|
|
133
|
+
}
|
|
134
|
+
return value === compareTo;
|
|
135
|
+
}
|
|
136
|
+
not(value, not) {
|
|
137
|
+
return !this.is(value, not);
|
|
138
|
+
}
|
|
139
|
+
regexTested(value, regexp) {
|
|
140
|
+
if (!regexp || !(regexp instanceof RegExp)) {
|
|
141
|
+
throw new Error('regexp argument is incorrect');
|
|
142
|
+
}
|
|
143
|
+
return regexp.test(value);
|
|
144
|
+
}
|
|
145
|
+
get gt() { return this.isNumberGt; }
|
|
146
|
+
get gte() { return this.isNumberGte; }
|
|
147
|
+
get lte() { return this.isNumberLte; }
|
|
148
|
+
get lt() { return this.isNumberLt; }
|
|
149
|
+
get eq() { return this.is; }
|
|
150
|
+
get isNot() { return this.not; }
|
|
151
|
+
get ne() { return this.not; }
|
|
152
|
+
get neq() { return this.not; }
|
|
153
|
+
get regex() { return this.regexTested; }
|
|
154
|
+
get regexp() { return this.regexTested; }
|
|
155
|
+
get test() { return this.regexTested; }
|
|
156
|
+
}
|
|
157
|
+
const validations = new ValidationUtility();
|
|
158
|
+
export default validations;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import validateType from "../validateType.js";
|
|
2
|
+
import validateRules from "../validateRules.js";
|
|
3
|
+
import { ValidationIds } from "../constants/details.js";
|
|
4
|
+
import ValidnoResult from "../ValidnoResult.js";
|
|
5
|
+
import _errors from "../utils/errors.js";
|
|
6
|
+
import _helpers from "../utils/helpers.js";
|
|
7
|
+
class ValidateEngine {
|
|
8
|
+
constructor(definition) {
|
|
9
|
+
this.definition = definition;
|
|
10
|
+
this.result = new ValidnoResult();
|
|
11
|
+
}
|
|
12
|
+
validate(data, validationKeys) {
|
|
13
|
+
const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
|
|
14
|
+
const schemaKeys = Object.entries(this.definition);
|
|
15
|
+
for (const [key, reqs] of schemaKeys) {
|
|
16
|
+
const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, validationKeys);
|
|
17
|
+
if (!toBeValidated)
|
|
18
|
+
continue;
|
|
19
|
+
const keyResult = this.validateKey({ key, data, reqs });
|
|
20
|
+
this.result.merge(keyResult);
|
|
21
|
+
}
|
|
22
|
+
return this.result.finish();
|
|
23
|
+
}
|
|
24
|
+
handleMissingKey(schema, input) {
|
|
25
|
+
const { key, nestedKey, data, reqs } = input;
|
|
26
|
+
const messageKey = nestedKey || key;
|
|
27
|
+
const messageTitle = reqs.title || messageKey;
|
|
28
|
+
if (!reqs.customMessage) {
|
|
29
|
+
return _errors.getMissingError(messageKey);
|
|
30
|
+
}
|
|
31
|
+
const errorMessage = reqs.customMessage({
|
|
32
|
+
keyword: ValidationIds.Missing,
|
|
33
|
+
value: data[key],
|
|
34
|
+
key: messageKey,
|
|
35
|
+
title: messageTitle,
|
|
36
|
+
reqs,
|
|
37
|
+
schema,
|
|
38
|
+
});
|
|
39
|
+
return errorMessage;
|
|
40
|
+
}
|
|
41
|
+
validateNestedKey(input) {
|
|
42
|
+
const { results, key, nestedKey, data, reqs } = input;
|
|
43
|
+
const nestedKeys = Object.keys(reqs);
|
|
44
|
+
const nestedResults = [];
|
|
45
|
+
for (const itemKey of nestedKeys) {
|
|
46
|
+
const deepParams = {
|
|
47
|
+
key: itemKey,
|
|
48
|
+
data: data[key],
|
|
49
|
+
reqs: reqs[itemKey],
|
|
50
|
+
nestedKey: `${nestedKey}.${itemKey}`
|
|
51
|
+
};
|
|
52
|
+
const deepResults = this.validateKey(deepParams);
|
|
53
|
+
nestedResults.push(deepResults.ok);
|
|
54
|
+
results.merge(deepResults);
|
|
55
|
+
}
|
|
56
|
+
results.fixParentByChilds(nestedKey, nestedResults);
|
|
57
|
+
return results;
|
|
58
|
+
}
|
|
59
|
+
validateKey(input) {
|
|
60
|
+
let { results, key, nestedKey, data, reqs } = input;
|
|
61
|
+
if (data === undefined) {
|
|
62
|
+
const noDataResult = new ValidnoResult();
|
|
63
|
+
noDataResult.setNoData();
|
|
64
|
+
noDataResult.finish();
|
|
65
|
+
return noDataResult;
|
|
66
|
+
}
|
|
67
|
+
if (!results)
|
|
68
|
+
results = new ValidnoResult();
|
|
69
|
+
if (!nestedKey)
|
|
70
|
+
nestedKey = key;
|
|
71
|
+
const hasMissing = _helpers.hasMissing(input);
|
|
72
|
+
if (_helpers.checkNestedIsMissing(reqs, data)) {
|
|
73
|
+
return this.handleMissingNestedKey(results, nestedKey);
|
|
74
|
+
}
|
|
75
|
+
if (_helpers.checkIsNested(reqs)) {
|
|
76
|
+
return this.validateNestedKey({ results, key, data, reqs, nestedKey });
|
|
77
|
+
}
|
|
78
|
+
return this.validateKeyDetails({
|
|
79
|
+
results,
|
|
80
|
+
key,
|
|
81
|
+
nestedKey,
|
|
82
|
+
data,
|
|
83
|
+
reqs,
|
|
84
|
+
hasMissing,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
handleMissingNestedKey(results, nestedKey) {
|
|
88
|
+
results.setMissing(nestedKey);
|
|
89
|
+
return results;
|
|
90
|
+
}
|
|
91
|
+
validateKeyDetails(params) {
|
|
92
|
+
const { results, key, nestedKey, data, reqs, hasMissing } = params;
|
|
93
|
+
const missedCheck = [];
|
|
94
|
+
const typeChecked = [];
|
|
95
|
+
const rulesChecked = [];
|
|
96
|
+
if (hasMissing) {
|
|
97
|
+
return this.handleMissingKeyValidation({ results, key, nestedKey, data, reqs }, missedCheck);
|
|
98
|
+
}
|
|
99
|
+
this.checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
|
|
100
|
+
this.checkRulesForKey(results, nestedKey, data[key], reqs, data, rulesChecked);
|
|
101
|
+
return this.finalizeValidation({ results, nestedKey, missedCheck, typeChecked, rulesChecked });
|
|
102
|
+
}
|
|
103
|
+
handleMissingKeyValidation(params, missedCheck) {
|
|
104
|
+
const schema = this.definition;
|
|
105
|
+
const { results, key, nestedKey, data, reqs } = params;
|
|
106
|
+
const errMsg = this.handleMissingKey(schema, { key, nestedKey, data, reqs });
|
|
107
|
+
missedCheck.push(false);
|
|
108
|
+
results.setMissing(nestedKey, errMsg);
|
|
109
|
+
return results;
|
|
110
|
+
}
|
|
111
|
+
checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
|
|
112
|
+
const typeCheck = validateType(key, value, reqs, nestedKey);
|
|
113
|
+
typeCheck.forEach((res) => {
|
|
114
|
+
if (!res.passed) {
|
|
115
|
+
typeChecked.push(false);
|
|
116
|
+
results.errors.push(res.details || '');
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
|
|
121
|
+
const ruleCheck = validateRules.call(this, nestedKey, value, reqs, data);
|
|
122
|
+
if (!ruleCheck.ok) {
|
|
123
|
+
rulesChecked.push(false);
|
|
124
|
+
ruleCheck.details.forEach((el) => {
|
|
125
|
+
if (!(nestedKey in results.errorsByKeys))
|
|
126
|
+
results.errorsByKeys[nestedKey] = [];
|
|
127
|
+
results.errors.push(el);
|
|
128
|
+
results.errorsByKeys[nestedKey] = ['1'];
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
finalizeValidation(checks) {
|
|
133
|
+
const { results, nestedKey, missedCheck, typeChecked, rulesChecked } = checks;
|
|
134
|
+
if (missedCheck.length)
|
|
135
|
+
results.setMissing(nestedKey);
|
|
136
|
+
const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
|
|
137
|
+
if (!isPassed) {
|
|
138
|
+
results.setFailed(nestedKey);
|
|
139
|
+
results.errorsByKeys[nestedKey] = [...results.errors];
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
results.setPassed(nestedKey);
|
|
143
|
+
}
|
|
144
|
+
return results.finish();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const validateSchema = (schemaDef, data, keysToValidate) => {
|
|
148
|
+
const engine = new ValidateEngine(schemaDef);
|
|
149
|
+
return engine.validate(data, keysToValidate);
|
|
150
|
+
};
|
|
151
|
+
export default validateSchema;
|