rushing/laravel-data-schemas
Composer 安装命令:
composer require rushing/laravel-data-schemas
包简介
Generate JSON Schemas from Laravel Data objects with validation attributes
README 文档
README
Generate JSON Schemas from Laravel Data objects with validation attributes.
This package automatically converts your Spatie Laravel Data objects into JSON Schema files, preserving validation rules, descriptions, and examples. Perfect for AI-powered applications, API documentation, and frontend validation.
Features
- 🎯 Single Source of Truth: Define structure, validation, types, and schemas all in your Data objects
- 🔄 Automatic Generation: Run
schemas:generatesimilar totypescript:transform - 📝 Rich Metadata: Add descriptions and examples via custom attributes
- 🎨 Flexible Output: Configure path structure (namespace, flat, or custom)
- 🔌 Extensible: Custom collectors, generators, and path generators
- ✅ Validation Mapping: Automatic conversion of Spatie validation attributes to JSON Schema constraints
Installation
Install via Composer:
composer require rushing/laravel-data-schemas
Publish the configuration file (optional):
php artisan vendor:publish --tag=data-schemas-config
Usage
1. Add Attributes to Your Data Objects
<?php namespace App\Data; use Spatie\LaravelData\Data; use Spatie\LaravelData\Attributes\Validation\Max; use Spatie\LaravelData\Attributes\Validation\Email; use Rushing\LaravelDataSchemas\Attributes\Description; use Rushing\LaravelDataSchemas\Attributes\Example; class UserData extends Data { public function __construct( #[Description('User full name')] #[Example('John Doe')] #[Max(255)] public string $name, #[Description('User email address')] #[Example('john@example.com')] #[Email] public string $email, #[Description('User age in years')] #[Example(25)] #[Example(42)] public ?int $age = null, ) {} }
2. Generate JSON Schemas
# Generate all schemas php artisan schemas:generate # Generate for specific path php artisan schemas:generate --path=app/Data/Schemas # Generate for specific class php artisan schemas:generate --class="App\Data\UserData" # Override output directory php artisan schemas:generate --output=storage/schemas
3. Use Generated Schemas
// resources/schemas/App/Data/UserData.schema.json { "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "UserData", "type": "object", "title": "UserData", "properties": { "name": { "type": "string", "description": "User full name", "examples": ["John Doe"], "maxLength": 255 }, "email": { "type": "string", "description": "User email address", "examples": ["john@example.com"], "format": "email" }, "age": { "type": ["integer", "null"], "description": "User age in years", "examples": [25, 42] } }, "required": ["name", "email"] }
Configuration
The config/data-schemas.php file provides extensive configuration options:
return [ // Paths to scan for Data objects 'auto_discover_types' => [ app_path('Data'), ], // Filter by namespace patterns (optional) 'namespaces' => [ // 'App\\Data\\Schemas\\*', ], // Output directory 'output_directory' => resource_path('schemas'), // Path structure: 'namespace', 'flat', or 'custom' 'path_structure' => 'namespace', // Custom path generator callable 'custom_path_generator' => null, // Pretty print JSON 'format_output' => true, // JSON Schema version 'schema_version' => 'https://json-schema.org/draft/2020-12/schema', // Include $schema and $id 'schema_metadata' => [ '$schema' => true, '$id' => true, ], // Custom validation attribute mappings 'validation_mapping' => [], ];
Path Structure Options
Namespace Structure (default)
resources/schemas/
└── App/
└── Data/
└── UserData.schema.json
Flat Structure
resources/schemas/
├── UserData.schema.json
├── ProductData.schema.json
└── OrderData.schema.json
Custom Structure
'path_structure' => 'custom', 'custom_path_generator' => function (ReflectionClass $class, string $baseDir) { return $baseDir . '/api/' . $class->getShortName() . '.json'; },
Supported Validation Attributes
The package automatically maps Spatie validation attributes to JSON Schema constraints:
| Validation Attribute | JSON Schema Constraint |
|---|---|
#[Max(255)] |
maxLength (string), maximum (number), maxItems (array) |
#[Min(1)] |
minLength (string), minimum (number), minItems (array) |
#[Between(1, 100)] |
minimum + maximum |
#[Email] |
format: "email" |
#[Uuid] |
format: "uuid" |
#[Url] |
format: "uri" |
#[Enum(StatusEnum::class)] |
enum: [values] |
#[Nullable] |
type: ["string", "null"] |
Custom Attributes
Description
Add human-readable descriptions to properties or classes:
#[Description('User profile information')] class UserData extends Data { #[Description('Primary email address for notifications')] public string $email; }
Example
Add one or multiple examples (repeatable attribute):
#[Example('John Doe')] #[Example('Jane Smith')] public string $name;
Integration with Build Process
Add to your package.json scripts:
{
"scripts": {
"types:generate": "php artisan typescript:transform",
"schemas:generate": "php artisan schemas:generate",
"build": "npm run types:generate && npm run schemas:generate && vite build"
}
}
Advanced Usage
Custom Validation Mapping
Override or extend validation attribute mappings:
// config/data-schemas.php 'validation_mapping' => [ CustomValidationAttribute::class => function ($attr) { return ['pattern' => $attr->getPattern()]; }, ],
Custom Path Generator
Implement the PathGenerator interface:
use Rushing\LaravelDataSchemas\PathGenerators\PathGenerator; class ApiPathGenerator implements PathGenerator { public function getSchemaPath(ReflectionClass $class): string { return resource_path('api/schemas/' . $class->getShortName() . '.json'); } }
Register in config:
'path_generator' => App\Generators\ApiPathGenerator::class,
Custom Generator
Implement the Generator interface for custom schema formats:
use Rushing\LaravelDataSchemas\Generators\Generator; class OpenApiGenerator implements Generator { public function canGenerate(ReflectionClass $class): bool { // Your logic } public function generate(ReflectionClass $class): array { // Generate OpenAPI schema format } }
Requirements
- PHP 8.2+
- Laravel 11.0+ or 12.0+
- spatie/laravel-data 4.0+ or 5.0+
License
The MIT License (MIT). Please see License File for more information.
Credits
- Stephen Rushing
- Inspired by spatie/laravel-typescript-transformer
- Built for the Spatie Laravel Data ecosystem
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
rushing/laravel-data-schemas 适用场景与选型建议
rushing/laravel-data-schemas 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.42k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「spatie」 「json-schema」 「laravel-data」 「schema-generation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rushing/laravel-data-schemas 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rushing/laravel-data-schemas 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rushing/laravel-data-schemas 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
Easily optimize images using PHP
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 1.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-30