validno 0.2.2 → 0.2.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.
@@ -10,24 +10,35 @@ class ValidnoResult {
10
10
  this.errorsByKeys = (results === null || results === void 0 ? void 0 : results.errorsByKeys) || {};
11
11
  this.byKeys = (results === null || results === void 0 ? void 0 : results.byKeys) || {};
12
12
  }
13
- fixByKey(key, result) {
13
+ setKeyStatus(key, result) {
14
14
  this.byKeys[key] = result;
15
- if (result === true)
16
- this.passed.push(key);
15
+ }
16
+ fixParentByChilds(parentKey, childChecks = []) {
17
+ const isEveryOk = childChecks.every(c => c === true);
18
+ this.setKeyStatus(parentKey, isEveryOk);
19
+ if (isEveryOk === true)
20
+ this.setPassed(parentKey);
17
21
  else
18
- this.failed.push(key);
22
+ this.setFailed(parentKey);
19
23
  }
20
- pushMissing(key, errMsg) {
21
- this.missed.push(key);
22
- this.fixByKey(key, false);
24
+ setMissing(key, errMsg) {
23
25
  const error = errMsg || _errors.getMissingError(key);
24
- this.pushError(key, error);
26
+ this.missed.push(key);
27
+ this.setFailed(key, error);
28
+ this.setKeyStatus(key, false);
25
29
  }
26
- pushError(key, msg) {
30
+ setPassed(key) {
31
+ this.passed.push(key);
32
+ this.setKeyStatus(key, true);
33
+ }
34
+ setFailed(key, msg) {
27
35
  if (key in this.errorsByKeys === false) {
28
36
  this.errorsByKeys[key] = [];
29
37
  }
30
- this.byKeys[key] = false;
38
+ this.failed.push(key);
39
+ this.setKeyStatus(key, false);
40
+ if (!msg)
41
+ return;
31
42
  this.errors.push(msg);
32
43
  this.errorsByKeys[key].push(msg);
33
44
  }
@@ -50,11 +61,19 @@ class ValidnoResult {
50
61
  }
51
62
  return this;
52
63
  }
64
+ clearEmptyErrorsByKeys() {
65
+ for (const key in this.errorsByKeys) {
66
+ if (!this.errorsByKeys[key].length) {
67
+ delete this.errorsByKeys[key];
68
+ }
69
+ }
70
+ }
53
71
  finish() {
54
72
  if (this.failed.length)
55
73
  this.ok = false;
56
74
  else
57
75
  this.ok = true;
76
+ this.clearEmptyErrorsByKeys();
58
77
  return this;
59
78
  }
60
79
  }
package/dist/dev.js CHANGED
@@ -1,105 +1,91 @@
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;
1
+ import { Schema } from "./Schema.js";
2
+ const validateConfig = (cfg) => {
3
+ const cfgSchema = new Schema({
4
+ methods: {
5
+ create: {
6
+ type: Object,
7
+ required: false,
8
+ },
9
+ createMany: {
10
+ type: Object,
11
+ required: false,
12
+ },
13
+ get: {
14
+ type: Object,
15
+ required: false,
16
+ },
17
+ getAll: {
18
+ type: Object,
19
+ required: false,
20
+ },
21
+ update: {
22
+ type: Object,
23
+ required: false,
24
+ },
25
+ delete: {
26
+ type: Object,
27
+ required: false,
28
+ },
29
+ deleteMany: {
30
+ type: Object,
31
+ required: false,
32
+ },
33
+ export: {
34
+ type: Object,
35
+ required: false,
36
+ },
37
+ distinct: {
38
+ type: Object,
39
+ required: false,
40
+ },
41
+ custom: {
42
+ type: Array,
43
+ required: false,
44
+ rules: {},
45
+ },
46
+ },
60
47
  });
61
- };
62
- const is = (value, compareTo) => {
63
- if (value === compareTo) {
64
- return true;
65
- }
66
- const bothArrays = _validations.isArray(value) && _validations.isArray(compareTo);
67
- if (bothArrays) {
68
- return compareArrs(value, compareTo);
69
- }
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;
48
+ const res = cfgSchema.validate(cfg);
49
+ console.log(res);
50
+ if (res.ok !== true) {
51
+ const errorsMsg = res.errors.join('; ');
52
+ throw new Error(errorsMsg);
75
53
  }
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
54
  };
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++;
101
- }
102
- console.timeEnd('test ' + func.name);
55
+ const uniTestService = {
56
+ methods: {
57
+ getAll: {
58
+ isActive: true,
59
+ sort: {
60
+ default: 'createdAt',
61
+ },
62
+ search: {
63
+ fields: ['title'],
64
+ },
65
+ },
66
+ export: {
67
+ isActive: false,
68
+ },
69
+ get: {
70
+ isActive: true,
71
+ },
72
+ create: null,
73
+ createMany: {
74
+ isActive: true,
75
+ },
76
+ update: {
77
+ isActive: true,
78
+ },
79
+ delete: {
80
+ isActive: true,
81
+ },
82
+ deleteMany: {
83
+ isActive: true,
84
+ },
85
+ distinct: {
86
+ isActive: true,
87
+ fields: ['title', '_id'],
88
+ },
89
+ },
103
90
  };
104
- doTest(is);
105
- doTest(is3);
91
+ validateConfig(uniTestService);
package/dist/validate.js CHANGED
@@ -24,7 +24,7 @@ function generateMsg(input) {
24
24
  function handleDeepKey(input) {
25
25
  const { results, key, deepKey, data, reqs } = input;
26
26
  const nesctedKeys = Object.keys(reqs);
27
- results.fixByKey(deepKey, false);
27
+ const nestedResults = [];
28
28
  let i = 0;
29
29
  while (i < nesctedKeys.length) {
30
30
  const nestedKey = nesctedKeys[i];
@@ -35,9 +35,11 @@ function handleDeepKey(input) {
35
35
  deepKey: `${deepKey}.${nestedKey}`
36
36
  };
37
37
  const deepResults = handleKey.call(this, deepParams);
38
+ nestedResults.push(deepResults.ok);
38
39
  results.merge(deepResults);
39
40
  i++;
40
41
  }
42
+ results.fixParentByChilds(deepKey, nestedResults);
41
43
  return results;
42
44
  }
43
45
  export function handleKey(input) {
@@ -52,7 +54,7 @@ export function handleKey(input) {
52
54
  const typeChecked = [];
53
55
  const rulesChecked = [];
54
56
  if (_helpers.checkNestedIsMissing(reqs, data)) {
55
- results.pushMissing(deepKey);
57
+ results.setMissing(deepKey);
56
58
  return results;
57
59
  }
58
60
  if (hasNested) {
@@ -61,7 +63,7 @@ export function handleKey(input) {
61
63
  if (hasMissing) {
62
64
  let errMsg = generateMsg.call(this, input);
63
65
  missedCheck.push(false);
64
- results.pushMissing(deepKey, errMsg);
66
+ results.setMissing(deepKey, errMsg);
65
67
  return results;
66
68
  }
67
69
  const typeCheck = checkType(key, data[key], reqs, deepKey);
@@ -82,12 +84,17 @@ export function handleKey(input) {
82
84
  });
83
85
  }
84
86
  if (missedCheck.length)
85
- results.pushMissing(deepKey);
87
+ results.setMissing(deepKey);
86
88
  const isPassed = (!typeChecked.length && !rulesChecked.length && !missedCheck.length);
87
- results.fixByKey(deepKey, isPassed);
88
- results.errorsByKeys[deepKey] = [
89
- ...results.errors
90
- ];
89
+ if (!isPassed) {
90
+ results.setFailed(deepKey);
91
+ results.errorsByKeys[deepKey] = [
92
+ ...results.errors
93
+ ];
94
+ }
95
+ else {
96
+ results.setPassed(deepKey);
97
+ }
91
98
  return results.finish();
92
99
  }
93
100
  function validate(schema, data, keysToCheck) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {