validno 0.2.0 → 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
@@ -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);
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {