soyuka / json-schema-bundle
Justin Rainbow的json-schema库的Symfony2工具包。
Requires
- justinrainbow/json-schema: <2
- psr/cache: ^1.0
- soyuka/php-to-json-schema: ~1.0.1
- symfony/console: ^3.0
- symfony/finder: ^3.0
- symfony/framework-bundle: ^3.0
- symfony/property-access: ^3.0
- symfony/property-info: ^3.0
- symfony/validator: ^3.0
Requires (Dev)
- doctrine/doctrine-bundle: ^1.6
- doctrine/orm: ^2.5
- phpunit/phpunit: ^5.0@dev
- symfony/cache: ^3.1@dev
Suggests
- doctrine/orm: To convert Doctrine entities to json schema's based on metadata
- symfony/cache: Enable caching abilities
README
基于对 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.