wwwision / jsonschema
PHP Classes to represent JSON schema, see https://json-schema.org/
Fund package maintenance!
bwaidelich
Paypal
Requires
- php: >=8.1
- webmozart/assert: ^1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3
- phpstan/phpstan: ^2
- phpunit/phpunit: ^10 || ^11 || ^12
- roave/security-advisories: dev-latest
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