Package Data | |
---|---|
Maintainer Username: | jespejo |
Maintainer Contact: | jesus@okaybueno.com (Jesús Espejo) |
Package Create Date: | 2017-09-06 |
Package Last Update: | 2023-02-02 |
Home Page: | https://www.okaybueno.com |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:01:35 |
Package Statistics | |
---|---|
Total Downloads: | 4,675 |
Monthly Downloads: | 3 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 3 |
A package that provides validation as a service for Laravel 5.x.
There is a lot of talk about where to perform the data validation: repositories? controllers? gateways? services? models? Each one has its point; good things and bad things... We personally found useful to extract this logic to a service that can be injected into other services on the same -or higher- layer.
So the goal of this package is to provide a simple validation service that can be injected into other services, and that although it uses the Laravel Validation class by default, it can be extended to use other validation libraries. This is a highly opinionated way of solving this issue.
composer.json
or by running composer require okaybueno/validation-service
in your project's folder.php artisan vendor:publish --provider="OkayBueno\Validation\ValidationServiceProvider"
.config/validators.php
file according to your needs, specifying the base namespace and directories for your validators.You just need to create a Validation interface with the different validation methods, and then create a new ´src´ directory
where you will then create the validation class that extends the main validation class (LaravelValidator
) and implements
the previous interface. It sounds weird, huh? Lets see it with an example...
I personally like to split my Laravel code from my app code, so inside the app folder I usually create a folder that includes ALL the business logic that is lowly coupled to the framework: app/MyWebApp. Inside that folder I like to split my files into different folders, based on the role of these: Models? Repositories? Helpers? Services? Validators? And so on...
Once again -and this is a matter of taste, that's all- I like to split my services into areas of responsibility: Users, Auth,
Mailing, etc. Therefore, I like to split my validators in different folders that then contain the different interfaces,
along with the src
folder that contains the implementation. So the folder structure inside my projects looks pretty
much like this most of the times:
+-- app
| +-- MyApp
| +-- Models
| +-- Helpers
| +-- Repositories
| .
| .
| +-- Validation
| +-- Auth
| +-- Mailing
| +-- Users
| +-- UserValidatorInterface.php
| +-- src
+-- UsersValidator.php
Let's take a look at how I would implement the UserValidatorInterface and the UserValidator classes:
<?php
namespace MyApp\Validators\Users;
interface UserValidatorInterface
{
const EXISTS_BY_ID = 'existsById';
const EXISTS_BY_EMAIL = 'existsByEmail';
public function existsById();
public function existsByEmail();
}
<?php
namespace MyApp\Validators\Users\src;
use OkayBueno\Validation\src\LaravelValidator;
class UserValidator extends LaravelValidator implements UserValidatorInterface
{
public function existsById()
{
return [
'id' => 'required|exists:users,id'
];
}
public function existsByEmail()
{
return [
'email' => 'required|exists:users,email'
];
}
}
Vôila! The package does the rest. Now you can inject the Validation service in any part of our app. I like to use ONLY inside other services, something like this...
<?php
namespace MyApp\Services\Frontend\Users\src;
use MyApp\Validators\Users\UsersValidatorInterface;
class UsersService implements UsersServicesInterface
{
protected $usersValidator;
public function __construct(
UsersValidatorInterface $usersValidatorInterface
)
{
$this->usersValidator = $usersValidatorInterface;
}
public function findUserById( $userId )
{
$data = [
'id' => $userId
]
if ( $this->usersValidator->with( $data )->passes( UsersValidatorInterface::EXISTS_BY_ID ) )
{
// It passes the validation, so do whatever.. fetch user in $user and return it (for example).
.
.
return $user;
}
// if we are at this point then the validation failed and this will return an array with the errors.
return $this->usersValidator->errors();
}
}
And that's all! Remember: The important thing is that the interface lives in the folder specified in the configuration file,
and that the implementation for that interface lives under the src
folder.
The MIT License (MIT). Please see License File for more information.