Search code examples
phprespect-validation

PHP Respect\Validation library exceptions shows errors but validate method returns true


I'm using https://github.com/Respect/Validation for validation. the package looked verry good so now I wanted to try it. I wanted to add some validation rules to a (decoded) jtw token, which is a PHP object. and the validation method worked.

Now I wanted to add error messages but those do not seem to work?

public function validateJwt($request, $response)
{


    $validator = v::objectType()->attribute('data');
    $validator->validate($this->jwt);
    var_dump($validator->validate($this->jwt)); // <-- this returns true

    try {
        $validator->assert('JWT');
    } catch(NestedValidationException $exception) {
        print_r($exception->getMessages());
        /*

        Array
        (
            [0] => "JWT" must be an object
            [1] => Attribute data must be present
        )

         */
    }
}

Why am I getting these messages because on the validate method, it returns true?


Solution

  • You most likely need to pass the token to the validator and not a string JWT.

    $validator->assert($this->jwt);