/
Email.js
56 lines (41 loc) · 1.21 KB
/
Email.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
"use strict";
const Constraint = require("../prototypes/Constraint");
const def = {
message: "This value is not a valid email address.",
};
const Email = 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));
};
Email.prototype = Object.create(Constraint.prototype);
Email.prototype.constructor = Email;
Email.prototype.INVALID_EMAIL_ERROR = "INVALID_EMAIL_ERROR";
Email.prototype.validate = function (value, context, path, extra) {
const opt = this.getOptions();
if (!Email.prototype.logic(value)) {
context
.buildViolation(opt.message)
.atPath(path)
// .setParameter('{{ value }}', $this->formatValue($value))
.setCode(Email.prototype.INVALID_EMAIL_ERROR)
.setInvalidValue(value)
.addViolation();
if (extra && extra.stop) {
return Promise.reject("stop Email");
}
}
return Promise.resolve("resolve Email");
};
Email.prototype.logic = function (email) {
if (typeof email === "string") {
return /^.+\@\S+\.\S+$/.test(email);
}
return false;
};
module.exports = Email;