-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (151 loc) · 5.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"use strict";
const Context = require("./logic/Context");
const connectAndSort = require("./logic/connectAndSort");
const delay = require("./utils/delay");
const promiseall = require("nlab/promiseall");
const ValidatorLogicError = require("./ValidatorLogicError");
// const log = require('../log/logn');
/**
* import validator, { test } from '@stopsopa/validator';
*
* @param data
* @param constraints
* @param options
* @returns {string}
*/
const modes = {
exceptionalThrow: "exceptionalThrow",
justStop: "justStop",
raw: "raw",
errors: "errors",
firstError: "firstError",
};
const modesList = Object.keys(modes);
const validator = (value, constraints, extra, debug) => {
const errorMode = extra && typeof extra.errorMode === "string" ? extra.errorMode : modes.exceptionalThrow;
if (!modesList.includes(errorMode)) {
throw new Error(
`@stopsopa/validator errorMode should be one of [${modesList.join(", ")}] but it is '${errorMode}'`
);
}
const context = new Context(value, extra);
const connected = connectAndSort({
value,
constraints,
context,
path: extra ? extra.path : undefined,
final: true,
});
let promise = Promise.resolve();
while (connected.length) {
(function (list) {
promise = promise.then(() => promiseall(list.map((c) => c())));
if (debug > 1) {
promise = promise.then(...delay.then(2500));
}
if (debug > 0) {
promise = promise.then(
(a) => {
console.log("debug resolved:", JSON.stringify(a));
return a;
},
(a) => {
console.log("debug rejected:", JSON.stringify(a));
return Promise.reject(a);
}
);
}
})(connected.shift());
}
const end = () => context.getViolations();
/**
* Other modes are:
* 'exceptionalThrow' (default) -
*
* 'justStop' - this will return list like in success mode (list of violations) as resolved promise
* the only side effect will be that it will not execute next promiseall
* (this was old default behaviour)
* next promiseall will not be triggered
*
* 'raw' - just raw list of results from last promiseall as rejected promise
* next promiseall will not be triggered
*
* 'errors' - just error from last result of last promiseall as rejected promise
* if last list have other resolved=true then those will be filtered out
* next promiseall will not be triggered
*
* 'firstError' - return first error and don't run next promiseall as rejected promise
* next promiseall will not be triggered
*
* NOTICE:
*
* Generally all above options (including default 'first') will stop processing next promiseall
* and result in rejected promise in case when any Callback validator return rejected promise.
*
* If you wish to run all promiseall then simply don't throw any errors from any defined Callback validator
*/
return promise.then(end, (e) => {
/**
* This catch generally means that something really returned rejected promise
* and it needs to be handled somehow.
* Another thing that could also happen is not triggering "next" promiseall.
* Either way this block defines what should happen next.
*
* By default (in case of mode 'exceptionalThrow') this block will just return violations using end() internal function
* or throw when Callback type validators throws at least one ValidatorLogicError type error.
* In that case first error of this type will be rethrown.
*/
try {
if (errorMode === modes.justStop) {
return end();
}
if (errorMode === modes.raw) {
return Promise.reject(e);
}
/**
* Filtering out resolved=false states and normalizing them to errors
*/
let errors = e
.filter((e) => e.resolved === false)
.map((e) => {
if (e.data instanceof Error) {
return e.data;
}
return new Error(String(e.data));
});
const exceptionalThrow = errors.find((e) => e instanceof ValidatorLogicError);
if (errorMode === modes.exceptionalThrow && exceptionalThrow) {
return Promise.reject(exceptionalThrow);
}
if (errorMode === modes.errors) {
return Promise.reject(errors);
}
let firstError = errors.shift();
if (errorMode === modes.firstError && firstError) {
return Promise.reject(firstError);
}
return end();
} catch (e) {
return Promise.reject(e);
}
});
};
validator.Required = require("./constraints/Required");
validator.Optional = require("./constraints/Optional");
validator.Collection = require("./constraints/Collection");
validator.All = require("./constraints/All");
validator.Blank = require("./constraints/Blank");
validator.Callback = require("./constraints/Callback");
validator.Choice = require("./constraints/Choice");
validator.Count = require("./constraints/Count");
validator.Email = require("./constraints/Email");
validator.IsFalse = require("./constraints/IsFalse");
validator.IsNull = require("./constraints/IsNull");
validator.IsTrue = require("./constraints/IsTrue");
validator.Length = require("./constraints/Length");
validator.NotBlank = require("./constraints/NotBlank");
validator.NotNull = require("./constraints/NotNull");
validator.Regex = require("./constraints/Regex");
validator.Type = require("./constraints/Type");
validator.ValidatorLogicError = ValidatorLogicError;
module.exports = validator;