bcastellano/json-schema-bundle

Symfony 扩展包,用于使用 json-schema 验证 JSON API 请求和响应

安装: 32

依赖: 0

建议: 0

安全: 0

星星: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

v1.0.1 2016-11-08 00:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:33 UTC


README

Build Status Coverage Status SensioLabsInsight License

Json-Schema 扩展包

Symfony 扩展包,用于根据 JSON Schema 规范验证 JSON API 请求和响应。

它自动验证 API 的请求和响应,使用可以从 JSON 主体自动生成的 json-schema 文件。

JSON Schema 在其规范中被描述为

JSON Schema 是一种用于定义 JSON 数据结构的 JSON 媒体类型。JSON Schema 提供了一个用于定义特定应用程序所需 JSON 数据及其交互的合同。JSON Schema 的目的是定义 JSON 数据的验证、文档、超链接导航和交互控制。

特性

  • Json Schema 服务用于验证 JSON
  • 监听器自动验证请求和响应
  • 从 JSON 主体(请求和响应内容)生成 Json Schema 文件

安装

Composer

composer require bcastellano/json-schema-bundle

加载扩展包

<?php
// app/AppKernel.php

use Bcastellano\JsonSchemaBundle\JsonSchemaBundle;

public function registerBundles()
{
    $bundles = array(
        // ...
        new JsonSchemaBundle(),
    );
}

配置

这是一个配置参数的完整示例

json_schema:
    # Validator
    validator:
        # Class to use in validation. Is an enum
        class: Bcastellano\JsonSchemaBundle\Validator\JsonSchemaValidator
        # To auto register request and response events to auto validate jsons 
        use_listener: true
    
    # Locator
    locator:
        # Class to locate schema files
        class: Bcastellano\JsonSchemaBundle\Locator\ControllerSchemaFileLocator
        # Directory for json schema files (also to save new ones)
        resources_dir: '%kernel.root_dir%/Resources/Schemas'
    
    # Configuration for schema generator
    generator:
        enabled: false
        # NOTE: these two configurations are incompatible, you can't configure both
        # this use any external command to generate schema from json
        command: '/path/to/command {{source_file}} --output {{target_file}}'
        # this use any custom service to generate schema from json
        service: 'some.custom.service'

许可证

此扩展包受 MIT 许可证 的约束