madesimple/slim-validation

摘要:Slim 中间件,允许进行简单的请求验证

v3.0.0 2020-06-19 22:57 UTC

This package is auto-updated.

Last update: 2024-09-22 05:55:12 UTC


README

Build Status

Slim Validation 是一个抽象类,可扩展以执行每个路由的验证。Slim Validation 允许两种类型的验证模型

  • 作为函数的验证
  • 作为中间件的验证

作为函数的验证是推荐的使用场景。由于 Slim v4 变得更加灵活,不再有直接从中间件中提取请求参数的方法;这意味着不再支持作为中间件的 getPathRules 检查。

在两种情况下,只需扩展抽象类 \MadeSimple\Slim\Middleware\Validation 并实现三个抽象方法

  1. getPathRules - 允许您验证请求路径。
  2. getQueryParameterRules - 允许您验证请求的查询参数。
  3. getParsedBodyRules - 允许您验证请求解析后的体。

Slim Validation 将违反路径规则的请求视为 404 Not Found。违反查询参数或解析体规则的请求应返回 422 Unprocessable Entity

例如

<?php
// Request class
namespace Requests;

class ModelRequestValidation extends \MadeSimple\Slim\Middleware\Validation
{
    /**
     * @return array Rule set for the request path.
     */
    protected function getPathRules(): array
    {
        return [
            // The model locator must be a uuid and pass your custom acl check
            'locator' => 'uuid|custom_acl_check'
        ];
    }

    protected function getQueryParameterRules(array $routeArguments): array
    {
        return [];
    }

    protected function getParsedBodyRules(array $routeArguments): array
    {
        return [];
    }
}

class PaginatedRequestValidation extends \MadeSimple\Slim\Middleware\Validation
{
    protected function getPathRules(): array
    {
        return [];
    }

    /**
     * @param array $routeArguments Route arguments
     *
     * @return array Rule set for the query parameters.
     */
    protected function getQueryParameterRules(array $routeArguments): array
    {
        return [
            // The page must be at least 1
            'page'  => 'is:int|min:1',
            // The items per page must at least 5 but no more than 100
            'limit' => 'is:int|min:5|max:100',
        ];
    }

    /**
     * @param array $routeArguments Route arguments
     *
     * @return array Rule set for the parsed body.
     */
    protected function getParsedBodyRules(array $routeArguments): array
    {
        return [];
    }
}

class PostRequestValidation extends \MadeSimple\Slim\Middleware\Validation
{
    protected function getPathRules(): array
    {
        return [];
    }

    protected function getQueryParameterRules(array $routeArguments): array
    {
        return [];
    }

    /**
     * @param array $routeArguments Route arguments
     *
     * @return array Rule set for the parsed body.
     */
    protected function getParsedBodyRules(array $routeArguments): array
    {
        return [
            // The current password is required
            'current_password' => 'required',
            // The updated password is required and must meet your custom password strength test
            'updated_password' => 'required|custom_password_strength_test',
            // The confirm password is required and must equal the `updated_password` property
            'confirm_password' => 'required|equals:updated_password',
        ];
    }
}

作为函数的验证

作为函数的验证是推荐的方法。如果请求未通过路径规则验证,则抛出 Slim\Exception\HttpNotFoundException。如果请求未通过查询参数或解析体验证,则抛出 MadeSimple\Slim\Middleware\InvalidRequestException。要使用作为函数的验证,创建如上所示的验证类,并在路由的可调用中调用 validate 方法,例如

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

//...
$app = new \Slim\App;
//...

$app->post('/route/path/foo', function (Request $request, Response $response) {
    // Validate the request immediately, if it is invalid then it will throw an exception
    (new \Request\FooPostValidation($this))->validate($request);

    // ...
});

作为中间件的验证

要使用作为中间件的验证,创建如上所示的验证类,并将其作为中间件添加到您希望应用该验证的路由,例如

<?php
// Routes

$app->post('/route/path/foo', \Controller\Foo::class . ':post')
    ->add(\Request\FooPostValidation::class);

如果请求未通过查询参数或解析体规则的验证,则检查容器中的 invalidRequestHandler。如果没有处理程序,则抛出 MadeSimple\Slim\Middleware\InvalidRequestException。如果有处理程序,则调用 invalidRequestHandler($request, $response, $errors)

官方文档