validno 0.1.3 → 0.1.4
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/app.js +25 -9
- package/dist/checkRules.js +5 -7
- package/dist/checkType.js +17 -8
- package/package.json +14 -3
- package/src/checkRules.ts +8 -8
- package/src/checkType.ts +26 -8
- package/tests/types.test.js +6 -1
- package/src/app.ts +0 -66
- package/tests/rules.test.js +0 -0
package/dist/app.js
CHANGED
|
@@ -4,6 +4,18 @@ const schema = new Schema({
|
|
|
4
4
|
type: String,
|
|
5
5
|
required: true,
|
|
6
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
|
+
},
|
|
7
19
|
val2: {
|
|
8
20
|
type: Number,
|
|
9
21
|
required: true,
|
|
@@ -11,19 +23,23 @@ const schema = new Schema({
|
|
|
11
23
|
min: 3
|
|
12
24
|
}
|
|
13
25
|
},
|
|
14
|
-
|
|
15
|
-
type: String,
|
|
26
|
+
srtOrNum: {
|
|
27
|
+
type: [String, Number],
|
|
28
|
+
required: true,
|
|
29
|
+
},
|
|
30
|
+
nl: {
|
|
31
|
+
type: null,
|
|
16
32
|
required: true,
|
|
17
33
|
},
|
|
18
34
|
});
|
|
19
35
|
const testObj = {
|
|
20
36
|
val: 'string',
|
|
21
|
-
val2:
|
|
22
|
-
|
|
37
|
+
val2: 4,
|
|
38
|
+
obj: {},
|
|
39
|
+
srtOrNum: ['ss'],
|
|
40
|
+
date: new Date(),
|
|
41
|
+
bool: true,
|
|
42
|
+
nl: null
|
|
23
43
|
};
|
|
24
44
|
const resAll = schema.validate(testObj);
|
|
25
|
-
const
|
|
26
|
-
const res2 = schema.validate(testObj, 'val2');
|
|
27
|
-
console.log(resAll);
|
|
28
|
-
console.log(res);
|
|
29
|
-
console.log(res2);
|
|
45
|
+
const str = 'test';
|
package/dist/checkRules.js
CHANGED
|
@@ -30,7 +30,7 @@ const rulesFunctions = {
|
|
|
30
30
|
isNot: (key, val, notEqualTo) => {
|
|
31
31
|
return {
|
|
32
32
|
result: _validations.isNot(val, notEqualTo),
|
|
33
|
-
details: `Значение
|
|
33
|
+
details: `Значение "${notEqualTo}" недопустимо`
|
|
34
34
|
};
|
|
35
35
|
},
|
|
36
36
|
min: (key, val, min) => {
|
|
@@ -93,7 +93,7 @@ const rulesFunctions = {
|
|
|
93
93
|
enum: (key, value, allowedList) => {
|
|
94
94
|
return {
|
|
95
95
|
result: allowedList.includes(value),
|
|
96
|
-
details: `Значение "${value}"
|
|
96
|
+
details: `Значение "${value}" недопустимо`
|
|
97
97
|
};
|
|
98
98
|
}
|
|
99
99
|
};
|
|
@@ -104,17 +104,15 @@ const checkRules = (key, value, requirements) => {
|
|
|
104
104
|
};
|
|
105
105
|
if (requirements.required !== true && value === undefined)
|
|
106
106
|
return result;
|
|
107
|
-
if ('rules' in requirements === false)
|
|
108
|
-
return result;
|
|
109
107
|
if (!requirements || !requirements.rules || !Object.keys(requirements.rules).length) {
|
|
110
108
|
return result;
|
|
111
109
|
}
|
|
112
110
|
const rules = requirements.rules;
|
|
113
111
|
const allResults = [];
|
|
114
|
-
const
|
|
112
|
+
const allRulesKeys = Object.keys(rules);
|
|
115
113
|
let i = 0;
|
|
116
|
-
while (i <
|
|
117
|
-
const ruleName =
|
|
114
|
+
while (i < allRulesKeys.length) {
|
|
115
|
+
const ruleName = allRulesKeys[i];
|
|
118
116
|
if (ruleName in rulesFunctions === false) {
|
|
119
117
|
i++;
|
|
120
118
|
continue;
|
package/dist/checkType.js
CHANGED
|
@@ -40,6 +40,7 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
|
|
|
40
40
|
return result;
|
|
41
41
|
};
|
|
42
42
|
const checkType = (key, value, requirements, keyName = key) => {
|
|
43
|
+
const isNotNull = value !== null;
|
|
43
44
|
if (value === undefined && requirements.required) {
|
|
44
45
|
return [{ key: keyName, passed: false, details: `Key ${keyName} is missing` }];
|
|
45
46
|
}
|
|
@@ -64,7 +65,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
64
65
|
});
|
|
65
66
|
break;
|
|
66
67
|
case Number:
|
|
67
|
-
const isNumber =
|
|
68
|
+
const isNumber = isNotNull && value.constructor === Number;
|
|
68
69
|
result.push({
|
|
69
70
|
key: keyName,
|
|
70
71
|
passed: isNumber,
|
|
@@ -72,7 +73,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
72
73
|
});
|
|
73
74
|
break;
|
|
74
75
|
case String:
|
|
75
|
-
const isString =
|
|
76
|
+
const isString = isNotNull && value.constructor === String;
|
|
76
77
|
result.push({
|
|
77
78
|
key: keyName,
|
|
78
79
|
passed: isString,
|
|
@@ -80,7 +81,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
80
81
|
});
|
|
81
82
|
break;
|
|
82
83
|
case Date:
|
|
83
|
-
const isDate =
|
|
84
|
+
const isDate = isNotNull && value.constructor === Date;
|
|
84
85
|
result.push({
|
|
85
86
|
key: keyName,
|
|
86
87
|
passed: isDate,
|
|
@@ -88,7 +89,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
88
89
|
});
|
|
89
90
|
break;
|
|
90
91
|
case Boolean:
|
|
91
|
-
const isBoolean =
|
|
92
|
+
const isBoolean = isNotNull && value.constructor === Boolean;
|
|
92
93
|
result.push({
|
|
93
94
|
key: keyName,
|
|
94
95
|
passed: isBoolean,
|
|
@@ -96,7 +97,15 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
96
97
|
});
|
|
97
98
|
break;
|
|
98
99
|
case Array:
|
|
99
|
-
const isArray =
|
|
100
|
+
const isArray = isNotNull && value.constructor === Array;
|
|
101
|
+
if (!isArray) {
|
|
102
|
+
result.push({
|
|
103
|
+
key: keyName,
|
|
104
|
+
passed: false,
|
|
105
|
+
details: getErrorDetails(keyName, requirements.type, value)
|
|
106
|
+
});
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
100
109
|
let isEachChecked = { passed: true, details: "" };
|
|
101
110
|
if ('eachType' in requirements) {
|
|
102
111
|
isEachChecked.passed = value.every((el) => {
|
|
@@ -116,7 +125,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
116
125
|
});
|
|
117
126
|
break;
|
|
118
127
|
case Object:
|
|
119
|
-
const isObject = _validations.isObject(value);
|
|
128
|
+
const isObject = _validations.isObject(value) && value.constructor === Object;
|
|
120
129
|
result.push({
|
|
121
130
|
key: keyName,
|
|
122
131
|
passed: isObject,
|
|
@@ -132,7 +141,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
132
141
|
});
|
|
133
142
|
break;
|
|
134
143
|
case null:
|
|
135
|
-
const isNull =
|
|
144
|
+
const isNull = value === null;
|
|
136
145
|
result.push({
|
|
137
146
|
key: keyName,
|
|
138
147
|
passed: isNull,
|
|
@@ -143,7 +152,7 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
143
152
|
result.push({
|
|
144
153
|
key: keyName,
|
|
145
154
|
passed: false,
|
|
146
|
-
details:
|
|
155
|
+
details: `Тип '${keyName}' не определен`
|
|
147
156
|
});
|
|
148
157
|
}
|
|
149
158
|
return result;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "validno",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"tsc": "tsc -b src/",
|
|
8
8
|
"tsc-watch": "tsc --b --watch src/",
|
|
9
|
-
"dev": "nodemon dist/
|
|
9
|
+
"dev": "nodemon dist/dev.js",
|
|
10
10
|
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
11
11
|
},
|
|
12
12
|
"author": "lesha2r",
|
|
@@ -22,5 +22,16 @@
|
|
|
22
22
|
"@types/node": "^18.14.2",
|
|
23
23
|
"eslint": "^8.14.0",
|
|
24
24
|
"jest": "^29.7.0"
|
|
25
|
-
}
|
|
25
|
+
},
|
|
26
|
+
"directories": {
|
|
27
|
+
"test": "tests"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/lesha2r/validno.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/lesha2r/validno/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/lesha2r/validno#readme"
|
|
26
37
|
}
|
package/src/checkRules.ts
CHANGED
|
@@ -39,7 +39,7 @@ const rulesFunctions: any = {
|
|
|
39
39
|
isNot: (key: string, val: any, notEqualTo: any) => {
|
|
40
40
|
return {
|
|
41
41
|
result: _validations.isNot(val, notEqualTo),
|
|
42
|
-
details: `Значение
|
|
42
|
+
details: `Значение "${notEqualTo}" недопустимо`
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
min: (key: string, val: number, min: number) => {
|
|
@@ -104,7 +104,7 @@ const rulesFunctions: any = {
|
|
|
104
104
|
enum: (key: string, value: any, allowedList: any[]) => {
|
|
105
105
|
return {
|
|
106
106
|
result: allowedList.includes(value),
|
|
107
|
-
details: `Значение "${value}"
|
|
107
|
+
details: `Значение "${value}" недопустимо`
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
110
|
};
|
|
@@ -120,11 +120,10 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
|
|
|
120
120
|
details: []
|
|
121
121
|
};
|
|
122
122
|
|
|
123
|
-
//
|
|
123
|
+
// Значение отсутствует, но НЕ является обязательным
|
|
124
124
|
if (requirements.required !== true && value === undefined) return result
|
|
125
125
|
|
|
126
|
-
//
|
|
127
|
-
if ('rules' in requirements === false) return result
|
|
126
|
+
// Для этого ключа нет правил
|
|
128
127
|
if (!requirements || !requirements.rules || !Object.keys(requirements.rules).length) {
|
|
129
128
|
return result
|
|
130
129
|
}
|
|
@@ -132,12 +131,13 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
|
|
|
132
131
|
const rules: TRule = requirements.rules
|
|
133
132
|
|
|
134
133
|
const allResults = []
|
|
135
|
-
const
|
|
134
|
+
const allRulesKeys = Object.keys(rules)
|
|
136
135
|
|
|
137
136
|
let i = 0;
|
|
138
|
-
while (i <
|
|
139
|
-
const ruleName =
|
|
137
|
+
while (i < allRulesKeys.length) {
|
|
138
|
+
const ruleName = allRulesKeys[i]
|
|
140
139
|
|
|
140
|
+
// Если правило, указанное для ключа, отсутствует в списке доступных
|
|
141
141
|
if (ruleName in rulesFunctions === false) {
|
|
142
142
|
i++
|
|
143
143
|
continue
|
package/src/checkType.ts
CHANGED
|
@@ -18,6 +18,7 @@ const getErrorDetails = (key: string, expectedType: any, receivedValue: any) =>
|
|
|
18
18
|
|
|
19
19
|
const checkTypeMultiple = (key: string, value: any, requirements: TSchemaItem | TSchemaInput, keyName = key) => {
|
|
20
20
|
const constructorNames = requirements.type.map((el:any) => String(el?.name || el))
|
|
21
|
+
|
|
21
22
|
const result = {
|
|
22
23
|
key: keyName,
|
|
23
24
|
passed: false,
|
|
@@ -44,6 +45,8 @@ const checkTypeMultiple = (key: string, value: any, requirements: TSchemaItem |
|
|
|
44
45
|
type TCheckTypeResult = {key: string, passed: boolean, details: string}
|
|
45
46
|
|
|
46
47
|
const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaInput, keyName = key): TCheckTypeResult[] => {
|
|
48
|
+
const isNotNull = value !== null
|
|
49
|
+
|
|
47
50
|
if (value === undefined && requirements.required) {
|
|
48
51
|
return [{key: keyName, passed: false, details: `Key ${keyName} is missing`}]
|
|
49
52
|
}
|
|
@@ -72,7 +75,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
72
75
|
})
|
|
73
76
|
break;
|
|
74
77
|
case Number:
|
|
75
|
-
const isNumber =
|
|
78
|
+
const isNumber = isNotNull && value.constructor === Number
|
|
79
|
+
|
|
76
80
|
result.push({
|
|
77
81
|
key: keyName,
|
|
78
82
|
passed: isNumber,
|
|
@@ -81,7 +85,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
81
85
|
|
|
82
86
|
break;
|
|
83
87
|
case String:
|
|
84
|
-
const isString =
|
|
88
|
+
const isString = isNotNull && value.constructor === String
|
|
89
|
+
|
|
85
90
|
result.push({
|
|
86
91
|
key: keyName,
|
|
87
92
|
passed: isString,
|
|
@@ -89,7 +94,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
89
94
|
})
|
|
90
95
|
break;
|
|
91
96
|
case Date:
|
|
92
|
-
const isDate =
|
|
97
|
+
const isDate = isNotNull && value.constructor === Date
|
|
98
|
+
|
|
93
99
|
result.push({
|
|
94
100
|
key: keyName,
|
|
95
101
|
passed: isDate,
|
|
@@ -97,7 +103,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
97
103
|
})
|
|
98
104
|
break;
|
|
99
105
|
case Boolean:
|
|
100
|
-
const isBoolean =
|
|
106
|
+
const isBoolean = isNotNull && value.constructor === Boolean
|
|
107
|
+
|
|
101
108
|
result.push({
|
|
102
109
|
key: keyName,
|
|
103
110
|
passed: isBoolean,
|
|
@@ -105,7 +112,17 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
105
112
|
})
|
|
106
113
|
break;
|
|
107
114
|
case Array:
|
|
108
|
-
const isArray =
|
|
115
|
+
const isArray = isNotNull && value.constructor === Array
|
|
116
|
+
|
|
117
|
+
if (!isArray) {
|
|
118
|
+
result.push({
|
|
119
|
+
key: keyName,
|
|
120
|
+
passed: false,
|
|
121
|
+
details: getErrorDetails(keyName, requirements.type, value)
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
109
126
|
|
|
110
127
|
let isEachChecked = { passed: true, details: ""}
|
|
111
128
|
|
|
@@ -132,7 +149,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
132
149
|
|
|
133
150
|
break;
|
|
134
151
|
case Object:
|
|
135
|
-
const isObject = _validations.isObject(value)
|
|
152
|
+
const isObject = _validations.isObject(value) && value.constructor === Object
|
|
136
153
|
result.push({
|
|
137
154
|
key: keyName,
|
|
138
155
|
passed: isObject,
|
|
@@ -150,7 +167,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
150
167
|
|
|
151
168
|
break;
|
|
152
169
|
case null:
|
|
153
|
-
const isNull =
|
|
170
|
+
const isNull = value === null
|
|
171
|
+
|
|
154
172
|
result.push({
|
|
155
173
|
key: keyName,
|
|
156
174
|
passed: isNull,
|
|
@@ -162,7 +180,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
|
|
|
162
180
|
result.push({
|
|
163
181
|
key: keyName,
|
|
164
182
|
passed: false,
|
|
165
|
-
details:
|
|
183
|
+
details: `Тип '${keyName}' не определен`
|
|
166
184
|
})
|
|
167
185
|
}
|
|
168
186
|
|
package/tests/types.test.js
CHANGED
|
@@ -280,6 +280,10 @@ describe("Проверка всех типов сразу", () => {
|
|
|
280
280
|
},
|
|
281
281
|
date: {
|
|
282
282
|
type: Date
|
|
283
|
+
},
|
|
284
|
+
test: {
|
|
285
|
+
type: Object,
|
|
286
|
+
required: true
|
|
283
287
|
}
|
|
284
288
|
})
|
|
285
289
|
|
|
@@ -290,7 +294,8 @@ describe("Проверка всех типов сразу", () => {
|
|
|
290
294
|
arr: null,
|
|
291
295
|
null: {},
|
|
292
296
|
bool: new Date(),
|
|
293
|
-
date: true
|
|
297
|
+
date: true,
|
|
298
|
+
test: undefined
|
|
294
299
|
}
|
|
295
300
|
|
|
296
301
|
const res = schema.validate(obj)
|
package/src/app.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
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
|
-
val3: {
|
|
49
|
-
type: String,
|
|
50
|
-
required: true,
|
|
51
|
-
},
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
const testObj = {
|
|
55
|
-
val: 'string',
|
|
56
|
-
val2: 1,
|
|
57
|
-
val3: 'ss'
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const resAll = schema.validate(testObj)
|
|
61
|
-
const res = schema.validate(testObj, ['val', 'val2'])
|
|
62
|
-
const res2 = schema.validate(testObj, 'val2')
|
|
63
|
-
|
|
64
|
-
console.log(resAll)
|
|
65
|
-
console.log(res)
|
|
66
|
-
console.log(res2)
|
package/tests/rules.test.js
DELETED
|
File without changes
|