forked from mikeerickson/validatorjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-messages.js
executable file
·152 lines (142 loc) · 4.73 KB
/
custom-messages.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
const { Validator, expect } = require("./setup.js");
describe("Validator custom messages", function() {
it("override the default message for the validator", function() {
const validator = new Validator({ name: "" }, { name: "required" }, { required: "Name is missing." });
expect(validator.fails()).to.be.true;
expect(validator.errors.get("name").length).to.equal(1);
expect(validator.errors.first("name")).to.equal("Name is missing.");
});
it("override the default message for a type of the validator", function() {
const validator = new Validator(
{ name: "A" },
{ name: "min:4" },
{
min: {
string: ":attribute is not long enough. Should be :min."
}
}
);
expect(validator.fails()).to.be.true;
expect(validator.errors.get("name").length).to.equal(1);
expect(validator.errors.first("name")).to.equal("name is not long enough. Should be 4.");
});
it("override the default message for the validator with several :attribute in message", function() {
const validator = new Validator(
{ name: "" },
{ name: "required" },
{
required: ":attribute is required. :attribute can't be empty."
}
);
expect(validator.fails()).to.be.true;
expect(validator.errors.get("name").length).to.equal(1);
expect(validator.errors.first("name")).to.equal("name is required. name can't be empty.");
});
it("override the default message for a type of the validator", function() {
const validator = new Validator(
{ name: "A" },
{
name: "min:4"
},
{
min: {
string: ":attribute is not long enough. Should be :min."
}
}
);
expect(validator.fails()).to.be.true;
expect(validator.errors.get("name").length).to.equal(1);
expect(validator.errors.first("name")).to.equal("name is not long enough. Should be 4.");
});
it("override the default message for a type of the validator with several :attribute and :min in message", function() {
const validator = new Validator(
{
name: "A"
},
{
name: "min:4"
},
{
min: {
string:
":attribute is not long enough. :attribute should be :min. Because needed string with :min symbols or more."
}
}
);
expect(validator.fails()).to.be.true;
expect(validator.errors.get("name").length).to.equal(1);
expect(validator.errors.first("name")).to.equal(
"name is not long enough. name should be 4. Because needed string with 4 symbols or more."
);
});
it("can be specified on a per attribute basis for a validator", function() {
const validator = new Validator(
{
name: "",
email: ""
},
{
name: "required",
email: "required"
},
{
"required.name": "Name is missing."
}
);
expect(validator.fails()).to.be.true;
expect(validator.errors.get("name").length).to.equal(1);
expect(validator.errors.first("name")).to.equal("Name is missing.");
expect(validator.errors.get("email").length).to.equal(1);
expect(validator.errors.first("email")).to.equal("The email field is required.");
});
it("can be specified for custom validators", function() {
Validator.register(
"telephone",
function(value, requirement, attribute) {
return value.match(/^\d{3}-\d{3}-\d{4}$/);
},
"The :attribute phone number is not in the format XXX-XXX-XXXX."
);
const validator = new Validator(
{
phone: "1234567890"
},
{
phone: "telephone"
},
{
telephone: "Wrong number."
}
);
expect(validator.fails()).to.be.true;
expect(validator.errors.get("phone").length).to.equal(1);
expect(validator.errors.first("phone")).to.equal("Wrong number.");
});
it("can be specified for custom validators per attribute", function() {
Validator.register(
"telephone",
function(value, requirement, attribute) {
return value.match(/^\d{3}-\d{3}-\d{4}$/);
},
"The :attribute phone number is not in the format XXX-XXX-XXXX."
);
const validator = new Validator(
{
phone: "1234567890",
fax: "1234567890"
},
{
phone: "telephone",
fax: "telephone"
},
{
"telephone.fax": "Why are you even using a fax?"
}
);
expect(validator.fails()).to.be.true;
expect(validator.errors.get("phone").length).to.equal(1);
expect(validator.errors.first("phone")).to.equal("The phone phone number is not in the format XXX-XXX-XXXX.");
expect(validator.errors.get("fax").length).to.equal(1);
expect(validator.errors.first("fax")).to.equal("Why are you even using a fax?");
});
});