bartosz-maciaszek / validation
适用于 PHP 5.6+ / HHVM 的验证库,受 Joi 启发。
1.1.0
2015-11-25 15:08 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: 5.0.*
- satooshi/php-coveralls: ~0.6.0
- squizlabs/php_codesniffer: ~2.0
This package is not auto-updated.
Last update: 2024-09-20 21:57:46 UTC
README
适用于 PHP 5.6+ / PHP 7 / HHVM 的验证库,受 Joi(来自 Hapi)的启发。
安装
推荐通过 Composer 安装此库
composer require bartosz-maciaszek/validation
示例
使用此库进行验证很简单。您可以验证基本类型,如下所示
<?php use Validation\Validation as V; V::validate('foobar', V::string(), function($err, $output) { if ($err) { echo 'Validation failed: ' . $err; exit; } echo $output; // 'foobar' });
您还可以链式调用其他断言
V::validate('user@example.com', V::string()->email(), function($err, $output) { // ... });
此库还支持转换
V::validate('FooBar', V::string()->lowercase(), function($err, $output) { // $output equals 'foobar'! });
想要更复杂的功能?让我们尝试验证一个数组!
$input = [ 'username' => 'foobar', 'password' => 'secret123', 'birthyear' => 1980, 'email' => 'foobar@example.com', 'sex' => 'male' ]; $schema = V::arr()->keys([ 'username' => V::string()->alphanum()->min(3)->max(30), 'password' => V::string()->regex('/[a-z-A-Z0-9]{3,30}/'), 'birthyear' => V::number()->integer()->min(1900)->max(2013), 'email' => V::string()->email(), 'sex' => V::string()->valid('male', 'female') ]); V::validate($input, $schema, function ($err, $output) { // $err === null -> valid! });
文档可以在 这里 找到(请注意,它尚未完成100% :))。
测试
要运行单元测试,只需安装依赖项并调用 make test
$ make deps
$ make test
贡献
欢迎贡献。如果您想帮忙,请fork仓库并提交一个pull request。为了保持编码风格,请确保您的代码符合PSR2标准。
$ make cs