Download the PHP package swaggest/json-schema without Composer
On this page you can find all versions of the php package swaggest/json-schema. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download swaggest/json-schema
More information about swaggest/json-schema
Files in swaggest/json-schema
Package json-schema
Short Description High definition PHP structures with JSON-schema based validation
License MIT
Informations about the package json-schema
Swaggest JSON-schema implementation for PHP
High definition PHP structures with JSON-schema based validation.
Supported schemas:
Installation
Usage
Structure definition can be done either with json-schema
or with
PHP
class extending Swaggest\JsonSchema\Structure\ClassStructure
Validating JSON data against given schema
Define your json-schema
Load it
Validate data
You can also call Schema::import
on string uri
to schema json data.
Or with boolean argument.
Understanding error cause
With complex schemas it may be hard to find out what's wrong with your data. Exception message can look like:
For ambiguous schemas defined with oneOf
/anyOf
message is indented multi-line string.
Processing path is a combination of schema and data pointers. You can use InvalidValue->getSchemaPointer()
and InvalidValue->getDataPointer()
to extract schema/data pointer.
You can receive Schema
instance that failed validation with InvalidValue->getFailedSubSchema
.
You can build error tree using InvalidValue->inspect()
.
PHP structured classes with validation
Validation of dynamic properties is performed on set, this can help to find source of invalid data at cost of some performance drop
Validation of native properties is performed only on import/export
Error messages provide a path to invalid data
Nested structures
Nested structures allow you to make composition: flatten several objects in one and separate back.
You can also use \Swaggest\JsonSchema\Structure\Composition
to dynamically create schema compositions. This can be
helpful to deal with results of database query on joined data.
Keys mapping
If property names of PHP objects should be different from raw data you can call ->addPropertyMapping
on owner schema.
It will affect data mapping:
You can have multiple mapping namespaces, controlling with mapping
property of Context
You can create your own pre-processor implementing Swaggest\JsonSchema\DataPreProcessor
.
Meta
Meta
is a way to complement Schema
with your own data. You can keep and retrieve it.
You can store it.
And get back.
Mapping without validation
If you want to tolerate invalid data or improve mapping performance you can specify skipValidation
flag in
processing Context
Overriding mapping classes
If you want to map data to a different class you can register mapping at top level of your importer structure.
Or specify it in processing context
Code quality and test coverage
Some code quality best practices are deliberately violated here ( see ) to allow best performance at maintenance cost.
Those violations are secured by comprehensive test coverage:
- draft-04, draft-06, draft-07 of JSON-Schema-Test-Suite
- test cases (excluding
$data
and few tests) of epoberezkin/ajv (a mature js implementation)
Contributing
Issues and pull requests are welcome!
Development supported by JetBrains.
All versions of json-schema with dependencies
ext-json Version *
phplang/scope-exit Version ^1.0
swaggest/json-diff Version ^3.8.2
symfony/polyfill-mbstring Version ^1.19