Download the PHP package slack/hack-json-schema without Composer
On this page you can find all versions of the php package slack/hack-json-schema. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package hack-json-schema
Hack JSON Schema
Hack JSON Schema is a library for validating JSON inputs, using JSON schemas, in Hack.
Given a JSON schema file, you can generate a validator in Hack to validate incoming JSON against the schema. If the JSON conforms to the schema, the validator will returned typed output.
There are several benefits to generation:
- We don't have to parse the JSON schema to validate the incoming object at runtime.
- We can output typed shapes that are generated from the JSON schema, increasing the type safety of downstream code.
Usage
Codegen::forPath
The most basic way to use this library is to generate a validator from a JSON schema file:
/path/to/MyJsonSchemaValidator.php
now exists with a class:
Each validator has a validate
method, which takes a decoded JSON object:
Codegen::forPaths
If you have multiple JSON schemas that leverage the $ref
attribute, you should prefer to use Codegen::forPaths
over Codegen::forPath
.
The workflow for Codegen::forPath
is:
- Given a JSON schema, "de-reference" the schema. De-referencing is the process of resolving all of the
$ref
paths with their actual schema. This creates a single de-referenced schema. - With the de-referenced schema, generate a validator.
This works well if you only have one primary schema, but if you have multiple schemas, each with common refs, you'll start to generate a lot of duplicate code.
In these cases, you can use Codegen::forPaths
.
By defining the source_root
and output_root
we can generate unique validators per $ref
we come across. We can then re-use those validators when generating other validators.
Developing
Installing Dependencies
We handle all dependencies through Docker. It's as simple as:
Running Tests
Related Libraries
This library was inspired by the ideas in these related libraries:
License
Hack JSON Schema is MIT-licensed.
All versions of hack-json-schema with dependencies
hhvm/hsl Version ^4.0.1
hhvm/type-assert Version ^4.1.0