validno 0.2.0 → 0.2.2

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.
@@ -0,0 +1,61 @@
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
+ this.byKeys = (results === null || results === void 0 ? void 0 : results.byKeys) || {};
12
+ }
13
+ fixByKey(key, result) {
14
+ this.byKeys[key] = result;
15
+ if (result === true)
16
+ this.passed.push(key);
17
+ else
18
+ this.failed.push(key);
19
+ }
20
+ pushMissing(key, errMsg) {
21
+ this.missed.push(key);
22
+ this.fixByKey(key, false);
23
+ const error = errMsg || _errors.getMissingError(key);
24
+ this.pushError(key, error);
25
+ }
26
+ pushError(key, msg) {
27
+ if (key in this.errorsByKeys === false) {
28
+ this.errorsByKeys[key] = [];
29
+ }
30
+ this.byKeys[key] = false;
31
+ this.errors.push(msg);
32
+ this.errorsByKeys[key].push(msg);
33
+ }
34
+ joinErrors(separator = '; ') {
35
+ return _errors.joinErrors(this.errors, separator);
36
+ }
37
+ merge(resultsNew) {
38
+ this.failed = [...this.failed, ...resultsNew.failed];
39
+ this.errors = [...this.errors, ...resultsNew.errors];
40
+ this.missed = [...this.missed, ...resultsNew.missed];
41
+ this.passed = [...this.passed, ...resultsNew.passed];
42
+ this.byKeys = Object.assign(Object.assign({}, this.byKeys), resultsNew.byKeys);
43
+ for (const key in resultsNew.errorsByKeys) {
44
+ if (key in this.errorsByKeys === false)
45
+ this.errorsByKeys[key] = [];
46
+ this.errorsByKeys[key] = [
47
+ ...this.errorsByKeys[key],
48
+ ...resultsNew.errorsByKeys[key]
49
+ ];
50
+ }
51
+ return this;
52
+ }
53
+ finish() {
54
+ if (this.failed.length)
55
+ this.ok = false;
56
+ else
57
+ this.ok = true;
58
+ return this;
59
+ }
60
+ }
61
+ export default ValidnoResult;
package/dist/checkType.js CHANGED
@@ -22,6 +22,7 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
22
22
  return result;
23
23
  };
24
24
  const checkType = (key, value, requirements, keyName = key) => {
25
+ var _a;
25
26
  const isNotNull = value !== null;
26
27
  const keyTitle = 'title' in requirements ? requirements.title : keyName;
27
28
  const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
@@ -149,7 +150,7 @@ const checkType = (key, value, requirements, keyName = key) => {
149
150
  break;
150
151
  default:
151
152
  const isInstanceOf = value instanceof typeBySchema;
152
- const isConstructorSame = value.constructor.name === typeBySchema.name;
153
+ const isConstructorSame = ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
153
154
  const checked = isInstanceOf && isConstructorSame;
154
155
  result.push({
155
156
  key: keyName,
package/dist/dev.js CHANGED
@@ -1,52 +1,105 @@
1
- import { Schema } from "./Schema.js";
2
- class Parent {
3
- constructor(value) {
4
- this.value = value;
5
- this.version = 1;
1
+ import { isDeepStrictEqual } from 'node:util';
2
+ import _validations from './utils/validations.js';
3
+ const listSame = [];
4
+ const TEST_MAX = 1 * 1000000;
5
+ const TYPES = [
6
+ 'json',
7
+ 'date',
8
+ 'string',
9
+ 'number',
10
+ 'array',
11
+ 'null',
12
+ 'boolean'
13
+ ];
14
+ const str = 'string';
15
+ const date = () => new Date(2025, 0, 1, 0, 0, 0, 0);
16
+ const json = () => {
17
+ return {
18
+ "surname": "Иванов",
19
+ "name": "Иван",
20
+ "patronymic": "Иванович",
21
+ "birthdate": "01.01.1990",
22
+ "birthplace": "Москва",
23
+ "phone": "8 926 766 48 48"
24
+ };
25
+ };
26
+ console.time('autofill');
27
+ let i = 0;
28
+ while (i < TEST_MAX) {
29
+ if (TYPES.includes('null'))
30
+ listSame.push(null);
31
+ if (TYPES.includes('boolean'))
32
+ listSame.push(true);
33
+ if (TYPES.includes('json'))
34
+ listSame.push(() => json());
35
+ if (TYPES.includes('date'))
36
+ listSame.push(() => date());
37
+ if (TYPES.includes('string'))
38
+ listSame.push(str);
39
+ if (TYPES.includes('number'))
40
+ listSame.push(Math.random());
41
+ if (TYPES.includes('array'))
42
+ listSame.push(() => [{ x: 1, y: { z: 0 } }, { x: 1 }, { x: 1 }]);
43
+ if (TYPES.includes('array'))
44
+ listSame.push(() => [1234451, 5535433, 7839262, 2134573, 10239752]);
45
+ if (TYPES.includes('array'))
46
+ listSame.push(() => [true, true, false, true, false]);
47
+ if (TYPES.includes('array'))
48
+ listSame.push(() => ['true', 'true', 'false', 'true', 'false']);
49
+ i++;
50
+ }
51
+ console.timeEnd('autofill');
52
+ const compareArrs = (v1, v2) => {
53
+ if (v1.length !== v2.length)
54
+ return false;
55
+ return v1.every((el, i) => {
56
+ if (_validations.isObject(el)) {
57
+ return JSON.stringify(el) === JSON.stringify(v2[i]);
58
+ }
59
+ return v2[i] === el;
60
+ });
61
+ };
62
+ const is = (value, compareTo) => {
63
+ if (value === compareTo) {
64
+ return true;
6
65
  }
7
- getV() {
8
- return this.version;
66
+ const bothArrays = _validations.isArray(value) && _validations.isArray(compareTo);
67
+ if (bothArrays) {
68
+ return compareArrs(value, compareTo);
9
69
  }
10
- }
11
- class Child extends Parent {
12
- constructor(value) {
13
- super(value);
14
- this.isDeep = true;
70
+ const bothObjects = _validations.isObject(value) && _validations.isObject(compareTo);
71
+ if (bothObjects) {
72
+ const valueStr = JSON.stringify(value);
73
+ const compareToStr = JSON.stringify(compareTo);
74
+ return valueStr === compareToStr;
15
75
  }
16
- }
17
- const testCust = new Parent(12345678);
18
- const testCust2 = new Child(12345678);
19
- const schema = new Schema({
20
- parent1: {
21
- type: Parent,
22
- required: true,
23
- title: 'custom class test',
24
- },
25
- child1: {
26
- type: Child,
27
- required: true,
28
- title: 'custom class test',
29
- },
30
- parent2: {
31
- type: Parent,
32
- required: true
33
- },
34
- err: {
35
- type: Error,
36
- required: true,
37
- title: 'custom class test',
38
- },
39
- func: {
40
- type: Function,
41
- required: false,
76
+ const bothDates = _validations.isDate(value) && _validations.isDate(compareTo);
77
+ if (bothDates) {
78
+ return value.getTime() === compareTo.getTime();
79
+ }
80
+ return value === compareTo;
81
+ };
82
+ const is2 = (value, compareTo) => {
83
+ return true;
84
+ };
85
+ const is3 = (v1, v2) => {
86
+ return isDeepStrictEqual(v1, v2);
87
+ };
88
+ const doTest = (func) => {
89
+ console.time('test ' + func.name);
90
+ let y = 0;
91
+ while (y < TEST_MAX) {
92
+ const v1 = typeof listSame[y] === 'function' ? listSame[y]() : listSame[y];
93
+ const v2 = typeof listSame[y] === 'function' ? listSame[y]() : listSame[y];
94
+ const result = func(v1, v2);
95
+ if (result !== true) {
96
+ console.log(v1);
97
+ console.log(v2);
98
+ throw new Error('RESULT != TRUE');
99
+ }
100
+ y++;
42
101
  }
43
- });
44
- const obj = {
45
- parent1: testCust,
46
- child1: testCust2,
47
- parent2: testCust2,
48
- err: new Error('ops'),
49
- func: () => { console.log('ya tut'); }
102
+ console.timeEnd('test ' + func.name);
50
103
  };
51
- const res = schema.validate(obj);
52
- console.log(res);
104
+ doTest(is);
105
+ doTest(is3);
@@ -0,0 +1,48 @@
1
+ import { defaultSchemaKeys } from "../Schema.js";
2
+ import ValidnoResult from "../ValidnoResult.js";
3
+ import _validations from "./validations.js";
4
+ const _helpers = {};
5
+ _helpers.checkIsNested = (obj) => {
6
+ if (!_validations.isObject(obj))
7
+ return false;
8
+ const objKeys = Object.keys(obj);
9
+ if (objKeys.every((k) => defaultSchemaKeys.includes(k))) {
10
+ return false;
11
+ }
12
+ else {
13
+ return true;
14
+ }
15
+ };
16
+ _helpers.mergeResults = (resultsOld, resultsNew) => {
17
+ const output = new ValidnoResult();
18
+ output.failed = [...resultsOld.failed, ...resultsNew.failed];
19
+ output.errors = [...resultsOld.errors, ...resultsNew.errors];
20
+ output.missed = [...resultsOld.missed, ...resultsNew.missed];
21
+ output.passed = [...resultsOld.passed, ...resultsNew.passed];
22
+ output.byKeys = Object.assign(Object.assign({}, resultsOld.byKeys), resultsNew.byKeys);
23
+ output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
24
+ return output;
25
+ };
26
+ _helpers.checkNestedIsMissing = (reqs, data) => {
27
+ const isRequired = reqs.required;
28
+ const isUndef = data === undefined;
29
+ const isEmpty = _validations.isObject(data) && !Object.keys(data).length;
30
+ return isRequired && (isUndef || isEmpty);
31
+ };
32
+ _helpers.areKeysLimited = (onlyKeys) => {
33
+ const hasArrayOfKeys = (Array.isArray(onlyKeys) && onlyKeys.length > 0);
34
+ const hasStringKey = (typeof onlyKeys === 'string' && onlyKeys.length > 0);
35
+ return hasArrayOfKeys || hasStringKey;
36
+ };
37
+ _helpers.needValidation = (key, hasLimits, onlyKeys) => {
38
+ const noLimits = !hasLimits;
39
+ const keyIsInList = (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
40
+ return noLimits || keyIsInList;
41
+ };
42
+ _helpers.hasMissing = (input) => {
43
+ const { reqs, data, key } = input;
44
+ const isRequired = reqs.required === true;
45
+ const missingData = (data === undefined || key in data === false || data[key] === undefined);
46
+ return isRequired && missingData;
47
+ };
48
+ export default _helpers;
@@ -0,0 +1 @@
1
+ "use strict";
@@ -2,86 +2,127 @@ const _validations = {};
2
2
  _validations.isString = (value) => {
3
3
  return typeof value === 'string';
4
4
  };
5
- _validations.isDate = (value) => {
6
- return value instanceof Date && String(value) !== 'Invalid Date';
7
- };
8
5
  _validations.isNumber = (value) => {
9
6
  return typeof value === 'number';
10
7
  };
11
- _validations.isNumberGte = (value, gte) => {
12
- return typeof value === 'number' && value >= gte;
13
- };
14
- _validations.isNumberLte = (value, lte) => {
15
- return typeof value === 'number' && value <= lte;
16
- };
17
8
  _validations.isArray = (value) => {
18
9
  return Array.isArray(value);
19
10
  };
20
11
  _validations.isObject = (value) => {
21
- return typeof value === 'object' && !Array.isArray(value) && value !== null;
12
+ var _a;
13
+ return value !== null && typeof value === 'object' && ((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Object' && !Array.isArray(value);
14
+ };
15
+ _validations.isDate = (value) => {
16
+ return value instanceof Date && String(value) !== 'Invalid Date';
17
+ };
18
+ _validations.isRegex = (value) => {
19
+ return value instanceof RegExp;
20
+ };
21
+ _validations.isBoolean = (value) => {
22
+ return typeof value === 'boolean';
23
+ };
24
+ _validations.isNull = (value) => {
25
+ return value === null;
26
+ };
27
+ _validations.isUndefined = (value) => {
28
+ return value === undefined;
29
+ };
30
+ _validations.isNullOrUndefined = (value) => {
31
+ return value === undefined || value === null;
32
+ };
33
+ _validations.isEmail = (value) => {
34
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
35
+ return emailRegex.test(value);
36
+ };
37
+ _validations.isDateYYYYMMDD = (value) => {
38
+ const regex = /^\d{4}-\d{2}-\d{2}$/;
39
+ return regex.test(value);
40
+ };
41
+ _validations.isHex = (value) => {
42
+ const regex = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
43
+ return regex.test(value);
22
44
  };
23
45
  _validations.lengthIs = (value, length) => {
46
+ if (typeof value !== 'string' && !Array.isArray(value))
47
+ return false;
48
+ if (typeof length !== 'number')
49
+ return false;
24
50
  return value.length === length;
25
51
  };
26
52
  _validations.lengthNot = (value, length) => {
53
+ if (typeof value !== 'string' && !Array.isArray(value))
54
+ return false;
55
+ if (typeof length !== 'number')
56
+ return false;
27
57
  return value.length !== length;
28
58
  };
29
59
  _validations.lengthMin = (value, min) => {
60
+ if (typeof value !== 'string' && !Array.isArray(value))
61
+ return false;
62
+ if (typeof min !== 'number')
63
+ return false;
30
64
  return value.length >= min;
31
65
  };
32
66
  _validations.lengthMax = (value, max) => {
67
+ if (typeof value !== 'string' && !Array.isArray(value))
68
+ return false;
69
+ if (typeof max !== 'number')
70
+ return false;
33
71
  return value.length <= max;
34
72
  };
35
- _validations.isEmail = (value) => {
36
- const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
37
- return emailRegex.test(value);
73
+ _validations.isNumberGte = (value, gte) => {
74
+ return typeof value === 'number' && value >= gte;
38
75
  };
39
- _validations.isRegex = (value) => {
40
- return value instanceof RegExp;
76
+ _validations.isNumberLte = (value, lte) => {
77
+ return typeof value === 'number' && value <= lte;
41
78
  };
42
79
  _validations.hasKey = (obj, key) => {
80
+ if (_validations.isObject(obj) === false)
81
+ return false;
43
82
  return key in obj;
44
83
  };
45
- _validations.isNot = (value, not) => {
46
- if (typeof not === 'object' && Array.isArray(not)) {
47
- for (let i = 0; i < not.length; i++) {
48
- if (value === not[i])
49
- return false;
84
+ const compareArrs = (v1, v2) => {
85
+ if (v1.length !== v2.length)
86
+ return false;
87
+ return v1.every((el, i) => {
88
+ if (_validations.isObject(el)) {
89
+ return JSON.stringify(el) === JSON.stringify(v2[i]);
50
90
  }
51
- return true;
52
- }
53
- return value !== not;
91
+ return v2[i] === el;
92
+ });
54
93
  };
55
94
  _validations.is = (value, compareTo) => {
56
- if (typeof compareTo === 'object' && Array.isArray(compareTo)) {
57
- for (let i = 0; i < compareTo.length; i++) {
58
- if (value === compareTo[i])
59
- return true;
60
- }
61
- return false;
95
+ if (value === compareTo) {
96
+ return true;
97
+ }
98
+ const bothArrays = _validations.isArray(value) && _validations.isArray(compareTo);
99
+ if (bothArrays) {
100
+ return compareArrs(value, compareTo);
101
+ }
102
+ const bothObjects = _validations.isObject(value) && _validations.isObject(compareTo);
103
+ if (bothObjects) {
104
+ const valueStr = JSON.stringify(value);
105
+ const compareToStr = JSON.stringify(compareTo);
106
+ return valueStr === compareToStr;
107
+ }
108
+ const bothDates = _validations.isDate(value) && _validations.isDate(compareTo);
109
+ if (bothDates) {
110
+ return value.getTime() === compareTo.getTime();
62
111
  }
63
112
  return value === compareTo;
64
113
  };
65
- _validations.isDateYYYYMMDD = (value) => {
66
- const regex = /^\d{4}-\d{2}-\d{2}$/;
67
- return regex.test(value);
68
- };
69
- _validations.regexTested = (value, regex) => {
70
- if (!regex)
71
- throw new Error('regex argument is not defined');
72
- return regex.test(value);
73
- };
74
- _validations.isHex = (value) => {
75
- const regex = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
76
- return regex.test(value);
77
- };
78
- _validations.isBoolean = (value) => {
79
- return typeof value === 'boolean';
80
- };
81
- _validations.isNull = (value) => {
82
- return value === null;
114
+ _validations.not = (value, not) => {
115
+ return !_validations.is(value, not);
83
116
  };
84
- _validations.isUndefined = (value) => {
85
- return value === undefined;
117
+ _validations.isNot = _validations.not;
118
+ _validations.ne = _validations.not;
119
+ _validations.regexTested = (value, regexp) => {
120
+ if (!regexp || regexp instanceof RegExp !== true) {
121
+ throw new Error('regexp argument is incorrect');
122
+ }
123
+ return regexp.test(value);
86
124
  };
125
+ _validations.regex = _validations.regexTested;
126
+ _validations.regexp = _validations.regexTested;
127
+ _validations.test = _validations.regexTested;
87
128
  export default _validations;
package/dist/validate.js CHANGED
@@ -1,89 +1,67 @@
1
1
  import checkType from "./checkType.js";
2
2
  import _errors from "./utils/errors.js";
3
3
  import checkRules from "./checkRules.js";
4
- import _validations from "./utils/validations.js";
4
+ import _helpers from "./utils/helpers.js";
5
5
  import { ErrorKeywords } from "./constants/details.js";
6
- import { defaultSchemaKeys } from "./Schema.js";
7
- export const getResultDefaults = () => {
8
- return {
9
- ok: null,
10
- missed: [],
11
- failed: [],
12
- passed: [],
13
- errors: [],
14
- byKeys: {},
15
- errorsByKeys: {},
16
- };
17
- };
18
- const checkIsNested = (obj) => {
19
- if (!_validations.isObject(obj))
20
- return false;
21
- const objKeys = Object.keys(obj);
22
- if (objKeys.every((k) => defaultSchemaKeys.includes(k))) {
23
- return false;
6
+ import ValidnoResult from "./ValidnoResult.js";
7
+ function generateMsg(input) {
8
+ let { results, key, deepKey, data, reqs } = input;
9
+ const keyForMsg = deepKey || key;
10
+ const keyTitle = 'title' in reqs ? reqs.title : keyForMsg;
11
+ if (reqs.customMessage && typeof reqs.customMessage === 'function') {
12
+ const errMsg = reqs.customMessage({
13
+ keyword: ErrorKeywords.Missing,
14
+ value: data[key],
15
+ key: keyForMsg,
16
+ title: keyTitle,
17
+ reqs: reqs,
18
+ schema: this.schema
19
+ });
20
+ return errMsg;
24
21
  }
25
- else {
26
- return true;
22
+ return _errors.getMissingError(keyForMsg);
23
+ }
24
+ function handleDeepKey(input) {
25
+ const { results, key, deepKey, data, reqs } = input;
26
+ const nesctedKeys = Object.keys(reqs);
27
+ results.fixByKey(deepKey, false);
28
+ let i = 0;
29
+ while (i < nesctedKeys.length) {
30
+ const nestedKey = nesctedKeys[i];
31
+ const deepParams = {
32
+ key: nestedKey,
33
+ data: data[key],
34
+ reqs: reqs[nestedKey],
35
+ deepKey: `${deepKey}.${nestedKey}`
36
+ };
37
+ const deepResults = handleKey.call(this, deepParams);
38
+ results.merge(deepResults);
39
+ i++;
27
40
  }
28
- };
29
- export const mergeResults = (resultsOld, resultsNew) => {
30
- const output = getResultDefaults();
31
- output.failed = [...resultsOld.failed, ...resultsNew.failed];
32
- output.errors = [...resultsOld.errors, ...resultsNew.errors];
33
- output.missed = [...resultsOld.missed, ...resultsNew.missed];
34
- output.passed = [...resultsOld.passed, ...resultsNew.passed];
35
- output.byKeys = Object.assign(Object.assign({}, resultsOld.byKeys), resultsNew.byKeys);
36
- output.errorsByKeys = Object.assign(Object.assign({}, resultsOld.errorsByKeys), resultsNew.errorsByKeys);
37
- return output;
38
- };
39
- export function handleReqKey(key, data, reqs, deepKey = key) {
40
- let results = getResultDefaults();
41
- const hasNested = checkIsNested(reqs);
42
- const keyTitle = 'title' in reqs ? reqs.title : deepKey;
41
+ return results;
42
+ }
43
+ export function handleKey(input) {
44
+ let { results, key, deepKey, data, reqs } = input;
45
+ if (!results)
46
+ results = new ValidnoResult();
47
+ if (!deepKey)
48
+ deepKey = key;
49
+ const hasNested = _helpers.checkIsNested(reqs);
50
+ const hasMissing = _helpers.hasMissing(input);
43
51
  const missedCheck = [];
44
52
  const typeChecked = [];
45
53
  const rulesChecked = [];
46
- if (reqs.required && (data === undefined ||
47
- (_validations.isObject(data) && !Object.keys(data).length))) {
48
- results.missed.push(deepKey);
49
- results.failed.push(deepKey);
50
- results.byKeys[deepKey] = false;
54
+ if (_helpers.checkNestedIsMissing(reqs, data)) {
55
+ results.pushMissing(deepKey);
51
56
  return results;
52
57
  }
53
58
  if (hasNested) {
54
- const nestedReqKeys = Object.keys(reqs);
55
- results.byKeys[deepKey] = true;
56
- let i = 0;
57
- while (i < nestedReqKeys.length) {
58
- const reqKeyI = nestedReqKeys[i];
59
- const deepResults = handleReqKey.call(this, reqKeyI, data[key], reqs[reqKeyI], deepKey + '.' + reqKeyI);
60
- results = mergeResults(results, deepResults);
61
- i++;
62
- }
63
- return results;
59
+ return handleDeepKey.call(this, { results, key, data, reqs, deepKey });
64
60
  }
65
- if (reqs.required === true &&
66
- (key in data === false || data === undefined || data[key] === undefined)) {
67
- console.log(data);
68
- let errMsg = _errors.getMissingError(deepKey);
69
- if (reqs.customMessage && typeof reqs.customMessage === 'function') {
70
- errMsg = reqs.customMessage({
71
- keyword: ErrorKeywords.Missing,
72
- value: data[key],
73
- key: deepKey,
74
- title: keyTitle,
75
- reqs: reqs,
76
- schema: this.schema
77
- });
78
- }
61
+ if (hasMissing) {
62
+ let errMsg = generateMsg.call(this, input);
79
63
  missedCheck.push(false);
80
- results.missed.push(deepKey);
81
- results.failed.push(deepKey);
82
- results.errors.push(errMsg);
83
- if (deepKey in results.errorsByKeys === false)
84
- results.errorsByKeys[deepKey] = [];
85
- results.errorsByKeys[deepKey].push(errMsg);
86
- results.byKeys[deepKey] = false;
64
+ results.pushMissing(deepKey, errMsg);
87
65
  return results;
88
66
  }
89
67
  const typeCheck = checkType(key, data[key], reqs, deepKey);
@@ -104,52 +82,27 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
104
82
  });
105
83
  }
106
84
  if (missedCheck.length)
107
- results.missed.push(deepKey);
108
- if (typeChecked.length || rulesChecked.length) {
109
- results.failed.push(deepKey);
110
- }
111
- else {
112
- results.passed.push(deepKey);
113
- }
85
+ results.pushMissing(deepKey);
86
+ const isPassed = (!typeChecked.length && !rulesChecked.length && !missedCheck.length);
87
+ results.fixByKey(deepKey, isPassed);
114
88
  results.errorsByKeys[deepKey] = [
115
89
  ...results.errors
116
90
  ];
117
- results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
118
- return results;
91
+ return results.finish();
119
92
  }
120
- const checkIfValidationIsNeeded = (key, hasLimits, onlyKeys) => {
121
- return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
122
- };
123
- function validate(schema, data, onlyKeys) {
124
- let results = getResultDefaults();
125
- const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0);
126
- for (const [key, reqs] of Object.entries(schema.schema)) {
127
- const isValidationRequired = checkIfValidationIsNeeded(key, areKeysLimited, onlyKeys);
128
- if (isValidationRequired) {
129
- const keyResult = handleReqKey.call(this, key, data, reqs);
130
- results = mergeResults(results, keyResult);
131
- }
93
+ function validate(schema, data, keysToCheck) {
94
+ const results = new ValidnoResult();
95
+ const hasKeysToCheck = _helpers.areKeysLimited(keysToCheck);
96
+ const schemaKeys = Object.entries(schema.schema);
97
+ for (const [key, reqs] of schemaKeys) {
98
+ const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, keysToCheck);
99
+ if (!toBeValidated)
100
+ continue;
101
+ const keyResult = handleKey.call(this, { key, data, reqs });
102
+ results.merge(keyResult);
132
103
  }
133
- if (results.failed.length)
134
- results.ok = false;
135
- else
136
- results.ok = true;
137
- return new ValidnoResult(results);
104
+ results.finish();
105
+ return results;
138
106
  }
139
107
  ;
140
- class ValidnoResult {
141
- constructor(results) {
142
- this.ok = results.ok;
143
- this.missed = results.missed;
144
- this.failed = results.failed;
145
- this.passed = results.passed;
146
- this.errors = results.errors;
147
- this.byKeys = results.byKeys;
148
- this.errorsByKeys = results.errorsByKeys;
149
- this.byKeys = results.byKeys;
150
- }
151
- joinErrors(separator = '; ') {
152
- return _errors.joinErrors(this.errors, separator);
153
- }
154
- }
155
108
  export default validate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {