Skip to content

Commit f9ebf46

Browse files
authored
Merge pull request #87 from Ragepanda12/error-message-formatting
Join error messages with punctuation more gramatically correctly
2 parents 30d0ba9 + 1e45225 commit f9ebf46

File tree

3 files changed

+139
-5
lines changed

3 files changed

+139
-5
lines changed

packages/core/src/comparator.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe("compare date fields", () => {
154154
secondDate.prop("onBlur")();
155155
form.update();
156156
expect(form.state().fields[0].errorMessages).toBe(
157-
"A value must be provided. Must be before second date"
157+
"A value must be provided. Must be before second date."
158158
);
159159
expect(form.state().fields[1].errorMessages).toBe("");
160160
});

packages/core/src/utilities/validation.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,31 @@ export const validateField: ValidateField = (
342342
const { required, visible, validWhen = {}, touched = false } = field;
343343
let isValid = true;
344344
let errorMessages = [];
345-
const formattedErrorMessage = () => errorMessages.join(". ");
345+
const formattedErrorMessage = () => {
346+
if(errorMessages.length === 0){
347+
return "";
348+
}
349+
else if(errorMessages.length === 1){
350+
// Don't add punctuation on singular error messages.
351+
return errorMessages[0];
352+
}
353+
else{
354+
// Reverse through list of error messages to preserve order
355+
let formattedErrorMessages = "";
356+
errorMessages.slice().reverse().forEach((message)=>{
357+
const lastChar = message.slice(-1);
358+
if(lastChar === "." || lastChar ==="!" || lastChar === "?"){
359+
formattedErrorMessages = `${message} ${formattedErrorMessages}`;
360+
}
361+
else {
362+
formattedErrorMessages = `${message}. ${formattedErrorMessages}`;
363+
}
364+
});
365+
// Remove trailing space
366+
return formattedErrorMessages.slice(0, -1);
367+
}
368+
369+
};
346370

347371
if (visible) {
348372
const value = getValueFromField(field);

packages/core/src/utilities/validation.test.js

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("validateField", () => {
4949
expect(validateField(testField, [testField], true).isValid).toBe(false);
5050
});
5151

52-
test("visible, required field with numberical value 0 is valid", () => {
52+
test("visible, required field with numerical value 0 is valid", () => {
5353
const testField = {
5454
...field1,
5555
visible: true,
@@ -99,6 +99,116 @@ describe("validateField", () => {
9999
expect(validateField(testField, [testField], true).isValid).toBe(true);
100100
});
101101

102+
test("correctly concats multiple error messages with ? punctuation", () => {
103+
const errorMessage = "Where's my data?";
104+
const errorMessage2 = "Where is my data!";
105+
const testField = {
106+
...field1,
107+
visible: true,
108+
required: true,
109+
value: "",
110+
missingValueMessage: errorMessage,
111+
validWhen:{
112+
isNot:{
113+
values:[""],
114+
message: errorMessage2,
115+
}
116+
}
117+
};
118+
const validationResult = validateField(testField, [testField], true);
119+
expect(validationResult.isValid).toBe(false);
120+
expect(validationResult.errorMessages).toBe("Where's my data? Where is my data!");
121+
expect(validateField(testField, [testField], true).isValid).toBe(false);
122+
});
123+
124+
test("correctly concats multiple error messages with ! punctuation", () => {
125+
const errorMessage = "Where's my data!";
126+
const errorMessage2 = "Where is my data?";
127+
const testField = {
128+
...field1,
129+
visible: true,
130+
required: true,
131+
value: "",
132+
missingValueMessage: errorMessage,
133+
validWhen:{
134+
isNot:{
135+
values:[""],
136+
message: errorMessage2,
137+
}
138+
}
139+
};
140+
const validationResult = validateField(testField, [testField], true);
141+
expect(validationResult.isValid).toBe(false);
142+
expect(validationResult.errorMessages).toBe("Where's my data! Where is my data?");
143+
expect(validateField(testField, [testField], true).isValid).toBe(false);
144+
});
145+
146+
test("correctly concats multiple error messages with no full stop", () => {
147+
const errorMessage = "Where's my data";
148+
const errorMessage2 = "Where is my data?";
149+
const testField = {
150+
...field1,
151+
visible: true,
152+
required: true,
153+
value: "",
154+
missingValueMessage: errorMessage,
155+
validWhen:{
156+
isNot:{
157+
values:[""],
158+
message: errorMessage2,
159+
}
160+
}
161+
};
162+
const validationResult = validateField(testField, [testField], true);
163+
expect(validationResult.isValid).toBe(false);
164+
expect(validationResult.errorMessages).toBe("Where's my data. Where is my data?");
165+
expect(validateField(testField, [testField], true).isValid).toBe(false);
166+
});
167+
168+
test("correctly concats multiple error messages with a full stop", () => {
169+
const errorMessage = "Where's my data.";
170+
const errorMessage2 = "Where is my data.";
171+
const testField = {
172+
...field1,
173+
visible: true,
174+
required: true,
175+
value: "",
176+
missingValueMessage: errorMessage,
177+
validWhen:{
178+
isNot:{
179+
values:[""],
180+
message: errorMessage2,
181+
}
182+
}
183+
};
184+
const validationResult = validateField(testField, [testField], true);
185+
expect(validationResult.isValid).toBe(false);
186+
expect(validationResult.errorMessages).toBe("Where's my data. Where is my data.");
187+
expect(validateField(testField, [testField], true).isValid).toBe(false);
188+
});
189+
190+
test("concats multiple error messages in order", () => {
191+
const errorMessage = "This should be first";
192+
const errorMessage2 = "This should be second";
193+
const testField = {
194+
...field1,
195+
visible: true,
196+
required: true,
197+
value: "",
198+
missingValueMessage: errorMessage,
199+
validWhen:{
200+
isNot:{
201+
values:[""],
202+
message: errorMessage2,
203+
}
204+
}
205+
};
206+
const validationResult = validateField(testField, [testField], true);
207+
expect(validationResult.isValid).toBe(false);
208+
expect(validationResult.errorMessages).toBe("This should be first. This should be second.");
209+
expect(validateField(testField, [testField], true).isValid).toBe(false);
210+
});
211+
102212
test("visible, required field with missing data shows custom message", () => {
103213
const errorMessage = "Where's my data?";
104214
const testField = {
@@ -239,7 +349,7 @@ describe("lengthIsLessThan validator", () => {
239349
).toBeUndefined();
240350
});
241351

242-
test("with invvalid value", () => {
352+
test("with invalid value", () => {
243353
expect(
244354
lengthIsLessThan({
245355
value: "test",
@@ -688,4 +798,4 @@ describe("hasValue", () => {
688798
test("null", () => {
689799
expect(hasValue(null)).toEqual(false);
690800
});
691-
});
801+
});

0 commit comments

Comments
 (0)