Looking to hire Laravel developers? Try LaraJobs

laravel-rules-to-schema maintained by specdocular

Description
Convert Laravel validation rules to JSON Schema
Last update
2026/07/24 04:19 (dev-main)
License
Downloads
455

Comments
comments powered by Disqus

Laravel Rules to Schema

Latest Version on Packagist PHP Version Tests codecov Code Style

Convert Laravel validation rules into JSON Schema Draft 2020-12 definitions.

Requirements

  • PHP 8.2 or higher
  • Laravel 10, 11, or 12

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

Contributing

Contributions are welcome. See CONTRIBUTING.md for setup, the required checks, and versioning.

Security

If you discover a security vulnerability, please follow the process in SECURITY.md rather than opening a public issue.

License

MIT. See LICENSE for details.