JSON Schema Support

JSON Schema is a popular vocabulary to describe the shape of JSON documents. A variant of JSON Schema is also used in OpenAPI specifications.

API Platform provides an infrastructure to generate JSON Schemas for any resource, represented in any format (including JSON-LD). The generated schema can be used with libraries such as react-json-schema-form to build forms for the documented resources, or to be used for validation.

Generating a JSON Schema

To export the schema corresponding to an API Resource, run the following command:

$ docker-compose exec php bin/console api:json-schema:generate 'App\Entity\Book'

To see all options available, try:

$ docker-compose exec php bin/console help api:json-schema:generate

Generating a JSON Schema Programmatically

To generate JSON Schemas programmatically, use the api_platform.json_schema.schema_factory service.

Testing

API Platform provides a PHPUnit assertion to test if a response is valid according to a given Schema: assertMatchesJsonSchema(). Refer to the testing documentation for more details.