validno 0.1.3 → 0.1.5

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.
@@ -12,8 +12,8 @@ const ensureRuleHasCorrectType = (value, allowedTypes) => {
12
12
  return isInAllowedList;
13
13
  };
14
14
  const rulesFunctions = {
15
- custom: (key, val, func) => {
16
- return func(val);
15
+ custom: (key, val, func, extra) => {
16
+ return func(val, extra);
17
17
  },
18
18
  isEmail: (key, val) => {
19
19
  return {
@@ -30,7 +30,7 @@ const rulesFunctions = {
30
30
  isNot: (key, val, notEqualTo) => {
31
31
  return {
32
32
  result: _validations.isNot(val, notEqualTo),
33
- details: `Значение не должно быть "${notEqualTo}"`
33
+ details: `Значение "${notEqualTo}" недопустимо`
34
34
  };
35
35
  },
36
36
  min: (key, val, min) => {
@@ -91,37 +91,48 @@ const rulesFunctions = {
91
91
  };
92
92
  },
93
93
  enum: (key, value, allowedList) => {
94
- return {
95
- result: allowedList.includes(value),
96
- details: `Значение "${value}" не допустимо`
94
+ const output = {
95
+ result: true,
96
+ details: ''
97
97
  };
98
+ if (!Array.isArray(value)) {
99
+ const isCorrect = allowedList.includes(value);
100
+ output.result = isCorrect,
101
+ output.details = isCorrect ? '' : `Значение "${value}" недопустимо`;
102
+ }
103
+ else {
104
+ const incorrectValues = [];
105
+ value.forEach((v) => !allowedList.includes(v) ? incorrectValues.push(v) : {});
106
+ const isCorrect = incorrectValues.length === 0;
107
+ output.result = isCorrect,
108
+ output.details = isCorrect ? '' : `Значения недопустимы: "${incorrectValues.join(', ')}"`;
109
+ }
110
+ return output;
98
111
  }
99
112
  };
100
- const checkRules = (key, value, requirements) => {
113
+ function checkRules(key, value, requirements, inputObj) {
101
114
  const result = {
102
115
  ok: true,
103
116
  details: []
104
117
  };
105
118
  if (requirements.required !== true && value === undefined)
106
119
  return result;
107
- if ('rules' in requirements === false)
108
- return result;
109
120
  if (!requirements || !requirements.rules || !Object.keys(requirements.rules).length) {
110
121
  return result;
111
122
  }
112
123
  const rules = requirements.rules;
113
124
  const allResults = [];
114
- const allRules = Object.keys(rules);
125
+ const allRulesKeys = Object.keys(rules);
115
126
  let i = 0;
116
- while (i < allRules.length) {
117
- const ruleName = allRules[i];
127
+ while (i < allRulesKeys.length) {
128
+ const ruleName = allRulesKeys[i];
118
129
  if (ruleName in rulesFunctions === false) {
119
130
  i++;
120
131
  continue;
121
132
  }
122
133
  const func = rulesFunctions[ruleName];
123
134
  const args = rules[ruleName];
124
- const result = func(key, value, args);
135
+ const result = func(key, value, args, { schema: this.schema, input: inputObj });
125
136
  allResults.push(result);
126
137
  i++;
127
138
  }
@@ -131,5 +142,6 @@ const checkRules = (key, value, requirements) => {
131
142
  result.details = failedResults.map(el => el.details);
132
143
  }
133
144
  return result;
134
- };
145
+ }
146
+ ;
135
147
  export default checkRules;
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 = _validations.isNumber(value);
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 = _validations.isString(value);
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 = _validations.isDate(value);
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 = _validations.isBoolean(value);
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 = _validations.isArray(value);
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 = _validations.isNull(value);
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: `No type specified for key: '${keyName}'`
155
+ details: `Тип '${keyName}' не определен`
147
156
  });
148
157
  }
149
158
  return result;
package/dist/validate.js CHANGED
@@ -35,7 +35,7 @@ export const mergeResults = (resultsOld, resultsNew) => {
35
35
  output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
36
36
  return output;
37
37
  };
38
- export const handleReqKey = (key, data, reqs, deepKey = key) => {
38
+ export function handleReqKey(key, data, reqs, deepKey = key) {
39
39
  let results = getResultDefaults();
40
40
  const hasNested = checkIsNested(reqs);
41
41
  const missedCheck = [];
@@ -54,7 +54,7 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
54
54
  let i = 0;
55
55
  while (i < nestedReqKeys.length) {
56
56
  const reqKeyI = nestedReqKeys[i];
57
- const deepResults = handleReqKey(reqKeyI, data[key], reqs[reqKeyI], deepKey + '.' + reqKeyI);
57
+ const deepResults = handleReqKey.call(this, reqKeyI, data[key], reqs[reqKeyI], deepKey + '.' + reqKeyI);
58
58
  results = mergeResults(results, deepResults);
59
59
  i++;
60
60
  }
@@ -77,7 +77,7 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
77
77
  results.errors.push(res.details);
78
78
  }
79
79
  });
80
- const ruleCheck = checkRules(deepKey, data[key], reqs);
80
+ const ruleCheck = checkRules.call(this, deepKey, data[key], reqs, data);
81
81
  if (!ruleCheck.ok) {
82
82
  rulesChecked.push(false);
83
83
  ruleCheck.details.forEach((el) => {
@@ -100,16 +100,16 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
100
100
  ];
101
101
  results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
102
102
  return results;
103
- };
103
+ }
104
104
  const isCheckNeeded = (key, hasLimits, onlyKeys) => {
105
105
  return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
106
106
  };
107
- const validate = (schema, data, onlyKeys) => {
107
+ function validate(schema, data, onlyKeys) {
108
108
  let results = getResultDefaults();
109
109
  const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0);
110
110
  for (const [key, reqs] of Object.entries(schema.schema)) {
111
111
  if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
112
- const keyResult = handleReqKey(key, data, reqs);
112
+ const keyResult = handleReqKey.call(this, key, data, reqs);
113
113
  results = mergeResults(results, keyResult);
114
114
  }
115
115
  }
@@ -118,5 +118,6 @@ const validate = (schema, data, onlyKeys) => {
118
118
  else
119
119
  results.ok = true;
120
120
  return results;
121
- };
121
+ }
122
+ ;
122
123
  export default validate;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
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/app.js",
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
@@ -1,4 +1,4 @@
1
- import { TSchemaItem } from "./Schema.js";
1
+ import { TSchema, TSchemaItem } from "./Schema.js";
2
2
  import _validations from "./utils/validations.js"
3
3
 
4
4
  export type TRule = {[key: string]: any}
@@ -21,8 +21,8 @@ const ensureRuleHasCorrectType = (value: any, allowedTypes: any[]) => {
21
21
  }
22
22
 
23
23
  const rulesFunctions: any = {
24
- custom: (key: string, val: any, func: Function) => {
25
- return func(val)
24
+ custom: (key: string, val: any, func: Function, extra: {schema: TSchema, input: {[key: string]: any}}) => {
25
+ return func(val, extra)
26
26
  },
27
27
  isEmail: (key: string, val: any) => {
28
28
  return {
@@ -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: `Значение не должно быть "${notEqualTo}"`
42
+ details: `Значение "${notEqualTo}" недопустимо`
43
43
  }
44
44
  },
45
45
  min: (key: string, val: number, min: number) => {
@@ -102,10 +102,25 @@ const rulesFunctions: any = {
102
102
  }
103
103
  },
104
104
  enum: (key: string, value: any, allowedList: any[]) => {
105
- return {
106
- result: allowedList.includes(value),
107
- details: `Значение "${value}" не допустимо`
105
+ const output = {
106
+ result: true,
107
+ details: ''
108
+ }
109
+
110
+ if (!Array.isArray(value)) {
111
+ const isCorrect = allowedList.includes(value)
112
+ output.result = isCorrect,
113
+ output.details = isCorrect ? '' : `Значение "${value}" недопустимо`
114
+ } else {
115
+ const incorrectValues: any[] = []
116
+ value.forEach((v: any) => !allowedList.includes(v) ? incorrectValues.push(v): {})
117
+ const isCorrect = incorrectValues.length === 0
118
+
119
+ output.result = isCorrect,
120
+ output.details = isCorrect ? '' : `Значения недопустимы: "${incorrectValues.join(', ')}"`
108
121
  }
122
+
123
+ return output
109
124
  }
110
125
  };
111
126
 
@@ -114,17 +129,16 @@ type TRulesResult = {
114
129
  details: string[]
115
130
  }
116
131
 
117
- const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
132
+ function checkRules (this: any, key: string, value: any, requirements: TSchemaItem, inputObj: any) {
118
133
  const result: TRulesResult = {
119
134
  ok: true,
120
135
  details: []
121
136
  };
122
137
 
123
- // If value is not provided AND not required by schema
138
+ // Значение отсутствует, но НЕ является обязательным
124
139
  if (requirements.required !== true && value === undefined) return result
125
140
 
126
- // No rules specified for the key
127
- if ('rules' in requirements === false) return result
141
+ // Для этого ключа нет правил
128
142
  if (!requirements || !requirements.rules || !Object.keys(requirements.rules).length) {
129
143
  return result
130
144
  }
@@ -132,12 +146,13 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
132
146
  const rules: TRule = requirements.rules
133
147
 
134
148
  const allResults = []
135
- const allRules = Object.keys(rules)
149
+ const allRulesKeys = Object.keys(rules)
136
150
 
137
151
  let i = 0;
138
- while (i < allRules.length) {
139
- const ruleName = allRules[i]
152
+ while (i < allRulesKeys.length) {
153
+ const ruleName = allRulesKeys[i]
140
154
 
155
+ // Если правило, указанное для ключа, отсутствует в списке доступных
141
156
  if (ruleName in rulesFunctions === false) {
142
157
  i++
143
158
  continue
@@ -146,7 +161,7 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
146
161
  const func = rulesFunctions[ruleName]
147
162
  const args = rules[ruleName]
148
163
 
149
- const result = func(key, value, args)
164
+ const result = func(key, value, args, {schema: this.schema, input: inputObj})
150
165
  allResults.push(result)
151
166
 
152
167
  i++;
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 = _validations.isNumber(value)
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 = _validations.isString(value);
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 = _validations.isDate(value)
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 = _validations.isBoolean(value);
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 = _validations.isArray(value);
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 = _validations.isNull(value);
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: `No type specified for key: '${keyName}'`
183
+ details: `Тип '${keyName}' не определен`
166
184
  })
167
185
  }
168
186
 
package/src/validate.ts CHANGED
@@ -51,10 +51,10 @@ export const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
51
51
  return output
52
52
  }
53
53
 
54
- export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key) => {
54
+ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInput, deepKey = key) {
55
55
  let results = getResultDefaults()
56
56
  const hasNested = checkIsNested(reqs)
57
-
57
+
58
58
  const missedCheck: boolean[] = [];
59
59
  const typeChecked: boolean[] = [];
60
60
  const rulesChecked: boolean[] = [];
@@ -80,7 +80,8 @@ export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey
80
80
  while (i < nestedReqKeys.length) {
81
81
  const reqKeyI: string = nestedReqKeys[i]
82
82
 
83
- const deepResults = handleReqKey(
83
+ const deepResults = handleReqKey.call(
84
+ this,
84
85
  reqKeyI,
85
86
  data[key],
86
87
  // @ts-ignore
@@ -122,7 +123,7 @@ export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey
122
123
 
123
124
  // Check all rules
124
125
  // @ts-ignore
125
- const ruleCheck = checkRules(deepKey, data[key], reqs);
126
+ const ruleCheck = checkRules.call(this, deepKey, data[key], reqs, data);
126
127
 
127
128
  if (!ruleCheck.ok) {
128
129
  rulesChecked.push(false)
@@ -158,14 +159,14 @@ const isCheckNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | stri
158
159
  return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && onlyKeys?.includes(key))
159
160
  }
160
161
 
161
- const validate = (schema: Schema, data: any, onlyKeys?: string | string[]): TResult => {
162
+ function validate(schema: Schema, data: any, onlyKeys?: string | string[]): TResult {
162
163
  let results: TResult = getResultDefaults()
163
164
  const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0)
164
165
 
165
166
  for (const [key, reqs] of Object.entries(schema.schema)) {
166
167
  if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
167
168
  // @ts-ignore
168
- const keyResult = handleReqKey(key, data, reqs)
169
+ const keyResult = handleReqKey.call(this, key, data, reqs)
169
170
 
170
171
  results = mergeResults(results, keyResult)
171
172
  }
@@ -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/dist/Validator.js DELETED
@@ -1,100 +0,0 @@
1
- import checkRules from "./checkRules.js";
2
- import checkType from "./checkType.js";
3
- import { defaultSchemaKeys } from "./Schema.js";
4
- const getResultDefault = () => {
5
- return {
6
- ok: null,
7
- missed: [],
8
- failed: [],
9
- passed: [],
10
- errors: [],
11
- byKeys: {}
12
- };
13
- };
14
- const checkIsNested = (obj) => {
15
- const objKeys = Object.keys(obj);
16
- if (objKeys.every((k) => defaultSchemaKeys.includes(k))) {
17
- return false;
18
- }
19
- else {
20
- return true;
21
- }
22
- };
23
- const handleReqKey = (key, data, reqs, deepKey = key) => {
24
- console.log('..................... ', key);
25
- const results = getResultDefault();
26
- const hasNested = checkIsNested(reqs);
27
- const missedCheck = [];
28
- const typeChecked = [];
29
- const rulesChecked = [];
30
- if (hasNested) {
31
- const nestedReqKeys = Object.keys(reqs);
32
- let i = 0;
33
- while (i < nestedReqKeys.length) {
34
- const reqKeyI = nestedReqKeys[i];
35
- const deepResults = handleReqKey(reqKeyI, data[key], reqs[reqKeyI], deepKey + '.' + reqKeyI);
36
- results.failed = [...results.failed, ...deepResults.failed];
37
- results.errors = [...results.errors, ...deepResults.errors];
38
- results.missed = [...results.missed, ...deepResults.missed];
39
- results.passed = [...results.passed, ...deepResults.passed];
40
- results.byKeys = Object.assign(Object.assign({}, results.byKeys), deepResults.byKeys);
41
- i++;
42
- }
43
- return results;
44
- }
45
- if (reqs.required === true && key in data === false) {
46
- missedCheck.push(false);
47
- results.errors.push(`Missing key '${deepKey}'`);
48
- return results;
49
- }
50
- const typeCheck = checkType(key, data[key], reqs, deepKey);
51
- typeCheck.forEach(res => {
52
- if (res.passed === false) {
53
- typeChecked.push(res.passed);
54
- results.errors.push(res.details);
55
- }
56
- });
57
- const ruleCheck = checkRules(deepKey, data[key], reqs);
58
- if (!ruleCheck.ok) {
59
- rulesChecked.push(false);
60
- ruleCheck.details.forEach((el) => results.errors.push(el));
61
- }
62
- if (missedCheck.length) {
63
- results.missed.push(deepKey);
64
- }
65
- if (typeChecked.length || rulesChecked.length) {
66
- results.failed.push(deepKey);
67
- }
68
- else {
69
- results.passed.push(deepKey);
70
- }
71
- console.log(missedCheck.length, typeChecked.length, rulesChecked.length);
72
- results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
73
- console.log(results);
74
- return results;
75
- };
76
- const validate = (schema, data) => {
77
- const results = getResultDefault();
78
- for (const [key, reqs] of Object.entries(schema.schema)) {
79
- const keyResult = handleReqKey(key, data, reqs);
80
- results.failed = [...results.failed, ...keyResult.failed];
81
- results.errors = [...results.errors, ...keyResult.errors];
82
- results.missed = [...results.missed, ...keyResult.missed];
83
- results.passed = [...results.passed, ...keyResult.passed];
84
- results.byKeys = Object.assign(Object.assign({}, results.byKeys), keyResult.byKeys);
85
- }
86
- if (results.failed.length)
87
- results.ok = false;
88
- else
89
- results.ok = true;
90
- results.missed = [...new Set(results.missed)];
91
- results.failed = [...new Set(results.failed)];
92
- results.passed = [...new Set(results.passed)];
93
- results.errors = [...new Set(results.errors)];
94
- return results;
95
- };
96
- class Validator {
97
- constructor() { }
98
- }
99
- Validator.validate = validate;
100
- export default Validator;
package/dist/app.js DELETED
@@ -1,29 +0,0 @@
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);
package/dist/sss.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,15 +0,0 @@
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/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)
File without changes