specdocular / laravel-rules-to-schema
Convert Laravel validation rules to JSON Schema
Installs: 215
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/specdocular/laravel-rules-to-schema
Requires
- php: ^8.2
- laravel/framework: ^10.0|^11.0|^12.0
- specdocular/php-json-schema: ^0.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8.0
- larastan/larastan: ^3.0
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^3.2
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/phpstan: ^2.1
- psalm/plugin-laravel: ^2.11
- psalm/plugin-mockery: ^1.2
- psalm/plugin-phpunit: ^0.19
- rector/rector: ^2.0
- slevomat/coding-standard: ^8.15
- squizlabs/php_codesniffer: ^3.8
- vimeo/psalm: ^6.0
README
Convert Laravel validation rules into JSON Schema Draft 2020-12 definitions.
Installation
composer require specdocular/laravel-rules-to-schema
The service provider is auto-discovered by Laravel.
Usage
use Specdocular\LaravelRulesToSchema\RuleToSchema; use Specdocular\LaravelRulesToSchema\ValidationRuleNormalizer; $converter = app(RuleToSchema::class); $rules = [ 'email' => ['required', 'email', 'max:255'], 'age' => ['required', 'integer', 'min:18', 'max:120'], 'tags' => ['array'], 'tags.*' => ['string', 'max:50'], ]; $normalizer = new ValidationRuleNormalizer(); $normalized = $normalizer->normalize($rules); $schema = $converter->transform($normalized); $compiled = $schema->compile();
Configuration
Publish the config file to customize rule parsers:
php artisan vendor:publish --tag=rules-to-schema-config
Features
- Converts 25+ built-in Laravel validation rules to JSON Schema
- Handles nested objects, arrays, and wildcard (
*) rules - Supports conditional rules (
required_if,required_with, etc.) - Extensible — register custom rule parsers via config
- Auto-registered as a Laravel service provider
Related Packages
| Package | Description |
|---|---|
| specdocular/php-json-schema | JSON Schema Draft 2020-12 builder (foundation) |
| specdocular/php-openapi | Object-oriented OpenAPI builder |
| specdocular/laravel-openapi | Laravel integration for OpenAPI generation |
License
MIT. See LICENSE for details.