ivangrigorov/vmvalidator

简单的请求模型验证

2.4.2 2022-05-27 20:04 UTC

This package is auto-updated.

Last update: 2024-09-24 02:01:30 UTC


README

VMValidator test check

VMValidator

"Buy Me A Coffee"

你好,这是基于PHP 8新特性提供的简单属性验证,适用于PHP模型。它可以作为独立组件使用,也可以在自定义项目或Symfony和Laravel等库中使用。

仅使用三行和一些属性

示例

<?php

use RMValidator\Attributes\PropertyAttributes\Collection\UniqueAttribute;
use RMValidator\Attributes\PropertyAttributes\File\FileExtensionAttribute;
use RMValidator\Attributes\PropertyAttributes\File\FileSizeAttribute;
use RMValidator\Attributes\PropertyAttributes\Numbers\RangeAttribute;
use RMValidator\Attributes\PropertyAttributes\Object\NestedAttribute;
use RMValidator\Attributes\PropertyAttributes\Strings\StringContainsAttribute;
use RMValidator\Enums\ValidationOrderEnum;
use RMValidator\Options\OptionsModel;
use RMValidator\Validators\MasterValidator;

require __DIR__ . '/vendor/autoload.php';


class Test 
{
    public function __construct(
        #[RangeAttribute(from:10, to:50)]
        #[RangeAttribute(from:10, to:30)]
        public int $param)
    {
        
    }

    #[RangeAttribute(from:10, to:30)]
    const propTest = 40;

    #[UniqueAttribute()]
    public function custom() {
        return ['asd', 'asdk'];
    }

    #[FileSizeAttribute(fileSizeBiggest: 20, fileSizeLowest: 10)]
    #[FileExtensionAttribute(expected:['php'])]
    private function getFile() {
        return __FILE__;
    }

    #[FileSizeAttribute(fileSizeBiggest: 20, fileSizeLowest: 10)]
    #[FileExtensionAttribute(expected:['php'])]
    public string $file = __FILE__;

    #[StringContainsAttribute(needle:"asd")]
    public string $string = "23asd";

    #[RangeAttribute(from:10, to:30)]
    public int $prop = 40;
}

class UpperTest
{
    #[NestedAttribute(excludedProperties:['param'])]
    private Test $test;

    public function __construct(Test $test) {
        $this->test = $test;
    }
}

$test = new Test(40);

try {
    MasterValidator::validate(new UpperTest($test), 
    new OptionsModel(orderOfValidation: [ValidationOrderEnum::PROPERTIES, 
                                         ValidationOrderEnum::METHODS,
                                         ValidationOrderEnum::CONSTANTS], 
                     excludedMethods: ['getFile'], 
                     excludedProperties: ['file']));
} catch(Exception $e) {
    var_dump($e);
}

安装

composer require ivangrigorov/vmvalidator

选项

配置运行时验证类的顺序(方法或属性优先),以及要排除的内容,直接配置在此

附加功能

  • 许多验证
  • 支持嵌套对象验证
  • 支持集合项目类型和集合项目验证
  • 支持自定义验证*
  • 支持或验证*
  • 可空检查
  • 可重复验证属性
  • 与私有属性和方法协同工作
  • 与构造器提升协同工作
  • 内存和时间分析
  • 自定义错误消息
  • 为异常提供自定义属性和方法名称
  • 严重程度级别
  • 调试探索器
  • 回调执行*

*自定义验证应在验证类中声明为静态

class Validation 
{
    public static function validate($valueToTest, $arg1): bool 
    {
        return $valueToTest == $arg1;
    }
}

方法应始终返回布尔值:true表示有效输入,false表示无效。

声明中

#[CustomAttribute(staticClassName: Validation::class, staticMethodName: 'validate', args: [2])]

您可以在验证函数中传递额外的参数以供使用,但第一个参数始终是要测试的值。

*或验证使用自定义属性名称

    #[RangeAttribute(from:10, to:30, name: "orOne")]
    #[RangeAttribute(from:10, to:40, name: "orTwo")]
    const tttt = 40;
    MasterValidator::validate($test, new OptionsModel(orAttributes: ['orOne', 'orTwo']));

*回调执行

$successCallable在验证成功时执行

$failureCallable在验证失败时执行

$forcedCallable在验证成功和失败时执行

严重程度

添加选项以使失败的验证抛出NOTICE|WARNING|ERROR

    #[RangeAttribute(from:10, to:30, severity: SeverityEnum::ERROR)]

调试

添加选项以显示每个类的所有验证

    MasterValidator::debug(<ClassToDebug>::class);

支持

  • 请求新的验证
  • 给个星吧
  • 只是打个招呼!