Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yup function test() #1474

Open
trananhchuong opened this issue Oct 5, 2021 · 0 comments
Open

Yup function test() #1474

trananhchuong opened this issue Oct 5, 2021 · 0 comments

Comments

@trananhchuong
Copy link

I use the function "mixed.test" to validate the input in "React hook form",
The problem occurs if i use "test" function for 2 inputs, always return "true"
My code is as follows:

const schema = yup.object().shape({
patientPhoneNumber: yup
.string()
.required("required")
.test("patientPhoneNumber", "invalid phone", (phoneNumber) => {
const patternRegexPhone = /^+?\d*$/;
const phoneNumberLengthCheck = phoneNumber.length < 9;
const patternPhoneCheck = patternRegexPhone.test(phoneNumber);
const hasPlusOperatorCheck =
phoneNumber.indexOf("+") !== -1 &&
(phoneNumber.length < 12 || phoneNumber.length > 13);
const hasNotPlusOperatorCheck =
phoneNumber.indexOf("+") === -1 &&
(phoneNumber.length < 9 || phoneNumber.length > 11);

  if (
    phoneNumberLengthCheck ||
    !patternPhoneCheck ||
    hasPlusOperatorCheck ||
    hasNotPlusOperatorCheck
  ) {
    return false;
  }
  return true;
}),

otpInput: yup
.string()
.required("required")
.test("otpInput", "invalid otp", (otp) => {
debugger;
if (otp.length > 6 || otp.length < 6) {
return false;
}
return true;
}),
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant