Skip to content

Instantly share code, notes, and snippets.

@gsimone
Created May 26, 2018 10:40
Show Gist options
  • Save gsimone/7e5c76987121ed9635395e3c69e3c366 to your computer and use it in GitHub Desktop.
Save gsimone/7e5c76987121ed9635395e3c69e3c366 to your computer and use it in GitHub Desktop.
Better Redux Form Error handling with Yup
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
}
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