Skip to content

Commit

Permalink
fix(mergeErrors): fix merge error if error is string
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała committed Feb 23, 2018
1 parent 89271f3 commit d7d9b23
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Schema {
setError(key, error, index) {
if (!this.errors[key]) this.errors[key] = [];
if (index > -1) {
this.errors[key][index] = mergeErrors(error, this.errors[key][index]);
this.errors[key][index] = mergeErrors(this.errors[key][index], error);
return;
}
this.errors[key].push(error);
Expand Down
6 changes: 6 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export const getErrorIndexFromKeys = (keys) => {
export const mergeErrors = (currentErrors = {}, nextErrors = {}) => {
const errors = {};
const errorKeys = new Set();
if (typeof nextErrors === 'string') {
if (!Array.isArray(currentErrors)) {
return [nextErrors];
}
return [...currentErrors, nextErrors];
}
Object.keys(currentErrors).forEach(key => errorKeys.add(key));
Object.keys(nextErrors).forEach(key => errorKeys.add(key));
errorKeys.forEach((key) => {
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,15 @@ describe('helpers', () => {
const nextErrors = undefined;
expect(mergeErrors(currentErrors, nextErrors)).toEqual({});
});
it('should return array with errors if next error is string', () => {
const currentErrors = ['foo error'];
const nextErrors = 'bar error';
expect(mergeErrors(currentErrors, nextErrors)).toEqual(['foo error', 'bar error']);
});
it('should return array with error if next error is string and current error is undefined', () => {
const currentErrors = undefined;
const nextErrors = 'bar error';
expect(mergeErrors(currentErrors, nextErrors)).toEqual(['bar error']);
});
});
});

0 comments on commit d7d9b23

Please sign in to comment.