This package has been discontinued and it won't receive any other updates. If you're using it please consider migrating to another solution – or fork it and depend on your own version of package.
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.
Install this package by adding it to your composer.json or by running composer require okaybueno/validation-service in your project's folder.
Publish the configuration file by running php artisan vendor:publish --provider="OkayBueno\Validation\ValidationServiceProvider".
Configure the config/validators.php file according to your needs, specifying the base namespace and directories for your validators.
Ready to go! No service provider or anything else needed :).
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:
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...
$this->usersValidator = $usersValidatorInterface;
publicfunctionfindUserById( $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)..
// 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.
New (breaking) version. Instead of having validators tied to services, now they live in their own folder and are automatically
bound when installing the package.