diff --git a/src/Schema.js b/src/Schema.js index e6f810f..013bf01 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -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); diff --git a/src/helpers.js b/src/helpers.js index 5deaad6..67af489 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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) => { diff --git a/src/helpers.test.js b/src/helpers.test.js index 0bc905a..9a887b2 100644 --- a/src/helpers.test.js +++ b/src/helpers.test.js @@ -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']); + }); }); });