question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Validation] required validate message is not appear with yup schema

See original GitHub issue

Describe the bug Required message is not appear with yup schema validation. only display ‘Invalid email’. I made yup schema what checks 2 validation, required and valid(w/ regex test)

To Reproduce Codesandbox: https://codesandbox.io/s/react-hook-form-required-not-appear-reproduce-losds

  1. submit form without fill the email field
  2. expect to display required message but invalid email is appeared.
  3. but native validate is works as what I expected

Expected behavior Display required error message

Screenshots image

Desktop (please complete the following information):

  • OS: MacOS 10.14.6
  • Browser: Chrome
  • Version: 76.0.3809.132
  • Library: 3.23.12

Additional context I think there is an issue in logic/validateWithSchema.ts. I’ve been through the same issue when I build a form without any form library just with yup. I have to find my old project to reference it. I need your opinion before that. do you think this is a bug? then, I’ll do.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:27 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
balzafincommented, Dec 30, 2019

Don’t know if this is already discussed somewhere else, but if you found this issue while searching for this problem like I did, one possible solution is to use Yup.transform (with numbers) like this:

const validationSchema = yup.object({
  age: yup
    .number()
    .transform((cv: unknown, ov: unknown) =>
      typeof ov === "string" && ov.trim() === "" ? undefined : cv
    )
    .positive("age must > 0")
    .required("age is required")
});

Here’s a working codesandbox forked from the @hitbear518 one https://codesandbox.io/s/react-hook-form-required-not-appear-reproduce-gitzy

In the OP’s case (RegExp.test for email), you could just skip the test if the value is falsy:

.test("email", "Invalid email", value =>
  !value || /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(
    value
  )
)

Codesandbox: https://codesandbox.io/s/react-hook-form-required-not-appear-reproduce-ycc4u

1reaction
balzafincommented, Dec 30, 2019

@bluebill1049 I noticed, thanks for the awesome work!

The order won’t help with required numbers though as the type (Yup.number()) must be defined first? That’s not a problem of this library though and the transform workaround is just fine imo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation with Yup doesn't show the correct message
For a custom message to number type you should call the typeError() function: numberField: Yup.number().
Read more >
Validation - Formik
Form-level validation is useful because you have complete access to all of your form's values and props whenever the function runs, so you...
Read more >
Yup Email Validation: String Schema Code Examples
When you call schema.isValid on a Yup array field, it will only return true if all of the elements in the array are...
Read more >
Yup - npm
Yup separates the parsing and validating functions into separate steps. cast() ... string.required(message?: string | function): Schema ...
Read more >
Validation - VeeValidate
Form-level Validation. vee-validate supports using a validation schema to define all your validations on your fields beforehand so you don't have to define...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found