Yup confirm password validation

Viewed 33
1 Answers

You may combine yup.string().oneOf() and yup.ref() methods to achieve the same.

import * as yup from "yup";
import YupPassword from "yup-password";
YupPassword(yup);

const schema = yup.object().shape({
  password: yup.string().password().required(),
  confirmPassword: yup
    .string()
    .oneOf([yup.ref("password"), null], "Passwords must match"),
});

schema
  .validate({ password: "12345678aB1!", confirmPassword: "12345678aB1!" })
  .then(() => console.log("Valid"))
  .catch((err) => console.log(err.message));

Related