Download the PHP package cortexphp/json-schema without Composer
On this page you can find all versions of the php package cortexphp/json-schema. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cortexphp/json-schema
More information about cortexphp/json-schema
Files in cortexphp/json-schema
Package json-schema
Short Description A fluent JSON Schema builder for PHP
License MIT
Homepage https://github.com/cortexphp/json-schema
Informations about the package json-schema
Fluently build and validate JSON Schemas
Support is limited to draft-07 at the moment.
Features
- 🏗️ Fluent Builder API - Build JSON Schemas using an intuitive fluent interface
- 📝 Draft-07 Support - Full support for JSON Schema Draft-07 specification
- ✅ Validation - Validate data against schemas with detailed error messages
- 🤝 Conditional Schemas - Support for if/then/else, allOf, anyOf, and not conditions
- 🔄 Reflection - Generate schemas from PHP classes and closures
- 💪 Type Safety - Built with PHP 8.3+ features and strict typing
Why?
I found myself looking for a nice, fluent way to build JSON Schemas, but couldn't find anything that fit my needs.
There are many use cases, but the most prevalent right now is usage around LLMs, in particular structured outputs and tool calling.
In fact I'm building an AI framework currently that uses this package to generate JSON Schemas in lots of scenarios. More to come on that soon!
Requirements
- PHP 8.3+
Installation
Usage
You can also use the objects directly instead of the factory methods.
Available Schema Types
String Schema
View JSON Schema
Number Schema
View JSON Schema
Integer Schema
View JSON Schema
Boolean Schema
View JSON Schema
Null Schema
View JSON Schema
Array Schema
View JSON Schema
Arrays also support validation of specific items using contains
:
You can also validate tuple-like arrays with different schemas for specific positions:
Object Schema
Objects support additional validation features:
Objects also support pattern-based property validation using patternProperties
:
Pattern properties can be combined with regular properties and additionalProperties
:
View JSON Schema
Union Schema
View JSON Schema
String Formats
Strings can be validated against various formats:
Conditional Validation
The schema specification supports several types of conditional validation:
Schema Definitions & References
You can define reusable schema components using definitions and reference them using $ref
:
You can also add multiple definitions at once:
The resulting JSON Schema will include both the definitions and references:
View JSON Schema
Validation
The validate method throws a SchemaException
when validation fails:
Common Schema Properties
All schema types support these common properties:
Converting to JSON Schema
This uses reflection to infer the schema from the parameters and docblocks.
From a Closure
From a Class
Credits
- Sean Tymon
- All Contributors
License
The MIT License (MIT). Please see License File for more information.