v-ghost2000 / json-schema
Json Schema Validator
Requires
- php: >=7.0
Requires (Dev)
- ext-bcmath: *
- ext-intl: *
- ext-json: *
- ext-mbstring: *
- opis/string: ^1.4
- phpunit/phpunit: ^6.5 || ^7.0
Suggests
- opis/string: A standalone library for manipulating multibyte strings
README
Отличие от огигинального репозитория заключается в том, что сделана правка позволяющая модифицировать данные в объектах с помощью фильтров.
Пример:
<?php class DateFilterObj implements IFilter { /** * @inheritDoc */ public function validate(&$date, array $args): bool { if (preg_match('#^(\d{1,2})\.(\d{1,2})\.(\d\d\d\d)$#', trim($date), $m)) { if (strlen($m[1]) === 1) { $m[1] = '0' . $m[1]; } if (strlen($m[2]) === 1) { $m[2] = '0' . $m[2]; } try { $date = DateTime::createFromFormat('Y-m-d', $m[3] . '-' . $m[2] . '-' . $m[1]); return true; } catch (Throwable $e) {} } return false; } }
<?php use Opis\JsonSchema\{ Validator, FilterContainer, Schema }; // Create a new FilterContainer $filters = new FilterContainer(); // Register our modulo filter $filters->add('string', 'dateFilter', new DateFilterObj); // Create a IValidator $validator = new Validator(); // Set filters to be used by validator $validator->setFilters($filters); $data = json_decode('{"date": "11.01.2020"}'); $schema = Schema::fromJsonString(' { "$schema": "http://json-schema.org/draft-07/schema#", "$id": "https://example.com/date.json", "type": "object", "properties": { "date": { "type": "string", "$filters": "dateFilter" } } } '); $result = $validator->schemaValidation($data, $schema); if ($result->isValid()) { echo '$data is valid', PHP_EOL; echo $data->date->format('Y-m-d'), PHP_EOL; // 2020-01-11 } else { /** @var ValidationError $error */ $error = $result->getFirstError(); echo '$data is invalid', PHP_EOL; echo "Error: ", $error->keyword(), PHP_EOL; echo json_encode($error->keywordArgs(), JSON_PRETTY_PRINT), PHP_EOL; }
Validate JSON documents
Opis JSON Schema is a PHP implementation for the JSON Schema standard (draft-07 and draft-06), that will help you validate all sorts of JSON documents, whether they are configuration files or a set of data sent to an RESTful API endpoint.
The library's key features:
- Fast validation (you can set maximum number of errors for a validation)
- Custom schema document loaders
- Support for if-then-else
- All string formats are supported
- Support for custom formats
- Support for custom media types
- Support for default value
- Support for custom variables using
$vars
keyword - Support for custom filters using
$filters
keyword - Advanced schema reuse using
$map
keyword - Support for json pointers (absolute and relative pointers)
- Support for URI templates
Documentation
The full documentation for this library can be found here. We provide documentation for both JSON Schema standard itself as well as for the library's own API.
License
Opis JSON Schema is licensed under the Apache License, Version 2.0.
Requirements
- PHP ^7.0
Installation
Opis JSON Schema is available on Packagist and it can be installed from a command line interface by using Composer.
composer require opis/json-schema
Or you could directly reference it into your composer.json
file as a dependency
{ "require": { "opis/json-schema": "^1.0" } }