How to Return an Object with Yup Errors in Its Latest Version
See original GitHub issueHello 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);
}
In the Code Sandbox Above Yup is in its outdated test version and see.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
how do i inspect the error?
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