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.

How to Return an Object with Yup Errors in Its Latest Version

See original GitHub issue

Hello Good afternoon, I am developing a ReactJS project in an online course. And the teacher uses Yup, when an error occurs, Yup returns an Object, this in your version: ^0.28.3.

With the version "yup": "^ 0.32.8", Yup returns me something like this:

ValidationError: 3 errors occurred
    at finishTestRun (http://localhost:3000/static/js/0.chunk.js:64956:20)
    at http://localhost:3000/static/js/0.chunk.js:64910:5
    at finishTestRun (http://localhost:3000/static/js/0.chunk.js:64956:11)
    at http://localhost:3000/static/js/0.chunk.js:64662:136

I want to force Yup to return an Object like this to me:

ValidationError {name: "ValidationError", value: {…}, path: undefined, type: undefined, errors: Array(3), …}
errors: (3) ["Nome é obrigatório", "Email é obrigatório", "No mínimo 6 dígitos"]
inner: (3) [ValidationError, ValidationError, ValidationError]
message: "3 errors occurred"
name: "ValidationError"
path: undefined
type: undefined
value: {name: "", email: "", password: ""}

What should I do?

The Project in question is this

I’m using Typescript. The file that uses Yup is this

Yup Code

  // eslint-disable-next-line @typescript-eslint/ban-types
  const handleSubmit = useCallback(async (data: object) => {
    try {
      const schema = Yup.object().shape({
        name: Yup.string().required('Nome é obrigatório'),
        email: Yup.string()
          .required('Email é obrigatório')
          .email('Digite um e-mail válido'),
        password: Yup.string().min(6, 'No mínimo 6 dígitos'),
      });

      await schema.validate(data, {
        abortEarly: false,
      });
    } catch (err) {
      console.log(err);
    }

Code Sandbox here

In the Code Sandbox Above Yup is in its outdated test version and see.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Daniel-Viniciuscommented, Jan 24, 2021

how do i inspect the error?

1reaction
jquensecommented, Jan 24, 2021

The error hasn’t changed it’s just showing you the message when you log it. If you inspect the error you’ll see it’s an object with all those properties

Read more comments on GitHub >

github_iconTop Results From Across the Web

get all validation errors · Issue #44 · jquense/yup - GitHub
I just switched from joi to yup, and was wondering if there was a way to get all the validation errors – not...
Read more >
How to use the yup.ValidationError function in yup - Snyk
To help you get started, we've selected a few yup.ValidationError examples ... { path: 'path' }); // ValidationError let error: ValidationError = new...
Read more >
Returning true in Yup test function is not clearing the previous ...
Create ref of clearErrors function and pass it to function returning schema so you can clear error when test is passed.
Read more >
Yup - npm
Dead simple Object schema validation. Latest version: 0.32.11, last published: a year ago. Start using yup in your project by running `npm i ......
Read more >
Validating Optional Objects with Yup - Object Partners
The Yup object validation library provides a lot of tools, ... Note that the below code is also a valid final return //...
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