In my case, I have a yup schema form validation. For example:
const form = yup.object().shape({
firstName: yup.string().nullable().required(),
lastName: yup.string().nullable().required(),
country: yup.string().nullable().required()
.test('validator', 'country is incorrect', value => {
return new Promise((resolve, reject) => {
api.post('url', value)
.then(() =>
resolve(false))
.catch(() =>
resolve(true)
})
}
});
Also, I want to validate country field asynchronously.
The flow should be the following:
- Synchronous validation should run
- IF no SYNCHRONOUS validation error
- THEN run ASYNCHRONOUS validation
- IF no ASYNCHRONOUS validation form should be valid
Issues that I faced:
I tried using .test() from yup but the order of validators was wrong.
For example, in this case, I want to execute the required validator and only if it's passed successfully without errors then run .test() (async validation).
How to achieve such behavior with Formik and Yup?