Skip to content

Commit 302ba3f

Browse files
committed
Move validator error messages to resources #15
1 parent 31c24f1 commit 302ba3f

File tree

3 files changed

+92
-24
lines changed

3 files changed

+92
-24
lines changed

dev/app.vue

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@
188188
if (users.length > 0) {
189189
this.selectRow(null, fakerator.random.arrayElement(users));
190190
}
191+
192+
// Localize validate errors
193+
VueFormGenerator.validators.resources.fieldIsRequired = "Ezt a mezőt kötelező kitölteni!";
194+
VueFormGenerator.validators.resources.textTooSmall = "A szöveg túl rövid! Jelenleg: {0}, minimum: {1}";
191195
}
192196
}
193197

src/utils/validators.js

+64-23
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,55 @@ import moment from "moment";
44
function checkEmpty(value, required) {
55
if (isNil(value) || value === "") {
66
if (required)
7-
return ["This field is required!"];
7+
return [msg(resources.fieldIsRequired)];
88
else
99
return [];
1010
}
1111
return null;
1212
}
1313

14+
function msg(text) {
15+
if (text != null && arguments.length > 1)
16+
for (let i = 1; i < arguments.length; i++)
17+
text = text.replace(/\{\d+?\}/, arguments[i]);
18+
19+
return text;
20+
}
21+
22+
let resources = {
23+
fieldIsRequired: "This field is required!",
24+
invalidFormat: "Invalid format!",
25+
26+
numberTooSmall: "The number is too small! Minimum: {0}",
27+
numberTooBig: "The number is too big! Maximum: {0}",
28+
invalidNumber: "Invalid number",
29+
30+
textTooSmall: "The length of text is too small! Current: {0}, Minimum: {1}",
31+
textTooBig: "The length of text is too big! Current: {0}, Maximum: {1}",
32+
thisNotText: "This is not a text!",
33+
34+
thisNotArray: "This is not an array!",
35+
36+
selectMinItems: "Select minimum {0} items!",
37+
selectMaxItems: "Select maximum {0} items!",
38+
39+
invalidDate: "Invalid date!",
40+
dateIsEarly: "The date is too early! Current: {0}, Minimum: {1}",
41+
dateIsLate: "The date is too late! Current: {0}, Maximum: {1}",
42+
43+
invalidEmail: "Invalid e-mail address!",
44+
invalidURL: "Invalid URL!",
45+
46+
invalidCard: "Invalid card format!",
47+
invalidCardNumber: "Invalid card number!",
48+
49+
invalidTextContainNumber: "Invalid text! Cannot contains numbers or special characters",
50+
invalidTextContainSpec: "Invalid text! Cannot contains special characters"
51+
};
52+
1453
module.exports = {
54+
55+
resources,
1556

1657
required(value, field) {
1758
return checkEmpty(value, field.required);
@@ -23,13 +64,13 @@ module.exports = {
2364
let err = [];
2465
if (isNumber(value)) {
2566
if (!isNil(field.min) && value < field.min)
26-
err.push("The number is too small! Minimum: " + field.min);
67+
err.push(msg(resources.numberTooSmall, field.min));
2768

2869
if (!isNil(field.max) && value > field.max)
29-
err.push("The number is too big! Maximum: " + field.max);
70+
err.push(msg(resources.numberTooBig, field.max));
3071

3172
} else
32-
err.push("This is not a number!");
73+
err.push(msg(resources.invalidNumber));
3374

3475
return err;
3576
},
@@ -38,14 +79,14 @@ module.exports = {
3879
let res = checkEmpty(value, field.required); if (res != null) return res;
3980

4081
if (!(Number(value) === value && value % 1 === 0))
41-
return ["Invalid number!"];
82+
return [msg(resources.invalidNumber)];
4283
},
4384

4485
double(value, field) {
4586
let res = checkEmpty(value, field.required); if (res != null) return res;
4687

4788
if (!(Number(value) === value && value % 1 !== 0))
48-
return ["Invalid number!"];
89+
return [msg(resources.invalidNumber)];
4990
},
5091

5192
string(value, field) {
@@ -54,13 +95,13 @@ module.exports = {
5495
let err = [];
5596
if (isString(value)) {
5697
if (!isNil(field.min) && value.length < field.min)
57-
err.push(`The length of text is too small! Current: ${value.length}, Minimum: ${field.min}`);
98+
err.push(msg(resources.textTooSmall, value.length, field.min));
5899

59100
if (!isNil(field.max) && value.length > field.max)
60-
err.push(`The length of text is too big! Current: ${value.length}, Maximum: ${field.max}`);
101+
err.push(msg(resources.textTooBig, value.length, field.max));
61102

62103
} else
63-
err.push("This is not a text!");
104+
err.push(msg(resources.thisNotText));
64105

65106
return err;
66107
},
@@ -69,20 +110,20 @@ module.exports = {
69110
if (field.required) {
70111

71112
if (!isArray(value))
72-
return ["Value is not an array!"];
113+
return [msg(resources.thisNotArray)];
73114

74115
if (value.length == 0)
75-
return ["This field is required!"];
116+
return [msg(resources.fieldIsRequired)];
76117
}
77118

78119
if (!isNil(value)) {
79120
if (!isNil(field.min))
80121
if (value.length < field.min)
81-
return ["Select minimum " + field.min + " items!"];
122+
return [msg(resources.selectMinItems, field.min)];
82123

83124
if (!isNil(field.max))
84125
if (value.length > field.max)
85-
return ["Select maximum " + field.max + " items!"];
126+
return [msg(resources.selectMaxItems, field.max)];
86127
}
87128
},
88129

@@ -91,20 +132,20 @@ module.exports = {
91132

92133
let m = moment(value);
93134
if (!m.isValid())
94-
return ["Invalid date!"];
135+
return [msg(resources.invalidDate)];
95136

96137
let err = [];
97138

98139
if (!isNil(field.min)) {
99140
let min = moment(field.min);
100141
if (m.isBefore(min))
101-
err.push(`The date is too early! Current: ${m.format("L")}, Minimum: ${min.format("L")}`);
142+
err.push(msg(resources.dateIsEarly, m.format("L"), min.format("L")));
102143
}
103144

104145
if (!isNil(field.max)) {
105146
let max = moment(field.max);
106147
if (m.isAfter(max))
107-
err.push(`The date is too late! Current: ${m.format("L")}, Maximum: ${max.format("L")}`);
148+
err.push(msg(resources.dateIsLate, m.format("L"), max.format("L")));
108149
}
109150

110151
return err;
@@ -116,7 +157,7 @@ module.exports = {
116157
if (!isNil(field.pattern)) {
117158
let re = new RegExp(field.pattern);
118159
if (!re.test(value))
119-
return ["Invalid format!"];
160+
return [msg(resources.invalidFormat)];
120161
}
121162
},
122163

@@ -125,15 +166,15 @@ module.exports = {
125166

126167
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
127168
if (!re.test(value))
128-
return ["Invalid e-mail address!"];
169+
return [msg(resources.invalidEmail)];
129170
},
130171

131172
url(value, field) {
132173
let res = checkEmpty(value, field.required); if (res != null) return res;
133174

134175
let re = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
135176
if (!re.test(value))
136-
return ["Invalid URL!"];
177+
return [msg(resources.invalidURL)];
137178
},
138179

139180
creditCard(value, field) {
@@ -145,7 +186,7 @@ module.exports = {
145186
const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/;
146187
const sanitized = value.replace(/[^0-9]+/g, "");
147188
if (!creditCard.test(sanitized)) {
148-
return ["Invalid card format!"];
189+
return [msg(resources.invalidCard)];
149190
}
150191
let sum = 0;
151192
let digit;
@@ -168,22 +209,22 @@ module.exports = {
168209
}
169210

170211
if (!((sum % 10) === 0 ? sanitized : false))
171-
return ["Invalid card number!"];
212+
return [msg(resources.invalidCardNumber)];
172213
},
173214

174215
alpha(value, field) {
175216
let res = checkEmpty(value, field.required); if (res != null) return res;
176217

177218
let re = /^[a-zA-Z]*$/;
178219
if (!re.test(value))
179-
return ["Invalid text! Cannot contains numbers or special characters"];
220+
return [msg(resources.invalidTextContainNumber)];
180221
},
181222

182223
alphaNumeric(value, field) {
183224
let res = checkEmpty(value, field.required); if (res != null) return res;
184225

185226
let re = /^[a-zA-Z0-9]*$/;
186227
if (!re.test(value))
187-
return ["Invalid text! Cannot contains special characters"];
228+
return [msg(resources.invalidTextContainSpec)];
188229
}
189230
};

test/unit/specs/utils/validators.spec.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,28 @@ describe("Validators", () => {
391391
field.required = false;
392392
check(v.alphaNumeric, null, field, 0);
393393
});
394-
});
394+
});
395+
396+
describe("test localized error messages", () => {
397+
398+
let field = {
399+
min: 5,
400+
max: 10,
401+
required: true
402+
};
403+
404+
it("should give the default error message", () => {
405+
expect(v.number(null, field)[0]).to.be.equal("This field is required!");
406+
expect(v.string("Ab", field)[0]).to.be.equal("The length of text is too small! Current: 2, Minimum: 5");
407+
});
408+
409+
it("should give the localized error message", () => {
410+
v.resources.fieldIsRequired = "A mezőt kötelező kitölteni!";
411+
v.resources.textTooSmall = "A szöveg túl rövid. {1} helyett {0}";
412+
413+
expect(v.number(null, field)[0]).to.be.equal("A mezőt kötelező kitölteni!");
414+
expect(v.string("Ab", field)[0]).to.be.equal("A szöveg túl rövid. 2 helyett 5");
415+
});
416+
417+
});
395418
});

0 commit comments

Comments
 (0)