Created
May 15, 2020 12:45
-
-
Save pratikagashe/07ea5b60461e41f65bf128a2919aee33 to your computer and use it in GitHub Desktop.
Formik validate vs yup validation schema
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
// With Yup validationSchema | |
validationSchema={Yup.object().shape({ | |
email: Yup.string() | |
.email() | |
.required('Enter valid email-id'), | |
})} | |
// With Formik Validate | |
validate={values => { | |
const errors = {}; | |
if (!values.email) { | |
errors.email = 'Required'; | |
} else if ( | |
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email) | |
) { | |
errors.email = 'Invalid email address'; | |
} | |
return errors; | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment