-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotNull.js
48 lines (35 loc) · 1.04 KB
/
NotNull.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
"use strict";
const Constraint = require("../prototypes/Constraint");
const def = {
message: "This value should be true.",
};
const NotNull = function (opt, extra) {
Constraint.apply(this, arguments); // call super constructor.
this.setExtra(extra);
if (typeof opt === "string") {
opt = {
message: opt,
};
}
this.setOptions(Object.assign({}, def, opt));
};
NotNull.prototype = Object.create(Constraint.prototype);
NotNull.prototype.constructor = NotNull;
NotNull.prototype.IS_NULL_ERROR = "IS_NULL_ERROR";
NotNull.prototype.validate = function (value, context, path, extra) {
const opt = this.getOptions();
if (value === null) {
context
.buildViolation(opt.message)
.atPath(path)
// .setParameter('{{ value }}', $this->formatValue($value))
.setCode(NotNull.prototype.IS_NULL_ERROR)
.setInvalidValue(value)
.addViolation();
if (extra && extra.stop) {
return Promise.reject("stop NotNull");
}
}
return Promise.resolve("resolve NotNull");
};
module.exports = NotNull;