fastest-validator icon indicating copy to clipboard operation
fastest-validator copied to clipboard

Custom validator regression with multiple rules

Open shawnmcknight opened this issue 3 years ago • 6 comments

#272 seems to have introduced a regression when using custom validations and multiple rules between 1.11.1 and 1.12.0.

If there are multiple rules, and an earlier rules fails while a later rule passes, the validation will still fail.

In this example codesandbox, you'll see a custom rule for a simple "even number" example. When using 1.12.0, the "Array passing" rule is failing, even though it should pass. If you change the version of fastest-validator to 1.11.1 then this same validation will be successful.

I debugged through a bit and I believe the problem is in makeCustomValidator method. Errors that come back from custom validators are pushed on to the main errors array. This wasn't a problem in 1.11.1 because the multi validator code used to confirm if the number of errors had changed between each validation, but in 1.12.0 it is instead using different variables for the results of each validation and conditionally pushing those results on to the errors array. However, because the custom validator is itself pushing on to the errors array, the multi validator's behavior is not occurring as expected.

shawnmcknight avatar Dec 30 '21 14:12 shawnmcknight

Thanks, we will check it.

icebob avatar Dec 31 '21 10:12 icebob

Maybe #290 is solution 🤣

0x0a0d avatar Mar 16 '22 00:03 0x0a0d

@shawnmcknight could you check that 290 solves your issue as well? It's not released you, you should install the master branch

icebob avatar Mar 21 '22 20:03 icebob

@0x0a0d @icebob Unfortunately, no I think its still a problem. I forked the codesandbox and installed from GitHub instead of npm and the "Array passing" test example using an array is still failing.

shawnmcknight avatar Mar 21 '22 22:03 shawnmcknight

I think it's codesandbox problem, I tried with master branch and the test worked as expected Screen Shot 2022-03-22 at 09 38 38

0x0a0d avatar Mar 22 '22 02:03 0x0a0d

@0x0a0d @icebob Hmm, yeah, not sure why the codesandbox is still failing. Maybe codesandbox doesn't properly allow installing from GitHub? Anyway, I had a failing unit test on my local repo with 1.12.0 and just installed the master branch version locally. The unit test is no longer failing, so I do think this resolves the issue. 👍

shawnmcknight avatar Mar 22 '22 13:03 shawnmcknight