validno 0.2.4 → 0.2.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.
package/dev.js ADDED
File without changes
package/dist/Schema.js CHANGED
@@ -1,18 +1,14 @@
1
+ import { ESchemaFields } from "./constants/schema.js";
1
2
  import validate from "./validate.js";
2
- export const defaultSchemaKeys = [
3
- "required",
4
- "type",
5
- "eachType",
6
- "rules",
7
- "title",
8
- "customMessage",
9
- "joinErrors"
10
- ];
3
+ export const defaultSchemaKeys = Object.values(ESchemaFields);
11
4
  export class Schema {
12
- constructor(inputSchema) {
13
- this.schema = inputSchema;
5
+ constructor(inputSchemaDefinition) {
6
+ if (!inputSchemaDefinition || typeof inputSchemaDefinition !== 'object') {
7
+ throw new Error("Invalid schema input");
8
+ }
9
+ this.schema = inputSchemaDefinition;
14
10
  }
15
- validate(data, keys) {
16
- return validate.call(this, this, data, keys);
11
+ validate(inputData, validationKeys) {
12
+ return validate.call(this, inputData, validationKeys);
17
13
  }
18
14
  }
package/dist/checkType.js CHANGED
@@ -2,22 +2,26 @@ import { ErrorKeywords } from "./constants/details.js";
2
2
  import _validations from "./utils/validations.js";
3
3
  import _errors from "./utils/errors.js";
4
4
  const checkTypeMultiple = (key, value, requirements, keyName = key) => {
5
- const constructorNames = requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el));
5
+ const constructorNames = Array.isArray(requirements.type)
6
+ ? requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el))
7
+ : [];
6
8
  const result = {
7
9
  key: keyName,
8
10
  passed: false,
9
11
  details: _errors.getErrorDetails(keyName, constructorNames.join('/'), value)
10
12
  };
11
13
  let i = 0;
12
- while (i < requirements.type.length) {
13
- const requirementsRe = Object.assign(Object.assign({}, requirements), { type: requirements.type[i] });
14
- const check = checkType(key, value, requirementsRe);
15
- if (check[0].passed === true) {
16
- result.passed = true;
17
- result.details = 'OK';
18
- return result;
14
+ if (Array.isArray(requirements.type)) {
15
+ while (i < requirements.type.length) {
16
+ const requirementsRe = Object.assign(Object.assign({}, requirements), { type: requirements.type[i] });
17
+ const check = checkType(key, value, requirementsRe);
18
+ if (check[0].passed === true) {
19
+ result.passed = true;
20
+ result.details = 'OK';
21
+ return result;
22
+ }
23
+ i++;
19
24
  }
20
- i++;
21
25
  }
22
26
  return result;
23
27
  };
@@ -149,8 +153,8 @@ const checkType = (key, value, requirements, keyName = key) => {
149
153
  });
150
154
  break;
151
155
  default:
152
- const isInstanceOf = value instanceof typeBySchema;
153
- const isConstructorSame = ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
156
+ const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
157
+ const isConstructorSame = typeof typeBySchema === 'function' && ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
154
158
  const checked = isInstanceOf && isConstructorSame;
155
159
  result.push({
156
160
  key: keyName,
@@ -0,0 +1,10 @@
1
+ export var ESchemaFields;
2
+ (function (ESchemaFields) {
3
+ ESchemaFields["Required"] = "required";
4
+ ESchemaFields["Type"] = "type";
5
+ ESchemaFields["EachType"] = "eachType";
6
+ ESchemaFields["Rules"] = "rules";
7
+ ESchemaFields["Title"] = "title";
8
+ ESchemaFields["CustomMessage"] = "customMessage";
9
+ ESchemaFields["JoinErrors"] = "joinErrors";
10
+ })(ESchemaFields || (ESchemaFields = {}));
package/dist/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  import { Schema } from "./Schema.js";
2
+ import _validations from "./utils/validations.js";
3
+ export const validations = _validations;
2
4
  export default Schema;
package/dist/validate.js CHANGED
@@ -4,111 +4,133 @@ import checkRules from "./checkRules.js";
4
4
  import _helpers from "./utils/helpers.js";
5
5
  import { ErrorKeywords } from "./constants/details.js";
6
6
  import ValidnoResult from "./ValidnoResult.js";
7
- function generateMsg(input) {
8
- let { key, deepKey, data, reqs } = input;
9
- const keyForMsg = deepKey || key;
10
- const keyTitle = 'title' in reqs ? reqs.title : keyForMsg;
11
- if (!reqs.customMessage)
12
- return _errors.getMissingError(keyForMsg);
13
- const errMsg = reqs.customMessage({
7
+ function handleMissingKey(schema, input) {
8
+ const { key, nestedKey, data, reqs } = input;
9
+ const messageKey = nestedKey || key;
10
+ const messageTitle = reqs.title || messageKey;
11
+ if (!reqs.customMessage) {
12
+ return _errors.getMissingError(messageKey);
13
+ }
14
+ const errorMessage = reqs.customMessage({
14
15
  keyword: ErrorKeywords.Missing,
15
16
  value: data[key],
16
- key: keyForMsg,
17
- title: keyTitle,
18
- reqs: reqs,
19
- schema: this.schema
17
+ key: messageKey,
18
+ title: messageTitle,
19
+ reqs,
20
+ schema,
20
21
  });
21
- return errMsg;
22
+ return errorMessage;
22
23
  }
23
- function handleDeepKey(input) {
24
- const { results, key, deepKey, data, reqs } = input;
25
- const nesctedKeys = Object.keys(reqs);
24
+ function validateNestedKey(input) {
25
+ const { results, key, nestedKey, data, reqs } = input;
26
+ const nestedKeys = Object.keys(reqs);
26
27
  const nestedResults = [];
27
- let i = 0;
28
- while (i < nesctedKeys.length) {
29
- const nestedKey = nesctedKeys[i];
28
+ for (const itemKey of nestedKeys) {
30
29
  const deepParams = {
31
- key: nestedKey,
30
+ key: itemKey,
32
31
  data: data[key],
33
- reqs: reqs[nestedKey],
34
- deepKey: `${deepKey}.${nestedKey}`
32
+ reqs: reqs[itemKey],
33
+ nestedKey: `${nestedKey}.${itemKey}`
35
34
  };
36
- const deepResults = handleKey.call(this, deepParams);
35
+ const deepResults = validateKey.call(this, deepParams);
37
36
  nestedResults.push(deepResults.ok);
38
37
  results.merge(deepResults);
39
- i++;
40
38
  }
41
- results.fixParentByChilds(deepKey, nestedResults);
39
+ results.fixParentByChilds(nestedKey, nestedResults);
42
40
  return results;
43
41
  }
44
- export function handleKey(input) {
45
- let { results, key, deepKey, data, reqs } = input;
42
+ function validateKey(input) {
43
+ let { results, key, nestedKey, data, reqs } = input;
46
44
  if (!results)
47
45
  results = new ValidnoResult();
48
- if (!deepKey)
49
- deepKey = key;
50
- const hasNested = _helpers.checkIsNested(reqs);
46
+ if (!nestedKey)
47
+ nestedKey = key;
51
48
  const hasMissing = _helpers.hasMissing(input);
52
- const missedCheck = [];
53
- const typeChecked = [];
54
- const rulesChecked = [];
55
49
  if (_helpers.checkNestedIsMissing(reqs, data)) {
56
- results.setMissing(deepKey);
57
- return results;
50
+ return handleMissingNestedKey(results, nestedKey);
58
51
  }
59
- if (hasNested) {
60
- return handleDeepKey.call(this, { results, key, data, reqs, deepKey });
52
+ if (_helpers.checkIsNested(reqs)) {
53
+ return validateNestedKey.call(this, { results, key, data, reqs, nestedKey });
61
54
  }
55
+ return validateKeyDetails.call(this, {
56
+ results,
57
+ key,
58
+ nestedKey,
59
+ data,
60
+ reqs,
61
+ hasMissing,
62
+ });
63
+ }
64
+ function handleMissingNestedKey(results, nestedKey) {
65
+ results.setMissing(nestedKey);
66
+ return results;
67
+ }
68
+ function validateKeyDetails(params) {
69
+ const { results, key, nestedKey, data, reqs, hasMissing } = params;
70
+ const missedCheck = [];
71
+ const typeChecked = [];
72
+ const rulesChecked = [];
62
73
  if (hasMissing) {
63
- let errMsg = generateMsg.call(this, input);
64
- missedCheck.push(false);
65
- results.setMissing(deepKey, errMsg);
66
- return results;
74
+ return handleMissingKeyValidation(this.schema, { results, key, nestedKey, data, reqs }, missedCheck);
67
75
  }
68
- const typeCheck = checkType(key, data[key], reqs, deepKey);
69
- typeCheck.forEach(res => {
70
- if (res.passed === false) {
71
- typeChecked.push(res.passed);
76
+ checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
77
+ checkRulesForKey.call(this, results, nestedKey, data[key], reqs, data, rulesChecked);
78
+ return finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked);
79
+ }
80
+ function handleMissingKeyValidation(schema, params, missedCheck) {
81
+ const { results, key, nestedKey, data, reqs } = params;
82
+ const errMsg = handleMissingKey(schema, { key, nestedKey, data, reqs });
83
+ missedCheck.push(false);
84
+ results.setMissing(nestedKey, errMsg);
85
+ return results;
86
+ }
87
+ function checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
88
+ const typeCheck = checkType(key, value, reqs, nestedKey);
89
+ typeCheck.forEach((res) => {
90
+ if (!res.passed) {
91
+ typeChecked.push(false);
72
92
  results.errors.push(res.details);
73
93
  }
74
94
  });
75
- const ruleCheck = checkRules.call(this, deepKey, data[key], reqs, data);
95
+ }
96
+ function checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
97
+ const ruleCheck = checkRules.call(this, nestedKey, value, reqs, data);
76
98
  if (!ruleCheck.ok) {
77
99
  rulesChecked.push(false);
78
100
  ruleCheck.details.forEach((el) => {
79
- if (deepKey in results.errorsByKeys)
80
- results.errorsByKeys[deepKey] = [];
101
+ if (!(nestedKey in results.errorsByKeys))
102
+ results.errorsByKeys[nestedKey] = [];
81
103
  results.errors.push(el);
82
- results.errorsByKeys[deepKey] = ['1'];
104
+ results.errorsByKeys[nestedKey] = ['1'];
83
105
  });
84
106
  }
107
+ }
108
+ function finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked) {
85
109
  if (missedCheck.length)
86
- results.setMissing(deepKey);
87
- const isPassed = (!typeChecked.length && !rulesChecked.length && !missedCheck.length);
110
+ results.setMissing(nestedKey);
111
+ const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
88
112
  if (!isPassed) {
89
- results.setFailed(deepKey);
90
- results.errorsByKeys[deepKey] = [
91
- ...results.errors
92
- ];
113
+ results.setFailed(nestedKey);
114
+ results.errorsByKeys[nestedKey] = [...results.errors];
93
115
  }
94
116
  else {
95
- results.setPassed(deepKey);
117
+ results.setPassed(nestedKey);
96
118
  }
97
119
  return results.finish();
98
120
  }
99
- function validate(schema, data, keysToCheck) {
121
+ function validateSchema(data, keysToCheck) {
100
122
  const output = new ValidnoResult();
101
123
  const hasKeysToCheck = _helpers.areKeysLimited(keysToCheck);
102
- const schemaKeys = Object.entries(schema.schema);
124
+ const schemaKeys = Object.entries(this.schema);
103
125
  for (const [key, reqs] of schemaKeys) {
104
126
  const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, keysToCheck);
105
127
  if (!toBeValidated)
106
128
  continue;
107
- const keyResult = handleKey.call(this, { key, data, reqs });
129
+ const keyResult = validateKey.call(this, { key, data, reqs });
108
130
  output.merge(keyResult);
109
131
  }
110
132
  output.finish();
111
133
  return output;
112
134
  }
113
135
  ;
114
- export default validate;
136
+ export default validateSchema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {