validno 0.1.10 → 0.2.0

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
@@ -5,7 +5,8 @@ export const defaultSchemaKeys = [
5
5
  "eachType",
6
6
  "rules",
7
7
  "title",
8
- "customMessage"
8
+ "customMessage",
9
+ "joinErrors"
9
10
  ];
10
11
  export class Schema {
11
12
  constructor(inputSchema) {
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) {
@@ -69,9 +50,10 @@ const checkType = (key, value, requirements, keyName = key) => {
69
50
  schema: null
70
51
  }) :
71
52
  null;
72
- const baseErrDetails = getErrorDetails(keyName, requirements.type, value);
53
+ const baseErrDetails = _errors.getErrorDetails(keyName, requirements.type, value);
73
54
  const getDetails = (isOK) => isOK ? 'OK' : customErrDetails || baseErrDetails;
74
- switch (requirements.type) {
55
+ const typeBySchema = requirements.type;
56
+ switch (typeBySchema) {
75
57
  case 'any':
76
58
  result.push({
77
59
  key: keyName,
@@ -166,10 +148,13 @@ const checkType = (key, value, requirements, keyName = key) => {
166
148
  });
167
149
  break;
168
150
  default:
151
+ const isInstanceOf = value instanceof typeBySchema;
152
+ const isConstructorSame = value.constructor.name === typeBySchema.name;
153
+ const checked = isInstanceOf && isConstructorSame;
169
154
  result.push({
170
155
  key: keyName,
171
- passed: false,
172
- details: `Тип '${keyName}' не определен`
156
+ passed: checked,
157
+ details: getDetails(checked)
173
158
  });
174
159
  }
175
160
  return result;
package/dist/dev.js CHANGED
@@ -1,63 +1,52 @@
1
1
  import { Schema } from "./Schema.js";
2
+ class Parent {
3
+ constructor(value) {
4
+ this.value = value;
5
+ this.version = 1;
6
+ }
7
+ getV() {
8
+ return this.version;
9
+ }
10
+ }
11
+ class Child extends Parent {
12
+ constructor(value) {
13
+ super(value);
14
+ this.isDeep = true;
15
+ }
16
+ }
17
+ const testCust = new Parent(12345678);
18
+ const testCust2 = new Child(12345678);
2
19
  const schema = new Schema({
3
- wrongType: {
4
- type: Number,
20
+ parent1: {
21
+ type: Parent,
5
22
  required: true,
6
- title: 'Не по типу',
7
- customMessage: (input) => {
8
- return 'wrong type 1 msg ' + input.keyword;
9
- }
23
+ title: 'custom class test',
10
24
  },
11
- missingKey: {
12
- type: Date,
25
+ child1: {
26
+ type: Child,
13
27
  required: true,
14
- title: 'Отсутствующий',
15
- customMessage: (input) => {
16
- return 'missing msg ' + input.keyword;
17
- }
28
+ title: 'custom class test',
18
29
  },
19
- wrongRule1: {
20
- type: String,
21
- required: true,
22
- title: 'Wrong Rule #1',
23
- rules: {
24
- is: 'xxx'
25
- },
26
- customMessage: (input) => {
27
- return 'wrong rule #1 msg ' + input.keyword;
28
- }
29
- },
30
- wrongRule2: {
31
- type: String,
32
- required: true,
33
- title: 'Wrong Rule #2',
34
- rules: {
35
- lengthMin: 999
36
- },
37
- customMessage: (input) => {
38
- return 'wrong rule #2 msg ' + input.keyword;
39
- }
30
+ parent2: {
31
+ type: Parent,
32
+ required: true
40
33
  },
41
- wrongRule3: {
42
- type: String,
34
+ err: {
35
+ type: Error,
43
36
  required: true,
44
- title: 'Wrong Rule #3',
45
- rules: {
46
- lengthMax: 1
47
- },
48
- customMessage: (input) => {
49
- const { keyword, value, key, title, reqs, schema, rules } = input;
50
- if (keyword === 'lengthMax') {
51
- return `Значение ${value} недопустимо для поля ${title}. Минимальная длина ${rules.lengthMax} символов`;
52
- }
53
- return 'Проверьте значение, кажется, что-то не в порядке';
54
- }
37
+ title: 'custom class test',
55
38
  },
39
+ func: {
40
+ type: Function,
41
+ required: false,
42
+ }
56
43
  });
57
44
  const obj = {
58
- wrongType: 'String',
59
- wrongRule1: 'qwertyuiop',
60
- wrongRule2: 'asdfghjkl',
61
- wrongRule3: 'zxcvbnmasd'
45
+ parent1: testCust,
46
+ child1: testCust2,
47
+ parent2: testCust2,
48
+ err: new Error('ops'),
49
+ func: () => { console.log('ya tut'); }
62
50
  };
63
- console.log('finalResult', schema.validate(obj));
51
+ const res = schema.validate(obj);
52
+ console.log(res);
@@ -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;
package/dist/validate.js CHANGED
@@ -1,9 +1,9 @@
1
- import checkRules from "./checkRules.js";
2
1
  import checkType from "./checkType.js";
3
2
  import _errors from "./utils/errors.js";
3
+ import checkRules from "./checkRules.js";
4
4
  import _validations from "./utils/validations.js";
5
- import { defaultSchemaKeys } from "./Schema.js";
6
5
  import { ErrorKeywords } from "./constants/details.js";
6
+ import { defaultSchemaKeys } from "./Schema.js";
7
7
  export const getResultDefaults = () => {
8
8
  return {
9
9
  ok: null,
@@ -62,7 +62,9 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
62
62
  }
63
63
  return results;
64
64
  }
65
- 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);
66
68
  let errMsg = _errors.getMissingError(deepKey);
67
69
  if (reqs.customMessage && typeof reqs.customMessage === 'function') {
68
70
  errMsg = reqs.customMessage({
@@ -78,6 +80,9 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
78
80
  results.missed.push(deepKey);
79
81
  results.failed.push(deepKey);
80
82
  results.errors.push(errMsg);
83
+ if (deepKey in results.errorsByKeys === false)
84
+ results.errorsByKeys[deepKey] = [];
85
+ results.errorsByKeys[deepKey].push(errMsg);
81
86
  results.byKeys[deepKey] = false;
82
87
  return results;
83
88
  }
@@ -129,7 +134,22 @@ function validate(schema, data, onlyKeys) {
129
134
  results.ok = false;
130
135
  else
131
136
  results.ok = true;
132
- return results;
137
+ return new ValidnoResult(results);
133
138
  }
134
139
  ;
140
+ class ValidnoResult {
141
+ constructor(results) {
142
+ this.ok = results.ok;
143
+ this.missed = results.missed;
144
+ this.failed = results.failed;
145
+ this.passed = results.passed;
146
+ this.errors = results.errors;
147
+ this.byKeys = results.byKeys;
148
+ this.errorsByKeys = results.errorsByKeys;
149
+ this.byKeys = results.byKeys;
150
+ }
151
+ joinErrors(separator = '; ') {
152
+ return _errors.joinErrors(this.errors, separator);
153
+ }
154
+ }
135
155
  export default validate;
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.10",
3
+ "version": "0.2.0",
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
  },