hadesarchitect/json-schema-bundle

Justin Rainbow的json-schema库的Symfony2组件。

安装数: 86,998

依赖者: 1

建议者: 0

安全: 0

星标: 12

关注者: 2

分支: 10

公开问题: 2

类型:symfony-bundle

v1.0.0 2016-06-16 22:54 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:58:09 UTC


README

Build Status Latest Stable Version Total Downloads Dependency Status SensioLabsInsight

Symfony组件,用于Justin Rainbow的JsonSchema库:https://github.com/justinrainbow/json-schema。提供symfony服务而不是传统的$validator = new Validator();。

A PHP Implementation for validating `JSON` Structures against a given `Schema`.
See https://json-schema.fullstack.org.cn for more details.

安装

通过composer安装

第一步:要求组件

composer require hadesarchitect/json-schema-bundle ~1.0

第二步:启用组件

<?php
// app/AppKernel.php

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

用法

示例

从预定义的模式中获取模式

// Acme\DemoBundle\Controller\DemoController
// Get the schema and data as objects

$retriever = $this->get('hades.json_schema.uri_retriever');
$schema = $retriever->retrieve('https://json-schema.fullstack.org.cn/geo'));

将对象与模式进行验证

// Acme\DemoBundle\Controller\DemoController
// Obtain validator
$validator = $this->get('hades.json_schema.validator');

// Get schema
$schema = $this->get('hades.json_schema.uri_retriever')->retrieve('https://json-schema.fullstack.org.cn/address');

// Prepare object
$object = array();
$object['country-name'] = 'Some country';
$object['region']       = 'Some region';
$object['locality']     = 'Some locality';

// Validate with check method, which throws exceptions in case of violations
$validator->check((object) $object, $schema); // Minimal requirements satisfied, so everything is OK

unset($object['country-name']);
unset($object['region']);

try {
    $validator->check((object) $object, $schema); // ViolationException thrown.
} catch (\HadesArchitect\JsonSchemaBundle\Exception\ViolationException $exc) {

    echo $exc->getMessage(); // The property region is required. The property country-name is required.

    foreach ($exc->getErrors() as $error) {
        // the property region is required
        // the property country-name is required
        echo $error->getViolation();
    }
}

// Also you can use isValid instead of check
$result = $validator->isValid((object) $object, $schema); // Result is false, because necessary properties not set

// Result is false now, so we can obtain errors from validator
if (!$result) {
    $errors = $validator->getErrors();

    foreach ($errors as $error) {
        // the property region is required
        // the property country-name is required
        echo $error->getViolation();
    }
    // Errors will be kept in validator till next validation (check or isValid)
}

可用服务

  • hades.json_schema.validator
  • hades.json_schema.uri_retriever
  • hades.json_schema.uri_resolver

参数

可以在您的parameters.yml中覆盖的参数

  • hades.json_schema.validator.class - 实际验证器类
  • hades.json_schema.validator.service.class - 验证器服务类
  • hades.json_schema.uri_resolver.class - 实际解析器类
  • hades.json_schema.uri_resolver.service.class - 解析器服务类
  • hades.json_schema.uri_retriever.class - 实际检索器类
  • hades.json_schema.uri_retriever.service.class - 检索器服务类