Validation icon indicating copy to clipboard operation
Validation copied to clipboard

keyValue exception message may be redundant

Open tchari opened this issue 5 years ago • 1 comments

In the case where we're missing one of the keys the KeyValue message is redundant. For example the following code...

    try {
      $test = ['password' => '12345'];
      v::key('password', v::stringType()->notEmpty())
        ->key('password_confirmation', v::stringType()->notEmpty())
        ->keyValue('password_confirmation', 'equals', 'password')
        ->assert($test);
    } catch (NestedValidationException $e) {
      var_dump($e->getMessages());
    }

results in the following dump:

array(2) {
  ["password_confirmation"]=>
  string(41) "Key password_confirmation must be present"
  ["keyValue"]=>
  string(43) "Key "password_confirmation" must be present"
}

Seems like the keyValue message should be suppressed if one of it's key dependencies is missing.

tchari avatar Apr 29 '20 23:04 tchari

I agree it should be suppressed. It is hard to do though!

Have you tried playing with the KeySet rule? https://github.com/Respect/Validation/blob/1.1/docs/rules/KeySet.md

Since it wraps multiple keys, it might report the exception in a friendlier manner.

alganet avatar Jun 04 '20 19:06 alganet

This seems to have been fixed already in 2.2, as I'm getting messages like this:

(
    [password] => password must be of type string
    [password_confirmation] => password_confirmation must equal "password"
)

Feel free to reopen if you're still experiencing this.

alganet avatar Feb 19 '23 18:02 alganet