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.

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:open
  • Created 4 years ago
  • Reactions:15
  • Comments:5

github_iconTop GitHub Comments

6reactions
LyricL-Gitstercommented, Jan 18, 2021

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:

const fullValidatorForSchema = (schema) => (values) => schema.validate(values, {
  abortEarly: false,
  strict: false,
}).then(() => ({})).catch(({inner}) => inner.reduce((memo, {path, message}) => ({
  ...memo,
  [path]: (memo[path] || []).concat(message),
}), {}))

So you can:

<Formik validate={fullValidatorForSchema(YupValidationSchema)} {...otherProps} />

And, then your Formik errors will be an array of all of the error messages.

2reactions
dieguezzcommented, Jun 25, 2020

You can do it programmatically. Instead of using validationSchema use validate and the validateFunction should look something like this:

export function validateSchema(values, schema) {
  return schema
    .validate(values, {
      abortEarly: false,
      strict: false,
    })
    .then((res) => {
      return {}
    })
    .catch(mapYupToFormikErrors)
}

and do something like

validateSchema(values, schema)

Read more comments on GitHub >

github_iconTop 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 >

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