soyuka/json-schema-bundle

Justin Rainbow的json-schema库的Symfony2工具包。

安装: 9,499

依赖者: 0

建议者: 0

安全性: 0

星标: 6

关注者: 4

分支: 10

类型:symfony-bundle

1.0.11 2018-06-13 14:51 UTC

This package is auto-updated.

Last update: 2024-09-19 20:40:26 UTC


README

Build Status SensioLabsInsight

基于对 https://github.com/HadesArchitect/JsonSchemaBundle 的分支

功能

  • 通过命令生成Json Schema(通过Doctrine或Php)
  • Json Schema验证注解
  • 接受类实例的Json Schema服务

安装

Composer

composer require soyuka/jsonschemabundle

加载包

<?php
// app/AppKernel.php

use Soyuka\JsonSchemaBundle\JsonSchemaBundle;

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

Json Schema生成

默认情况下,解析位于每个Bundle的每个Entity目录中的文件。默认策略使用php解析您的类(请参阅php-to-json-schema)。

app/console jsonschema:generate

如果您想解析另一个目录,只需通过--directory选项指定即可

app/console jsonschema:generate -d Model

如果您想使用Doctrine注解生成Json Schema,指定doctrine策略。请注意,需要doctrine/orm-bundle

app/console jsonschema:generate --strategy doctrine

每个Json Schema都将写入您选择的配置目录,保持以下树形结构

BundleName/Entity.json

默认值为%kernel.root_dir%/Resources/validators,所以假设解析的Entity位于AcmeBundle/Entity/Product,则模式将写入%kernel.root_dir%/Resources/validators/AcmeBundle/Product.json

验证注解

此包添加了JsonSchema 注解。如果没有提供选项,验证器将在json路径中查找(与模式生成相同的结构)。

最基础的例子将是

<?php
use Soyuka\JsonSchemaBundle\Constraints as JsonSchemaAssert;

/**
 * @JsonSchemaAssert\JsonSchema
 */
class DefaultJsonSchema
{
}

查看完整的类.

如果您想在不同的位置或远程位置使用Json Schema,指定您的选择路径

<?php
use Soyuka\JsonSchemaBundle\Constraints as JsonSchemaAssert;

/**
 * @JsonSchemaAssert\JsonSchema(schema = "validators/specific.json")
 */
class SpecificJsonSchema
{
}

schema选项值使用JsonSchema\Uri\UriRetriever检索(任何在该处工作值都可以在注解中使用)。

配置

json_schema:
    path: 'foobar'
    cache: 'some_psr_cache_service' #cache schema's

翻译

翻译域为JsonSchemaViolations,因此默认翻译将位于

%kernel.root_dir%/Resources/translations/JsonSchemaViolations.{lang}.{format}

每个翻译都将提供以下字符串参数

  • %property%:属性路径
  • %expected%:预期值
  • %invalid%:当前无效值

键是约束代码,例如minLength

<source>minLength</source>
<target>%property% is not long enough, expected %expected% got "%invalid%"</target>

技巧

如果您想自己处理JsonSchema验证,但使用justinrainbow/json-schema与Entity实例有问题,您可能想看看PropertyAccessorConstraint。在这里,它将覆盖ObjectConstraint,以便通过属性访问器访问对象属性。查看Validator类

许可证

The MIT License (MIT)

Copyright (c) 2015 Antoine Bluchet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.