mrsuh / json-validation-bundle

此包提供了一种在请求/响应中根据模式验证JSON的方法

安装数: 6342

依赖项: 0

建议: 0

安全性: 0

星标: 17

关注者: 2

分支: 5

开放性问题: 3

类型:symfony-bundle

4.1.1 2023-03-30 20:15 UTC

This package is auto-updated.

Last update: 2024-08-30 01:32:15 UTC


README

一个提供对模式进行请求/响应JSON验证注解的Symfony包。

与 joipolloi/json-validation-bundle 的不同之处

  • 增加了 response 验证
  • 支持 Symfony >=3.4, 4.*, 5.*, 6.*
  • 错误/警告日志
  • 单个验证器使用

版本

  • ^3 用于 Symfony < 6.*
  • ^4 用于 Symfony >= 6.*

安装

composer require mrsuh/json-validation-bundle ^4

使用

创建验证方案
更多详细信息请参阅 json-schema

JsonSchema/Request/myAction.json

{
    "description": "Request JSON schema",
    "type": "object",
    "properties": {
        "test": {
            "type": "string",
            "minLength": 1
        }
    },
    "required": [ "test" ]
}

JsonSchema/Response/myAction.json

{
    "description": "Response JSON schema",
    "type": "object",
    "properties": {
        "test": {
            "type": "string",
            "minLength": 1
        }
    },
    "required": [ "test" ]
}

创建带有注解 ValidateJsonRequest 和/或 ValidateJsonResponse 的控制器
如果您想从请求中获取解码的JSON数据,请指定 $validJson 参数
如果您想以 array 类型获取解码的JSON数据,请指定 $validJson 参数的类型
如果您想以 object 类型获取解码的JSON数据或未指定类型,请指定 $validJson 参数的类型

Controller/MyController.php

<?php

use Mrsuh\JsonValidationBundle\Annotation\ValidateJsonRequest;
use Mrsuh\JsonValidationBundle\Annotation\ValidateJsonResponse;
use Symfony\Component\HttpFoundation\JsonResponse;

class MyController
{
    /**
     * @ValidateJsonRequest("JsonSchema/Request/myAction.json", methods={"POST"}, emptyIsValid=true)
     * @ValidateJsonResponse("JsonSchema/Response/myAction.json", statuses={"200"}, emptyIsValid=true)
     */
    public function myAction(array $validJson): JsonResponse
    {
        return new JsonResponse($validJson);
    }
}

请求中传递了无效的JSON

如果请求中传递了无效的JSON并且配置了 enable_request_listenerenable_exception_listener 已启用
您将获得一个详细响应,如 RFC7807 中所述,带有标题 Content-Type:application/problem+jsonerror 日志条目

{
    "detail": "There was a problem with the JSON that was sent with the request",
    "errors": [
        {
            "constraint": "minLength",
            "context": 1,
            "message": "Must be at least 1 characters long",
            "minLength": 1,
            "pointer": "/test",
            "property": "test"
        }
    ],
    "status": 400,
    "title": "Unable to parse/validate JSON"
}
app.ERROR: Json request validation {"uri":"http://127.0.0.1:8000/my","schemaPath":"JsonSchema/Request/myAction.json","errors":[{"property":"test","pointer":"/test","message":"Must be at least 1 characters long","constraint":"minLength","context":1,"minLength":1}]} []

响应中传递了无效的JSON

如果响应中传递了无效的JSON并且配置了 enable_response_listener
您将获得 warning 日志条目

app.WARNING: Json response validation {"uri":"http://127.0.0.1:8000/my","schemaPath":"JsonSchema/Response/myAction.json","errors":[{"property":"test","pointer":"/test","message":"Must be at least 1 characters long","constraint":"minLength","context":1,"minLength":1}]} []

配置

mrsuh_json_validation:
    enable_request_listener: true #default value
    enable_response_listener: true #default value
    enable_exception_listener: true #default value

单个验证器使用

<?php

use Mrsuh\JsonValidationBundle\JsonValidator\JsonValidator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MyController
{
    public function myAction(Request $request, JsonValidator $validator): Response
    {
        $validator->validate($request->getContent(), 'JsonSchema/Request/myAction.json');
        $errors = $validator->getErrors();
        if(!empty($errors)) {
            // do something with errors
        }
        
        return new Response();
    }
}