wwwision/jsonschema

PHP Classes to represent JSON schema, see https://json-schema.org/

1.0.0 2025-04-10 07:56 UTC

This package is auto-updated.

Last update: 2025-04-10 08:05:40 UTC


README

PHP Classes to represent JSON Schemas, see JSON Schema

Usage

This package can be installed via composer:

composer require wwwision/jsonschema

With that, you can define a JSON Schema in PHP:

$schema = new ObjectSchema(
    title: 'Product',
    description: 'A product in the catalog',
    properties: ObjectProperties::create(
        id: new StringSchema(
            title: 'ID',
            description: 'The unique identifier for a product',
            format: StringFormat::uuid,
        ),
        title: new StringSchema(
            title: 'Product title',
            description: 'The name of the product',
        ),
        available: new BooleanSchema(
            title: 'Whether the product is available',
        ),
        price: new NumberSchema(
            title: 'Price',
            description: 'The price of the product',
            default: 0.0,
            minimum: 0.0,
        )
    )
);

$expected = <<<JSON
{
    "type": "object",
    "title": "Product",
    "description": "A product in the catalog",
    "properties": {
        "id": {
            "type": "string",
            "title": "ID",
            "description": "The unique identifier for a product",
            "format": "uuid"
        },
        "title": {
            "type": "string",
            "title": "Product title",
            "description": "The name of the product"
        },
        "available": {
            "type": "boolean",
            "title": "Whether the product is available"
        },
        "price": {
            "type": "number",
            "title": "Price",
            "description": "The price of the product",
            "default": 0,
            "minimum": 0
        }
    }
}
JSON;

assert(json_encode($schema, JSON_PRETTY_PRINT) === $expected);

JSON Schema generator

See wwwision/types-jsonschema package for a JSON Schema generator that can generate JSON Schemas from PHP classes.

Contribution

Contributions in the form of issues or pull requests are highly appreciated

License

See LICENSE