cortexphp/json-schema
Composer 安装命令:
composer require cortexphp/json-schema
包简介
A fluent JSON Schema builder for PHP
关键字:
README 文档
README
Features
- 🏗️ Fluent Builder API - Build JSON Schemas using an intuitive fluent interface
- 📝 Multi-Version Support - Support for JSON Schema Draft-06, 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 — including docblock array generics and constructor property promotion
- 💪 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 with broad tool compatibility
- Draft-06 (2017) - Legacy version for maximum compatibility with older tooling
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.
cortexphp/json-schema 适用场景与选型建议
cortexphp/json-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83.81k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「schema」 「json-schema」 「cortex」 「zod」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cortexphp/json-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cortexphp/json-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cortexphp/json-schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Like FormRequests, but for validating against a json-schema
Kinikit - PHP Application development framework MVC component
Extension for Opis JSON Schema
generate json schema files from openapi paths
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
统计信息
- 总下载量: 83.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-17