Created
May 26, 2018 10:40
-
-
Save gsimone/7e5c76987121ed9635395e3c69e3c366 to your computer and use it in GitHub Desktop.
Better Redux Form Error handling with Yup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yup from 'yup' | |
import yupErroToReduxFormError from './yupErroToReduxFormError' | |
// yup schema | |
const schema = yup.object().shape(...) | |
// this would be our sync Redux Form validation function | |
// yup can also validate asynchronously with the yup.validate method | |
export default (values) => { | |
let errors = {} | |
try { | |
const validation = schema | |
.validateSync(values, { | |
abortEarly: false | |
}) | |
} catch (err) { | |
errors = yupErrorToReduxFormError(err) | |
} | |
return errors | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const yupErrorToReduxFormError = error => | |
error && error.inner && error.inner.reduce((accumulator, err) => { | |
return { | |
...accumulator, | |
[err.path]: err.message | |
} | |
}, {}) | |
export default yupErrorToReduxFormError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment