hungthai1401/laravel-grpc-validation
| Install | |
|---|---|
composer require hungthai1401/laravel-grpc-validation |
|
| Latest Version: | v0.0.1 |
| PHP: | >=8.0 |
| License: | MIT |
| Last Updated: | May 14, 2025 |
| Links: | GitHub · Packagist |
Laravel gRPC Validation
A Laravel package for gRPC validation inspired by friendsofhyperf/grpc-validation. This package provides a seamless way to validate gRPC requests in Laravel applications using Attributes.
Features
- Attribute-based Validation: Utilize PHP Attributes to define validation rules directly on your gRPC service methods.
- Flexible Rule Definition: Supports passing validation rules as an array or using a Laravel
FormRequestclass for more complex validation logic. - PHP Compatibility: Designed to work with PHP 8.0 and above, ensuring modern PHP features and compatibility.
- Strict Standards: Adheres to PSR standards and passes PHPStan at the strictest level for robust code quality.
- Unit Tested: Comprehensive unit tests are included to ensure reliability and maintainability.
- Final Classes and Strict Types: Uses
finalclasses and strict type declarations for better code integrity and predictability.
Installation
You can install the package via Composer:
composer require hungthai1401/laravel-grpc-validation
Usage
First of all, you need to set up your gRPC server.
use Spiral\RoadRunner\GRPC\InvokerInterface;
$worker = Worker::create();
$invoker = $this->container->make(InvokerInterface::class);
$server = new GrpcServer($invoker);
$server->registerService(..., ...);
// The server will use the configuration from .rr.yaml or environment
$server->serve($worker);
return 0;
Then you need to define your gRPC service methods with validation attributes. Here's an example:
use HT\GrpcValidation\Validation;
use GoodByeFormRequest;
#[Validation(
rules: [
'name' => 'required|string|max:10',
'message' => 'required|string|max:500',
],
messages: [
'name.required' => 'Name is required',
'name.string' => 'Name must be a string',
'name.max' => 'Name must not exceed 10 characters',
'message.required' => 'Message is required',
'message.string' => 'Message must be a string',
'message.max' => 'Message must not exceed 500 characters',
],
attributes: [
'name' => 'Name',
'message' => 'Message',
],
)]
public function sayHello(HiUser $user)
{
$message = new HiReply();
$message->setMessage("Hello World");
$message->setUser($user);
return $message;
}
#[Validation(formRequest: GoodByeFormRequest::class)]
public function sayGoodBye(GoodByeUser $user)
{
$message = new GoodByeReply();
$message->setMessage("Goodbye World");
$message->setUser($user);
return $message;
}
Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an Issue on GitHub.
Thanks
License
This package is licensed under the MIT License. See the LICENSE file for more information.
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.