cortexphp/json-schema
最新稳定版本:0.6.0
Composer 安装命令:
composer require cortexphp/json-schema
包简介
A fluent JSON Schema builder for PHP
README 文档
README
Fluently build and validate JSON Schemas
Features
- 🏗️ 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
JSON Schema Version Support
This package supports multiple JSON Schema specification versions with automatic feature validation:
Supported Versions
- Draft 2020-12 - (Default) Latest version with
prefixItems, dynamic references, and format vocabularies - Draft 2019-09 - Adds advanced features like
$defs,unevaluatedProperties,deprecated - Draft-07 (2018) - Legacy version for maximum compatibility
Requirements
- PHP 8.3+
Installation
composer require cortexphp/json-schema
Quick Start
use 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);
Documentation
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 32.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-17