定制 basilicom/json-schema-request-validator-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

basilicom/json-schema-request-validator-bundle

最新稳定版本:v3.0.0

Composer 安装命令:

composer require basilicom/json-schema-request-validator-bundle

包简介

Easily validate Symfony request bodies via JSON schema and automatically reject invalid requests

README 文档

README

Easily validate Symfony request bodies via JSON schema and automatically reject invalid requests

Version

Version Symfony
1.x 3.4.x or 4.1.x
2.x 5.3.x
3.x 6.4.x or 7.0.x

Installation

via composer

composer require basilicom/json-schema-request-validator-bundle
use Basilicom\JsonSchemaRequestValidator\JsonSchemaRequestValidatorBundle;
// ...
return [
    // ...
    JsonSchemaRequestValidatorBundle::class => ['all' => true],
];

Usage

The controller needs to implement the JsonSchemaRequestValidationControllerInterface. All request bodies of its actions then will be validated with JSON schema files set via the interface method setJsonSchemaFilePathsInFilePathProvider(FilePathProvider $filePathProvider).

All actions of this controller must have a JSON schema file which must be mapped via the route name.

The system automatically rejects an invalid incoming requests with status code "400 Bad Request". If no JSON schema file can be found it will respond with "500 Internal Server Error".

Example Symfony controller

<?php

namespace AppBundle\Controller;

use Basilicom\JsonSchemaRequestValidator\Controller\JsonSchemaRequestValidationControllerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class TestingEndpointsController extends AbstractController implements JsonSchemaRequestValidationControllerInterface
{
    /**
     * @Route("/testing", methods={"POST"}, name="testing_post")
     *
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function testingPost(Request $request): JsonResponse
    {
        return new JsonResponse(['success']);
    }
    
    /**
     * @Route("/testing", methods={"GET"}, name="testing_get")
     *
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function testingGet(Request $request): JsonResponse
    {
        return new JsonResponse(['success']);
    }

    public function setJsonSchemaFilePathsInFilePathProvider(FilePathProvider $filePathProvider)
    {
        $filePathProvider->setIgnoreRouteName('testing_get', true);
        $filePathProvider->setJsonSchemaFilePathForRouteName('testing_post', __DIR__ . '/../Resources/jsonschemas/test.json');
    }
}

统计信息

  • 总下载量: 5.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 10
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-31