validno 0.1.11 → 0.2.1

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/dist/checkType.js CHANGED
@@ -1,31 +1,12 @@
1
1
  import { ErrorKeywords } from "./constants/details.js";
2
2
  import _validations from "./utils/validations.js";
3
- const getErrorDetails = (key, expectedType, receivedValue) => {
4
- let receivedType = '';
5
- if (typeof receivedValue === 'string')
6
- receivedType = 'String';
7
- else if (typeof receivedValue === 'number')
8
- receivedType = 'Number';
9
- else if (typeof receivedValue === 'boolean')
10
- receivedType = 'Noolean';
11
- else if (receivedValue === null)
12
- receivedType = 'null';
13
- else if (Array.isArray(receivedValue))
14
- receivedType = 'Array';
15
- else if (_validations.isDate(receivedValue))
16
- receivedType = 'Date';
17
- else if (_validations.isObject(receivedValue))
18
- receivedType = 'Object';
19
- else if (receivedValue === undefined)
20
- receivedType = 'undefined';
21
- return `Проверьте тип "${key}": ожидался ${(expectedType === null || expectedType === void 0 ? void 0 : expectedType.name) || expectedType}, получен ${receivedType || 'unknown'}`;
22
- };
3
+ import _errors from "./utils/errors.js";
23
4
  const checkTypeMultiple = (key, value, requirements, keyName = key) => {
24
5
  const constructorNames = requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el));
25
6
  const result = {
26
7
  key: keyName,
27
8
  passed: false,
28
- details: getErrorDetails(keyName, constructorNames.join('/'), value)
9
+ details: _errors.getErrorDetails(keyName, constructorNames.join('/'), value)
29
10
  };
30
11
  let i = 0;
31
12
  while (i < requirements.type.length) {
@@ -41,6 +22,7 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
41
22
  return result;
42
23
  };
43
24
  const checkType = (key, value, requirements, keyName = key) => {
25
+ var _a;
44
26
  const isNotNull = value !== null;
45
27
  const keyTitle = 'title' in requirements ? requirements.title : keyName;
46
28
  const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
@@ -69,9 +51,10 @@ const checkType = (key, value, requirements, keyName = key) => {
69
51
  schema: null
70
52
  }) :
71
53
  null;
72
- const baseErrDetails = getErrorDetails(keyName, requirements.type, value);
54
+ const baseErrDetails = _errors.getErrorDetails(keyName, requirements.type, value);
73
55
  const getDetails = (isOK) => isOK ? 'OK' : customErrDetails || baseErrDetails;
74
- switch (requirements.type) {
56
+ const typeBySchema = requirements.type;
57
+ switch (typeBySchema) {
75
58
  case 'any':
76
59
  result.push({
77
60
  key: keyName,
@@ -166,10 +149,13 @@ const checkType = (key, value, requirements, keyName = key) => {
166
149
  });
167
150
  break;
168
151
  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);
154
+ const checked = isInstanceOf && isConstructorSame;
169
155
  result.push({
170
156
  key: keyName,
171
- passed: false,
172
- details: `Тип '${keyName}' не определен`
157
+ passed: checked,
158
+ details: getDetails(checked)
173
159
  });
174
160
  }
175
161
  return result;
package/dist/dev.js CHANGED
@@ -1,41 +1,105 @@
1
- import { Schema } from "./Schema.js";
2
- const schema = new Schema({
3
- ops1: {
4
- type: Number,
5
- required: true,
6
- title: 'titl',
7
- customMessage: (input) => {
8
- return 'Wrong type 1 msg ' + input.keyword;
9
- }
10
- },
11
- ops2: {
12
- type: String,
13
- required: true,
14
- title: 'titl',
15
- customMessage: (input) => {
16
- return 'Missing msg ' + input.keyword;
17
- }
18
- },
19
- miss: {
20
- type: String,
21
- required: true,
22
- title: 'titl',
23
- customMessage: (input) => {
24
- return 'Missing msg ' + input.keyword;
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]);
25
58
  }
59
+ return v2[i] === el;
60
+ });
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;
26
75
  }
27
- });
28
- const obj = {
29
- ops1: 'String',
30
- ops2: 123
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);
31
87
  };
32
- const obj2 = {
33
- ops1: 222,
34
- ops2: 'str',
35
- miss: 'here'
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);
36
103
  };
37
- const res = schema.validate(obj);
38
- const res2 = schema.validate(obj2);
39
- console.log('finalResult');
40
- console.log(res2);
41
- console.log(res2.joinErrors());
104
+ doTest(is);
105
+ doTest(is3);
@@ -1,3 +1,17 @@
1
1
  const _errors = {};
2
2
  _errors.getMissingError = (key) => `Ключ '${key}' отсутствует`;
3
+ _errors.getErrorDetails = (key, expectedType, receivedValue) => {
4
+ var _a;
5
+ let receivedType = '';
6
+ if (receivedValue === undefined)
7
+ receivedType = 'undefined';
8
+ else if (receivedValue === null)
9
+ receivedType = 'null';
10
+ else
11
+ receivedType = ((_a = receivedValue.constructor) === null || _a === void 0 ? void 0 : _a.name) || typeof receivedValue || 'na';
12
+ return `Проверьте тип "${key}": ожидался ${(expectedType === null || expectedType === void 0 ? void 0 : expectedType.name) || expectedType}, получен ${receivedType || 'unknown'}`;
13
+ };
14
+ _errors.joinErrors = (errorsArr, separator = '; ') => {
15
+ return (errorsArr === null || errorsArr === void 0 ? void 0 : errorsArr.join(`${separator}`)) || '';
16
+ };
3
17
  export default _errors;
@@ -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
@@ -4,10 +4,6 @@ import checkRules from "./checkRules.js";
4
4
  import _validations from "./utils/validations.js";
5
5
  import { ErrorKeywords } from "./constants/details.js";
6
6
  import { defaultSchemaKeys } from "./Schema.js";
7
- function joinErrors(separator = ';') {
8
- var _a;
9
- return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.join(`${separator} `)) || '';
10
- }
11
7
  export const getResultDefaults = () => {
12
8
  return {
13
9
  ok: null,
@@ -66,7 +62,9 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
66
62
  }
67
63
  return results;
68
64
  }
69
- if (reqs.required === true && key in data === false || data === undefined) {
65
+ if (reqs.required === true &&
66
+ (key in data === false || data === undefined || data[key] === undefined)) {
67
+ console.log(data);
70
68
  let errMsg = _errors.getMissingError(deepKey);
71
69
  if (reqs.customMessage && typeof reqs.customMessage === 'function') {
72
70
  errMsg = reqs.customMessage({
@@ -151,8 +149,7 @@ class ValidnoResult {
151
149
  this.byKeys = results.byKeys;
152
150
  }
153
151
  joinErrors(separator = '; ') {
154
- var _a;
155
- return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.join(`${separator} `)) || '';
152
+ return _errors.joinErrors(this.errors, separator);
156
153
  }
157
154
  }
158
155
  export default validate;
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.11",
3
+ "version": "0.2.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "tsc": "tsc -b src/",
8
8
  "tsc-watch": "tsc --b --watch src/",
9
+ "tscw": "tsc --b --watch src/",
9
10
  "dev": "nodemon dist/dev.js",
10
11
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
11
12
  },