Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rules are not ChainedValidator #1359

Closed
danielspk opened this issue Jun 23, 2021 · 2 comments
Closed

Rules are not ChainedValidator #1359

danielspk opened this issue Jun 23, 2021 · 2 comments

Comments

@danielspk
Copy link

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
Copy link
Author

Hello. Is this project still active?

@alganet
Copy link
Member

alganet commented Feb 18, 2023

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.

@alganet alganet closed this as completed Feb 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants