slack / hack-json-schema
Hack JSON Schema generator
Installs: 2 350
Dependents: 0
Suggesters: 0
Security: 0
Stars: 27
Watchers: 12
Forks: 14
Open Issues: 18
Language:Hack
Requires
- facebook/hack-codegen: ^4.1.0
- hhvm/hsl: ^4.0.1
- hhvm/type-assert: ^4.1.0
Requires (Dev)
- facebook/fbexpect: ^2.3.0
- hhvm/hacktest: ^1.3|^2.0
- hhvm/hhast: ^4.0.5
- dev-master
- v4.102.15
- v4.102.14
- v4.102.13
- v4.102.12
- v4.102.11
- v4.102.10
- v4.102.9
- v4.102.8
- v4.102.7
- v4.102.6
- v4.102.5
- v4.102.4
- v4.102.3
- v4.102.2
- v4.102.1
- 4.102.0
- 4.80.0
- v4.56.5
- v4.56.4
- v4.56.3
- v4.56.2
- v4.56.1
- v4.27.1
- v4.27.0
- v4.0.0
- dev-ih_coerce_refs
- dev-fix-cyclic-ref
- dev-scobb-hack-generated-enum
- dev-ih_fix_empty_objects
- dev-ih_optimized_any_of_unions
- dev-ih_shape_unions_1
- dev-ih_shape_unions
- dev-ih_fix_ci
- dev-ih_nonnull_unions
- dev-number-max-constraint
- dev-ih_oneof
- dev-ih_fix_discard_additional_properties
- dev-ih_allow_disabling_type_inference
- dev-ih_bugfix_merged_shapes
- dev-ih_improve_UntypedBuilder_type_inference
- dev-ih_use_null_type
- dev-ih_untyped_coerce_default
- dev-ih_top_level_ref_aliases
- dev-ih_more_refactor
- dev-ih_handle_hackEnum_refs
- dev-ih_support_unique_hackEnum
- dev-ih_format
- dev-ih_bump_hhvm_version
- dev-ih_update_hhconfig
- dev-ih_hackEnum_constraint
- dev-ih_upgrade_hhvm
- dev-ih_schema_extension_v2
- dev-ih_allow_toplevel_refs
- dev-ignore-additional-properties
- dev-ssandler_function_reference_syntax
- dev-mwildehahn-patch-1
- dev-Roach-CLA-assistant-test
This package is auto-updated.
Last update: 2024-11-04 22:21:27 UTC
README
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:
use type Slack\Hack\JsonSchema\Codegen\Codegen; Codegen::forPath('/path/to/json-schema.json', shape( 'validator' => shape( 'file' => '/path/to/MyJsonSchemaValidator.php', 'class' => 'MyJsonSchemaValidator', ), ))->build();
/path/to/MyJsonSchemaValidator.php
now exists with a class:
final class MyJsonSchemaValidator extends BaseValidator { ... class contents }
Each validator has a validate
method, which takes a decoded JSON object:
$json = json_decode($args['json_input'], true); $validator = new MyJsonSchemaValidator($json); $validator->validate(); if (!$validator->isValid()) { print_r("invalid_json", $validator->getErrors()); return; } // JSON is valid, get typed object: $validated = $validator->getValidatedInput();
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
.
use type Slack\Hack\JsonSchema\Codegen\Codegen; $schemas = vec['/path/to/json-schema-1.json', '/path/to/json-schema-2.json', '/path/to/json-schema-3.json']; Codegen::forPaths($schemas, shape( 'validator' => shape( 'refs' => shape( 'unique' => shape( 'source_root' => '/path/to', 'output_root' => '/path/gen' ) ) ), ))->build();
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:
make install
Running Tests
make test
Related Libraries
This library was inspired by the ideas in these related libraries:
License
Hack JSON Schema is MIT-licensed.