validno 0.1.1 → 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 CHANGED
@@ -9,7 +9,7 @@ export class Schema {
9
9
  constructor(inputSchema) {
10
10
  this.schema = inputSchema;
11
11
  }
12
- validate(data) {
13
- return validate.call(this, this, data);
12
+ validate(data, keys) {
13
+ return validate.call(this, this, data, keys);
14
14
  }
15
15
  }
package/dist/app.js ADDED
@@ -0,0 +1,29 @@
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
+ val3: {
15
+ type: String,
16
+ required: true,
17
+ },
18
+ });
19
+ const testObj = {
20
+ val: 'string',
21
+ val2: 1,
22
+ val3: 'ss'
23
+ };
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);
28
+ console.log(res);
29
+ console.log(res2);
@@ -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 = [];
@@ -101,11 +101,17 @@ 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 validate = (schema, data) => {
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
- const keyResult = handleReqKey(key, data, reqs);
108
- results = mergeResults(results, keyResult);
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;
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/Schema.ts CHANGED
@@ -34,7 +34,7 @@ export class Schema {
34
34
  this.schema = inputSchema
35
35
  }
36
36
 
37
- validate(data: any) {
38
- return validate.call(this, this, data)
37
+ validate(data: any, keys?: string | string[]) {
38
+ return validate.call(this, this, data, keys)
39
39
  }
40
40
  }
package/src/app.ts ADDED
@@ -0,0 +1,66 @@
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/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
 
@@ -154,14 +154,21 @@ const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key)
154
154
  return results
155
155
  }
156
156
 
157
- const validate = (schema: Schema, data: any): TResult => {
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
- // @ts-ignore
162
- const keyResult = handleReqKey(key, data, reqs)
166
+ if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
167
+ // @ts-ignore
168
+ const keyResult = handleReqKey(key, data, reqs)
163
169
 
164
- results = mergeResults(results, keyResult)
170
+ results = mergeResults(results, keyResult)
171
+ }
165
172
  }
166
173
 
167
174
  if (results.failed.length) results.ok = false