jquense / yup

Dead simple Object schema validation
MIT License
22.9k stars 934 forks source link

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

Closed Daniel-Vinicius closed 3 years ago

Daniel-Vinicius commented 3 years ago

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.

jquense commented 3 years ago

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

Daniel-Vinicius commented 3 years ago

how do i inspect the error?

jquense commented 3 years ago

i'm not sure what you mean, use the error how you want it is an object.

Daniel-Vinicius commented 3 years ago

OK so nothing has changed the error is still an object? If so my problem was solved!

jquense commented 3 years ago

Ya, nothing has changed