adamthehutt / laravel-sanitized-requests
Sanitize request parameters before validation
Installs: 2 260
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: >=7.3
- illuminate/database: ^5.8 | ^6.0 | ^7.0 | ^8.0
- illuminate/support: ^5.8 | ^6.0 | ^7.0 | ^8.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.2
README
Sanitize request input before passing to validation.
Installation
composer require adamthehutt/laravel-sanitized-requests
Usage
Create a Request class and use the SanitizesInput
trait. Then implement
the sanitize(Sanitizer $sanitizer)
method to clean up user input before it is
validated.
use AdamTheHutt\SanitizedRequests\SanitizesInput; use AdamTheHutt\SanitizedRequests\Sanitizer; use Illuminate\Foundation\Http\FormRequest; class StoreItem extends FormRequest { use SanitizesInput; public function sanitize(Sanitizer $sanitizer): void { $sanitizer->castToInt("user_id") ->castToBoolean("for_sale") ->castToFloat("price") ->forgetIfEmpty("item_description") ->trimWhitespace("nested.*.wildcard.param"); } }
Sanitizers
The following sanitize methods are currently supported:
- castToBool($key) - Casts the string input value to a boolean
- castToInt($key) - Casts the string input value to an integer
- castToFloat($key) - Casts the string input value to a float
- castToString($key) - Casts the input value to a string
- forgetIfEmpty($key) - Removes from input both empty strings and arrays with only empty values
- map($key, $callable) - Applies an arbitrary callback to the input value(s)
- trimWhitespace($key) - Trims whitespace from a string input value