diff --git a/src/processSubmitErrors.js b/src/processSubmitErrors.js index 06df514..9f8e3c1 100644 --- a/src/processSubmitErrors.js +++ b/src/processSubmitErrors.js @@ -1,18 +1,32 @@ import { SubmissionError } from 'redux-form' +import _ from 'lodash' // added for empty check -const processSubmitErrors = response => { - let errors = {} - if (response.hasOwnProperty('errors')) { - for (let field in response.errors.children) { - let value = response.errors.children[field] - if (value.hasOwnProperty('errors')) { - errors[field] = value.errors[0] +const convertToReduxFormErrors = (obj) => { + + let objectWithoutChildrenAndFalseErrors = {} + Object.keys(obj).map( (name) => { + if(name === 'children'){ + objectWithoutChildrenAndFalseErrors = { ...objectWithoutChildrenAndFalseErrors, ...convertToReduxFormErrors(obj[name])} + }else{ + if( obj[name].hasOwnProperty('children')){ // if children, take field from it and set them directly as own field + objectWithoutChildrenAndFalseErrors[name] = convertToReduxFormErrors(obj[name]) + }else{ + if( obj[name].hasOwnProperty('errors') && !_.isEmpty(obj[name]['errors']) ){ // using lodash for empty error check, dont add them if empty + objectWithoutChildrenAndFalseErrors[name] = obj[name]['errors'] + } + } } - } - throw new SubmissionError(errors) + } + ) + return objectWithoutChildrenAndFalseErrors +} +const processSubmitErrors = (errors) => { + if(errors.hasOwnProperty('errors')){ + errors = convertToReduxFormErrors(errors.errors) + throw new SubmissionError(errors) } - return {} + return {} // dont know why original processSubmitErrors return empty object, keep this to prevent any BC break } export default processSubmitErrors