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?
There are different approach on solving this problem.
Using test
const validationSchema = Yup.object().shape({
newPassword: Yup.string().test('empty-check','Password must be at least 8 characters',password=>password.length==0
Using when
const validationSchema = Yup.object().shape({
newPassword: Yup.string().when('newPassword',{
is:(password)=>password.length>0
then: Yup.string().min(8, 'Password must be at least 8 characters');
});