inok / slim-validation4
基于 Respect/Validation 的轻量级验证中间件
1.0.2
2023-04-16 16:11 UTC
Requires
- php: >=7.4.0
- ext-simplexml: *
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- respect/validation: ^2.2.3
- slim/slim: 4.*
Requires (Dev)
- ext-json: *
- php-di/php-di: ^6.3
- phpunit/phpunit: 9.5.21
- scrutinizer/ocular: 1.9
- slim/psr7: ^1.3
This package is auto-updated.
Last update: 2024-09-16 19:06:37 UTC
README
Slim 框架的验证库。它内部使用 Respect/Validation。
目录
安装
通过 Composer
$ composer require inok/slim-validation4
需要 Slim 4.0.0 或更高版本。
使用
在大多数情况下,您希望为单个路由注册 Inok\Slim\Validation
,但是,因为它是一个中间件,您也可以为所有路由注册它。
按路由注册
use Respect\Validation\Validator as v; $app = new \Slim\App(); //Create the validators $usernameValidator = v::alnum()->noWhitespace()->length(1, 10); $ageValidator = v::numericVal()->positive()->between(1, 20); $validators = [ 'username' => $usernameValidator, 'age' => $ageValidator ]; $app->get('/api/myEndPoint',function ($req, $res, $args) { //Here you expect 'username' and 'age' parameters if($req->getAttribute('has_errors')){ //There are errors, read them $errors = $req->getAttribute('errors'); /* $errors contains: [ 'username' => [ 'length' => '"davidepastore" must have a length between 1 and 10', ], 'age' => [ 'between' => '"89" must be between 1 and 20', ], ]; */ } else { //No errors } })->add(new \Inok\Slim\Validation\Validation($validators)); $app->run();
对所有路由注册
use Respect\Validation\Validator as v; $app = new \Slim\App(); //Create the validators $usernameValidator = v::alnum()->noWhitespace()->length(1, 10); $ageValidator = v::numericVal()->positive()->between(1, 20); $validators = [ 'username' => $usernameValidator, 'age' => $ageValidator ]; // Register middleware for all routes // If you are implementing per-route checks you must not add this $app->add(new \Inok\Slim\Validation\Validation($validators)); $app->get('/foo', function ($req, $res, $args) { //Here you expect 'username' and 'age' parameters if($req->getAttribute('has_errors')){ //There are errors, read them $errors = $req->getAttribute('errors'); /* $errors contains: [ 'username' => [ 'length' => '"davidepastore" must have a length between 1 and 10', ], 'age' => [ 'between' => '"89" must be between 1 and 20', ], ]; */ } else { //No errors } }); $app->post('/bar', function ($req, $res, $args) { //Here you expect 'username' and 'age' parameters if($req->getAttribute('has_errors')){ //There are errors, read them $errors = $req->getAttribute('errors'); } else { //No errors } }); $app->run();
路由参数
use Respect\Validation\Validator as v; $app = new \Slim\App(); //Create the validators $routeParamValidator = v::numericVal()->positive(); $validators = [ 'param' => $routeParamValidator, ]; $app->get('/foo/{param}', function ($req, $res, $args) { //Here you expect 'param' route parameter if($req->getAttribute('has_errors')){ //There are errors, read them $errors = $req->getAttribute('errors'); /* $errors contains: [ 'param' => [ 'numeric' => '"wrong" must be numeric', ], ]; */ } else { //No errors } })->add(new \Inok\Slim\Validation\Validation($validators)); $app->run();
请注意,请求参数优先于路由参数,因此如果您为路由和请求参数使用相同的名称,则最后一个将获胜,并且它将用于验证。
JSON 请求
您还可以验证 JSON 请求。假设您的请求体是
{ "type": "emails", "objectid": "1", "email": { "id": 1, "enable_mapping": "1", "name": "rq3r", "created_at": "2016-08-23 13:36:29", "updated_at": "2016-08-23 14:36:47" } }
并且您想验证 email.name
键。您可以这样操作
use Respect\Validation\Validator as v; $app = new \Slim\App(); //Create the validators $typeValidator = v::alnum()->noWhitespace()->length(3, 5); $emailNameValidator = v::alnum()->noWhitespace()->length(1, 2); $validators = [ 'type' => $typeValidator, 'email' => [ 'name' => $emailNameValidator, ], ];
如果有错误,结果将是
//In your route $errors = $req->getAttribute('errors'); print_r($errors); /* Array ( [email.name] => Array ( 'length' => "rq3r" must have a length between 1 and 2 ) ) */
XML 请求
您还可以验证 XML 请求。假设您的请求体是
假设您有一个包含 XML 的 POST 请求体
<person> <type>emails</type> <objectid>1</objectid> <email> <id>1</id> <enable_mapping>1</enable_mapping> <name>rq3r</name> <created_at>2016-08-23 13:36:29</created_at> <updated_at>2016-08-23 14:36:47</updated_at> </email> </person>
并且您想验证 email.name
键。您可以这样操作
use Respect\Validation\Validator as v; $app = new \Slim\App(); //Create the validators $typeValidator = v::alnum()->noWhitespace()->length(3, 5); $emailNameValidator = v::alnum()->noWhitespace()->length(1, 2); $validators = [ 'type' => $typeValidator, 'email' => [ 'name' => $emailNameValidator, ], ];
如果有错误,结果将是
//In your route $errors = $req->getAttribute('errors'); print_r($errors); /* Array ( [email.name] => Array ( 'length' => "rq3r" must have a length between 1 and 2 ) ) */
错误翻译
您可以提供一个可调用的函数来翻译错误。
use Respect\Validation\Validator as v; $app = new \Slim\App(); //Create the validators $usernameValidator = v::alnum()->noWhitespace()->length(1, 10); $ageValidator = v::numericVal()->positive()->between(1, 20); $validators = [ 'username' => $usernameValidator, 'age' => $ageValidator ]; $translator = function($message){ $messages = [ 'These rules must pass for {{name}}' => 'Queste regole devono passare per {{name}}', '{{name}} must be a string' => '{{name}} deve essere una stringa', '{{name}} must have a length between {{minValue}} and {{maxValue}}' => '{{name}} deve avere una dimensione di caratteri compresa tra {{minValue}} e {{maxValue}}', ]; return $messages[$message]; }; $middleware = new \Inok\Slim\Validation\Validation($validators, $translator); // Register middleware for all routes or only for one... $app->run();
测试
$ vendor\bin\phpunit
贡献
有关详细信息,请参阅 CONTRIBUTING。