Skip to content

Instantly share code, notes, and snippets.

@kilgarenone
Last active October 20, 2018 03:28
Show Gist options
  • Save kilgarenone/cef4d29ffdf1381bef30d2333372f7c7 to your computer and use it in GitHub Desktop.
Save kilgarenone/cef4d29ffdf1381bef30d2333372f7c7 to your computer and use it in GitHub Desktop.
yup validator
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