Handle multiple errors returned by Yup
See original GitHub issue🚀 Feature request
When Yup returns a ValidationError
aggregating multiple errors I’d like Formik to list all aggregated errors.
Current Behavior
Currently if Yup returns ValidationError then its message
is shown.
Desired Behavior
With following validation:
clinicalFiles: Yup.array()
.min(10, 'We expect you to provide 10 files')
.max(10, 'We expect you to provide 10 files')
.test('valid-domains', 'Domains are invalid', function (value: File[]) {
let errors = validateFileNames(value.map(f => f.name));
if (errors.length > 0) {
let ve = this.createError({ message: 'Error that is shown' });
ve.errors = errors; // these are not shown
return ve;
}
return true;
})
I’d expect Formik to show all aggregated errors.
Suggested Solution
If ValidationError ve
packs other errors then show all of them, instead of just ve.message
Who does this impact? Who is this for?
Ppl using Yup validations (which are recommended by Formik)
Describe alternatives you’ve considered
Concatenating all errors:
Yup.array().test('valid-domains', 'Domains are invalid', function (value: File[]) {
let errors = validateFileNames(value.map(f => f.name));
if (errors.length > 0) {
return this.createError({ message: errors.join(". ") });
}
return true;
})
Issue Analytics
- State:
- Created 4 years ago
- Reactions:15
- Comments:5
Top Results From Across the Web
(Yup) how to create multiple errors using a single .test() function
How do I create an array of errors and return it instead? I tried: returning an array of createErrors() but I got the...
Read more >Easy Error Handling and Data Validation with Yup - Code Daily
It's a declarative way to handle both synchronous and asynchronous error ... Formik knows how to map the errors returned from yup to...
Read more >Error handling in Step Functions - AWS Documentation
Learn about AWS Step Functions error handling. ... The first two errors match the first retrier and cause waits of one and two...
Read more >Exception Handling — Python 3.11.1 documentation
Most C API functions also return an error indicator, usually NULL if they are ... the error and clearing the exception or returning...
Read more >Understanding GraphQL Error Handling Mechanisms in ...
Application Set up. We will use a simple spring-boot sample-graphql-error-handling that will expose two endpoints: createUser(username ...
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 FreeTop 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
Top GitHub Comments
Expanding on @dieguezz, this will give you a full
validate
method for a schema until such time that we can pass schema options to Formik:So you can:
And, then your Formik errors will be an array of all of the error messages.
You can do it programmatically. Instead of using
validationSchema
usevalidate
and the validateFunction should look something like this:and do something like
validateSchema(values, schema)