validno 0.1.10 → 0.1.11

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/dev.js CHANGED
@@ -1,63 +1,41 @@
1
1
  import { Schema } from "./Schema.js";
2
2
  const schema = new Schema({
3
- wrongType: {
3
+ ops1: {
4
4
  type: Number,
5
5
  required: true,
6
- title: 'Не по типу',
6
+ title: 'titl',
7
7
  customMessage: (input) => {
8
- return 'wrong type 1 msg ' + input.keyword;
8
+ return 'Wrong type 1 msg ' + input.keyword;
9
9
  }
10
10
  },
11
- missingKey: {
12
- type: Date,
13
- required: true,
14
- title: 'Отсутствующий',
15
- customMessage: (input) => {
16
- return 'missing msg ' + input.keyword;
17
- }
18
- },
19
- wrongRule1: {
11
+ ops2: {
20
12
  type: String,
21
13
  required: true,
22
- title: 'Wrong Rule #1',
23
- rules: {
24
- is: 'xxx'
25
- },
14
+ title: 'titl',
26
15
  customMessage: (input) => {
27
- return 'wrong rule #1 msg ' + input.keyword;
16
+ return 'Missing msg ' + input.keyword;
28
17
  }
29
18
  },
30
- wrongRule2: {
19
+ miss: {
31
20
  type: String,
32
21
  required: true,
33
- title: 'Wrong Rule #2',
34
- rules: {
35
- lengthMin: 999
36
- },
22
+ title: 'titl',
37
23
  customMessage: (input) => {
38
- return 'wrong rule #2 msg ' + input.keyword;
24
+ return 'Missing msg ' + input.keyword;
39
25
  }
40
- },
41
- wrongRule3: {
42
- type: String,
43
- 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
- }
55
- },
26
+ }
56
27
  });
57
28
  const obj = {
58
- wrongType: 'String',
59
- wrongRule1: 'qwertyuiop',
60
- wrongRule2: 'asdfghjkl',
61
- wrongRule3: 'zxcvbnmasd'
29
+ ops1: 'String',
30
+ ops2: 123
31
+ };
32
+ const obj2 = {
33
+ ops1: 222,
34
+ ops2: 'str',
35
+ miss: 'here'
62
36
  };
63
- console.log('finalResult', schema.validate(obj));
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());
package/dist/validate.js CHANGED
@@ -1,9 +1,13 @@
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
+ function joinErrors(separator = ';') {
8
+ var _a;
9
+ return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.join(`${separator} `)) || '';
10
+ }
7
11
  export const getResultDefaults = () => {
8
12
  return {
9
13
  ok: null,
@@ -78,6 +82,9 @@ export function handleReqKey(key, data, reqs, deepKey = key) {
78
82
  results.missed.push(deepKey);
79
83
  results.failed.push(deepKey);
80
84
  results.errors.push(errMsg);
85
+ if (deepKey in results.errorsByKeys === false)
86
+ results.errorsByKeys[deepKey] = [];
87
+ results.errorsByKeys[deepKey].push(errMsg);
81
88
  results.byKeys[deepKey] = false;
82
89
  return results;
83
90
  }
@@ -129,7 +136,23 @@ function validate(schema, data, onlyKeys) {
129
136
  results.ok = false;
130
137
  else
131
138
  results.ok = true;
132
- return results;
139
+ return new ValidnoResult(results);
133
140
  }
134
141
  ;
142
+ class ValidnoResult {
143
+ constructor(results) {
144
+ this.ok = results.ok;
145
+ this.missed = results.missed;
146
+ this.failed = results.failed;
147
+ this.passed = results.passed;
148
+ this.errors = results.errors;
149
+ this.byKeys = results.byKeys;
150
+ this.errorsByKeys = results.errorsByKeys;
151
+ this.byKeys = results.byKeys;
152
+ }
153
+ joinErrors(separator = '; ') {
154
+ var _a;
155
+ return ((_a = this.errors) === null || _a === void 0 ? void 0 : _a.join(`${separator} `)) || '';
156
+ }
157
+ }
135
158
  export default validate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {