pi-space / laravel-typed-request
This is my package laravel-typed-request
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
README
Laravel Typed Request is a package that enhances Laravel Form Request by introducing Typed Classes and Attribute-based Validation Rules.
This package provides a seamless way to create type-safe request classes, resulting in improved code readability and enhanced IDE support.
Features
- Define Typed Form Request classes with attribute-based validation rules.
- Improved code readability and maintainability.
- Enhanced IDE support for auto-completion and error checking.
Installation
You can install the package via composer:
composer require pi-space/laravel-typed-request
Usage
Creating Typed Form Request Classes
Laravel Typed Request simplifies the process of creating Typed Form Request classes. Let's take an example of creating an ArticleRequest
class:
- To create a typed request class, use the following Artisan command:
php artisan make:typed-request ArticleRequest
- Open the generated
app/Http/Requests/ArticleRequest.php
file and update it as follows:
namespace PiSpace\LaravelTypedRequest\Tests\Requests; use PiSpace\LaravelTypedRequest\Rules\Date; use PiSpace\LaravelTypedRequest\Rules\MaxLength; use PiSpace\LaravelTypedRequest\Rules\Nullable; use PiSpace\LaravelTypedRequest\Rules\Required; use PiSpace\LaravelTypedRequest\Rules\RequiredIfPatch; use PiSpace\LaravelTypedRequest\Rules\StringValue; use PiSpace\LaravelTypedRequest\TypedFormRequest; class ArticleRequest extends TypedFormRequest { #[Required, StringValue, MaxLength(255)] public readonly string $title; #[Required, StringValue, MaxLength(255)] public readonly string $body; #[Nullable, Date, RequiredIfPatch] public readonly ?string $published_at; }
Important Notes
- The typed request class must extend the
PiSpace\LaravelTypedRequest\TypedFormRequest
class. - All properties in the typed request class must be
readonly
.
Available Rules
- All available rules are located in the
PiSpace\LaravelTypedRequest\Rules
namespace. These rules are attribute-based and are similar to Laravel validation rules. - You can create custom rules by extending the
PiSpace\LaravelTypedRequest\Rules\RuleAttribute
class. - For example, the
RequiredIfPatch
rule checks if the request method isPATCH
, making the attribute required only in that case. - You have access to the request instance inside custom rule classes, allowing you to access request data.
Creating a Custom Rule
Let's create a custom rule called RequiredIfPatch
:
- To create the custom rule class, use the following Artisan command:
php artisan make:rule-attribute RequiredIfPatch
- Open the generated
app/Rules/RequiredIfPatch.php
file and update it as follows:
#[Attribute] class RequiredIfPatch extends RuleAttribute { public function rule(): \Illuminate\Validation\Rules\RequiredIf { return Rule::requiredIf(fn() => $this->request->isMethod('PATCH')); } }
Now you can use the RequiredIfPatch
rule in your typed request classes.
Changelog
For a detailed list of changes, see the CHANGELOG.
Contributing
We welcome contributions! Please see CONTRIBUTING for details on how to get involved.
Security Vulnerabilities
If you discover any security vulnerabilities, please review our security policy on how to report them.
Credits
License
The MIT License (MIT). See License File for more information.