Sanitize your request inputs with the following features :
- entries sanitizing (https://github.com/Okipa/php-data-sanitizer).
- null entries exclusion.
- values safety check.
Laravel version | PHP version | Package version |
---|---|---|
^5.5 | ^7.1 | ^1.1 |
^5.0 | ^7.0 | ^1.0 |
- Install the package with composer :
composer require "okipa/laravel-request-sanitizer:^1.1"
- Extends the
Okipa\LaravelRequestSanitizer\RequestSanitizer
in yourapp/Http/Requests/Request.php
class.
<?php
use Okipa\LaravelRequestSanitizer\RequestSanitizer;
class Request extends RequestSanitizer
{
// your laravel project base request custom features.
}
<?php
namespace App\Http\Requests;
use Okipa\LaravelRequestSanitizer\RequestSanitizer;
class EditUserRequest extends RequestSanitizer
{
protected $sanitizeEntries = true; // default value
protected $exceptFromSanitize = ['user.phone_number']; // except the phone number from the sanitizing treatment in order to keep the phone number first zero (example : 0240506070)
protected $excludeNullEntries = true; // default value
protected $exceptFromNullExclusion = ['user.company_name']; // is kept in the request keys even if its value is null
protected $safetyChecks = ['user.newsletter.subscription' => 'boolean', 'user.permissions' => 'array']; // will make sure that the declared keys will be returned with a default value if not found in the request
/**
* Execute some treatments just after the request creation
*/
public function before()
{
// execute your custom request treatments here
$this->merge(['formatted_date' => Carbon::createFromFormat('d/m/Y H:i:s', $this->input('user.created_at')->toDateTimeString()]);
}
/**
* Set the validation rules
*
* @return array
*/
public function rules()
{
return [
// other rules ...
'user.phone_number' => 'required|string',
'user.company_name' => 'nullable|string|max:255',
'user.newsletter.subscription' => 'required|boolean',
'user.permission' => 'required|array',
'formatted_date' => 'required|date|format:Y-m-d H:i:s'
];
}
}
protected $sanitizeEntries = true
Recursively sanitize the request entries.
To check how data will be sanitized, check the used package : https://github.com/Okipa/php-data-sanitizer.
Declare this property to false to disable the request entries sanitizing.protected $exceptFromSanitize = []
Except the declared keys (dot notation accepted) from the request entries sanitizing.
It can be a good option when you have numbers beginning with a zero that you want to keep that way, for example.protected $excludeNullEntries = true
Recursively exclude all the null entries from the request.
Declare this property to false to disable the null entries exclusion.protected $exceptFromNullExclusion = []
Except the declared keys (dot notation accepted) from the null entries exclusion.
protected $safetyChecks = []
Set which request keys (dot notation accepted) should be safety checked, according to their types.
Use case :protected $safetyChecks = ['active' => 'boolean', 'permissions' => 'array'];
.
Accepted types values :boolean
/array
.
The keys declared in this array will take the following values (according to their declared types) if they are not found in the request :- boolean :
false
- array:
[]
- boolean :
before()
This package gives you the opportunity to declare this method in your request.
It will be executed before all the request attributes treatments.
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.