validno 0.1.0 → 0.1.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/sss.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,3 @@
1
+ const _errors = {};
2
+ _errors.getMissingError = (key) => `Ключ '${key}' отсутствует`;
3
+ export default _errors;
package/dist/validate.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import checkRules from "./checkRules.js";
2
2
  import checkType from "./checkType.js";
3
3
  import { defaultSchemaKeys } from "./Schema.js";
4
+ import _errors from "./utils/errors.js";
4
5
  import _validations from "./utils/validations.js";
5
6
  const getResultDefaults = () => {
6
7
  return {
@@ -60,8 +61,13 @@ const handleReqKey = (key, data, reqs, deepKey = key) => {
60
61
  return results;
61
62
  }
62
63
  if (reqs.required === true && key in data === false || data === undefined) {
64
+ const errMsg = _errors.getMissingError(deepKey);
63
65
  missedCheck.push(false);
64
- results.errors.push(`Missing key '${deepKey}'`);
66
+ results.missed.push(deepKey);
67
+ results.failed.push(deepKey);
68
+ results.errors.push(errMsg);
69
+ results.byKeys[deepKey] = false;
70
+ results.errorsByKeys[deepKey] = [errMsg];
65
71
  return results;
66
72
  }
67
73
  const typeCheck = checkType(key, data[key], reqs, deepKey);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/checkRules.ts CHANGED
@@ -3,6 +3,8 @@ import _validations from "./utils/validations.js"
3
3
 
4
4
  export type TRule = {[key: string]: any}
5
5
 
6
+ type TLengths = string | Array<any>
7
+
6
8
  const rulesFunctions: any = {
7
9
  custom: (key: string, val: any, func: Function) => {
8
10
  return func(val)
@@ -25,38 +27,38 @@ const rulesFunctions: any = {
25
27
  details: `Значение не должно быть "${notEqualTo}"`
26
28
  }
27
29
  },
28
- min: (key: string, val: any, min: number) => {
30
+ min: (key: string, val: number, min: number) => {
29
31
  return {
30
32
  result: _validations.isNumberGte(val, min),
31
33
  details: "Значение не может быть меньше " + min
32
34
  }
33
35
  },
34
- max: (key: string, val: any, max: number) => {
36
+ max: (key: string, val: number, max: number) => {
35
37
  return {
36
38
  result: _validations.isNumberLte(val, max),
37
39
  details: "Значение не может быть больше " + max
38
40
  }
39
41
  },
40
- minMax: (key: string, val: any, minMax: [min: number, max: number]) => {
42
+ minMax: (key: string, val: number, minMax: [min: number, max: number]) => {
41
43
  const [min, max] = minMax
42
44
  return {
43
45
  result: _validations.isNumberGte(val, min) && _validations.isNumberLte(val, max),
44
46
  details: `Значение должно быть в пределах ${min}-${max}`
45
47
  }
46
48
  },
47
- length: (key: string, val: any, length: number) => {
49
+ length: (key: string, val: TLengths, length: number) => {
48
50
  return {
49
51
  result: _validations.lengthIs(val, length),
50
52
  details: "Количество символов должно быть равным " + length
51
53
  }
52
54
  },
53
- lengthNot: (key: string, val: any, lengthNot: number) => {
55
+ lengthNot: (key: string, val: TLengths, lengthNot: number) => {
54
56
  return {
55
57
  result: _validations.lengthNot(val, lengthNot),
56
58
  details: "Количество символов не должно быть равным " + lengthNot
57
59
  }
58
60
  },
59
- lengthMinMax: (key: string, val: any, minMax: [min: number, max: number]) => {
61
+ lengthMinMax: (key: string, val: TLengths, minMax: [min: number, max: number]) => {
60
62
  const [min, max] = minMax
61
63
 
62
64
  return {
@@ -64,13 +66,13 @@ const rulesFunctions: any = {
64
66
  details: `Длина должна быть от ${min} до ${max} символов`
65
67
  }
66
68
  },
67
- lengthMin: (key: string, val: any, min: number) => {
69
+ lengthMin: (key: string, val: TLengths, min: number) => {
68
70
  return {
69
71
  result: _validations.lengthMin(val, min),
70
72
  details: `Длина не может быть меньше ${min} символов`
71
73
  }
72
74
  },
73
- lengthMax: (key: string, val: any, max: number) => {
75
+ lengthMax: (key: string, val: TLengths, max: number) => {
74
76
  return {
75
77
  result: _validations.lengthMax(val, max),
76
78
  details: `Длина не может быть больше ${max} символов`
@@ -0,0 +1,6 @@
1
+ const _errors: {[key: string]: Function} = {}
2
+
3
+ _errors.getMissingError = (key: string) => `Ключ '${key}' отсутствует`
4
+
5
+ export default _errors
6
+
package/src/validate.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import checkRules from "./checkRules.js";
2
2
  import checkType from "./checkType.js";
3
3
  import { defaultSchemaKeys, Schema, TSchemaInput } from "./Schema.js";
4
+ import _errors from "./utils/errors.js";
4
5
  import _validations from "./utils/validations.js";
5
6
 
6
7
  type TResult = {
@@ -98,8 +99,14 @@ const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey = key)
98
99
  // Check missing keys
99
100
  // @ts-ignore
100
101
  if (reqs.required === true && key in data === false || data === undefined) {
102
+ const errMsg = _errors.getMissingError(deepKey)
103
+
101
104
  missedCheck.push(false)
102
- results.errors.push(`Missing key '${deepKey}'`)
105
+ results.missed.push(deepKey)
106
+ results.failed.push(deepKey)
107
+ results.errors.push(errMsg)
108
+ results.byKeys[deepKey] = false
109
+ results.errorsByKeys[deepKey] = [errMsg]
103
110
  return results
104
111
  }
105
112
 
@@ -0,0 +1,131 @@
1
+ import {describe, expect, test} from '@jest/globals';
2
+ import { Schema } from '../dist/Schema.js';
3
+ import _errors from '../dist/utils/errors.js';
4
+
5
+ describe('Тестирование обработки пропущенных свойста', () => {
6
+ test('Отсутствующий ключ правильно отображается в результате', () => {
7
+ const missedKey = 'testKey'
8
+ const missedKey2 = 'testKey2'
9
+
10
+ const okKey = 'okKey'
11
+
12
+ const scheme = new Schema({
13
+ [missedKey]: {
14
+ required: true,
15
+ type: String
16
+ },
17
+ [missedKey2]: {
18
+ required: true,
19
+ type: String
20
+ },
21
+ [okKey]: {
22
+ required: true,
23
+ type: String
24
+ }
25
+ })
26
+
27
+ const obj = {
28
+ [okKey]: 'string'
29
+ }
30
+
31
+ const result = scheme.validate(obj)
32
+
33
+ expect(result).toEqual({
34
+ ok: false,
35
+ failed: [missedKey, missedKey2],
36
+ missed: [missedKey, missedKey2],
37
+ errors: [
38
+ _errors.getMissingError(missedKey),
39
+ _errors.getMissingError(missedKey2)
40
+ ],
41
+ passed: [okKey],
42
+ byKeys: {
43
+ [missedKey]: false,
44
+ [missedKey2]: false,
45
+ [okKey]: true
46
+ },
47
+ errorsByKeys: {
48
+ [okKey]: [],
49
+ [missedKey]: [_errors.getMissingError(missedKey)],
50
+ [missedKey2]: [_errors.getMissingError(missedKey2)]
51
+ }
52
+ })
53
+ })
54
+
55
+ test('Присутствующий ключ правильно отображается в результате', () => {
56
+ const key = 'testKey'
57
+ const key2 = 'testKey2'
58
+
59
+ const scheme = new Schema({
60
+ [key]: {
61
+ required: true,
62
+ type: String
63
+ },
64
+ [key2]: {
65
+ required: true,
66
+ type: String
67
+ }
68
+ })
69
+
70
+ const obj = {
71
+ [key]: 'string',
72
+ [key2]: 'string'
73
+ }
74
+
75
+ const result = scheme.validate(obj)
76
+
77
+ expect(result).toEqual({
78
+ ok: true,
79
+ failed: [],
80
+ missed: [],
81
+ errors: [],
82
+ passed: [key, key2],
83
+ byKeys: {
84
+ [key]: true,
85
+ [key2]: true
86
+ },
87
+ errorsByKeys: {
88
+ [key]: [],
89
+ [key2]: []
90
+ }
91
+ })
92
+ })
93
+
94
+ test('Отсутствующий необязательный ключ правильно отображается в результах', () => {
95
+ const key = 'testKey'
96
+ const key2 = 'testKey2'
97
+
98
+ const scheme = new Schema({
99
+ [key]: {
100
+ required: false,
101
+ type: String
102
+ },
103
+ [key2]: {
104
+ required: true,
105
+ type: String
106
+ }
107
+ })
108
+
109
+ const obj = {
110
+ [key2]: 'string'
111
+ }
112
+
113
+ const result = scheme.validate(obj)
114
+
115
+ expect(result).toEqual({
116
+ ok: true,
117
+ failed: [],
118
+ missed: [],
119
+ errors: [],
120
+ passed: [key, key2],
121
+ byKeys: {
122
+ [key]: true,
123
+ [key2]: true
124
+ },
125
+ errorsByKeys: {
126
+ [key]: [],
127
+ [key2]: []
128
+ }
129
+ })
130
+ })
131
+ })
File without changes
@@ -1,7 +1,7 @@
1
1
  import {describe, expect, test} from '@jest/globals';
2
2
  import { Schema } from '../dist/Schema.js';
3
3
 
4
- describe("Проверка каждого типа по отдельности", () => {
4
+ describe('Проверка каждого типа по отдельности', () => {
5
5
  test('Тест String', () => {
6
6
  const key = 'testKey'
7
7
  const scheme = new Schema({
package/src/app.ts DELETED
@@ -1,199 +0,0 @@
1
- import Schema from "./index.js";
2
-
3
- const testSchema1 = new Schema({
4
- isMissing: {
5
- required: false,
6
- type: 'any',
7
- },
8
- category: {
9
- required: true,
10
- type: String,
11
- rules: {
12
- lengthMinMax: [5,10]
13
- }
14
- },
15
- costPrice: {
16
- required: true,
17
- type: Number,
18
- },
19
- dateFrom: {
20
- required: true,
21
- type: Date,
22
-
23
- },
24
- isActive: {
25
- required: true,
26
- type: Boolean,
27
- },
28
- list: {
29
- required: true,
30
- type: Array,
31
- },
32
- listTyped: {
33
- required: true,
34
- type: Array,
35
- eachType: Number
36
- },
37
- xNull: {
38
- required: true,
39
- type: [null, Number]
40
- },
41
- reg: {
42
- required: true,
43
- type: RegExp
44
- },
45
- oneOfStr: {
46
- required: false,
47
- type: String,
48
- rules: {
49
- enum: ['done', 'new', 'cancel']
50
- }
51
- },
52
- email: {
53
- required: true,
54
- type: String,
55
- rules: {
56
- isEmail: true,
57
- lengthMin: 55
58
- },
59
- }
60
- }
61
- )
62
-
63
- const okObj1 = {
64
- unknown: 1,
65
- category: 'xx',
66
- isMissing: '...',
67
- isActive: true,
68
- costPrice: 100,
69
- dateFrom: new Date(),
70
- list: [],
71
- listTyped: [1, 2, 3, 0.00001],
72
- xNull: null,
73
- reg: new RegExp('.*'),
74
- oneOfStr: 'done',
75
- email: 'xxx@gmail.com'
76
- }
77
-
78
- const okObjResult = testSchema1.validate(okObj1)
79
- console.log(okObjResult)
80
-
81
- // const rulesSchema = new Schema({
82
- // xMultiple: {
83
- // required: true,
84
- // type: [String, null]
85
- // },
86
- // xNumb: {
87
- // required: true,
88
- // type: 'any',
89
- // rules: {
90
- // custom: (v: any) => v === 3,
91
- // is: 3,
92
- // isNot: 2,
93
- // min: 3,
94
- // max: 3,
95
- // regex: /3|4/
96
- // }
97
- // },
98
- // xStr: {
99
- // required: true,
100
- // type: String,
101
- // rules: {
102
- // custom: (v: any) => typeof v === 'string',
103
- // is: 'lol',
104
- // isNot: 'rofl',
105
- // length: 3,
106
- // lengthNot: 999,
107
- // lengthMin: 3,
108
- // lengthMax: 3,
109
- // regex: /\D{3}/
110
- // }
111
- // },
112
- // xArrEach: {
113
- // required: true,
114
- // type: Array,
115
- // eachType: [Number, String]
116
- // },
117
- // str: {
118
- // required: false,
119
- // type: String
120
- // }
121
- // })
122
-
123
- // const rulesObj = {
124
- // xMultiple: 'str',
125
- // xNumb: 3,
126
- // xStr: 'lol',
127
- // xArrEach: [3,2,1,null],
128
- // str: '233'
129
- // }
130
-
131
- // const rulesObjResult = Validator.validate(rulesSchema, rulesObj)
132
- // console.log(rulesObjResult)
133
-
134
- const deepObjScheme = new Schema({
135
- nameStr: {
136
- required: true,
137
- type: String,
138
- rules: {
139
- lengthMin: 5
140
- }
141
- },
142
- address: {
143
- city: {
144
- required: true,
145
- type: String,
146
- },
147
- street: {
148
- required: true,
149
- type: String
150
- },
151
- coords: {
152
- x: {
153
- required: true,
154
- type: Number,
155
- },
156
- y: {
157
- required: true,
158
- type: Number,
159
- rules: {
160
- min: 1
161
- }
162
- }
163
- },
164
- },
165
- deep1: {
166
- deep2: {
167
- deep3: {
168
- deep4: {
169
- required: true,
170
- type: String,
171
- rules: {
172
- lengthMinMax: [10,15]
173
- }
174
- }
175
- }
176
- }
177
- }
178
- })
179
-
180
- const deepTestObj = {
181
- nameStr: "Alek",
182
- address: {
183
- city: 'Moscow',
184
- street: "41, Komsomolskiy",
185
- coords: {
186
- x: 1,
187
- y: 2
188
- }
189
- },
190
- deep1: {
191
- deep2: {
192
- deep3: {
193
- deep4: 'ssss'
194
- }
195
- }
196
- }
197
- }
198
-
199
- console.log('deepObj', deepObjScheme.validate(deepTestObj).ok)