Respect / Validation

The most awesome validation engine ever created for PHP
https://respect-validation.readthedocs.io
MIT License
5.76k stars 774 forks source link

Rules are not ChainedValidator #1359

Closed danielspk closed 1 year ago

danielspk commented 3 years ago

Hello. The StaticValidator interface indicates for example that the static method stringType returns a ChainedValidator, but in reality it returns a Validator.

interface StaticValidator
{
    // ...
    public static function stringType(): ChainedValidator;
    // ...
}

Example:

$demoValidator = ($this->validator)::stringType()
    ->notEmpty()
    ->length(1, 255);

$this->assertValidator($demoValidator, 'hello'); // work
$this->assertValidatorTwo($demoValidator, 'hello'); // not work

public function assertValidator(Validator $validator, $value)
{
    // ...
}

public function assertValidatorTwo(ChainedValidator $validator, $value)
{
    // ...
}

This behavior for example causes PhpStorm to report a warning incorrectly: Expected parameter of type '\Respect\Validation\Validator', '\Respect\Validation\ChainedValidator' provided

danielspk commented 2 years ago

Hello. Is this project still active?

alganet commented 1 year ago

You need to switch to typing the validators by the interface:

public function assertValidator(Validatable $validator, $value)
{
    // ...
}

Using the Validatable interface instead of Validator façade will make PhpStorm typing checks work.

We don't recommend reusing the façade as a composition in your internal code. If you're building validators dynamically, you should read about the Concrete API and create new Rules\... by yourself.