redbox / validator
https://github.com/johnnymast/redbox-validator
v0.20
2023-03-30 19:46 UTC
Requires
- php: >=8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.15
- pestphp/pest: ^1.22
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-08-28 19:24:14 UTC
README
Redbox-validation 是一个简单易用的验证类。其实现基于 Laravel 的验证包(但不是重写),在使用到你的项目中时没有外部依赖。
安装
$ composer require redbox/validation
使用
该包使用灵活,语法简单。
基本使用
use Redbox\Validation\Validator; $validator = new Validator([ 'foo' => 'The quick brown fox jumps over the lazy dog' ]); $data = $validator->validate([ 'food' => 'string' ]); /** * Validator::passes() is also * available. */ if ($validator->fails()) { /** * Handle errors use $validator->errors() * to get the errors. */ }
使用闭包
你可以使用自己的闭包来添加自定义规则
use Redbox\Validation\ValidationContext; use Redbox\Validation\Validator; $validator = new Validator([ 'foo' => 'The quick brown fox jumps over the lazy dog' ]); $data = $validator->validate([ /** * This will validate field 'foo' equals 'baz' in this case it will not so * the validation will fail and an error will be set for field 'foo'. */ 'foo' => function (ValidationContext $context): bool { return ($context->keyExists() && $context->getValue() === 'baz') or $context->addError("Field {$context->getKey()} does not equal 'baz'."); } ]); /** * Validator::passes() is also * available. */ if ($validator->fails()) { /** * Handle errors use $validator->errors() * to get the errors. */ print_r($validator->errors()); }
可用规则
该包提供以下验证规则。
许可证
MIT 许可证 (MIT)
版权所有 © 2023 Johnny Mast
特此授予任何人免费获得本软件及其相关文档文件(以下简称“软件”)的副本的权利,不受任何限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向提供软件的人员进行此类操作,但前提是以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,还是与商业性、特定目的适用性或非侵权相关的保证。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任负责,无论是在合同行为、侵权行为或其他行为中产生的,无论是由于软件或其使用或其他方式导致的。