validno 0.1.2 → 0.1.4

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/Schema.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import validate from "./validate.js";
2
- import validateKey from "./validateKey.js";
3
2
  export const defaultSchemaKeys = [
4
3
  "required",
5
4
  "type",
@@ -10,10 +9,7 @@ export class Schema {
10
9
  constructor(inputSchema) {
11
10
  this.schema = inputSchema;
12
11
  }
13
- validate(data) {
14
- return validate.call(this, this, data);
15
- }
16
- validateKey(key, data) {
17
- return validateKey.call(this, this, data, key);
12
+ validate(data, keys) {
13
+ return validate.call(this, this, data, keys);
18
14
  }
19
15
  }
package/dist/app.js CHANGED
@@ -4,19 +4,42 @@ const schema = new Schema({
4
4
  type: String,
5
5
  required: true,
6
6
  },
7
+ date: {
8
+ type: Date,
9
+ required: true,
10
+ },
11
+ obj: {
12
+ type: Object,
13
+ required: true,
14
+ },
15
+ bool: {
16
+ type: Boolean,
17
+ required: true,
18
+ },
7
19
  val2: {
8
20
  type: Number,
9
21
  required: true,
10
22
  rules: {
11
23
  min: 3
12
24
  }
13
- }
25
+ },
26
+ srtOrNum: {
27
+ type: [String, Number],
28
+ required: true,
29
+ },
30
+ nl: {
31
+ type: null,
32
+ required: true,
33
+ },
14
34
  });
15
35
  const testObj = {
16
36
  val: 'string',
17
- val2: 1
37
+ val2: 4,
38
+ obj: {},
39
+ srtOrNum: ['ss'],
40
+ date: new Date(),
41
+ bool: true,
42
+ nl: null
18
43
  };
19
- const res = schema.validate(testObj);
20
- const res2 = schema.validateKey('val2', testObj);
21
- console.log(res);
22
- console.log(res2);
44
+ const resAll = schema.validate(testObj);
45
+ const str = 'test';
@@ -30,7 +30,7 @@ const rulesFunctions = {
30
30
  isNot: (key, val, notEqualTo) => {
31
31
  return {
32
32
  result: _validations.isNot(val, notEqualTo),
33
- details: `Значение не должно быть "${notEqualTo}"`
33
+ details: `Значение "${notEqualTo}" недопустимо`
34
34
  };
35
35
  },
36
36
  min: (key, val, min) => {
@@ -93,7 +93,7 @@ const rulesFunctions = {
93
93
  enum: (key, value, allowedList) => {
94
94
  return {
95
95
  result: allowedList.includes(value),
96
- details: `Значение "${value}" не допустимо`
96
+ details: `Значение "${value}" недопустимо`
97
97
  };
98
98
  }
99
99
  };
@@ -104,17 +104,15 @@ const checkRules = (key, value, requirements) => {
104
104
  };
105
105
  if (requirements.required !== true && value === undefined)
106
106
  return result;
107
- if ('rules' in requirements === false)
108
- return result;
109
107
  if (!requirements || !requirements.rules || !Object.keys(requirements.rules).length) {
110
108
  return result;
111
109
  }
112
110
  const rules = requirements.rules;
113
111
  const allResults = [];
114
- const allRules = Object.keys(rules);
112
+ const allRulesKeys = Object.keys(rules);
115
113
  let i = 0;
116
- while (i < allRules.length) {
117
- const ruleName = allRules[i];
114
+ while (i < allRulesKeys.length) {
115
+ const ruleName = allRulesKeys[i];
118
116
  if (ruleName in rulesFunctions === false) {
119
117
  i++;
120
118
  continue;
package/dist/checkType.js CHANGED
@@ -40,6 +40,7 @@ const checkTypeMultiple = (key, value, requirements, keyName = key) => {
40
40
  return result;
41
41
  };
42
42
  const checkType = (key, value, requirements, keyName = key) => {
43
+ const isNotNull = value !== null;
43
44
  if (value === undefined && requirements.required) {
44
45
  return [{ key: keyName, passed: false, details: `Key ${keyName} is missing` }];
45
46
  }
@@ -64,7 +65,7 @@ const checkType = (key, value, requirements, keyName = key) => {
64
65
  });
65
66
  break;
66
67
  case Number:
67
- const isNumber = _validations.isNumber(value);
68
+ const isNumber = isNotNull && value.constructor === Number;
68
69
  result.push({
69
70
  key: keyName,
70
71
  passed: isNumber,
@@ -72,7 +73,7 @@ const checkType = (key, value, requirements, keyName = key) => {
72
73
  });
73
74
  break;
74
75
  case String:
75
- const isString = _validations.isString(value);
76
+ const isString = isNotNull && value.constructor === String;
76
77
  result.push({
77
78
  key: keyName,
78
79
  passed: isString,
@@ -80,7 +81,7 @@ const checkType = (key, value, requirements, keyName = key) => {
80
81
  });
81
82
  break;
82
83
  case Date:
83
- const isDate = _validations.isDate(value);
84
+ const isDate = isNotNull && value.constructor === Date;
84
85
  result.push({
85
86
  key: keyName,
86
87
  passed: isDate,
@@ -88,7 +89,7 @@ const checkType = (key, value, requirements, keyName = key) => {
88
89
  });
89
90
  break;
90
91
  case Boolean:
91
- const isBoolean = _validations.isBoolean(value);
92
+ const isBoolean = isNotNull && value.constructor === Boolean;
92
93
  result.push({
93
94
  key: keyName,
94
95
  passed: isBoolean,
@@ -96,7 +97,15 @@ const checkType = (key, value, requirements, keyName = key) => {
96
97
  });
97
98
  break;
98
99
  case Array:
99
- const isArray = _validations.isArray(value);
100
+ const isArray = isNotNull && value.constructor === Array;
101
+ if (!isArray) {
102
+ result.push({
103
+ key: keyName,
104
+ passed: false,
105
+ details: getErrorDetails(keyName, requirements.type, value)
106
+ });
107
+ break;
108
+ }
100
109
  let isEachChecked = { passed: true, details: "" };
101
110
  if ('eachType' in requirements) {
102
111
  isEachChecked.passed = value.every((el) => {
@@ -116,7 +125,7 @@ const checkType = (key, value, requirements, keyName = key) => {
116
125
  });
117
126
  break;
118
127
  case Object:
119
- const isObject = _validations.isObject(value);
128
+ const isObject = _validations.isObject(value) && value.constructor === Object;
120
129
  result.push({
121
130
  key: keyName,
122
131
  passed: isObject,
@@ -132,7 +141,7 @@ const checkType = (key, value, requirements, keyName = key) => {
132
141
  });
133
142
  break;
134
143
  case null:
135
- const isNull = _validations.isNull(value);
144
+ const isNull = value === null;
136
145
  result.push({
137
146
  key: keyName,
138
147
  passed: isNull,
@@ -143,7 +152,7 @@ const checkType = (key, value, requirements, keyName = key) => {
143
152
  result.push({
144
153
  key: keyName,
145
154
  passed: false,
146
- details: `No type specified for key: '${keyName}'`
155
+ details: `Тип '${keyName}' не определен`
147
156
  });
148
157
  }
149
158
  return result;
package/dist/validate.js CHANGED
@@ -101,11 +101,17 @@ export const handleReqKey = (key, data, reqs, deepKey = key) => {
101
101
  results.byKeys[deepKey] = (missedCheck.length + typeChecked.length + rulesChecked.length) === 0;
102
102
  return results;
103
103
  };
104
- const validate = (schema, data) => {
104
+ const isCheckNeeded = (key, hasLimits, onlyKeys) => {
105
+ return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && (onlyKeys === null || onlyKeys === void 0 ? void 0 : onlyKeys.includes(key)));
106
+ };
107
+ const validate = (schema, data, onlyKeys) => {
105
108
  let results = getResultDefaults();
109
+ const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0);
106
110
  for (const [key, reqs] of Object.entries(schema.schema)) {
107
- const keyResult = handleReqKey(key, data, reqs);
108
- results = mergeResults(results, keyResult);
111
+ if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
112
+ const keyResult = handleReqKey(key, data, reqs);
113
+ results = mergeResults(results, keyResult);
114
+ }
109
115
  }
110
116
  if (results.failed.length)
111
117
  results.ok = false;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
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
- "dev": "nodemon dist/app.js",
9
+ "dev": "nodemon dist/dev.js",
10
10
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
11
11
  },
12
12
  "author": "lesha2r",
@@ -22,5 +22,16 @@
22
22
  "@types/node": "^18.14.2",
23
23
  "eslint": "^8.14.0",
24
24
  "jest": "^29.7.0"
25
- }
25
+ },
26
+ "directories": {
27
+ "test": "tests"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/lesha2r/validno.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/lesha2r/validno/issues"
35
+ },
36
+ "homepage": "https://github.com/lesha2r/validno#readme"
26
37
  }
package/src/Schema.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import validate from "./validate.js"
2
- import validateKey from "./validateKey.js"
3
2
 
4
3
  export type TSchemaItem = {
5
4
  required: boolean,
@@ -35,11 +34,7 @@ export class Schema {
35
34
  this.schema = inputSchema
36
35
  }
37
36
 
38
- validate(data: any) {
39
- return validate.call(this, this, data)
40
- }
41
-
42
- validateKey(key: string, data: any) {
43
- return validateKey.call(this, this, data, key)
37
+ validate(data: any, keys?: string | string[]) {
38
+ return validate.call(this, this, data, keys)
44
39
  }
45
40
  }
package/src/checkRules.ts CHANGED
@@ -39,7 +39,7 @@ const rulesFunctions: any = {
39
39
  isNot: (key: string, val: any, notEqualTo: any) => {
40
40
  return {
41
41
  result: _validations.isNot(val, notEqualTo),
42
- details: `Значение не должно быть "${notEqualTo}"`
42
+ details: `Значение "${notEqualTo}" недопустимо`
43
43
  }
44
44
  },
45
45
  min: (key: string, val: number, min: number) => {
@@ -104,7 +104,7 @@ const rulesFunctions: any = {
104
104
  enum: (key: string, value: any, allowedList: any[]) => {
105
105
  return {
106
106
  result: allowedList.includes(value),
107
- details: `Значение "${value}" не допустимо`
107
+ details: `Значение "${value}" недопустимо`
108
108
  }
109
109
  }
110
110
  };
@@ -120,11 +120,10 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
120
120
  details: []
121
121
  };
122
122
 
123
- // If value is not provided AND not required by schema
123
+ // Значение отсутствует, но НЕ является обязательным
124
124
  if (requirements.required !== true && value === undefined) return result
125
125
 
126
- // No rules specified for the key
127
- if ('rules' in requirements === false) return result
126
+ // Для этого ключа нет правил
128
127
  if (!requirements || !requirements.rules || !Object.keys(requirements.rules).length) {
129
128
  return result
130
129
  }
@@ -132,12 +131,13 @@ const checkRules = (key: string, value: any, requirements: TSchemaItem) => {
132
131
  const rules: TRule = requirements.rules
133
132
 
134
133
  const allResults = []
135
- const allRules = Object.keys(rules)
134
+ const allRulesKeys = Object.keys(rules)
136
135
 
137
136
  let i = 0;
138
- while (i < allRules.length) {
139
- const ruleName = allRules[i]
137
+ while (i < allRulesKeys.length) {
138
+ const ruleName = allRulesKeys[i]
140
139
 
140
+ // Если правило, указанное для ключа, отсутствует в списке доступных
141
141
  if (ruleName in rulesFunctions === false) {
142
142
  i++
143
143
  continue
package/src/checkType.ts CHANGED
@@ -18,6 +18,7 @@ const getErrorDetails = (key: string, expectedType: any, receivedValue: any) =>
18
18
 
19
19
  const checkTypeMultiple = (key: string, value: any, requirements: TSchemaItem | TSchemaInput, keyName = key) => {
20
20
  const constructorNames = requirements.type.map((el:any) => String(el?.name || el))
21
+
21
22
  const result = {
22
23
  key: keyName,
23
24
  passed: false,
@@ -44,6 +45,8 @@ const checkTypeMultiple = (key: string, value: any, requirements: TSchemaItem |
44
45
  type TCheckTypeResult = {key: string, passed: boolean, details: string}
45
46
 
46
47
  const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaInput, keyName = key): TCheckTypeResult[] => {
48
+ const isNotNull = value !== null
49
+
47
50
  if (value === undefined && requirements.required) {
48
51
  return [{key: keyName, passed: false, details: `Key ${keyName} is missing`}]
49
52
  }
@@ -72,7 +75,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
72
75
  })
73
76
  break;
74
77
  case Number:
75
- const isNumber = _validations.isNumber(value)
78
+ const isNumber = isNotNull && value.constructor === Number
79
+
76
80
  result.push({
77
81
  key: keyName,
78
82
  passed: isNumber,
@@ -81,7 +85,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
81
85
 
82
86
  break;
83
87
  case String:
84
- const isString = _validations.isString(value);
88
+ const isString = isNotNull && value.constructor === String
89
+
85
90
  result.push({
86
91
  key: keyName,
87
92
  passed: isString,
@@ -89,7 +94,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
89
94
  })
90
95
  break;
91
96
  case Date:
92
- const isDate = _validations.isDate(value)
97
+ const isDate = isNotNull && value.constructor === Date
98
+
93
99
  result.push({
94
100
  key: keyName,
95
101
  passed: isDate,
@@ -97,7 +103,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
97
103
  })
98
104
  break;
99
105
  case Boolean:
100
- const isBoolean = _validations.isBoolean(value);
106
+ const isBoolean = isNotNull && value.constructor === Boolean
107
+
101
108
  result.push({
102
109
  key: keyName,
103
110
  passed: isBoolean,
@@ -105,7 +112,17 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
105
112
  })
106
113
  break;
107
114
  case Array:
108
- const isArray = _validations.isArray(value);
115
+ const isArray = isNotNull && value.constructor === Array
116
+
117
+ if (!isArray) {
118
+ result.push({
119
+ key: keyName,
120
+ passed: false,
121
+ details: getErrorDetails(keyName, requirements.type, value)
122
+ });
123
+
124
+ break;
125
+ }
109
126
 
110
127
  let isEachChecked = { passed: true, details: ""}
111
128
 
@@ -132,7 +149,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
132
149
 
133
150
  break;
134
151
  case Object:
135
- const isObject = _validations.isObject(value)
152
+ const isObject = _validations.isObject(value) && value.constructor === Object
136
153
  result.push({
137
154
  key: keyName,
138
155
  passed: isObject,
@@ -150,7 +167,8 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
150
167
 
151
168
  break;
152
169
  case null:
153
- const isNull = _validations.isNull(value);
170
+ const isNull = value === null
171
+
154
172
  result.push({
155
173
  key: keyName,
156
174
  passed: isNull,
@@ -162,7 +180,7 @@ const checkType = (key: string, value: any, requirements: TSchemaItem | TSchemaI
162
180
  result.push({
163
181
  key: keyName,
164
182
  passed: false,
165
- details: `No type specified for key: '${keyName}'`
183
+ details: `Тип '${keyName}' не определен`
166
184
  })
167
185
  }
168
186
 
package/src/validate.ts CHANGED
@@ -154,14 +154,21 @@ export const handleReqKey = (key: string, data: any, reqs: TSchemaInput, deepKey
154
154
  return results
155
155
  }
156
156
 
157
- const validate = (schema: Schema, data: any): TResult => {
157
+ const isCheckNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | string[]) => {
158
+ return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && onlyKeys?.includes(key))
159
+ }
160
+
161
+ const validate = (schema: Schema, data: any, onlyKeys?: string | string[]): TResult => {
158
162
  let results: TResult = getResultDefaults()
159
-
163
+ const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0)
164
+
160
165
  for (const [key, reqs] of Object.entries(schema.schema)) {
161
- // @ts-ignore
162
- const keyResult = handleReqKey(key, data, reqs)
166
+ if (isCheckNeeded(key, areKeysLimited, onlyKeys)) {
167
+ // @ts-ignore
168
+ const keyResult = handleReqKey(key, data, reqs)
163
169
 
164
- results = mergeResults(results, keyResult)
170
+ results = mergeResults(results, keyResult)
171
+ }
165
172
  }
166
173
 
167
174
  if (results.failed.length) results.ok = false
@@ -280,6 +280,10 @@ describe("Проверка всех типов сразу", () => {
280
280
  },
281
281
  date: {
282
282
  type: Date
283
+ },
284
+ test: {
285
+ type: Object,
286
+ required: true
283
287
  }
284
288
  })
285
289
 
@@ -290,7 +294,8 @@ describe("Проверка всех типов сразу", () => {
290
294
  arr: null,
291
295
  null: {},
292
296
  bool: new Date(),
293
- date: true
297
+ date: true,
298
+ test: undefined
294
299
  }
295
300
 
296
301
  const res = schema.validate(obj)
package/src/app.ts DELETED
@@ -1,59 +0,0 @@
1
- import Schema from "./index.js";
2
-
3
- // const testSchema = new Schema({
4
- // field: {
5
- // type: Number,
6
- // required: true,
7
- // rules: {
8
- // lengthMin: 99
9
- // }
10
- // },
11
- // field2: {
12
- // type: "xxx",
13
- // required: true,
14
- // rules: {
15
- // lengthMin: 10
16
- // }
17
- // }
18
- // });
19
-
20
- // class StringOwn {
21
- // constructor(v: any) {
22
- // v = v
23
- // }
24
- // }
25
-
26
- // const strOwn = new StringOwn('xxxxuis')
27
-
28
- // const body = {
29
- // "field": 33,
30
- // "field2": "Ssdasjk"
31
- // }
32
-
33
- // const validationResult = testSchema.validate(body);
34
-
35
-
36
- const schema = new Schema({
37
- val: {
38
- type: String,
39
- required: true,
40
- },
41
- val2: {
42
- type: Number,
43
- required: true,
44
- rules: {
45
- min: 3
46
- }
47
- }
48
- })
49
-
50
- const testObj = {
51
- val: 'string',
52
- val2: 1
53
- }
54
-
55
- const res = schema.validate(testObj)
56
- const res2 = schema.validateKey('val2', testObj)
57
-
58
- console.log(res)
59
- console.log(res2)
@@ -1,21 +0,0 @@
1
- import { Schema } from "./Schema.js";
2
- import { getResultDefaults, handleReqKey, mergeResults, TResult } from "./validate.js";
3
-
4
- const validateKey = (schema: Schema, data: any, key: string): TResult => {
5
- if (key in schema.schema === false) {
6
- throw new Error(`Ключ ${key} отсутствует в схеме`)
7
- }
8
-
9
- let results: TResult = getResultDefaults()
10
- //@ts-ignore
11
- const keyResult = handleReqKey(key, data, schema.schema[key])
12
-
13
- results = mergeResults(results, keyResult)
14
-
15
- if (results.failed.length) results.ok = false
16
- else results.ok = true
17
-
18
- return results;
19
- };
20
-
21
- export default validateKey;
File without changes