定制 phindmarsh/statham 二次开发

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

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

phindmarsh/statham

最新稳定版本:v1.0.4

Composer 安装命令:

composer require phindmarsh/statham

包简介

A JSON Schema validator

README 文档

README

A JSON Schema validator written in PHP

This is a (pretty much straight up) port of the Javascript ZSchema library for validating JSON against JSON schemas, and validating JSON Schemas themselves.

Why

Motivation for writing this was to validate swagger.io files in PHP, and none of the existing validators worked with recursive schemas, or resolved nested references properly. The Javascript one worked as I needed it to, so I ported it.

So Statham will:

  • Not recurse infinitely when validating a schema that references itself
  • Properly deal with $refs that are located in different schemas

Usage

// schemas used during validation
$schemas = [
  "http://json-schema.org/draft-04/schema#"
  "http://swagger.io/v2/schema.json#"
];

$statham = new \Statham\Statham();

// download each schema and add to internal cache
foreach($schemas as $schema_url){
    $schema = json_decode(file_get_contents($schema_url));
    $statham->setRemoteReference($schema_url, $schema);
}

// just validate the schema (no data)
$statham->validateSchema($schemas[1]);

// validate $json_to_validate against a given schema
$statham->validate($json_to_validate, $schemas[1]);

Statham doesn't automatically download externally referenced schemas, (it totally could, but reasons), so use $statham->setRemoteReference($url, $schema_object) for any schemas that will be used during validation.

Schemas can either be passed as an object (doesn't support schemas as arrays, but again, totally could), or if you've used $statham->setRemoteReference() you can pass it as a string, being the URL of the schema.

JSON data can only be passed as objects, so just use json_decode($json) (no second true argument) and you'll be fine.

Todo

It doesn't check formats properly (so emails, dates, etc aren't validated they are anything beyond a string). This is easy enough to do, I just haven't yet.

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-25