- 🏗️ Fluent Builder API - Build JSON Schemas using an intuitive fluent interface
- 📝 Multi-Version Support - Support for JSON Schema Draft-07, Draft 2019-09, and Draft 2020-12
- ✅ 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, Enums and Closures
- 💪 Type Safety - Built with PHP 8.3+ features and strict typing
- 🔍 Version-Aware Features - Automatic validation of version-specific features with helpful error messages
This package supports multiple JSON Schema specification versions with automatic feature validation:
- Draft-07 (2018) - Default version for maximum compatibility
- Draft 2019-09 - Adds advanced features like
$defs,unevaluatedProperties,deprecated - Draft 2020-12 - Latest version with
prefixItems, dynamic references, and format vocabularies
- PHP 8.3+
composer require cortexphp/json-schemause Cortex\JsonSchema\Schema;
use Cortex\JsonSchema\Enums\SchemaFormat;
// Create a schema
$schema = Schema::object('user')
->description('User schema')
->properties(
Schema::string('name')
->minLength(2)
->maxLength(100)
->required(),
Schema::string('email')
->format(SchemaFormat::Email)
->required(),
Schema::integer('age')
->minimum(18)
->maximum(150),
Schema::boolean('active')
->default(true),
Schema::object('settings')
->additionalProperties(false)
->properties(
Schema::string('theme')
->enum(['light', 'dark']),
),
);
$data = [
'name' => 'John Doe',
'email' => 'john@example.com',
'age' => 30,
'active' => true,
'settings' => [
'theme' => 'light',
],
];
if ($schema->isValid($data)) {
echo "Valid!";
} else {
try {
$schema->validate($data);
} catch (\Cortex\JsonSchema\Exceptions\SchemaException $e) {
echo $e->getMessage();
}
}
// Convert to array
$schema->toArray();
// Convert to JSON string
echo $schema->toJson(JSON_PRETTY_PRINT);The MIT License (MIT). Please see License File for more information.
