validno 0.1.2 → 0.1.3
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 +2 -6
- package/dist/app.js +11 -4
- package/dist/validate.js +9 -3
- package/package.json +1 -1
- package/src/Schema.ts +2 -7
- package/src/app.ts +11 -4
- package/src/validate.ts +12 -5
- package/src/validateKey.ts +0 -21
package/dist/Schema.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import validate from "./validate.js";
|
|
2
|
-
import validateKey from "./validateKey.js";
|
|
3
2
|
export const defaultSchemaKeys = [
|
|
4
3
|
"required",
|
|
5
4
|
"type",
|
|
@@ -10,10 +9,7 @@ export class Schema {
|
|
|
10
9
|
constructor(inputSchema) {
|
|
11
10
|
this.schema = inputSchema;
|
|
12
11
|
}
|
|
13
|
-
validate(data) {
|
|
14
|
-
return validate.call(this, this, data);
|
|
15
|
-
}
|
|
16
|
-
validateKey(key, data) {
|
|
17
|
-
return validateKey.call(this, this, data, key);
|
|
12
|
+
validate(data, keys) {
|
|
13
|
+
return validate.call(this, this, data, keys);
|
|
18
14
|
}
|
|
19
15
|
}
|
package/dist/app.js
CHANGED
|
@@ -10,13 +10,20 @@ const schema = new Schema({
|
|
|
10
10
|
rules: {
|
|
11
11
|
min: 3
|
|
12
12
|
}
|
|
13
|
-
}
|
|
13
|
+
},
|
|
14
|
+
val3: {
|
|
15
|
+
type: String,
|
|
16
|
+
required: true,
|
|
17
|
+
},
|
|
14
18
|
});
|
|
15
19
|
const testObj = {
|
|
16
20
|
val: 'string',
|
|
17
|
-
val2: 1
|
|
21
|
+
val2: 1,
|
|
22
|
+
val3: 'ss'
|
|
18
23
|
};
|
|
19
|
-
const
|
|
20
|
-
const
|
|
24
|
+
const resAll = schema.validate(testObj);
|
|
25
|
+
const res = schema.validate(testObj, ['val', 'val2']);
|
|
26
|
+
const res2 = schema.validate(testObj, 'val2');
|
|
27
|
+
console.log(resAll);
|
|
21
28
|
console.log(res);
|
|
22
29
|
console.log(res2);
|
package/dist/validate.js
CHANGED
|
@@ -101,11 +101,17 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
|
|
|
101
101
|
results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
|
|
102
102
|
return results;
|
|
103
103
|
};
|
|
104
|
-
const
|
|
104
|
+
const isCheckNeeded = (key, hasLimits, onlyKeys) => {
|
|
105
|
+
return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
|
|
106
|
+
};
|
|
107
|
+
const validate = (schema, data, onlyKeys) => {
|
|
105
108
|
let results = getResultDefaults();
|
|
109
|
+
const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0);
|
|
106
110
|
for (const [key, reqs] of Object.entries(schema.schema)) {
|
|
107
|
-
|
|
108
|
-
|
|
111
|
+
if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
|
|
112
|
+
const keyResult = handleReqKey(key, data, reqs);
|
|
113
|
+
results = mergeResults(results, keyResult);
|
|
114
|
+
}
|
|
109
115
|
}
|
|
110
116
|
if (results.failed.length)
|
|
111
117
|
results.ok = false;
|
package/package.json
CHANGED
package/src/Schema.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import validate from "./validate.js"
|
|
2
|
-
import validateKey from "./validateKey.js"
|
|
3
2
|
|
|
4
3
|
export type TSchemaItem = {
|
|
5
4
|
required: boolean,
|
|
@@ -35,11 +34,7 @@ export class Schema {
|
|
|
35
34
|
this.schema = inputSchema
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
validate(data: any) {
|
|
39
|
-
return validate.call(this, this, data)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
validateKey(key: string, data: any) {
|
|
43
|
-
return validateKey.call(this, this, data, key)
|
|
37
|
+
validate(data: any, keys?: string | string[]) {
|
|
38
|
+
return validate.call(this, this, data, keys)
|
|
44
39
|
}
|
|
45
40
|
}
|
package/src/app.ts
CHANGED
|
@@ -44,16 +44,23 @@ const schema = new Schema({
|
|
|
44
44
|
rules: {
|
|
45
45
|
min: 3
|
|
46
46
|
}
|
|
47
|
-
}
|
|
47
|
+
},
|
|
48
|
+
val3: {
|
|
49
|
+
type: String,
|
|
50
|
+
required: true,
|
|
51
|
+
},
|
|
48
52
|
})
|
|
49
53
|
|
|
50
54
|
const testObj = {
|
|
51
55
|
val: 'string',
|
|
52
|
-
val2: 1
|
|
56
|
+
val2: 1,
|
|
57
|
+
val3: 'ss'
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
const
|
|
56
|
-
const
|
|
60
|
+
const resAll = schema.validate(testObj)
|
|
61
|
+
const res = schema.validate(testObj, ['val', 'val2'])
|
|
62
|
+
const res2 = schema.validate(testObj, 'val2')
|
|
57
63
|
|
|
64
|
+
console.log(resAll)
|
|
58
65
|
console.log(res)
|
|
59
66
|
console.log(res2)
|
package/src/validate.ts
CHANGED
|
@@ -154,14 +154,21 @@ export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey
|
|
|
154
154
|
return results
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
const
|
|
157
|
+
const isCheckNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | string[]) => {
|
|
158
|
+
return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && onlyKeys?.includes(key))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const validate = (schema: Schema, data: any, onlyKeys?: string | string[]): TResult => {
|
|
158
162
|
let results: TResult = getResultDefaults()
|
|
159
|
-
|
|
163
|
+
const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0)
|
|
164
|
+
|
|
160
165
|
for (const [key, reqs] of Object.entries(schema.schema)) {
|
|
161
|
-
|
|
162
|
-
|
|
166
|
+
if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
const keyResult = handleReqKey(key, data, reqs)
|
|
163
169
|
|
|
164
|
-
|
|
170
|
+
results = mergeResults(results, keyResult)
|
|
171
|
+
}
|
|
165
172
|
}
|
|
166
173
|
|
|
167
174
|
if (results.failed.length) results.ok = false
|
package/src/validateKey.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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;
|