-
-
Save kilgarenone/cef4d29ffdf1381bef30d2333372f7c7 to your computer and use it in GitHub Desktop.
yup validator
This file contains hidden or 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 { Schema } from 'yup'; | |
const validator = <T>(schema: Schema<T>) => async formValues => { | |
try { | |
await schema.validate(formValues, { abortEarly: false }) | |
return {} | |
} catch (errors) { | |
return errors.inner.reduce( | |
(errors, err) => ({ | |
...errors, | |
[err.path]: err.message | |
}), | |
{} | |
) | |
} | |
} | |
export default validator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment