senadir/zod-php-to-schema
Composer 安装命令:
composer require senadir/zod-php-to-schema
包简介
Convert Zod-like PHP objects to JSON Schema definitions
README 文档
README
Convert Zod-like PHP schema definitions to JSON Schema. This package provides a fluent API for defining schemas in PHP and converting them to JSON Schema format.
Installation
composer require nadir/zod-php-to-schema
Usage
use Zod2Schema\Zod2Schema\Z; use Zod2Schema\Zod2Schema\Schema; // By default, fields are not required $userSchema = Z::object([ 'name' => Z::string()->min(2), 'age' => Z::number()->min(0)->max(120), 'email' => Z::string()->email(), 'tags' => Z::array(Z::string()) ])->to_json_schema(); echo json_encode($userSchema, JSON_PRETTY_PRINT);
Which will output:
{
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 2
},
"age": {
"type": "number",
"minimum": 0,
"maximum": 120
},
"email": {
"type": "string",
"format": "email"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
You can also enable required fields globally:
use Zod2Schema\Zod2Schema\Z; use Zod2Schema\Zod2Schema\Schema; // Enable required fields globally Schema::set_require_all_fields(true); // Now all fields will be required by default, but you can mark specific fields as optional $userSchemaWithOptional = Z::object([ 'name' => Z::string()->min(2), // required 'age' => Z::number()->optional(), // optional 'email' => Z::string()->email(), // required 'tags' => Z::array(Z::string())->optional() // optional ])->to_json_schema(); echo json_encode($userSchemaWithOptional, JSON_PRETTY_PRINT);
Which will output:
{
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 2
},
"age": {
"type": "number",
"minimum": 0,
"maximum": 120
},
"email": {
"type": "string",
"format": "email"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["name", "email"]
}
You can also define a union type:
// Define a union type $statusSchema = Z::union([ Z::literal('active'), Z::literal('inactive'), Z::literal('pending') ])->to_json_schema(); echo json_encode($statusSchema, JSON_PRETTY_PRINT);
Which will output:
{
"anyOf": [
{
"type": "string",
"const": "active"
},
{
"type": "string",
"const": "inactive"
},
{
"type": "string",
"const": "pending"
}
]
}
Required Fields
By default, fields are not marked as required in the generated JSON Schema. You can enable required fields globally using:
Schema::set_require_all_fields(true);
When enabled:
- All fields are required by default
- Use
optional()to mark specific fields as optional - This applies to nested objects as well
- The setting can be toggled on/off as needed
Example with optional fields:
Schema::set_require_all_fields(true); $schema = Z::object([ 'id' => Z::number(), // required 'name' => Z::string(), // required 'nickname' => Z::string()->optional(), // optional 'contact' => Z::object([ 'email' => Z::string()->email(), // required 'phone' => Z::string()->optional() // optional ]) ])->to_json_schema();
Available Schema Types
Z::string()- String schema with various string-specific validationsZ::number()- Number schema with numeric validationsZ::boolean()- Boolean schemaZ::array()- Array schema with item type definitionsZ::object()- Object schema with property definitionsZ::literal()- Literal value schemaZ::union()- Union type schema
String Schema Methods
min(int $length)- Minimum string lengthmax(int $length)- Maximum string lengthemail()- Email format validationurl()- URL format validationregex(string $pattern)- Regular expression patternoptional()- Mark the field as optional
Number Schema Methods
min(float $min)- Minimum valuemax(float $max)- Maximum valueint()- Integer validationpositive()- Positive number validationnegative()- Negative number validationoptional()- Mark the field as optional
Array Schema Methods
min(int $length)- Minimum array lengthmax(int $length)- Maximum array lengthnonempty()- Array must not be emptyoptional()- Mark the field as optional
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
senadir/zod-php-to-schema 适用场景与选型建议
senadir/zod-php-to-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「schema」 「validation」 「json-schema」 「zod」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 senadir/zod-php-to-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 senadir/zod-php-to-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 senadir/zod-php-to-schema 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Like FormRequests, but for validating against a json-schema
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
JSON Schema builder and validator for MCP tool definitions
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-04