Skip to content
Advertisement

(Yup) how to create multiple errors using a single .test() function

EDIT: while the accepted solution worked, this worked much better in my use case

I have one function that validates that neither input field A nor input field B are empty and with the way my form is built I have to write only one function to check both. (The actual function is much more complicated so I opted to create the example function below)

this is my test function:

JavaScript

The result of this is that when A and B are empty I return the first createError so the rest of the function is skipped and this is what the formik.errors object looks like:

JavaScript

How do I create an array of errors and return it instead?

I tried:

returning an array of createErrors() but I got the same result,

using createErrors with an array of paths and messages but the formik.errors looked like this:

JavaScript

instead of the desired:

JavaScript

Advertisement

Answer

You can use ValidationError from Yup to achieve this goal. The constructor of ValidationError accepts array of other ValidationError instances. The individual ValidationError instances are capable of holding path and error message and the parent instance will combine them.

Here’s the example test function:

JavaScript

It is also possible to do the same thing with createError judging by the source code.

You can pass a function instead of a string to message property of createError call. In this case the function would be called within createError => ValidationError.formatError here. And if this function returned an array of the properly built VaildationError instances we would get the same result.

Here’s an example test function for that approach:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement