validno 0.1.8 → 0.1.9

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.
@@ -121,6 +121,7 @@ function checkRules(key, value, requirements, inputObj) {
121
121
  return result;
122
122
  }
123
123
  const rules = requirements.rules;
124
+ const title = requirements.title || key;
124
125
  const allResults = [];
125
126
  const allRulesKeys = Object.keys(rules);
126
127
  let i = 0;
@@ -133,6 +134,17 @@ function checkRules(key, value, requirements, inputObj) {
133
134
  const func = rulesFunctions[ruleName];
134
135
  const args = rules[ruleName];
135
136
  const result = func(key, value, args, { schema: this.schema, input: inputObj });
137
+ if (requirements.customMessage && typeof requirements.customMessage === 'function') {
138
+ result.details = requirements.customMessage({
139
+ keyword: ruleName,
140
+ value: value,
141
+ key: key,
142
+ title: title,
143
+ reqs: requirements,
144
+ schema: this.schema,
145
+ rules: rules,
146
+ });
147
+ }
136
148
  allResults.push(result);
137
149
  i++;
138
150
  }
package/dist/checkType.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { ErrorKeywords } from "./constants/details.js";
1
2
  import _validations from "./utils/validations.js";
2
3
  const getErrorDetails = (key, expectedType, receivedValue) => {
3
4
  let receivedType = '';
@@ -32,7 +33,7 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
32
33
  const check = checkType(key, value, requirementsRe);
33
34
  if (check[0].passed === true) {
34
35
  result.passed = true;
35
- result.details = 'Passed';
36
+ result.details = 'OK';
36
37
  return result;
37
38
  }
38
39
  i++;
@@ -41,8 +42,10 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
41
42
  };
42
43
  const checkType = (key, value, requirements, keyName = key) => {
43
44
  const isNotNull = value !== null;
45
+ const keyTitle = 'title' in requirements ? requirements.title : keyName;
46
+ const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
44
47
  if (value === undefined && requirements.required) {
45
- return [{ key: keyName, passed: false, details: `Key ${keyName} is missing` }];
48
+ return [{ key: keyName, passed: false, details: `Значение "${keyName}" отсутствует` }];
46
49
  }
47
50
  let result = [];
48
51
  if (Array.isArray(requirements.type)) {
@@ -52,16 +55,28 @@ const checkType = (key, value, requirements, keyName = key) => {
52
55
  result.push({
53
56
  key: keyName,
54
57
  passed: true,
55
- details: 'Passed'
58
+ details: 'OK'
56
59
  });
57
60
  return result;
58
61
  }
62
+ const customErrDetails = hasCustomMessage ?
63
+ requirements.customMessage({
64
+ keyword: ErrorKeywords.Type,
65
+ value: value,
66
+ key: keyName,
67
+ title: keyTitle,
68
+ reqs: requirements,
69
+ schema: null
70
+ }) :
71
+ null;
72
+ const baseErrDetails = getErrorDetails(keyName, requirements.type, value);
73
+ const getDetails = (isOK) => isOK ? 'OK' : customErrDetails || baseErrDetails;
59
74
  switch (requirements.type) {
60
75
  case 'any':
61
76
  result.push({
62
77
  key: keyName,
63
78
  passed: true,
64
- details: 'Passed'
79
+ details: 'OK'
65
80
  });
66
81
  break;
67
82
  case Number:
@@ -69,7 +84,7 @@ const checkType = (key, value, requirements, keyName = key) => {
69
84
  result.push({
70
85
  key: keyName,
71
86
  passed: isNumber,
72
- details: isNumber ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
87
+ details: getDetails(isNumber)
73
88
  });
74
89
  break;
75
90
  case String:
@@ -77,17 +92,17 @@ const checkType = (key, value, requirements, keyName = key) => {
77
92
  result.push({
78
93
  key: keyName,
79
94
  passed: isString,
80
- details: isString ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
95
+ details: getDetails(isString)
81
96
  });
82
97
  break;
83
98
  case Date:
84
99
  const isDate = isNotNull && value.constructor === Date;
85
100
  const isValid = isDate && !isNaN(value.getTime());
86
- const errorMsg = isValid ? getErrorDetails(keyName, requirements.type, value) : 'Дата невалидна';
101
+ const errorMsg = isValid ? getDetails(isDate) : 'Дата невалидна';
87
102
  result.push({
88
103
  key: keyName,
89
104
  passed: isDate && isValid,
90
- details: isDate && isValid ? 'Passed' : errorMsg
105
+ details: isDate && isValid ? 'OK' : errorMsg
91
106
  });
92
107
  break;
93
108
  case Boolean:
@@ -95,7 +110,7 @@ const checkType = (key, value, requirements, keyName = key) => {
95
110
  result.push({
96
111
  key: keyName,
97
112
  passed: isBoolean,
98
- details: isBoolean ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
113
+ details: isBoolean ? 'OK' : getDetails(isBoolean)
99
114
  });
100
115
  break;
101
116
  case Array:
@@ -104,7 +119,7 @@ const checkType = (key, value, requirements, keyName = key) => {
104
119
  result.push({
105
120
  key: keyName,
106
121
  passed: false,
107
- details: getErrorDetails(keyName, requirements.type, value)
122
+ details: getDetails(isArray)
108
123
  });
109
124
  break;
110
125
  }
@@ -123,7 +138,7 @@ const checkType = (key, value, requirements, keyName = key) => {
123
138
  result.push({
124
139
  key: keyName,
125
140
  passed: isOk,
126
- details: isOk ? 'Passed' : !isEachChecked.passed ? isEachChecked.details : getErrorDetails(keyName, requirements.type, value)
141
+ details: isOk ? 'OK' : !isEachChecked.passed ? isEachChecked.details : getDetails(isOk)
127
142
  });
128
143
  break;
129
144
  case Object:
@@ -131,7 +146,7 @@ const checkType = (key, value, requirements, keyName = key) => {
131
146
  result.push({
132
147
  key: keyName,
133
148
  passed: isObject,
134
- details: isObject ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
149
+ details: isObject ? 'OK' : getDetails(isObject)
135
150
  });
136
151
  break;
137
152
  case RegExp:
@@ -139,7 +154,7 @@ const checkType = (key, value, requirements, keyName = key) => {
139
154
  result.push({
140
155
  key: keyName,
141
156
  passed: isRegex,
142
- details: isRegex ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
157
+ details: isRegex ? 'OK' : getDetails(isRegex)
143
158
  });
144
159
  break;
145
160
  case null:
@@ -147,7 +162,7 @@ const checkType = (key, value, requirements, keyName = key) => {
147
162
  result.push({
148
163
  key: keyName,
149
164
  passed: isNull,
150
- details: isNull ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
165
+ details: isNull ? 'OK' : getDetails(isNull)
151
166
  });
152
167
  break;
153
168
  default:
@@ -0,0 +1,6 @@
1
+ export var ErrorKeywords;
2
+ (function (ErrorKeywords) {
3
+ ErrorKeywords["Missing"] = "missing";
4
+ ErrorKeywords["Type"] = "type";
5
+ ErrorKeywords["Rule"] = "rule";
6
+ })(ErrorKeywords || (ErrorKeywords = {}));
package/dist/validate.js CHANGED
@@ -3,6 +3,7 @@ import checkType from "./checkType.js";
3
3
  import _errors from "./utils/errors.js";
4
4
  import _validations from "./utils/validations.js";
5
5
  import { defaultSchemaKeys } from "./Schema.js";
6
+ import { ErrorKeywords } from "./constants/details.js";
6
7
  export const getResultDefaults = () => {
7
8
  return {
8
9
  ok: null,
@@ -11,9 +12,7 @@ export const getResultDefaults = () => {
11
12
  passed: [],
12
13
  errors: [],
13
14
  byKeys: {},
14
- byTitles: {},
15
15
  errorsByKeys: {},
16
- errorsByTitles: {}
17
16
  };
18
17
  };
19
18
  const checkIsNested = (obj) => {
@@ -34,9 +33,7 @@ export const mergeResults = (resultsOld, resultsNew) => {
34
33
  output.missed = [...resultsOld.missed, ...resultsNew.missed];
35
34
  output.passed = [...resultsOld.passed, ...resultsNew.passed];
36
35
  output.byKeys = Object.assign(Object.assign({}, resultsOld.byKeys), resultsNew.byKeys);
37
- output.byTitles = Object.assign(Object.assign({}, resultsOld.byTitles), resultsNew.byTitles);
38
36
  output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
39
- output.errorsByTitles = Object.assign(Object.assign({}, resultsOld.errorsByTitles), resultsNew.errorsByTitles);
40
37
  return output;
41
38
  };
42
39
  export function handleReqKey(key, data, reqs, deepKey = key) {
@@ -51,13 +48,11 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
51
48
  results.missed.push(deepKey);
52
49
  results.failed.push(deepKey);
53
50
  results.byKeys[deepKey] = false;
54
- results.byTitles[keyTitle] = false;
55
51
  return results;
56
52
  }
57
53
  if (hasNested) {
58
54
  const nestedReqKeys = Object.keys(reqs);
59
55
  results.byKeys[deepKey] = true;
60
- results.byTitles[keyTitle] = true;
61
56
  let i = 0;
62
57
  while (i < nestedReqKeys.length) {
63
58
  const reqKeyI = nestedReqKeys[i];
@@ -71,7 +66,12 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
71
66
  let errMsg = _errors.getMissingError(deepKey);
72
67
  if (reqs.customMessage && typeof reqs.customMessage === 'function') {
73
68
  errMsg = reqs.customMessage({
74
- value: data[key], key: deepKey, title: keyTitle, reqs: reqs, schema: this.schema
69
+ keyword: ErrorKeywords.Missing,
70
+ value: data[key],
71
+ key: deepKey,
72
+ title: keyTitle,
73
+ reqs: reqs,
74
+ schema: this.schema
75
75
  });
76
76
  }
77
77
  missedCheck.push(false);
@@ -79,9 +79,6 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
79
79
  results.failed.push(deepKey);
80
80
  results.errors.push(errMsg);
81
81
  results.byKeys[deepKey] = false;
82
- results.byTitles[keyTitle] = false;
83
- results.errorsByKeys[deepKey] = [errMsg];
84
- results.errorsByTitles[keyTitle] = [errMsg];
85
82
  return results;
86
83
  }
87
84
  const typeCheck = checkType(key, data[key], reqs, deepKey);
@@ -97,11 +94,8 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
97
94
  ruleCheck.details.forEach((el) => {
98
95
  if (deepKey in results.errorsByKeys)
99
96
  results.errorsByKeys[deepKey] = [];
100
- if (deepKey in results.errorsByTitles)
101
- results.errorsByTitles[keyTitle] = [];
102
97
  results.errors.push(el);
103
98
  results.errorsByKeys[deepKey] = ['1'];
104
- results.errorsByTitles[keyTitle] = ['1'];
105
99
  });
106
100
  }
107
101
  if (missedCheck.length)
@@ -115,21 +109,18 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
115
109
  results.errorsByKeys[deepKey] = [
116
110
  ...results.errors
117
111
  ];
118
- results.errorsByTitles[keyTitle] = [
119
- ...results.errors
120
- ];
121
112
  results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
122
- results.byTitles[keyTitle] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
123
113
  return results;
124
114
  }
125
- const isCheckNeeded = (key, hasLimits, onlyKeys) => {
115
+ const checkIfValidationIsNeeded = (key, hasLimits, onlyKeys) => {
126
116
  return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
127
117
  };
128
118
  function validate(schema, data, onlyKeys) {
129
119
  let results = getResultDefaults();
130
120
  const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0);
131
121
  for (const [key, reqs] of Object.entries(schema.schema)) {
132
- if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
122
+ const isValidationRequired = checkIfValidationIsNeeded(key, areKeysLimited, onlyKeys);
123
+ if (isValidationRequired) {
133
124
  const keyResult = handleReqKey.call(this, key, data, reqs);
134
125
  results = mergeResults(results, keyResult);
135
126
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/checkRules.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ErrorKeywords } from "./constants/details.js";
1
2
  import { TSchema, TSchemaItem } from "./Schema.js";
2
3
  import _validations from "./utils/validations.js"
3
4
 
@@ -144,6 +145,7 @@ function checkRules (this: any, key: string, value: any, requirements: TSchemaIt
144
145
  }
145
146
 
146
147
  const rules: TRule = requirements.rules
148
+ const title = requirements.title || key
147
149
 
148
150
  const allResults = []
149
151
  const allRulesKeys = Object.keys(rules)
@@ -162,6 +164,19 @@ function checkRules (this: any, key: string, value: any, requirements: TSchemaIt
162
164
  const args = rules[ruleName]
163
165
 
164
166
  const result = func(key, value, args, {schema: this.schema, input: inputObj})
167
+
168
+ if (requirements.customMessage && typeof requirements.customMessage === 'function') {
169
+ result.details = requirements.customMessage({
170
+ keyword: ruleName,
171
+ value: value,
172
+ key: key,
173
+ title: title,
174
+ reqs: requirements,
175
+ schema: this.schema,
176
+ rules: rules,
177
+ })
178
+ }
179
+
165
180
  allResults.push(result)
166
181
 
167
182
  i++;
package/src/checkType.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ErrorKeywords } from "./constants/details.js";
1
2
  import { TSchemaInput, TSchemaItem } from "./Schema.js";
2
3
  import _validations from "./utils/validations.js";
3
4
 
@@ -32,7 +33,7 @@ const checkTypeMultiple = (key: string, value: any, requirements: TSchemaItem |
32
33
 
33
34
  if (check[0].passed === true) {
34
35
  result.passed = true
35
- result.details = 'Passed'
36
+ result.details = 'OK'
36
37
  return result
37
38
  }
38
39
 
@@ -46,9 +47,11 @@ type TCheckTypeResult = {key: string, passed: boolean, details: string}
46
47
 
47
48
  const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaInput, keyName = key): TCheckTypeResult[] => {
48
49
  const isNotNull = value !== null
50
+ const keyTitle = 'title' in requirements ? requirements.title : keyName
51
+ const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function'
49
52
 
50
53
  if (value === undefined && requirements.required) {
51
- return [{key: keyName, passed: false, details: `Key ${keyName} is missing`}]
54
+ return [{key: keyName, passed: false, details: `Значение "${keyName}" отсутствует`}]
52
55
  }
53
56
 
54
57
  let result: TCheckTypeResult[] = []
@@ -61,17 +64,34 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
61
64
  result.push({
62
65
  key: keyName,
63
66
  passed: true,
64
- details: 'Passed'
67
+ details: 'OK'
65
68
  })
66
69
  return result
67
70
  }
71
+
72
+
73
+ const customErrDetails = hasCustomMessage ?
74
+ //@ts-ignore
75
+ requirements.customMessage({
76
+ keyword: ErrorKeywords.Type,
77
+ value: value,
78
+ key: keyName,
79
+ title: keyTitle,
80
+ reqs: requirements,
81
+ schema: null
82
+ }) :
83
+ null;
84
+
85
+ const baseErrDetails = getErrorDetails(keyName, requirements.type, value)
68
86
 
87
+ const getDetails = (isOK: boolean) => isOK ? 'OK' : customErrDetails || baseErrDetails
88
+
69
89
  switch (requirements.type) {
70
90
  case 'any':
71
91
  result.push({
72
92
  key: keyName,
73
93
  passed: true,
74
- details: 'Passed'
94
+ details: 'OK'
75
95
  })
76
96
  break;
77
97
  case Number:
@@ -80,7 +100,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
80
100
  result.push({
81
101
  key: keyName,
82
102
  passed: isNumber,
83
- details: isNumber ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
103
+ details: getDetails(isNumber)
84
104
  })
85
105
 
86
106
  break;
@@ -90,18 +110,18 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
90
110
  result.push({
91
111
  key: keyName,
92
112
  passed: isString,
93
- details: isString ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
113
+ details: getDetails(isString)
94
114
  })
95
115
  break;
96
116
  case Date:
97
117
  const isDate = isNotNull && value.constructor === Date
98
118
  const isValid = isDate && !isNaN(value.getTime())
99
- const errorMsg = isValid ? getErrorDetails(keyName, requirements.type, value) : 'Дата невалидна'
119
+ const errorMsg = isValid ? getDetails(isDate) : 'Дата невалидна'
100
120
 
101
121
  result.push({
102
122
  key: keyName,
103
123
  passed: isDate && isValid,
104
- details: isDate && isValid ? 'Passed' : errorMsg
124
+ details: isDate && isValid ? 'OK' : errorMsg
105
125
  })
106
126
  break;
107
127
  case Boolean:
@@ -110,7 +130,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
110
130
  result.push({
111
131
  key: keyName,
112
132
  passed: isBoolean,
113
- details: isBoolean ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
133
+ details: isBoolean ? 'OK' : getDetails(isBoolean)
114
134
  })
115
135
  break;
116
136
  case Array:
@@ -120,7 +140,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
120
140
  result.push({
121
141
  key: keyName,
122
142
  passed: false,
123
- details: getErrorDetails(keyName, requirements.type, value)
143
+ details: getDetails(isArray)
124
144
  });
125
145
 
126
146
  break;
@@ -146,16 +166,17 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
146
166
  result.push({
147
167
  key: keyName,
148
168
  passed: isOk,
149
- details: isOk ? 'Passed' : !isEachChecked.passed ? isEachChecked.details : getErrorDetails(keyName, requirements.type, value)
169
+ details: isOk ? 'OK' : !isEachChecked.passed ? isEachChecked.details : getDetails(isOk)
150
170
  })
151
171
 
152
172
  break;
153
173
  case Object:
154
174
  const isObject = _validations.isObject(value) && value.constructor === Object
175
+
155
176
  result.push({
156
177
  key: keyName,
157
178
  passed: isObject,
158
- details: isObject ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
179
+ details: isObject ? 'OK' : getDetails(isObject)
159
180
  })
160
181
 
161
182
  break;
@@ -164,7 +185,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
164
185
  result.push({
165
186
  key: keyName,
166
187
  passed: isRegex,
167
- details: isRegex ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
188
+ details: isRegex ? 'OK' : getDetails(isRegex)
168
189
  })
169
190
 
170
191
  break;
@@ -174,7 +195,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
174
195
  result.push({
175
196
  key: keyName,
176
197
  passed: isNull,
177
- details: isNull ? 'Passed' : getErrorDetails(keyName, requirements.type, value)
198
+ details: isNull ? 'OK' : getDetails(isNull)
178
199
  })
179
200
 
180
201
  break;
@@ -0,0 +1,5 @@
1
+ export enum ErrorKeywords {
2
+ Missing = 'missing',
3
+ Type = 'type',
4
+ Rule = 'rule'
5
+ }
package/src/validate.ts CHANGED
@@ -3,6 +3,7 @@ import checkType from "./checkType.js";
3
3
  import _errors from "./utils/errors.js";
4
4
  import _validations from "./utils/validations.js";
5
5
  import { defaultSchemaKeys, Schema, TSchemaInput } from "./Schema.js";
6
+ import { ErrorKeywords } from "./constants/details.js";
6
7
 
7
8
  export type TResult = {
8
9
  ok: null | boolean,
@@ -11,9 +12,7 @@ export type TResult = {
11
12
  passed: string[],
12
13
  errors: string[],
13
14
  byKeys: {[key: string]: boolean},
14
- byTitles: {[key: string]: boolean},
15
15
  errorsByKeys: {[key: string]: string[]},
16
- errorsByTitles: {[key: string]: string[]}
17
16
  };
18
17
 
19
18
  export const getResultDefaults = (): TResult => {
@@ -24,9 +23,7 @@ export const getResultDefaults = (): TResult => {
24
23
  passed: [],
25
24
  errors: [],
26
25
  byKeys: {},
27
- byTitles: {},
28
26
  errorsByKeys: {},
29
- errorsByTitles: {}
30
27
  };
31
28
  }
32
29
 
@@ -50,9 +47,7 @@ export const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
50
47
  output.missed = [...resultsOld.missed, ...resultsNew.missed]
51
48
  output.passed = [...resultsOld.passed, ...resultsNew.passed]
52
49
  output.byKeys = {...resultsOld.byKeys, ...resultsNew.byKeys}
53
- output.byTitles = {...resultsOld.byTitles, ...resultsNew.byTitles}
54
50
  output.errorsByKeys = {...resultsOld.errorsByKeys, ...resultsNew.errorsByKeys}
55
- output.errorsByTitles = {...resultsOld.errorsByTitles, ...resultsNew.errorsByTitles}
56
51
 
57
52
  return output
58
53
  }
@@ -75,8 +70,7 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
75
70
  results.missed.push(deepKey)
76
71
  results.failed.push(deepKey)
77
72
  results.byKeys[deepKey] = false
78
- // @ts-ignore
79
- results.byTitles[keyTitle] = false
73
+
80
74
  return results
81
75
  }
82
76
 
@@ -84,8 +78,6 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
84
78
  if (hasNested) {
85
79
  const nestedReqKeys: string[] = Object.keys(reqs)
86
80
  results.byKeys[deepKey] = true
87
- // @ts-ignore
88
- results.byTitles[keyTitle] = true
89
81
 
90
82
  let i = 0;
91
83
  while (i < nestedReqKeys.length) {
@@ -112,11 +104,16 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
112
104
  // @ts-ignore
113
105
  if (reqs.required === true && key in data === false || data === undefined) {
114
106
  let errMsg = _errors.getMissingError(deepKey)
115
-
107
+
116
108
  if (reqs.customMessage && typeof reqs.customMessage === 'function') {
117
109
  // @ts-ignore
118
110
  errMsg = reqs.customMessage({
119
- value: data[key], key: deepKey, title: keyTitle, reqs: reqs, schema: this.schema
111
+ keyword: ErrorKeywords.Missing,
112
+ value: data[key],
113
+ key: deepKey,
114
+ title: keyTitle,
115
+ reqs: reqs,
116
+ schema: this.schema
120
117
  })
121
118
  }
122
119
 
@@ -125,11 +122,7 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
125
122
  results.failed.push(deepKey)
126
123
  results.errors.push(errMsg)
127
124
  results.byKeys[deepKey] = false
128
- // @ts-ignore
129
- results.byTitles[keyTitle] = false
130
- results.errorsByKeys[deepKey] = [errMsg]
131
- // @ts-ignore
132
- results.errorsByTitles[keyTitle] = [errMsg]
125
+
133
126
  return results
134
127
  }
135
128
 
@@ -151,14 +144,9 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
151
144
  rulesChecked.push(false)
152
145
  ruleCheck.details.forEach((el) => {
153
146
  if (deepKey in results.errorsByKeys) results.errorsByKeys[deepKey] = []
154
- // @ts-ignore
155
- if (deepKey in results.errorsByTitles) results.errorsByTitles[keyTitle] = []
156
147
 
157
148
  results.errors.push(el)
158
-
159
149
  results.errorsByKeys[deepKey] = ['1']
160
- // @ts-ignore
161
- results.errorsByTitles[keyTitle] = ['1']
162
150
  })
163
151
  }
164
152
 
@@ -175,24 +163,14 @@ export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInp
175
163
  ...results.errors
176
164
  ]
177
165
 
178
- // @ts-ignore
179
- results.errorsByTitles[keyTitle] = [
180
- ...results.errors
181
- ]
182
-
183
166
  results.byKeys[deepKey] = (
184
167
  missedCheck.length + typeChecked.length + rulesChecked.length
185
168
  ) === 0
186
169
 
187
- // @ts-ignore
188
- results.byTitles[keyTitle] = (
189
- missedCheck.length + typeChecked.length + rulesChecked.length
190
- ) === 0
191
-
192
170
  return results
193
171
  }
194
172
 
195
- const isCheckNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | string[]) => {
173
+ const checkIfValidationIsNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | string[]) => {
196
174
  return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && onlyKeys?.includes(key))
197
175
  }
198
176
 
@@ -201,7 +179,9 @@ function validate(schema: Schema, data: any, onlyKeys?: string | string[]): TRes
201
179
  const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0)
202
180
 
203
181
  for (const [key, reqs] of Object.entries(schema.schema)) {
204
- if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
182
+ const isValidationRequired = checkIfValidationIsNeeded(key, areKeysLimited, onlyKeys)
183
+
184
+ if (isValidationRequired) {
205
185
  // @ts-ignore
206
186
  const keyResult = handleReqKey.call(this, key, data, reqs)
207
187