zerifas/json
Composer 安装命令:
composer require zerifas/json
包简介
Define a schema for your JSON documents, and validate them
README 文档
README
This is a small library allowing you to define a schema for your JSON documents, validate them, and get a "safe" version of the document, with all optional values set to their defaults.
Installation
Use composer:
$ composer required zerifas/json
Usage
<?php require 'vendor/autoload.php'; use Zerifas\JSON; $schema = new JSON\Obj([ 'id' => new JSON\Number(), 'enabled' => new JSON\OptionalBoolean(false), 'array' => new JSON\Arr(), 'stringArray' => new JSON\Arr(new JSON\Str()), 'optionalArray' => new JSON\OptionalArr(), 'optionalStringArray' => new JSON\OptionalArr(new JSON\Str()), 'optionalObj' => new JSON\OptionalObj( [ 'name' => new JSON\Str(), ], [ 'name' => 'Alice', ] ), ]); $v = new JSON\Validator($schema); $json = '{"id":1,"array":[],"stringArray":["Hello","World"]}'; if ($v->isValid($json)) { $doc = $v->getDocument(); echo implode(', ', $doc->stringArray), PHP_EOL; // Hello, World echo $doc->optionalObj->name, PHP_EOL; // Alice } // This is not valid for 2 reasons: `id` is missing, and `array` is a number. $json = '{"array":15,"stringArray":[]}'; if (!$v->isValid($json)) { // Errors will be an array: // [ // 'Key path \'id\' is required, but missing.', // 'Key path \'array\' should be array, but is number.', // ] foreach ($v->getErrors() as $err) { echo $err, PHP_EOL; } }
统计信息
- 总下载量: 439
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2016-10-06