xtompie/validation

4.2 2023-12-23 17:49 UTC

This package is auto-updated.

Last update: 2024-09-23 19:38:59 UTC


README

用于验证模型和输入数据的验证组件。处理任何数据类型 - 数组、对象、标量、获取方法。易于扩展。类型提示/自动完成。流畅语法。

use Xtompie\Validation\Validation;

$result = Validation::of($input)
    ->key('email')->required()->email()
    ->key('password')->required()->min(3)
    ->group()
    ->main('password')->callback(fn($input) => $input['email'] != $input['password'])
    ->group()
    ->key('email')->callback(fn($email) => !inUse($email))
    ->result();

$resultXtompie\Result\Result

要求

PHP >= 8.0

安装

使用composer

composer require xtompie/validation

文档

主题

验证主题可以通过以下方式提供

Validation::of($input);
Validation::new()->withSubject($input);
Validation::new()->validate($input);

Validation::of($input)
    /* Group 1 */
    ->group()
    /* Group 2 */
    ->group()
    /* Group 3 */
    ;

如果组验证过程中发生错误,后续组将不会进行验证,验证将停止。

目标

Validation::new()
    ->main() // validation will target main subject
    ->property($name) // when subject is an object, will target property named $name
    ->method($name) // when subject is an object, will target getter method named $name
    ->key($key) // when subject is an array, will target array value where key is $key
    ->take($callback) // custom target $callback, as first argument main subject will be given
;

嵌套目标

目标可以是嵌套的,例如。

    $validation = Validation::of(['person' => ['name' => 'John']])
        ->key('person')
        ->nested()->key('name')->required()->lengthMin(10)
    ;
    $validation->errors()->first()->space(); // person.name

嵌套()函数后的目标与最后一个目标相关。可以通过unested()group()main()目标重置嵌套。嵌套可以向下多级组合。错误中的空间将自动生成。

过滤器

在验证器之前应用过滤器

Validation::new()
    ->key('name')
        ->filter(fn($x) => ucfirst($x)) // custom callback filter
        ->trim()
;

必需/可选

默认情况下,目标是可选的。如果目标必需,请使用required方法。

Validation::new()
    ->key('name')->required()
;

验证器

Validation::new()
    ->key('name')
    // raw validator, validator return Result
    ->validator(fn ($value) => strlen($value) !== 13 ? Result::ofSuccess() : Result::ofErrorMsg('Length can not be 13'))
    // custom callback
    ->callback(fn ($value) => strlen($value) !== 13, 'Length can not be 13')
    ->notBlank('Fill name!')
;

源中的所有列表验证器

标量

$ok = Validation::of($email)->required()->email()->success();

如果没有提供目标,则将使用主目标、验证主题。

验证反馈

$v = Validation::new();
$v->result(); // Xtompie\Result\Result
$v->errors(); // Xtompie\Result\ErrorCollection
$v->error(); // ?Xtompie\Result\Error first error
$v->success(); // bool
$v->fail(); // bool

扩展

组件由3个元素组成。

  1. ValidationValidator - 构建器和验证器。
  2. ValidationCore - ValidationValidator的包装器。提供流畅语法,处理验证主题。
  3. Validation - 通过继承扩展ValidationCore。提供具体的验证、过滤器、消息、键。

继承

Validation或ValidationCore可以通过继承进行扩展。

namespace App\Shared\Validation;

use App\Shared\Dao\Dao;
use Xtompie\Validation\Validation as BaseValidation;

class Validation extends BaseValidation
{
    public function __construct(
        protected Dao $dao,
    ) {}

    protected function msgs(): array
    {
        return array_merge(parent::msgs(), [
            'dao_not_exists' => 'Value {value} already exists',
        ]);
    }

    public function trim(): static
    {
        return $this->filter(fn($v) => trim($v));
    }

    public function digit($msg = 'Only digits allowed', $key = 'digit'): static
    {
        return $this->validator(fn($v) => ctype_digit($v) ? Result::ofSucces() : Result::ofErrorMsg($msg, $key));
    }

    public function daoNotExists(string $table, string $field, ?string $exceptId = null, ?string $msg = null)
    {
        return $this->validator(fn ($v) => $this->test(
            !$this->dao->exists($table, [$field => $v, 'id !=' => $exceptId]),
            'dao_not_exists',
            $msg,
            ['{value}' => $v]
        ));
    }
}

namespace App\User\Application\Service;
use App\Shared\Validation\Validation;

class CreateUserService
{
    public function __construct(
        protected Validation $validation,
    ) {}

    public function __invoke(string $email): Result
    {
        $result = $this->validation->withSubject($email)
            ->required()
            ->email()
            ->daoNotExists('user', 'email')
        ;
        if ($result->fail()) {
            return $result;
        }

        // create user

        return Result::ofSuccess();
    }
}