Async validation with Formik, Yup and React

Day's Night picture Day's Night · Apr 23, 2019 · Viewed 11.1k times · Source

I want to make async validation with formik and validationschema with yup but i can't find the example or demo.

Answer

이석규 picture 이석규 · Sep 11, 2019
const validationSchema = Yup.object().shape({
    username:
        Yup.string()
            .test('checkDuplUsername', 'same name exists', function (value) {
                return new Promise((resolve, reject) => {
                    kn.http({
                        url: `/v1/users/${value}`,
                        method: 'head',
                    }).then(() => {
                        // exists
                        resolve(false)
                    }).catch(() => {
                        // note exists
                        resolve(true)
                    })
                })
            })
})

Yup provides asynchronous processing through the test method.
(kn is my ajax promise function)
have a nice day.