If you haven’t read my post on Composite Validators, take a look at that first, as this is going to build off of what was created in that post.
Returning multiple errors in validators
In order to give users the best feedback when validating their input, it may be useful to know everything that’s wrong with what they entered, not just the first error encountered. Because of this, the implementation we created before needs to be modified to return multiple errors if the input is invalid.
So what does that look like?
We just have to make some relatively small changes in a few places.
We just need to change the invalid case to return an array of errors:
These are all still going to return a single error, but they just need to do it as an array of one error. For Example:
This one is essentially just packaging things up a little differently:
We’re going to use
reduce to help with this. It will iterate through all of the validators and call
validate on them. If it is valid, it will just return whatever the previous result was. If it’s invalid, it will return a new error, concatenating the new found errors with any previous ones.
Example of it used
This will print the output:
invalid([PasswordValidatorError.tooShort, PasswordValidatorError.noUppercaseLetter, PasswordValidatorError.noNumber]) invalid([PasswordValidatorError.noUppercaseLetter, PasswordValidatorError.noNumber]) invalid([PasswordValidatorError.noUppercaseLetter]) valid
There are many ways to take this pattern of using composite validators and modify it to fit the different needs an application might have. Being able to return multiple errors can be essential in order to provide useful feedback to users.
Hit me up on twitter or my email linked below with any feedback!
Take a look at Part 2 of extending composite validators to take advantage of