yup

Yup validation check if not empty


const validationSchema = Yup.object().shape({
    newPassword: Yup.string().min(8, 'Password must be at least 8 characters');
});

I want to validation check only if newPassword field is not empty. How could I do?


Solution

  • You can use test to do this:

    const validationSchema = Yup.object().shape({
        newPassword: Yup.string().test(
            'empty-check',
            'Password must be at least 8 characters',
             password => password.length == 0
        });