validno 0.1.1 → 0.1.2
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 +4 -0
- package/dist/app.js +22 -0
- package/dist/checkRules.js +13 -0
- package/dist/validate.js +3 -3
- package/dist/validateKey.js +15 -0
- package/package.json +1 -1
- package/src/Schema.ts +5 -0
- package/src/app.ts +59 -0
- package/src/checkRules.ts +18 -0
- package/src/validate.ts +4 -4
- package/src/validateKey.ts +21 -0
package/dist/Schema.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import validate from "./validate.js";
|
|
2
|
+
import validateKey from "./validateKey.js";
|
|
2
3
|
export const defaultSchemaKeys = [
|
|
3
4
|
"required",
|
|
4
5
|
"type",
|
|
@@ -12,4 +13,7 @@ export class Schema {
|
|
|
12
13
|
validate(data) {
|
|
13
14
|
return validate.call(this, this, data);
|
|
14
15
|
}
|
|
16
|
+
validateKey(key, data) {
|
|
17
|
+
return validateKey.call(this, this, data, key);
|
|
18
|
+
}
|
|
15
19
|
}
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Schema from "./index.js";
|
|
2
|
+
const schema = new Schema({
|
|
3
|
+
val: {
|
|
4
|
+
type: String,
|
|
5
|
+
required: true,
|
|
6
|
+
},
|
|
7
|
+
val2: {
|
|
8
|
+
type: Number,
|
|
9
|
+
required: true,
|
|
10
|
+
rules: {
|
|
11
|
+
min: 3
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const testObj = {
|
|
16
|
+
val: 'string',
|
|
17
|
+
val2: 1
|
|
18
|
+
};
|
|
19
|
+
const res = schema.validate(testObj);
|
|
20
|
+
const res2 = schema.validateKey('val2', testObj);
|
|
21
|
+
console.log(res);
|
|
22
|
+
console.log(res2);
|
package/dist/checkRules.js
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import _validations from "./utils/validations.js";
|
|
2
|
+
export const rulesParams = {
|
|
3
|
+
lengthMin: {
|
|
4
|
+
allowedTypes: [String]
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
const ensureRuleHasCorrectType = (value, allowedTypes) => {
|
|
8
|
+
const isInAllowedList = allowedTypes.some(TYPE => {
|
|
9
|
+
const getType = (el) => Object.prototype.toString.call(el);
|
|
10
|
+
return getType(new TYPE()) == getType(value);
|
|
11
|
+
});
|
|
12
|
+
return isInAllowedList;
|
|
13
|
+
};
|
|
2
14
|
const rulesFunctions = {
|
|
3
15
|
custom: (key, val, func) => {
|
|
4
16
|
return func(val);
|
|
@@ -60,6 +72,7 @@ const rulesFunctions = {
|
|
|
60
72
|
};
|
|
61
73
|
},
|
|
62
74
|
lengthMin: (key, val, min) => {
|
|
75
|
+
ensureRuleHasCorrectType(val, rulesParams['lengthMin'].allowedTypes);
|
|
63
76
|
return {
|
|
64
77
|
result: _validations.lengthMin(val, min),
|
|
65
78
|
details: `Длина не может быть меньше ${min} символов`
|
package/dist/validate.js
CHANGED
|
@@ -3,7 +3,7 @@ import checkType from "./checkType.js";
|
|
|
3
3
|
import { defaultSchemaKeys } from "./Schema.js";
|
|
4
4
|
import _errors from "./utils/errors.js";
|
|
5
5
|
import _validations from "./utils/validations.js";
|
|
6
|
-
const getResultDefaults = () => {
|
|
6
|
+
export const getResultDefaults = () => {
|
|
7
7
|
return {
|
|
8
8
|
ok: null,
|
|
9
9
|
missed: [],
|
|
@@ -25,7 +25,7 @@ const checkIsNested = (obj) => {
|
|
|
25
25
|
return true;
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
|
-
const mergeResults = (resultsOld, resultsNew) => {
|
|
28
|
+
export const mergeResults = (resultsOld, resultsNew) => {
|
|
29
29
|
const output = getResultDefaults();
|
|
30
30
|
output.failed = [...resultsOld.failed, ...resultsNew.failed];
|
|
31
31
|
output.errors = [...resultsOld.errors, ...resultsNew.errors];
|
|
@@ -35,7 +35,7 @@ const mergeResults = (resultsOld, resultsNew) => {
|
|
|
35
35
|
output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
|
|
36
36
|
return output;
|
|
37
37
|
};
|
|
38
|
-
const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
38
|
+
export const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
39
39
|
let results = getResultDefaults();
|
|
40
40
|
const hasNested = checkIsNested(reqs);
|
|
41
41
|
const missedCheck = [];
|
|
@@ -0,0 +1,15 @@
|
|
|
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;
|
package/package.json
CHANGED
package/src/Schema.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import validate from "./validate.js"
|
|
2
|
+
import validateKey from "./validateKey.js"
|
|
2
3
|
|
|
3
4
|
export type TSchemaItem = {
|
|
4
5
|
required: boolean,
|
|
@@ -37,4 +38,8 @@ export class Schema {
|
|
|
37
38
|
validate(data: any) {
|
|
38
39
|
return validate.call(this, this, data)
|
|
39
40
|
}
|
|
41
|
+
|
|
42
|
+
validateKey(key: string, data: any) {
|
|
43
|
+
return validateKey.call(this, this, data, key)
|
|
44
|
+
}
|
|
40
45
|
}
|
package/src/app.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import Schema from "./index.js";
|
|
2
|
+
|
|
3
|
+
// const testSchema = new Schema({
|
|
4
|
+
// field: {
|
|
5
|
+
// type: Number,
|
|
6
|
+
// required: true,
|
|
7
|
+
// rules: {
|
|
8
|
+
// lengthMin: 99
|
|
9
|
+
// }
|
|
10
|
+
// },
|
|
11
|
+
// field2: {
|
|
12
|
+
// type: "xxx",
|
|
13
|
+
// required: true,
|
|
14
|
+
// rules: {
|
|
15
|
+
// lengthMin: 10
|
|
16
|
+
// }
|
|
17
|
+
// }
|
|
18
|
+
// });
|
|
19
|
+
|
|
20
|
+
// class StringOwn {
|
|
21
|
+
// constructor(v: any) {
|
|
22
|
+
// v = v
|
|
23
|
+
// }
|
|
24
|
+
// }
|
|
25
|
+
|
|
26
|
+
// const strOwn = new StringOwn('xxxxuis')
|
|
27
|
+
|
|
28
|
+
// const body = {
|
|
29
|
+
// "field": 33,
|
|
30
|
+
// "field2": "Ssdasjk"
|
|
31
|
+
// }
|
|
32
|
+
|
|
33
|
+
// const validationResult = testSchema.validate(body);
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const schema = new Schema({
|
|
37
|
+
val: {
|
|
38
|
+
type: String,
|
|
39
|
+
required: true,
|
|
40
|
+
},
|
|
41
|
+
val2: {
|
|
42
|
+
type: Number,
|
|
43
|
+
required: true,
|
|
44
|
+
rules: {
|
|
45
|
+
min: 3
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const testObj = {
|
|
51
|
+
val: 'string',
|
|
52
|
+
val2: 1
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const res = schema.validate(testObj)
|
|
56
|
+
const res2 = schema.validateKey('val2', testObj)
|
|
57
|
+
|
|
58
|
+
console.log(res)
|
|
59
|
+
console.log(res2)
|
package/src/checkRules.ts
CHANGED
|
@@ -5,6 +5,21 @@ export type TRule = {[key: string]: any}
|
|
|
5
5
|
|
|
6
6
|
type TLengths = string | Array<any>
|
|
7
7
|
|
|
8
|
+
export const rulesParams = {
|
|
9
|
+
lengthMin: {
|
|
10
|
+
allowedTypes: [String]
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const ensureRuleHasCorrectType = (value: any, allowedTypes: any[]) => {
|
|
15
|
+
const isInAllowedList = allowedTypes.some(TYPE => {
|
|
16
|
+
const getType = (el: any) => Object.prototype.toString.call(el)
|
|
17
|
+
return getType(new TYPE()) == getType(value)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
return isInAllowedList
|
|
21
|
+
}
|
|
22
|
+
|
|
8
23
|
const rulesFunctions: any = {
|
|
9
24
|
custom: (key: string, val: any, func: Function) => {
|
|
10
25
|
return func(val)
|
|
@@ -67,6 +82,8 @@ const rulesFunctions: any = {
|
|
|
67
82
|
}
|
|
68
83
|
},
|
|
69
84
|
lengthMin: (key: string, val: TLengths, min: number) => {
|
|
85
|
+
ensureRuleHasCorrectType(val, rulesParams['lengthMin'].allowedTypes)
|
|
86
|
+
|
|
70
87
|
return {
|
|
71
88
|
result: _validations.lengthMin(val, min),
|
|
72
89
|
details: `Длина не может быть меньше ${min} символов`
|
|
@@ -137,6 +154,7 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
|
|
|
137
154
|
|
|
138
155
|
// If key has failed rules checks
|
|
139
156
|
const failedResults = allResults.filter(el => el.result === false)
|
|
157
|
+
|
|
140
158
|
if (failedResults.length) {
|
|
141
159
|
result.ok = false
|
|
142
160
|
result.details = failedResults.map(el => el.details)
|
package/src/validate.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { defaultSchemaKeys, Schema, TSchemaInput } from "./Schema.js";
|
|
|
4
4
|
import _errors from "./utils/errors.js";
|
|
5
5
|
import _validations from "./utils/validations.js";
|
|
6
6
|
|
|
7
|
-
type TResult = {
|
|
7
|
+
export type TResult = {
|
|
8
8
|
ok: null | boolean,
|
|
9
9
|
missed: string[],
|
|
10
10
|
failed: string[],
|
|
@@ -14,7 +14,7 @@ type TResult = {
|
|
|
14
14
|
errorsByKeys: {[key: string]: string[]}
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
const getResultDefaults = (): TResult => {
|
|
17
|
+
export const getResultDefaults = (): TResult => {
|
|
18
18
|
return {
|
|
19
19
|
ok: null,
|
|
20
20
|
missed: [],
|
|
@@ -38,7 +38,7 @@ const checkIsNested = (obj: {[key: string]: any}) => {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
41
|
+
export const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
42
42
|
const output = getResultDefaults()
|
|
43
43
|
|
|
44
44
|
output.failed = [...resultsOld.failed, ...resultsNew.failed]
|
|
@@ -51,7 +51,7 @@ const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
|
|
|
51
51
|
return output
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key) => {
|
|
54
|
+
export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key) => {
|
|
55
55
|
let results = getResultDefaults()
|
|
56
56
|
const hasNested = checkIsNested(reqs)
|
|
57
57
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Schema } from "./Schema.js";
|
|
2
|
+
import { getResultDefaults, handleReqKey, mergeResults, TResult } from "./validate.js";
|
|
3
|
+
|
|
4
|
+
const validateKey = (schema: Schema, data: any, key: string): TResult => {
|
|
5
|
+
if (key in schema.schema === false) {
|
|
6
|
+
throw new Error(`Ключ ${key} отсутствует в схеме`)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let results: TResult = getResultDefaults()
|
|
10
|
+
//@ts-ignore
|
|
11
|
+
const keyResult = handleReqKey(key, data, schema.schema[key])
|
|
12
|
+
|
|
13
|
+
results = mergeResults(results, keyResult)
|
|
14
|
+
|
|
15
|
+
if (results.failed.length) results.ok = false
|
|
16
|
+
else results.ok = true
|
|
17
|
+
|
|
18
|
+
return results;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default validateKey;
|