validno 0.1.11 → 0.2.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/checkType.js +10 -25
- package/dist/dev.js +40 -29
- package/dist/utils/errors.js +14 -0
- package/dist/validate.js +4 -7
- package/package.json +2 -1
package/dist/checkType.js
CHANGED
|
@@ -1,31 +1,12 @@
|
|
|
1
1
|
import { ErrorKeywords } from "./constants/details.js";
|
|
2
2
|
import _validations from "./utils/validations.js";
|
|
3
|
-
|
|
4
|
-
let receivedType = '';
|
|
5
|
-
if (typeof receivedValue === 'string')
|
|
6
|
-
receivedType = 'String';
|
|
7
|
-
else if (typeof receivedValue === 'number')
|
|
8
|
-
receivedType = 'Number';
|
|
9
|
-
else if (typeof receivedValue === 'boolean')
|
|
10
|
-
receivedType = 'Noolean';
|
|
11
|
-
else if (receivedValue === null)
|
|
12
|
-
receivedType = 'null';
|
|
13
|
-
else if (Array.isArray(receivedValue))
|
|
14
|
-
receivedType = 'Array';
|
|
15
|
-
else if (_validations.isDate(receivedValue))
|
|
16
|
-
receivedType = 'Date';
|
|
17
|
-
else if (_validations.isObject(receivedValue))
|
|
18
|
-
receivedType = 'Object';
|
|
19
|
-
else if (receivedValue === undefined)
|
|
20
|
-
receivedType = 'undefined';
|
|
21
|
-
return `Проверьте тип "${key}": ожидался ${(expectedType === null || expectedType === void 0 ? void 0 : expectedType.name) || expectedType}, получен ${receivedType || 'unknown'}`;
|
|
22
|
-
};
|
|
3
|
+
import _errors from "./utils/errors.js";
|
|
23
4
|
const checkTypeMultiple = (key, value, requirements, keyName = key) => {
|
|
24
5
|
const constructorNames = requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el));
|
|
25
6
|
const result = {
|
|
26
7
|
key: keyName,
|
|
27
8
|
passed: false,
|
|
28
|
-
details: getErrorDetails(keyName, constructorNames.join('/'), value)
|
|
9
|
+
details: _errors.getErrorDetails(keyName, constructorNames.join('/'), value)
|
|
29
10
|
};
|
|
30
11
|
let i = 0;
|
|
31
12
|
while (i < requirements.type.length) {
|
|
@@ -69,9 +50,10 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
69
50
|
schema: null
|
|
70
51
|
}) :
|
|
71
52
|
null;
|
|
72
|
-
const baseErrDetails = getErrorDetails(keyName, requirements.type, value);
|
|
53
|
+
const baseErrDetails = _errors.getErrorDetails(keyName, requirements.type, value);
|
|
73
54
|
const getDetails = (isOK) => isOK ? 'OK' : customErrDetails || baseErrDetails;
|
|
74
|
-
|
|
55
|
+
const typeBySchema = requirements.type;
|
|
56
|
+
switch (typeBySchema) {
|
|
75
57
|
case 'any':
|
|
76
58
|
result.push({
|
|
77
59
|
key: keyName,
|
|
@@ -166,10 +148,13 @@ const checkType = (key, value, requirements, keyName = key) => {
|
|
|
166
148
|
});
|
|
167
149
|
break;
|
|
168
150
|
default:
|
|
151
|
+
const isInstanceOf = value instanceof typeBySchema;
|
|
152
|
+
const isConstructorSame = value.constructor.name === typeBySchema.name;
|
|
153
|
+
const checked = isInstanceOf && isConstructorSame;
|
|
169
154
|
result.push({
|
|
170
155
|
key: keyName,
|
|
171
|
-
passed:
|
|
172
|
-
details:
|
|
156
|
+
passed: checked,
|
|
157
|
+
details: getDetails(checked)
|
|
173
158
|
});
|
|
174
159
|
}
|
|
175
160
|
return result;
|
package/dist/dev.js
CHANGED
|
@@ -1,41 +1,52 @@
|
|
|
1
1
|
import { Schema } from "./Schema.js";
|
|
2
|
+
class Parent {
|
|
3
|
+
constructor(value) {
|
|
4
|
+
this.value = value;
|
|
5
|
+
this.version = 1;
|
|
6
|
+
}
|
|
7
|
+
getV() {
|
|
8
|
+
return this.version;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
class Child extends Parent {
|
|
12
|
+
constructor(value) {
|
|
13
|
+
super(value);
|
|
14
|
+
this.isDeep = true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const testCust = new Parent(12345678);
|
|
18
|
+
const testCust2 = new Child(12345678);
|
|
2
19
|
const schema = new Schema({
|
|
3
|
-
|
|
4
|
-
type:
|
|
20
|
+
parent1: {
|
|
21
|
+
type: Parent,
|
|
5
22
|
required: true,
|
|
6
|
-
title: '
|
|
7
|
-
customMessage: (input) => {
|
|
8
|
-
return 'Wrong type 1 msg ' + input.keyword;
|
|
9
|
-
}
|
|
23
|
+
title: 'custom class test',
|
|
10
24
|
},
|
|
11
|
-
|
|
12
|
-
type:
|
|
25
|
+
child1: {
|
|
26
|
+
type: Child,
|
|
13
27
|
required: true,
|
|
14
|
-
title: '
|
|
15
|
-
customMessage: (input) => {
|
|
16
|
-
return 'Missing msg ' + input.keyword;
|
|
17
|
-
}
|
|
28
|
+
title: 'custom class test',
|
|
18
29
|
},
|
|
19
|
-
|
|
20
|
-
type:
|
|
30
|
+
parent2: {
|
|
31
|
+
type: Parent,
|
|
32
|
+
required: true
|
|
33
|
+
},
|
|
34
|
+
err: {
|
|
35
|
+
type: Error,
|
|
21
36
|
required: true,
|
|
22
|
-
title: '
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
37
|
+
title: 'custom class test',
|
|
38
|
+
},
|
|
39
|
+
func: {
|
|
40
|
+
type: Function,
|
|
41
|
+
required: false,
|
|
26
42
|
}
|
|
27
43
|
});
|
|
28
44
|
const obj = {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
ops2: 'str',
|
|
35
|
-
miss: 'here'
|
|
45
|
+
parent1: testCust,
|
|
46
|
+
child1: testCust2,
|
|
47
|
+
parent2: testCust2,
|
|
48
|
+
err: new Error('ops'),
|
|
49
|
+
func: () => { console.log('ya tut'); }
|
|
36
50
|
};
|
|
37
51
|
const res = schema.validate(obj);
|
|
38
|
-
|
|
39
|
-
console.log('finalResult');
|
|
40
|
-
console.log(res2);
|
|
41
|
-
console.log(res2.joinErrors());
|
|
52
|
+
console.log(res);
|
package/dist/utils/errors.js
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
1
|
const _errors = {};
|
|
2
2
|
_errors.getMissingError = (key) => `Ключ '${key}' отсутствует`;
|
|
3
|
+
_errors.getErrorDetails = (key, expectedType, receivedValue) => {
|
|
4
|
+
var _a;
|
|
5
|
+
let receivedType = '';
|
|
6
|
+
if (receivedValue === undefined)
|
|
7
|
+
receivedType = 'undefined';
|
|
8
|
+
else if (receivedValue === null)
|
|
9
|
+
receivedType = 'null';
|
|
10
|
+
else
|
|
11
|
+
receivedType = ((_a = receivedValue.constructor) === null || _a === void 0 ? void 0 : _a.name) || typeof receivedValue || 'na';
|
|
12
|
+
return `Проверьте тип "${key}": ожидался ${(expectedType === null || expectedType === void 0 ? void 0 : expectedType.name) || expectedType}, получен ${receivedType || 'unknown'}`;
|
|
13
|
+
};
|
|
14
|
+
_errors.joinErrors = (errorsArr, separator = '; ') => {
|
|
15
|
+
return (errorsArr === null || errorsArr === void 0 ? void 0 : errorsArr.join(`${separator}`)) || '';
|
|
16
|
+
};
|
|
3
17
|
export default _errors;
|
package/dist/validate.js
CHANGED
|
@@ -4,10 +4,6 @@ import checkRules from "./checkRules.js";
|
|
|
4
4
|
import _validations from "./utils/validations.js";
|
|
5
5
|
import { ErrorKeywords } from "./constants/details.js";
|
|
6
6
|
import { defaultSchemaKeys } from "./Schema.js";
|
|
7
|
-
function joinErrors(separator = ';') {
|
|
8
|
-
var _a;
|
|
9
|
-
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.join(`${separator} `)) || '';
|
|
10
|
-
}
|
|
11
7
|
export const getResultDefaults = () => {
|
|
12
8
|
return {
|
|
13
9
|
ok: null,
|
|
@@ -66,7 +62,9 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
|
|
|
66
62
|
}
|
|
67
63
|
return results;
|
|
68
64
|
}
|
|
69
|
-
if (reqs.required === true &&
|
|
65
|
+
if (reqs.required === true &&
|
|
66
|
+
(key in data === false || data === undefined || data[key] === undefined)) {
|
|
67
|
+
console.log(data);
|
|
70
68
|
let errMsg = _errors.getMissingError(deepKey);
|
|
71
69
|
if (reqs.customMessage && typeof reqs.customMessage === 'function') {
|
|
72
70
|
errMsg = reqs.customMessage({
|
|
@@ -151,8 +149,7 @@ class ValidnoResult {
|
|
|
151
149
|
this.byKeys = results.byKeys;
|
|
152
150
|
}
|
|
153
151
|
joinErrors(separator = '; ') {
|
|
154
|
-
|
|
155
|
-
return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.join(`${separator} `)) || '';
|
|
152
|
+
return _errors.joinErrors(this.errors, separator);
|
|
156
153
|
}
|
|
157
154
|
}
|
|
158
155
|
export default validate;
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "validno",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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
|
+
"tscw": "tsc --b --watch src/",
|
|
9
10
|
"dev": "nodemon dist/dev.js",
|
|
10
11
|
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
11
12
|
},
|