vjik/yii-validator-scenarios

The scenario feature for Yii Validator

1.0.0 2023-02-22 13:25 UTC

This package is auto-updated.

Last update: 2024-11-07 12:59:55 UTC


README

Yii Validator Scenarios


Latest Stable Version Total Downloads Build status Coverage Status Mutation testing badge type-coverage static analysis psalm-level

The package provides validator rule On that implement the scenario feature for Yii Validator.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with composer:

composer require vjik/yii-validator-scenarios

General usage

The scenario feature implement via the rule On and a validation context parameter.

Configure rules:

use Vjik\Yii\ValidatorScenarios\On;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Length;
use Yiisoft\Validator\Rule\Required;

final class UserDto
{
    public function __construct(
        #[On(
            'register',
            [new Required(), new Length(min: 7, max: 10)]
        )]
        public string $name,

        #[Required]
        #[Email]
        public string $email,

        #[On(
            ['login', 'register'],
            [new Required(), new Length(min: 8)],
        )]
        public string $password,
    ) {
    }
}

Or same without attributes:

use Vjik\Yii\ValidatorScenarios\On;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Length;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\RulesProviderInterface;

final class UserDto implements RulesProviderInterface
{
    public function __construct(
        public string $name,
        public string $email,
        public string $password,
    ) {
    }

    public function getRules(): iterable
    {
        return [
            'name' => new On(
                'register',
                [new Required(), new Length(min: 7, max: 10)],
            ),
            'email' => [new Required(), new Email()],
            'password' => new On(
                ['login', 'register'],
                [new Required(), new Length(min: 8)],
            ),
        ];
    }
}

Pass the scenario to the validator through the context:

use Yiisoft\Validator\ValidationContext;
use Yiisoft\Validator\Validator;

$result = (new Validator())->validate(
    $userDto, 
    context: new ValidationContext([
        On::SCENARIO_PARAMETER => $scenario,
    ]),
);

Rules that will be applied according to scenarios:

register

login

Without scenario

On rule parameters

$scenario

The scenario(s) that $rules are in. null if rules used always. Defaults to null.

$rules

Rules that will be applied according to $scenario. Defaults to empty array.

$not

Whether the scenario check should be inverted. When this parameter is set true, the validator checks whether the current scenario is among $scenario and if NOT, $rules will be applied. Defaults to false.

$skipOnEmpty

Whether skip $rules on empty value or not, and which value consider as empty. Defaults to null.

$skipOnError

A boolean value where true means to skip $rules when the previous one errored and false — do not skip. Defaults to false.

$when

The closure that allow to apply $rules under certain conditions only. Defaults to null.

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii Validator Scenarios is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.