validno 0.1.4 → 0.1.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/dist/checkRules.js +22 -8
- package/dist/validate.js +8 -7
- package/package.json +1 -1
- package/src/checkRules.ts +23 -8
- package/src/validate.ts +7 -6
- package/dist/Validator.js +0 -100
- package/dist/app.js +0 -45
- package/dist/sss.js +0 -1
- package/dist/validateKey.js +0 -15
package/dist/checkRules.js
CHANGED
|
@@ -12,8 +12,8 @@ const ensureRuleHasCorrectType = (value, allowedTypes) => {
|
|
|
12
12
|
return isInAllowedList;
|
|
13
13
|
};
|
|
14
14
|
const rulesFunctions = {
|
|
15
|
-
custom: (key, val, func) => {
|
|
16
|
-
return func(val);
|
|
15
|
+
custom: (key, val, func, extra) => {
|
|
16
|
+
return func(val, extra);
|
|
17
17
|
},
|
|
18
18
|
isEmail: (key, val) => {
|
|
19
19
|
return {
|
|
@@ -91,13 +91,26 @@ const rulesFunctions = {
|
|
|
91
91
|
};
|
|
92
92
|
},
|
|
93
93
|
enum: (key, value, allowedList) => {
|
|
94
|
-
|
|
95
|
-
result:
|
|
96
|
-
details:
|
|
94
|
+
const output = {
|
|
95
|
+
result: true,
|
|
96
|
+
details: ''
|
|
97
97
|
};
|
|
98
|
+
if (!Array.isArray(value)) {
|
|
99
|
+
const isCorrect = allowedList.includes(value);
|
|
100
|
+
output.result = isCorrect,
|
|
101
|
+
output.details = isCorrect ? '' : `Значение "${value}" недопустимо`;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const incorrectValues = [];
|
|
105
|
+
value.forEach((v) => !allowedList.includes(v) ? incorrectValues.push(v) : {});
|
|
106
|
+
const isCorrect = incorrectValues.length === 0;
|
|
107
|
+
output.result = isCorrect,
|
|
108
|
+
output.details = isCorrect ? '' : `Значения недопустимы: "${incorrectValues.join(', ')}"`;
|
|
109
|
+
}
|
|
110
|
+
return output;
|
|
98
111
|
}
|
|
99
112
|
};
|
|
100
|
-
|
|
113
|
+
function checkRules(key, value, requirements, inputObj) {
|
|
101
114
|
const result = {
|
|
102
115
|
ok: true,
|
|
103
116
|
details: []
|
|
@@ -119,7 +132,7 @@ const checkRules = (key, value, requirements) => {
|
|
|
119
132
|
}
|
|
120
133
|
const func = rulesFunctions[ruleName];
|
|
121
134
|
const args = rules[ruleName];
|
|
122
|
-
const result = func(key, value, args);
|
|
135
|
+
const result = func(key, value, args, { schema: this.schema, input: inputObj });
|
|
123
136
|
allResults.push(result);
|
|
124
137
|
i++;
|
|
125
138
|
}
|
|
@@ -129,5 +142,6 @@ const checkRules = (key, value, requirements) => {
|
|
|
129
142
|
result.details = failedResults.map(el => el.details);
|
|
130
143
|
}
|
|
131
144
|
return result;
|
|
132
|
-
}
|
|
145
|
+
}
|
|
146
|
+
;
|
|
133
147
|
export default checkRules;
|
package/dist/validate.js
CHANGED
|
@@ -35,7 +35,7 @@ export const mergeResults = (resultsOld, resultsNew) => {
|
|
|
35
35
|
output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
|
|
36
36
|
return output;
|
|
37
37
|
};
|
|
38
|
-
export
|
|
38
|
+
export function handleReqKey(key, data, reqs, deepKey = key) {
|
|
39
39
|
let results = getResultDefaults();
|
|
40
40
|
const hasNested = checkIsNested(reqs);
|
|
41
41
|
const missedCheck = [];
|
|
@@ -54,7 +54,7 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
|
54
54
|
let i = 0;
|
|
55
55
|
while (i < nestedReqKeys.length) {
|
|
56
56
|
const reqKeyI = nestedReqKeys[i];
|
|
57
|
-
const deepResults = handleReqKey(reqKeyI, data[key], reqs[reqKeyI], deepKey + '.' + reqKeyI);
|
|
57
|
+
const deepResults = handleReqKey.call(this, reqKeyI, data[key], reqs[reqKeyI], deepKey + '.' + reqKeyI);
|
|
58
58
|
results = mergeResults(results, deepResults);
|
|
59
59
|
i++;
|
|
60
60
|
}
|
|
@@ -77,7 +77,7 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
|
77
77
|
results.errors.push(res.details);
|
|
78
78
|
}
|
|
79
79
|
});
|
|
80
|
-
const ruleCheck = checkRules(deepKey, data[key], reqs);
|
|
80
|
+
const ruleCheck = checkRules.call(this, deepKey, data[key], reqs, data);
|
|
81
81
|
if (!ruleCheck.ok) {
|
|
82
82
|
rulesChecked.push(false);
|
|
83
83
|
ruleCheck.details.forEach((el) => {
|
|
@@ -100,16 +100,16 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
|
100
100
|
];
|
|
101
101
|
results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
|
|
102
102
|
return results;
|
|
103
|
-
}
|
|
103
|
+
}
|
|
104
104
|
const isCheckNeeded = (key, hasLimits, onlyKeys) => {
|
|
105
105
|
return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
|
|
106
106
|
};
|
|
107
|
-
|
|
107
|
+
function validate(schema, data, onlyKeys) {
|
|
108
108
|
let results = getResultDefaults();
|
|
109
109
|
const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0);
|
|
110
110
|
for (const [key, reqs] of Object.entries(schema.schema)) {
|
|
111
111
|
if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
|
|
112
|
-
const keyResult = handleReqKey(key, data, reqs);
|
|
112
|
+
const keyResult = handleReqKey.call(this, key, data, reqs);
|
|
113
113
|
results = mergeResults(results, keyResult);
|
|
114
114
|
}
|
|
115
115
|
}
|
|
@@ -118,5 +118,6 @@ const validate = (schema, data, onlyKeys) => {
|
|
|
118
118
|
else
|
|
119
119
|
results.ok = true;
|
|
120
120
|
return results;
|
|
121
|
-
}
|
|
121
|
+
}
|
|
122
|
+
;
|
|
122
123
|
export default validate;
|
package/package.json
CHANGED
package/src/checkRules.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TSchemaItem } from "./Schema.js";
|
|
1
|
+
import { TSchema, TSchemaItem } from "./Schema.js";
|
|
2
2
|
import _validations from "./utils/validations.js"
|
|
3
3
|
|
|
4
4
|
export type TRule = {[key: string]: any}
|
|
@@ -21,8 +21,8 @@ const ensureRuleHasCorrectType = (value: any, allowedTypes: any[]) => {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
const rulesFunctions: any = {
|
|
24
|
-
custom: (key: string, val: any, func: Function) => {
|
|
25
|
-
return func(val)
|
|
24
|
+
custom: (key: string, val: any, func: Function, extra: {schema: TSchema, input: {[key: string]: any}}) => {
|
|
25
|
+
return func(val, extra)
|
|
26
26
|
},
|
|
27
27
|
isEmail: (key: string, val: any) => {
|
|
28
28
|
return {
|
|
@@ -102,10 +102,25 @@ const rulesFunctions: any = {
|
|
|
102
102
|
}
|
|
103
103
|
},
|
|
104
104
|
enum: (key: string, value: any, allowedList: any[]) => {
|
|
105
|
-
|
|
106
|
-
result:
|
|
107
|
-
details:
|
|
105
|
+
const output = {
|
|
106
|
+
result: true,
|
|
107
|
+
details: ''
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!Array.isArray(value)) {
|
|
111
|
+
const isCorrect = allowedList.includes(value)
|
|
112
|
+
output.result = isCorrect,
|
|
113
|
+
output.details = isCorrect ? '' : `Значение "${value}" недопустимо`
|
|
114
|
+
} else {
|
|
115
|
+
const incorrectValues: any[] = []
|
|
116
|
+
value.forEach((v: any) => !allowedList.includes(v) ? incorrectValues.push(v): {})
|
|
117
|
+
const isCorrect = incorrectValues.length === 0
|
|
118
|
+
|
|
119
|
+
output.result = isCorrect,
|
|
120
|
+
output.details = isCorrect ? '' : `Значения недопустимы: "${incorrectValues.join(', ')}"`
|
|
108
121
|
}
|
|
122
|
+
|
|
123
|
+
return output
|
|
109
124
|
}
|
|
110
125
|
};
|
|
111
126
|
|
|
@@ -114,7 +129,7 @@ type TRulesResult = {
|
|
|
114
129
|
details: string[]
|
|
115
130
|
}
|
|
116
131
|
|
|
117
|
-
|
|
132
|
+
function checkRules (this: any, key: string, value: any, requirements: TSchemaItem, inputObj: any) {
|
|
118
133
|
const result: TRulesResult = {
|
|
119
134
|
ok: true,
|
|
120
135
|
details: []
|
|
@@ -146,7 +161,7 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
|
|
|
146
161
|
const func = rulesFunctions[ruleName]
|
|
147
162
|
const args = rules[ruleName]
|
|
148
163
|
|
|
149
|
-
const result = func(key, value, args)
|
|
164
|
+
const result = func(key, value, args, {schema: this.schema, input: inputObj})
|
|
150
165
|
allResults.push(result)
|
|
151
166
|
|
|
152
167
|
i++;
|
package/src/validate.ts
CHANGED
|
@@ -51,10 +51,10 @@ export const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
|
51
51
|
return output
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
export
|
|
54
|
+
export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInput, deepKey = key) {
|
|
55
55
|
let results = getResultDefaults()
|
|
56
56
|
const hasNested = checkIsNested(reqs)
|
|
57
|
-
|
|
57
|
+
|
|
58
58
|
const missedCheck: boolean[] = [];
|
|
59
59
|
const typeChecked: boolean[] = [];
|
|
60
60
|
const rulesChecked: boolean[] = [];
|
|
@@ -80,7 +80,8 @@ export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey
|
|
|
80
80
|
while (i < nestedReqKeys.length) {
|
|
81
81
|
const reqKeyI: string = nestedReqKeys[i]
|
|
82
82
|
|
|
83
|
-
const deepResults = handleReqKey(
|
|
83
|
+
const deepResults = handleReqKey.call(
|
|
84
|
+
this,
|
|
84
85
|
reqKeyI,
|
|
85
86
|
data[key],
|
|
86
87
|
// @ts-ignore
|
|
@@ -122,7 +123,7 @@ export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey
|
|
|
122
123
|
|
|
123
124
|
// Check all rules
|
|
124
125
|
// @ts-ignore
|
|
125
|
-
const ruleCheck = checkRules(deepKey, data[key], reqs);
|
|
126
|
+
const ruleCheck = checkRules.call(this, deepKey, data[key], reqs, data);
|
|
126
127
|
|
|
127
128
|
if (!ruleCheck.ok) {
|
|
128
129
|
rulesChecked.push(false)
|
|
@@ -158,14 +159,14 @@ const isCheckNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | stri
|
|
|
158
159
|
return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && onlyKeys?.includes(key))
|
|
159
160
|
}
|
|
160
161
|
|
|
161
|
-
|
|
162
|
+
function validate(schema: Schema, data: any, onlyKeys?: string | string[]): TResult {
|
|
162
163
|
let results: TResult = getResultDefaults()
|
|
163
164
|
const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0)
|
|
164
165
|
|
|
165
166
|
for (const [key, reqs] of Object.entries(schema.schema)) {
|
|
166
167
|
if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
|
|
167
168
|
// @ts-ignore
|
|
168
|
-
const keyResult = handleReqKey(key, data, reqs)
|
|
169
|
+
const keyResult = handleReqKey.call(this, key, data, reqs)
|
|
169
170
|
|
|
170
171
|
results = mergeResults(results, keyResult)
|
|
171
172
|
}
|
package/dist/Validator.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import checkRules from "./checkRules.js";
|
|
2
|
-
import checkType from "./checkType.js";
|
|
3
|
-
import { defaultSchemaKeys } from "./Schema.js";
|
|
4
|
-
const getResultDefault = () => {
|
|
5
|
-
return {
|
|
6
|
-
ok: null,
|
|
7
|
-
missed: [],
|
|
8
|
-
failed: [],
|
|
9
|
-
passed: [],
|
|
10
|
-
errors: [],
|
|
11
|
-
byKeys: {}
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
const checkIsNested = (obj) => {
|
|
15
|
-
const objKeys = Object.keys(obj);
|
|
16
|
-
if (objKeys.every((k) => defaultSchemaKeys.includes(k))) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
24
|
-
console.log('..................... ', key);
|
|
25
|
-
const results = getResultDefault();
|
|
26
|
-
const hasNested = checkIsNested(reqs);
|
|
27
|
-
const missedCheck = [];
|
|
28
|
-
const typeChecked = [];
|
|
29
|
-
const rulesChecked = [];
|
|
30
|
-
if (hasNested) {
|
|
31
|
-
const nestedReqKeys = Object.keys(reqs);
|
|
32
|
-
let i = 0;
|
|
33
|
-
while (i < nestedReqKeys.length) {
|
|
34
|
-
const reqKeyI = nestedReqKeys[i];
|
|
35
|
-
const deepResults = handleReqKey(reqKeyI, data[key], reqs[reqKeyI], deepKey + '.' + reqKeyI);
|
|
36
|
-
results.failed = [...results.failed, ...deepResults.failed];
|
|
37
|
-
results.errors = [...results.errors, ...deepResults.errors];
|
|
38
|
-
results.missed = [...results.missed, ...deepResults.missed];
|
|
39
|
-
results.passed = [...results.passed, ...deepResults.passed];
|
|
40
|
-
results.byKeys = Object.assign(Object.assign({}, results.byKeys), deepResults.byKeys);
|
|
41
|
-
i++;
|
|
42
|
-
}
|
|
43
|
-
return results;
|
|
44
|
-
}
|
|
45
|
-
if (reqs.required === true && key in data === false) {
|
|
46
|
-
missedCheck.push(false);
|
|
47
|
-
results.errors.push(`Missing key '${deepKey}'`);
|
|
48
|
-
return results;
|
|
49
|
-
}
|
|
50
|
-
const typeCheck = checkType(key, data[key], reqs, deepKey);
|
|
51
|
-
typeCheck.forEach(res => {
|
|
52
|
-
if (res.passed === false) {
|
|
53
|
-
typeChecked.push(res.passed);
|
|
54
|
-
results.errors.push(res.details);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
const ruleCheck = checkRules(deepKey, data[key], reqs);
|
|
58
|
-
if (!ruleCheck.ok) {
|
|
59
|
-
rulesChecked.push(false);
|
|
60
|
-
ruleCheck.details.forEach((el) => results.errors.push(el));
|
|
61
|
-
}
|
|
62
|
-
if (missedCheck.length) {
|
|
63
|
-
results.missed.push(deepKey);
|
|
64
|
-
}
|
|
65
|
-
if (typeChecked.length || rulesChecked.length) {
|
|
66
|
-
results.failed.push(deepKey);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
results.passed.push(deepKey);
|
|
70
|
-
}
|
|
71
|
-
console.log(missedCheck.length, typeChecked.length, rulesChecked.length);
|
|
72
|
-
results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
|
|
73
|
-
console.log(results);
|
|
74
|
-
return results;
|
|
75
|
-
};
|
|
76
|
-
const validate = (schema, data) => {
|
|
77
|
-
const results = getResultDefault();
|
|
78
|
-
for (const [key, reqs] of Object.entries(schema.schema)) {
|
|
79
|
-
const keyResult = handleReqKey(key, data, reqs);
|
|
80
|
-
results.failed = [...results.failed, ...keyResult.failed];
|
|
81
|
-
results.errors = [...results.errors, ...keyResult.errors];
|
|
82
|
-
results.missed = [...results.missed, ...keyResult.missed];
|
|
83
|
-
results.passed = [...results.passed, ...keyResult.passed];
|
|
84
|
-
results.byKeys = Object.assign(Object.assign({}, results.byKeys), keyResult.byKeys);
|
|
85
|
-
}
|
|
86
|
-
if (results.failed.length)
|
|
87
|
-
results.ok = false;
|
|
88
|
-
else
|
|
89
|
-
results.ok = true;
|
|
90
|
-
results.missed = [...new Set(results.missed)];
|
|
91
|
-
results.failed = [...new Set(results.failed)];
|
|
92
|
-
results.passed = [...new Set(results.passed)];
|
|
93
|
-
results.errors = [...new Set(results.errors)];
|
|
94
|
-
return results;
|
|
95
|
-
};
|
|
96
|
-
class Validator {
|
|
97
|
-
constructor() { }
|
|
98
|
-
}
|
|
99
|
-
Validator.validate = validate;
|
|
100
|
-
export default Validator;
|
package/dist/app.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import Schema from "./index.js";
|
|
2
|
-
const schema = new Schema({
|
|
3
|
-
val: {
|
|
4
|
-
type: String,
|
|
5
|
-
required: true,
|
|
6
|
-
},
|
|
7
|
-
date: {
|
|
8
|
-
type: Date,
|
|
9
|
-
required: true,
|
|
10
|
-
},
|
|
11
|
-
obj: {
|
|
12
|
-
type: Object,
|
|
13
|
-
required: true,
|
|
14
|
-
},
|
|
15
|
-
bool: {
|
|
16
|
-
type: Boolean,
|
|
17
|
-
required: true,
|
|
18
|
-
},
|
|
19
|
-
val2: {
|
|
20
|
-
type: Number,
|
|
21
|
-
required: true,
|
|
22
|
-
rules: {
|
|
23
|
-
min: 3
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
srtOrNum: {
|
|
27
|
-
type: [String, Number],
|
|
28
|
-
required: true,
|
|
29
|
-
},
|
|
30
|
-
nl: {
|
|
31
|
-
type: null,
|
|
32
|
-
required: true,
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
const testObj = {
|
|
36
|
-
val: 'string',
|
|
37
|
-
val2: 4,
|
|
38
|
-
obj: {},
|
|
39
|
-
srtOrNum: ['ss'],
|
|
40
|
-
date: new Date(),
|
|
41
|
-
bool: true,
|
|
42
|
-
nl: null
|
|
43
|
-
};
|
|
44
|
-
const resAll = schema.validate(testObj);
|
|
45
|
-
const str = 'test';
|
package/dist/sss.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/dist/validateKey.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { getResultDefaults, handleReqKey, mergeResults } from "./validate.js";
|
|
2
|
-
const validateKey = (schema, data, key) => {
|
|
3
|
-
if (key in schema.schema === false) {
|
|
4
|
-
throw new Error(`Ключ ${key} отсутствует в схеме`);
|
|
5
|
-
}
|
|
6
|
-
let results = getResultDefaults();
|
|
7
|
-
const keyResult = handleReqKey(key, data, schema.schema[key]);
|
|
8
|
-
results = mergeResults(results, keyResult);
|
|
9
|
-
if (results.failed.length)
|
|
10
|
-
results.ok = false;
|
|
11
|
-
else
|
|
12
|
-
results.ok = true;
|
|
13
|
-
return results;
|
|
14
|
-
};
|
|
15
|
-
export default validateKey;
|