everest / validation
Everest - 验证组件
Requires
- php: >=8.0
README
Everest的验证组件旨在以简单直观的方式验证用户输入。
安装
只需前往项目目录,其中包含composer.json
文件,并输入
composer require everest/validation
用法
本软件包提供两种数据验证方法。您可以选择对特定要求(类型)的单个值进行验证,或者对数组或类似数组的每个键的验证链进行验证。
验证单个值
use Everest\Validation\Validation; $int = Validation::integer('10'); // $int -> 10 $noint = Validation::integer('foo'); // Will throw \Everest\Validation\InvalidValidationException
使用验证链验证值数组
use Everest\Validation\Validate; $data = [ 'foo' => '10', 'bar' => 'foo' ]; $data = Validate::lazy($data) ->that('foo')->integer()->between(0, 20) ->that('bar')->enum(['bar', 'foo'])->upperCase() ->execute(); // $data -> ['foo' => 10, 'bar' => 'FOO']
您可以使用额外的验证链,通过->or()
分隔它们
use Everest\Validation\Validate; $data = [ 'bar' => 'foo' ]; $data = Validate::lazy($data) ->that('bar')->integer()->between(0, 20) ->or()->string()->minLength(2) ->execute(); // $data -> ['bar' => 'FOO']
严格和懒惰验证
可以选择使用Validate::strict()
和Validate::lazy()
。前者将在第一个错误发生时抛出InvalidValidationException
,而后者将收集所有错误,并抛出InvalidLazyValidationException
,该异常提供了::getErrors()
和::getErrorsGroupedByKey()
方法来访问所有捆绑的InvalidValidationException
异常。
验证嵌套数组
可以使用点符号来验证嵌套值。
use Everest\Validation\Validate; $data = [ 'foo' => [ ['bar' => 1, 'name' => 'Some name'], ['bar' => 2, 'name' => 'Some other name'] ] ]; $data = Validate::lazy($data) ->that('foo.*.bar')->integer()->between(0, 20) ->that('foo.*.name')->string()->upperCase() ->execute(); // $data -> [ // 'foo' => [ // ['bar' => 1, 'name' => 'SOME NAME'], // ['bar' => 2, 'name' => 'SOME OTHER NAME'] // ] // ]
可选参数
参数可以标记为可选,也可以带有默认值。如果验证使用默认值作为后备,则该值不再由验证链进行验证!
use Everest\Validation\Validate; $data = ['foo' => 10]; $result = Validate::lazy($data) ->that('foo')->integer() ->that('bar')->optional(/* no default */)->integer() ->execute(); // $result -> ['foo' => 10] $result = Validate::lazy($data) ->that('foo')->integer() ->that('bar')->optional(null)->integer() ->execute(); // $result -> ['foo' => 10, 'bar' => null]
类型
类型是必须满足的规则。
数组
Validation::array($value)
,验证给定的值是否为数组。
范围
Validation::between($value, numeric $min, numeric $max)
,验证给定的值是否满足$min <= $value <= $max
。
布尔值
Validation::boolean($value)
,验证给定的值是否为布尔值或布尔值。结果将转换为布尔值。此类型将$value
解释如下
其他所有值都将抛出InvalidValidationException
。
日期时间
Validation::dateTime($value, string $pattern)
,验证给定的值是否与提供的日期模式匹配,并返回一个新的DateTime
实例。
DateTimeImmutable
与DateTime相同,但返回一个新的DateTimeImmutable
实例。
枚举
Validation::dateTime($value, array $enum)
,验证给定的值是否与$enum
中的值匹配。如果$enum
是一个关联数组,则尝试将$value
与键匹配,并返回关联的值。
浮点数
Validation::float($value)
,验证给定的值是否为数值。结果将转换为浮点数。
整数
Validation::integer($value)
,验证给定的值是否为整数或整数值。结果将转换为整数。
键存在
Validation::keyExisits($value, $key)
,验证给定的值是否为数组,并且提供的键存在于该数组中。
长度
Validation::length($value, int $length)
,验证给定的值是否与提供的字符串长度匹配使用strlen
。
长度范围
Validation::lengthBetween($value, int $min, int $max)
,验证给定的值字符串长度在提供的最小值和最大值之间。
长度最大
Validation::lengthMax($value, int $max)
,验证给定的值字符串长度小于或等于提供的最大值。
长度最小
Validation::lengthMin($value, int $min)
,验证给定的值字符串长度是否大于或等于指定的最小值。
最大值
Validation::max($value, int $max)
,验证给定的数值是否小于或等于指定的最大值。
最小值
Validation::min($value, int $min)
,验证给定的数值是否大于或等于指定的最小值。
非空
Validation::notEmpty($value)
,验证给定的值是否非空。
为空
Validation::null($value)
,验证给定的值是否为null
。
字符串
Validation::string($value)
,验证给定的值是否为字符串。
过滤器
可以使用过滤器来转换验证结果。
小写
Validation::lowerCase($value)
,对提供的值执行strtolower
。
移除标签
Validation::stripTags($value)
,对提供的值执行strip_tags
。
去除空白字符
Validation::trim($value)
,对提供的值执行trim
。
大写
Validation::upperCase($value)
,对提供的值执行strtoupper
。
自定义类型
可以通过创建一个新的类,该类继承自Everest\Validation\Types\Type
,来添加自定义类型。
<?php class CustomType extends \Everest\Validation\Types\Type { public static $errorName = 'invalid_custom_error'; public static $errorMessage = '%s is not a valid custom type.'; public function __invoke($value, $message = null, string $key = null, $customArg1 = null, $customArg2 = null) { if (/* Your invalid condition here */) { $message = sprintf( self::generateErrorMessage($message ?: self::$errorMessage), self::stringify($value) ); throw new InvalidValidationException(self::$errorName, $message, $key, $value); } /** * You may transform/cast the result before retuning it. * In this case it is usefull to add a custom argument as * `$doCast` = false flag */ return $value; } }
在下一步中,您需要将您的类型与Everest\Validation\Validation
类连接起来。
<?php // Add as class. A singleton instance will be created when the type is requested the first time \Everest\Validation\Validation::addType('custom_name', CustomType::CLASS); // Add as instance. You can also supply a instance of your custom type. // E.g. when you need to do some configuration in `__construct()` \Everest\Validation\Validation::addType('custom_name', new CustomType());
如果您想要覆盖现有类型,您需要将true
作为第三个参数传递给\Everest\Validation\Validation::addType
。
现在您可以通过Validation::custom_type($var)
或通过在验证链中使用->custom_type()
来使用自定义类型。
许可
本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE.md文件。