Coder Social home page Coder Social logo

laravel-request-filters's Introduction

Laravel request filters

Software License Latest Version on Packagist Total Downloads

About

Laravel provides tools to validate HTTP requests allowing developers to ensure the input data is in the correct structure.

This package provides tools to filter the valid data into the format intended.

It feels like it is part of the Laravel framework and couldn't be any simpler to use.

Requirements

Laravel 5.6+

Features

  • Format input with a collection of pre-made and tested Filters
  • FilterRequests trait that easily plugs into a FormRequest and enable filtering
  • InputFilter that allows developers to easily implement their own filters
  • RequestFiltering tool that can apply the same filters to any string you pass in
  • Nested AND array filtering just like Laravel's own validator ๐Ÿ‘Œ
  • String based filtering similar to Laravel's validator + custom parsable filters

Included Filters

Filter class Usage
FilterCapitalize Capitalizes the first character of each word
FilterSanitize Escapes characters based on php's own validator constants
FilterSanitizeEmail Sanitizes email
FilterSanitizeText Sanitizes text generically
FilterSanitizeEncoded URL Encodes text
FilterNumeric Removes all non-numerical characters
FilterStripTag Removes HTML and PHP tags, keeps what you want
FilterToLower Converts to lowercase
FilterToUpper Converts to uppercase
FilterTrim Trim leading and trailing white space
FilterDate Format into a specified Carbon date string see Carbon docs

Included Filters (as string literals)

Filter class Usage
'capitalize' Capitalizes the first character of each word
'email' Sanitizes email
'sanitize' Sanitizes text generically
'encode' URL Encodes text
'number' Removes all non-numerical characters
'strip' Removes HTML and PHP tags, keeps what you want
'lowercase' Converts to lowercase
'uppercase' Converts to uppercase
'trim' Trim leading and trailing white space
'date' Format into a specified Carbon date string see Carbon docs

You can make your own custom filters by implementing the FilterInterface :)

How to use

Import via composer:

composer require guerrilla/laravel-request-filters

In your FormRequest use the following trait:

use Guerrilla\RequestFilters\FilterRequests

Describe your filters (Laravel rules included for familiarisation):

public function rules():array {
    return [
        'email' => ['required', 'email', 'bail'],
        'name' => ['required'],
        'employees.*.name' =>['required']
    ];
}
public function filters():array {
    return [
        'email' => [new FilterTrim, new FilterSanitizeEmail],
        'name' => [new FilterTrim, new FilterSanitizeText, new FilterCapitalize],
        'employees.*.name' => [new FilterCapitalize],
        'date' => [new FilterTrim, new FilterDate('d/m/Y')]
    ];
}

Or use the string based syntax:

public function filters():array {
    return [
        'email' => 'trim|email',
        'name' => 'trim|sanitize|capitalize',
        'employees.*.name' => 'capitalize',
        'date' => 'trim|date:d/m/Y'
    ];
}

Validate the request as per normal but, the results will be now filtered :)

$input = $request->validated();
echo $input['email'];
//trimmed and sanitized email!

You can optionally just run the filter on any string you like outside of the request:

$validated_input = $request->validate([...]);

$filtered_result = InputFilter::filter(
                  $validated_input,
                  [
                  'email' => [new FilterTrim],
                  'name' => [new FilterTrim],
                  'meta.*.attributes' => [new MyCustom1Filter(1), new MyCustom2Filter(2)] 
                  ]
);

Using your own custom filtering rules to the string parsing syntax is easy!

$validated_input = $request->validate([...]);

$custom_filters = [
    'custom1' => MyCustom1Filter::class,
    'custom2' => MyCustom2Filter::class
];

$filtered_result = InputFilter::filterFromString(
                  $validated_input,
                  [
                  'email' => 'trim',
                  'name' => 'trim',
                  'meta.*.attributes' => 'custom1:1|custom2:2'
                  ],
                  $custom_filters
);

License

MIT

laravel-request-filters's People

Contributors

guerrillacontra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

laravel-request-filters's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.