Coder Social home page Coder Social logo

yii-validator-scenarios's Introduction

Yii Validator Scenarios


Latest Stable Version Total Downloads Build 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

Attrubute Rules
name Required, Length
email Required, Email
password Required, Length

login

Attrubute Rules
name
email Required, Email
password Required, Length

Without scenario

Attrubute Rules
name
email Required, Email
password

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.

yii-validator-scenarios's People

Contributors

vjik avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.