forked from icebob/fastest-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.js
44 lines (37 loc) · 922 Bytes
/
multi.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
"use strict";
/** Signature: function(value, field, parent, errors, context)
*/
module.exports = function({ schema, messages }, path, context) {
const src = [];
src.push(`
var hasValid = false;
var newVal = value;
var checkErrors = [];
`);
for (let i = 0; i < schema.rules.length; i++) {
src.push(`
if (!hasValid) {
var _errors = [];
`);
const rule = this.getRuleFromSchema(schema.rules[i]);
src.push(this.compileRule(rule, context, path, `var tmpVal = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value, field, parent, _errors, context);`, "tmpVal"));
src.push(`
if (_errors.length == 0) {
hasValid = true;
newVal = tmpVal;
} else {
Array.prototype.push.apply(checkErrors, _errors);
}
}
`);
}
src.push(`
if (!hasValid) {
Array.prototype.push.apply(errors, checkErrors);
}
return newVal;
`);
return {
source: src.join("\n")
};
};