Download the PHP package aneterial/laravel-data-validator without Composer
On this page you can find all versions of the php package aneterial/laravel-data-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aneterial/laravel-data-validator
More information about aneterial/laravel-data-validator
Files in aneterial/laravel-data-validator
Package laravel-data-validator
Short Description Laravel package for convenient validation and hydration of a request into DTO structures
License MIT
Homepage https://github.com/aneterial/laravel-data-validator
Informations about the package laravel-data-validator
laravel-data-validator
Contents
- Prerequisites
- Installation
- Usage
- Examples
-
Restrictions
Prerequisites
- PHP 8.2 or higher
- Laravel 11
for dev
- PHPUnit &11
- phpstan ^1.11
- php-cs-fixer ^3.58
Installation
Install package via composer
Usage
You now have a class attribute DataValidator\Attributes\RequestProperty
at your disposal. Add it to the properties of your DTO and set the necessary configuration fields
Description of fields:
property
: name of the request key that matches the propertyrules
: validation rules based on component semantics Laravel Validation, accepts only string valuerequestDataType
: to indicate where a field is expected - in the request body (default)const RequestProperty::BODY_TYPE
or in query stringconst RequestProperty::QUERY_TYPE
listRules
: if the value is an array (list) - set the validation rules for each element according to the semantics of Laravel Validation
Next, you need to get a DataValidator\DataManager
instance in your controller from app DI container and pass the request essence to it, indicating the DTO class that you expect to receive after validation and filling with data.
Next, the Laravel validator will check the request entity (instanse of \Illuminate\Http\Request
), and if the data is incorrect, it will throw an \Illuminate\Validation\ValidationException
. If the data is correct, the Manager will create and fill the DTO object with data, which you can use in your application
If your endpoint involves passing array of objects [{...}, {...}, {...}]
, you can use a method that will validate the request and return an array of DTOs
Examples
Here are some examples of using validation by attributes
1) DTO with lists
2) DTO with non required properties, if it not required - it should be nullable, except array - it can be empty array
3) DTO with nested object
4) DTO with list of nested object
5) DTO with enum of BackedEnum
property, you should set enum type to property and no more rules are required except, if you want - indicate type of enum
6) DTO with enums of BackedEnum
property
Restrictions
if DTO has nested objects or arrays of nested objects, the requestDataType
of these entities is ignored and taken from the parrent entity
So if you use
In nested object requestDataType
will not work and it be RequestProperty::BODY_TYPE
like in parrent entity
Method DataManager::validateAndConvertList
can only work with data from request body, so all properties of entity will be force casted to type RequestProperty::BODY_TYPE
in this usage