validno 0.4.1 → 0.4.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.
Files changed (39) hide show
  1. package/dist/constants/details.d.ts +3 -2
  2. package/dist/constants/details.d.ts.map +1 -1
  3. package/dist/constants/details.js +2 -1
  4. package/dist/dev.js +74 -52
  5. package/dist/engine/methods/handleNestedKey.js +1 -1
  6. package/dist/engine/methods/validate.d.ts.map +1 -1
  7. package/dist/engine/methods/validate.js +7 -0
  8. package/dist/engine/methods/validateType.js +1 -1
  9. package/dist/utils/helpers.d.ts.map +1 -1
  10. package/package.json +1 -1
  11. package/dist/ValidnoResult.js +0 -102
  12. package/dist/checkType.js +0 -1
  13. package/dist/validate/index.js +0 -1
  14. package/dist/validate/validate.js +0 -151
  15. package/dist/validate.js +0 -151
  16. package/dist/validateEngine/ValidateEngine.js +0 -44
  17. package/dist/validateEngine/index.js +0 -2
  18. package/dist/validateEngine/methods/ValidateEngine.js +0 -139
  19. package/dist/validateEngine/methods/checkRulesForKey.js +0 -15
  20. package/dist/validateEngine/methods/checkValueType.js +0 -134
  21. package/dist/validateEngine/methods/finalizeValidation.js +0 -15
  22. package/dist/validateEngine/methods/finishValidation.js +0 -15
  23. package/dist/validateEngine/methods/handleKey.js +0 -43
  24. package/dist/validateEngine/methods/handleMissingKey.js +0 -19
  25. package/dist/validateEngine/methods/handleMissingKeyValidation.js +0 -9
  26. package/dist/validateEngine/methods/handleNestedKey.js +0 -19
  27. package/dist/validateEngine/methods/validate.js +0 -14
  28. package/dist/validateEngine/methods/validateKey.js +0 -31
  29. package/dist/validateEngine/methods/validateKeyDetails.js +0 -13
  30. package/dist/validateEngine/methods/validateKeyValue.js +0 -13
  31. package/dist/validateEngine/methods/validateNestedKey.js +0 -19
  32. package/dist/validateEngine/methods/validateType.js +0 -134
  33. package/dist/validateEngine/validate.js +0 -14
  34. package/dist/validateRules.js +0 -159
  35. package/dist/validateSchema/ValidateEngine.js +0 -147
  36. package/dist/validateSchema/index.js +0 -6
  37. package/dist/validateSchema/validate.js +0 -151
  38. package/dist/validateSchema.js +0 -6
  39. package/dist/validateType.js +0 -124
@@ -5,7 +5,8 @@ export declare enum ValidationIds {
5
5
  }
6
6
  export declare enum ValidationDetails {
7
7
  OK = "OK",
8
- INVALID_DATE = "Invalid date",
9
- CustomRuleFailed = "Custom rule failed"
8
+ InvalidDate = "Invalid date",
9
+ CustomRuleFailed = "Custom rule failed",
10
+ BadInput = "Validation input must be a plain object"
10
11
  }
11
12
  //# sourceMappingURL=details.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"details.d.ts","sourceRoot":"","sources":["../../src/constants/details.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IACrB,OAAO,YAAY;IACnB,IAAI,SAAS;IACb,IAAI,SAAS;CAChB;AAED,oBAAY,iBAAiB;IACzB,EAAE,OAAO;IACT,YAAY,iBAAiB;IAC7B,gBAAgB,uBAAuB;CAC1C"}
1
+ {"version":3,"file":"details.d.ts","sourceRoot":"","sources":["../../src/constants/details.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IACrB,OAAO,YAAY;IACnB,IAAI,SAAS;IACb,IAAI,SAAS;CAChB;AAED,oBAAY,iBAAiB;IACzB,EAAE,OAAO;IACT,WAAW,iBAAiB;IAC5B,gBAAgB,uBAAuB;IACvC,QAAQ,4CAA4C;CACvD"}
@@ -7,6 +7,7 @@ export var ValidationIds;
7
7
  export var ValidationDetails;
8
8
  (function (ValidationDetails) {
9
9
  ValidationDetails["OK"] = "OK";
10
- ValidationDetails["INVALID_DATE"] = "Invalid date";
10
+ ValidationDetails["InvalidDate"] = "Invalid date";
11
11
  ValidationDetails["CustomRuleFailed"] = "Custom rule failed";
12
+ ValidationDetails["BadInput"] = "Validation input must be a plain object";
12
13
  })(ValidationDetails || (ValidationDetails = {}));
package/dist/dev.js CHANGED
@@ -1,57 +1,79 @@
1
- import Schema from './index.js';
2
- const configSchema = new Schema({
3
- api: {
4
- port: {
5
- type: Number,
6
- required: true,
7
- rules: { min: 1000, max: 9999 }
8
- },
9
- cors: {
10
- enabled: { type: Boolean, required: true },
11
- origins: {
12
- type: Array,
13
- eachType: String,
14
- required: false
15
- }
1
+ import { Schema } from "./Schema.js";
2
+ const test = () => {
3
+ console.log('--- Custom Test ---');
4
+ const AccessScopes = {
5
+ Public: 'public',
6
+ Project: 'workspace',
7
+ User: 'user'
8
+ };
9
+ const collectionSettingsSchema = new Schema({
10
+ auth: { type: null },
11
+ scope: {
12
+ type: String,
13
+ rules: {
14
+ enum: Object.values(AccessScopes)
15
+ },
16
16
  },
17
- rateLimit: {
18
- enabled: { type: Boolean, required: true },
19
- maxRequests: {
20
- type: Number,
21
- required: false,
22
- rules: { min: 1, max: 10000 }
17
+ methods: {
18
+ get: {
19
+ isActive: { type: Boolean },
20
+ scope: {
21
+ type: String,
22
+ rules: { enum: Object.values(AccessScopes) },
23
+ required: false
24
+ }
25
+ },
26
+ getAll: {
27
+ isActive: { type: Boolean },
28
+ scope: {
29
+ type: String,
30
+ rules: {
31
+ enum: Object.values(AccessScopes)
32
+ },
33
+ required: false
34
+ },
35
+ sort: { default: { type: String } },
36
+ filter: { fields: { type: Array, eachType: String } },
37
+ search: { fields: { type: Array, eachType: String } }
38
+ },
39
+ create: {
40
+ isActive: { type: Boolean },
41
+ scope: {
42
+ type: String,
43
+ rules: { enum: Object.values(AccessScopes) },
44
+ required: false
45
+ }
23
46
  },
24
- windowMs: {
25
- type: Number,
26
- required: false,
27
- rules: { min: 1000, max: 86400000 }
47
+ update: {
48
+ isActive: { type: Boolean },
49
+ scope: {
50
+ type: String,
51
+ rules: { enum: Object.values(AccessScopes) },
52
+ required: false
53
+ },
54
+ allowedFields: { type: Array, eachType: String }
55
+ },
56
+ delete: {
57
+ isActive: { type: Boolean },
58
+ scope: {
59
+ type: String,
60
+ rules: { enum: Object.values(AccessScopes) },
61
+ required: false
62
+ }
63
+ },
64
+ distinct: {
65
+ isActive: { type: Boolean },
66
+ scope: {
67
+ type: String,
68
+ rules: { enum: Object.values(AccessScopes) },
69
+ required: false
70
+ },
71
+ fields: { type: Array, eachType: String }
28
72
  }
29
73
  }
30
- }
31
- });
32
- const exampleConfigFile = {
33
- "app": {
34
- "name": "MyApp",
35
- "version": "1.0.0",
36
- "environment": "development"
37
- },
38
- "database": {
39
- "host": "localhost",
40
- "port": 5432,
41
- "name": "myappdb",
42
- "ssl": false
43
- },
44
- "api": {
45
- "port": 3000,
46
- "cors": {
47
- "enabled": true,
48
- "origins": ["http://localhost:3000"]
49
- },
50
- "rateLimit": {
51
- "enabled": true,
52
- "maxRequests": 1000,
53
- "windowMs": 60000
54
- }
55
- },
74
+ });
75
+ const settingsData = { text: 1 };
76
+ const res = collectionSettingsSchema.validate(settingsData);
77
+ console.log(res);
56
78
  };
57
- const res = configSchema.validate(exampleConfigFile);
79
+ test();
@@ -5,7 +5,7 @@ function handleNestedKey(input) {
5
5
  for (const itemKey of nestedKeys) {
6
6
  const deepParams = {
7
7
  key: itemKey,
8
- data: data[key],
8
+ data: data ? data[key] : undefined,
9
9
  reqs: reqs[itemKey],
10
10
  nestedKey: `${nestedKey}.${itemKey}`
11
11
  };
@@ -1 +1 @@
1
- {"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validate.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,MAAM,qBAAqB,CAAA;AAC/C,OAAO,cAAwC,MAAM,sBAAsB,CAAA;AAE3E,iBAAS,QAAQ,CACb,IAAI,EAAE,cAAc,EACpB,IAAI,EAAE,GAAG,EACT,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GACnC,aAAa,CAaf;AAED,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../src/engine/methods/validate.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,MAAM,qBAAqB,CAAA;AAC/C,OAAO,cAAwC,MAAM,sBAAsB,CAAA;AAG3E,iBAAS,QAAQ,CACb,IAAI,EAAE,cAAc,EACpB,IAAI,EAAE,GAAG,EACT,cAAc,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GACnC,aAAa,CAmBf;AAED,eAAe,QAAQ,CAAC"}
@@ -1,5 +1,12 @@
1
1
  import _helpers from "../../utils/helpers.js";
2
+ import ValidnoResult from "../ValidnoResult.js";
3
+ import { ValidationDetails } from "../../constants/details.js";
2
4
  function validate(data, validationKeys) {
5
+ if (typeof data !== "object" || data === null || Array.isArray(data)) {
6
+ const result = new ValidnoResult();
7
+ result.setFailed("", ValidationDetails.BadInput);
8
+ return result.finish();
9
+ }
3
10
  const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
4
11
  const schemaKeys = Object.entries(this.definition);
5
12
  for (const [key, reqs] of schemaKeys) {
@@ -69,7 +69,7 @@ const handleTypeValidation = (key, value, requirements, keyName = key) => {
69
69
  const isDate = isNotNull && value.constructor === Date;
70
70
  const isValid = isDate && !isNaN(value.getTime());
71
71
  const isValidDate = isDate && isValid;
72
- result.push(_validateType.getResult(keyName, isValidDate, getDetails(isValidDate, ValidationDetails.INVALID_DATE)));
72
+ result.push(_validateType.getResult(keyName, isValidDate, getDetails(isValidDate, ValidationDetails.InvalidDate)));
73
73
  break;
74
74
  }
75
75
  case Boolean: {
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/utils/helpers.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,UAAU,WAAW;IACnB,aAAa,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,OAAO,CAAC;IACrD,oBAAoB,EAAE,CAAC,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC;IACrE,cAAc,EAAE,CAAC,QAAQ,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EAAE,GAAG,OAAO;IAmBlD,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAWhD,OAAO,CAAC,SAAS;CA4BlB;AAED,QAAA,MAAM,OAAO,eAAsB,CAAC;AACpC,eAAe,OAAO,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "A lightweight TypeScript validation library for runtime data validation. Used as part of Kodzero API framework.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,102 +0,0 @@
1
- import _errors from "./utils/errors.js";
2
- class ValidnoResult {
3
- constructor(results) {
4
- this.ok = (results === null || results === void 0 ? void 0 : results.ok) !== undefined ? results.ok : null;
5
- this.missed = (results === null || results === void 0 ? void 0 : results.missed) || [];
6
- this.failed = (results === null || results === void 0 ? void 0 : results.failed) || [];
7
- this.passed = (results === null || results === void 0 ? void 0 : results.passed) || [];
8
- this.errors = (results === null || results === void 0 ? void 0 : results.errors) || [];
9
- this.byKeys = (results === null || results === void 0 ? void 0 : results.byKeys) || {};
10
- this.errorsByKeys = (results === null || results === void 0 ? void 0 : results.errorsByKeys) || {};
11
- }
12
- setNoData() {
13
- this.ok = false;
14
- this.errors = ['Отсутствует объект для проверки'];
15
- }
16
- setKeyStatus(key, result) {
17
- this.byKeys[key] = result;
18
- }
19
- fixParentByChilds(parentKey, childChecks = []) {
20
- const isEveryOk = childChecks.every(check => check === true);
21
- this.setKeyStatus(parentKey, isEveryOk);
22
- if (isEveryOk) {
23
- this.setPassed(parentKey);
24
- }
25
- else {
26
- this.setFailed(parentKey);
27
- }
28
- }
29
- setMissing(key, errMsg) {
30
- const error = errMsg || _errors.getMissingError(key);
31
- this.missed.push(key);
32
- this.setFailed(key, error);
33
- this.setKeyStatus(key, false);
34
- }
35
- setPassed(key) {
36
- this.passed.push(key);
37
- this.setKeyStatus(key, true);
38
- }
39
- setFailed(key, msg) {
40
- if (!(key in this.errorsByKeys)) {
41
- this.errorsByKeys[key] = [];
42
- }
43
- this.failed.push(key);
44
- this.setKeyStatus(key, false);
45
- if (!msg)
46
- return;
47
- this.errors.push(msg);
48
- this.errorsByKeys[key].push(msg);
49
- }
50
- joinErrors(separator = '; ') {
51
- return _errors.joinErrors(this.errors, separator);
52
- }
53
- merge(resultsNew) {
54
- this.failed = [...this.failed, ...resultsNew.failed];
55
- this.errors = [...this.errors, ...resultsNew.errors];
56
- this.missed = [...this.missed, ...resultsNew.missed];
57
- this.passed = [...this.passed, ...resultsNew.passed];
58
- this.byKeys = Object.assign(Object.assign({}, this.byKeys), resultsNew.byKeys);
59
- for (const key in resultsNew.errorsByKeys) {
60
- if (!(key in this.errorsByKeys)) {
61
- this.errorsByKeys[key] = [];
62
- }
63
- this.errorsByKeys[key] = [
64
- ...this.errorsByKeys[key],
65
- ...resultsNew.errorsByKeys[key]
66
- ];
67
- }
68
- return this;
69
- }
70
- clearEmptyErrorsByKeys() {
71
- for (const key in this.errorsByKeys) {
72
- if (!this.errorsByKeys[key].length) {
73
- delete this.errorsByKeys[key];
74
- }
75
- }
76
- }
77
- finish() {
78
- if (this.failed.length || this.errors.length) {
79
- this.ok = false;
80
- }
81
- else {
82
- this.ok = true;
83
- }
84
- this.clearEmptyErrorsByKeys();
85
- return this;
86
- }
87
- data() {
88
- return {
89
- ok: this.ok,
90
- missed: this.missed,
91
- failed: this.failed,
92
- passed: this.passed,
93
- errors: this.errors,
94
- byKeys: this.byKeys,
95
- errorsByKeys: this.errorsByKeys,
96
- };
97
- }
98
- isValid() {
99
- return this.ok === true;
100
- }
101
- }
102
- export default ValidnoResult;
package/dist/checkType.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,151 +0,0 @@
1
- import validateType from "../validateType.js";
2
- import validateRules from "../validateRules.js";
3
- import { ValidationIds } from "../constants/details.js";
4
- import ValidnoResult from "../ValidnoResult.js";
5
- import _errors from "../utils/errors.js";
6
- import _helpers from "../utils/helpers.js";
7
- class ValidateEngine {
8
- constructor(definition) {
9
- this.definition = definition;
10
- this.result = new ValidnoResult();
11
- }
12
- validate(data, validationKeys) {
13
- const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
14
- const schemaKeys = Object.entries(this.definition);
15
- for (const [key, reqs] of schemaKeys) {
16
- const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, validationKeys);
17
- if (!toBeValidated)
18
- continue;
19
- const keyResult = this.validateKey({ key, data, reqs });
20
- this.result.merge(keyResult);
21
- }
22
- return this.result.finish();
23
- }
24
- handleMissingKey(schema, input) {
25
- const { key, nestedKey, data, reqs } = input;
26
- const messageKey = nestedKey || key;
27
- const messageTitle = reqs.title || messageKey;
28
- if (!reqs.customMessage) {
29
- return _errors.getMissingError(messageKey);
30
- }
31
- const errorMessage = reqs.customMessage({
32
- keyword: ValidationIds.Missing,
33
- value: data[key],
34
- key: messageKey,
35
- title: messageTitle,
36
- reqs,
37
- schema,
38
- });
39
- return errorMessage;
40
- }
41
- validateNestedKey(input) {
42
- const { results, key, nestedKey, data, reqs } = input;
43
- const nestedKeys = Object.keys(reqs);
44
- const nestedResults = [];
45
- for (const itemKey of nestedKeys) {
46
- const deepParams = {
47
- key: itemKey,
48
- data: data[key],
49
- reqs: reqs[itemKey],
50
- nestedKey: `${nestedKey}.${itemKey}`
51
- };
52
- const deepResults = this.validateKey(deepParams);
53
- nestedResults.push(deepResults.ok);
54
- results.merge(deepResults);
55
- }
56
- results.fixParentByChilds(nestedKey, nestedResults);
57
- return results;
58
- }
59
- validateKey(input) {
60
- let { results, key, nestedKey, data, reqs } = input;
61
- if (data === undefined) {
62
- const noDataResult = new ValidnoResult();
63
- noDataResult.setNoData();
64
- noDataResult.finish();
65
- return noDataResult;
66
- }
67
- if (!results)
68
- results = new ValidnoResult();
69
- if (!nestedKey)
70
- nestedKey = key;
71
- const hasMissing = _helpers.hasMissing(input);
72
- if (_helpers.checkNestedIsMissing(reqs, data)) {
73
- return this.handleMissingNestedKey(results, nestedKey);
74
- }
75
- if (_helpers.checkIsNested(reqs)) {
76
- return this.validateNestedKey({ results, key, data, reqs, nestedKey });
77
- }
78
- return this.validateKeyDetails({
79
- results,
80
- key,
81
- nestedKey,
82
- data,
83
- reqs,
84
- hasMissing,
85
- });
86
- }
87
- handleMissingNestedKey(results, nestedKey) {
88
- results.setMissing(nestedKey);
89
- return results;
90
- }
91
- validateKeyDetails(params) {
92
- const { results, key, nestedKey, data, reqs, hasMissing } = params;
93
- const missedCheck = [];
94
- const typeChecked = [];
95
- const rulesChecked = [];
96
- if (hasMissing) {
97
- return this.handleMissingKeyValidation({ results, key, nestedKey, data, reqs }, missedCheck);
98
- }
99
- this.checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
100
- this.checkRulesForKey(results, nestedKey, data[key], reqs, data, rulesChecked);
101
- return this.finalizeValidation({ results, nestedKey, missedCheck, typeChecked, rulesChecked });
102
- }
103
- handleMissingKeyValidation(params, missedCheck) {
104
- const schema = this.definition;
105
- const { results, key, nestedKey, data, reqs } = params;
106
- const errMsg = this.handleMissingKey(schema, { key, nestedKey, data, reqs });
107
- missedCheck.push(false);
108
- results.setMissing(nestedKey, errMsg);
109
- return results;
110
- }
111
- checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
112
- const typeCheck = validateType(key, value, reqs, nestedKey);
113
- typeCheck.forEach((res) => {
114
- if (!res.passed) {
115
- typeChecked.push(false);
116
- results.errors.push(res.details || '');
117
- }
118
- });
119
- }
120
- checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
121
- const ruleCheck = validateRules.call(this, nestedKey, value, reqs, data);
122
- if (!ruleCheck.ok) {
123
- rulesChecked.push(false);
124
- ruleCheck.details.forEach((el) => {
125
- if (!(nestedKey in results.errorsByKeys))
126
- results.errorsByKeys[nestedKey] = [];
127
- results.errors.push(el);
128
- results.errorsByKeys[nestedKey] = ['1'];
129
- });
130
- }
131
- }
132
- finalizeValidation(checks) {
133
- const { results, nestedKey, missedCheck, typeChecked, rulesChecked } = checks;
134
- if (missedCheck.length)
135
- results.setMissing(nestedKey);
136
- const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
137
- if (!isPassed) {
138
- results.setFailed(nestedKey);
139
- results.errorsByKeys[nestedKey] = [...results.errors];
140
- }
141
- else {
142
- results.setPassed(nestedKey);
143
- }
144
- return results.finish();
145
- }
146
- }
147
- const validateSchema = (schemaDef, data, keysToValidate) => {
148
- const engine = new ValidateEngine(schemaDef);
149
- return engine.validate(data, keysToValidate);
150
- };
151
- export default validateSchema;
package/dist/validate.js DELETED
@@ -1,151 +0,0 @@
1
- import validateType from "./validateType.js";
2
- import validateRules from "./validateRules.js";
3
- import { ValidationIds } from "./constants/details.js";
4
- import ValidnoResult from "./ValidnoResult.js";
5
- import _errors from "./utils/errors.js";
6
- import _helpers from "./utils/helpers.js";
7
- class ValidateEngine {
8
- constructor(definition) {
9
- this.definition = definition;
10
- this.result = new ValidnoResult();
11
- }
12
- validate(data, validationKeys) {
13
- const hasKeysToCheck = _helpers.areKeysLimited(validationKeys);
14
- const schemaKeys = Object.entries(this.definition);
15
- for (const [key, reqs] of schemaKeys) {
16
- const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, validationKeys);
17
- if (!toBeValidated)
18
- continue;
19
- const keyResult = this.validateKey({ key, data, reqs });
20
- this.result.merge(keyResult);
21
- }
22
- return this.result.finish();
23
- }
24
- handleMissingKey(schema, input) {
25
- const { key, nestedKey, data, reqs } = input;
26
- const messageKey = nestedKey || key;
27
- const messageTitle = reqs.title || messageKey;
28
- if (!reqs.customMessage) {
29
- return _errors.getMissingError(messageKey);
30
- }
31
- const errorMessage = reqs.customMessage({
32
- keyword: ValidationIds.Missing,
33
- value: data[key],
34
- key: messageKey,
35
- title: messageTitle,
36
- reqs,
37
- schema,
38
- });
39
- return errorMessage;
40
- }
41
- validateNestedKey(input) {
42
- const { results, key, nestedKey, data, reqs } = input;
43
- const nestedKeys = Object.keys(reqs);
44
- const nestedResults = [];
45
- for (const itemKey of nestedKeys) {
46
- const deepParams = {
47
- key: itemKey,
48
- data: data[key],
49
- reqs: reqs[itemKey],
50
- nestedKey: `${nestedKey}.${itemKey}`
51
- };
52
- const deepResults = this.validateKey(deepParams);
53
- nestedResults.push(deepResults.ok);
54
- results.merge(deepResults);
55
- }
56
- results.fixParentByChilds(nestedKey, nestedResults);
57
- return results;
58
- }
59
- validateKey(input) {
60
- let { results, key, nestedKey, data, reqs } = input;
61
- if (data === undefined) {
62
- const noDataResult = new ValidnoResult();
63
- noDataResult.setNoData();
64
- noDataResult.finish();
65
- return noDataResult;
66
- }
67
- if (!results)
68
- results = new ValidnoResult();
69
- if (!nestedKey)
70
- nestedKey = key;
71
- const hasMissing = _helpers.hasMissing(input);
72
- if (_helpers.checkNestedIsMissing(reqs, data)) {
73
- return this.handleMissingNestedKey(results, nestedKey);
74
- }
75
- if (_helpers.checkIsNested(reqs)) {
76
- return this.validateNestedKey({ results, key, data, reqs, nestedKey });
77
- }
78
- return this.validateKeyDetails({
79
- results,
80
- key,
81
- nestedKey,
82
- data,
83
- reqs,
84
- hasMissing,
85
- });
86
- }
87
- handleMissingNestedKey(results, nestedKey) {
88
- results.setMissing(nestedKey);
89
- return results;
90
- }
91
- validateKeyDetails(params) {
92
- const { results, key, nestedKey, data, reqs, hasMissing } = params;
93
- const missedCheck = [];
94
- const typeChecked = [];
95
- const rulesChecked = [];
96
- if (hasMissing) {
97
- return this.handleMissingKeyValidation({ results, key, nestedKey, data, reqs }, missedCheck);
98
- }
99
- this.checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
100
- this.checkRulesForKey(results, nestedKey, data[key], reqs, data, rulesChecked);
101
- return this.finalizeValidation({ results, nestedKey, missedCheck, typeChecked, rulesChecked });
102
- }
103
- handleMissingKeyValidation(params, missedCheck) {
104
- const schema = this.definition;
105
- const { results, key, nestedKey, data, reqs } = params;
106
- const errMsg = this.handleMissingKey(schema, { key, nestedKey, data, reqs });
107
- missedCheck.push(false);
108
- results.setMissing(nestedKey, errMsg);
109
- return results;
110
- }
111
- checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
112
- const typeCheck = validateType(key, value, reqs, nestedKey);
113
- typeCheck.forEach((res) => {
114
- if (!res.passed) {
115
- typeChecked.push(false);
116
- results.errors.push(res.details || '');
117
- }
118
- });
119
- }
120
- checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
121
- const ruleCheck = validateRules.call(this, nestedKey, value, reqs, data);
122
- if (!ruleCheck.ok) {
123
- rulesChecked.push(false);
124
- ruleCheck.details.forEach((el) => {
125
- if (!(nestedKey in results.errorsByKeys))
126
- results.errorsByKeys[nestedKey] = [];
127
- results.errors.push(el);
128
- results.errorsByKeys[nestedKey] = ['1'];
129
- });
130
- }
131
- }
132
- finalizeValidation(checks) {
133
- const { results, nestedKey, missedCheck, typeChecked, rulesChecked } = checks;
134
- if (missedCheck.length)
135
- results.setMissing(nestedKey);
136
- const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
137
- if (!isPassed) {
138
- results.setFailed(nestedKey);
139
- results.errorsByKeys[nestedKey] = [...results.errors];
140
- }
141
- else {
142
- results.setPassed(nestedKey);
143
- }
144
- return results.finish();
145
- }
146
- }
147
- const validateSchema = (schemaDef, data, keysToValidate) => {
148
- const engine = new ValidateEngine(schemaDef);
149
- return engine.validate(data, keysToValidate);
150
- };
151
- export default validateSchema;
@@ -1,44 +0,0 @@
1
- import ValidnoResult from "../ValidnoResult.js";
2
- import validate from "./methods/validate.js";
3
- import handleMissingKey from "./methods/handleMissingKey.js";
4
- import handleNestedKey from "./methods/handleNestedKey.js";
5
- import handleKey from "./methods/handleKey.js";
6
- import handleMissingKeyValidation from "./methods/handleMissingKeyValidation.js";
7
- import checkRulesForKey from "./methods/checkRulesForKey.js";
8
- import finishValidation from "./methods/finishValidation.js";
9
- import validateType from "./methods/validateType.js";
10
- class ValidateEngine {
11
- constructor(definition) {
12
- this.definition = definition;
13
- this.result = new ValidnoResult();
14
- }
15
- validate(data, validationKeys) {
16
- return validate.call(this, data, validationKeys);
17
- }
18
- handleKey(input) {
19
- return handleKey.call(this, input);
20
- }
21
- handleNestedKey(input) {
22
- return handleNestedKey.call(this, input);
23
- }
24
- handleMissingKey(schema, input) {
25
- return handleMissingKey(schema, input);
26
- }
27
- handleMissingNestedKey(nestedKey, results) {
28
- results.setMissing(nestedKey);
29
- return results;
30
- }
31
- handleMissingKeyValidation(params) {
32
- return handleMissingKeyValidation.call(this, params);
33
- }
34
- validateType(input) {
35
- return validateType(input);
36
- }
37
- checkRulesForKey(input) {
38
- return checkRulesForKey.call(this, input);
39
- }
40
- finishValidation(checks) {
41
- return finishValidation(checks);
42
- }
43
- }
44
- export default ValidateEngine;
@@ -1,2 +0,0 @@
1
- import ValidateEngine from "./ValidateEngine.js";
2
- export default ValidateEngine;