Giter Site home page Giter Site logo

mvlabs / ze-content-validation Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 7.0 103 KB

Middleware for automating validation of incoming input.

Home Page: http://www.mvlabs.it

PHP 100.00%
php middleware middlewares inputfilter validations zend-expressive psr-15

ze-content-validation's Introduction

Zend Expressive Content Validation

Build Status

Introduction

Zend Expressive Content Validation is a Middleware for automating validation of incoming input.

Allows the following:

  • Defining named input filters.
  • Mapping named input filters to routes.
  • Returning a PSR-7 response representation of application/problem with validation error messages on invalid input using Zend Problem Details

Installation

Run the following composer command:

$ composer require mvlabs/ze-content-validation

Configuration

The ze-content-validation key is a mapping between routes names as the key, and the value being an array of mappings that determine which HTTP method to respond to and what input filter to map to for the given request. The keys for the mapping can either be an HTTP method or * wildcard for applying to any http method.

Example:

'ze-content-validation' => [
    'user.add' => [
        'POST' =>  \App\InputFilter\UserInputFilter::class
    ],
],

In the above example, the \App\InputFilter\UserInputFilter will be selected for POST requests.

input_filter_spec

input_filter_spec is for configuration-driven creation of input filters. The keys for this array will be a unique name, but more often based off the service name it is mapped to under the ze-content-validation key. The values will be an input filter configuration array, as is described in the ZF2 manual section on input filters.

Example:

    'input_filter_specs' => [
        'App\\InputFilter\\LoginInputFilter' => [
            0 => [
                'name' => 'displayName',
                'required' => true,
                'filters' =>[],
                'validators' => [
                     0 => [
                        'name' => 'not_empty',
                     ]   
                ],
                
            ],
            1 => [
                'name' => 'password',
                'required' => true,
                'filters' => [],
                'validators' => [
                    0 => [
                        'name' => 'not_empty',
                    ],
                    1 => [
                        'name' => 'string_length',
                        'options' => [
                            'min' => 8, 
                            'max' => 12
                        ],
                    ],
                ],                
            ],
        ],
    ],

Validating

In the following request, an email value is provided with an invalid format, and the displayName field is omitted entirely:

POST /users HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8

{
    "email": "foo",
    "password": "mySecretPassword!"
    
}

The response:

HTTP/1.1 422 Unprocessable Entity
Content-Type: application/problem+json

{
  "detail": "Validation Failed",
  "status": 422,
  "title": "Unprocessable Entity",
  "type": "https://httpstatus.es/422",
  "errors": {
    "email": {
        "emailAddressInvalidFormat": "The input is not a valid email address. Use the basic format local-part@hostname"
    },
    "displayName": {
      "isEmpty": "Value is required and can't be empty"
    }    
  },
}

ze-content-validation's People

Contributors

drigani avatar slaff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ze-content-validation's Issues

PSR-15 support?

Hi guys, I love this project, but as I am sure you know, it is not PSR-15 compatible.

Do you plan to update it for PSR-15?
Do you have another project that does so?

Thanks and regards,
Massi.

Exception when body is empty

I am using this project to check the correctness of the query string parameters of a PUT call which is body-less by design.
I understand this is an edge case, but this is throwing an exception.

ZE\ContentValidation\Exception\UnexpectedValueException: Data Extractor `ZE\ContentValidation\Extractor\BodyExtractor` returned a `NULL` instead of an `array`
 in: /app/src/vendor/mvlabs/ze-content-validation/src/Extractor/DataExtractorChain.php: 54
#0 [internal function]: ZE\ContentValidation\Extractor\DataExtractorChain->ZE\ContentValidation\Extractor\{closure}(Object(ZE\ContentValidation\Extractor\BodyExtractor))
#1 /app/src/vendor/mvlabs/ze-content-validation/src/Extractor/DataExtractorChain.php(64): array_map(Object(Closure), Array)
#2 /app/src/vendor/mvlabs/ze-content-validation/src/Validator/ValidatorHandler.php(71): ZE\ContentValidation\Extractor\DataExtractorChain->getDataFromRequest(Object(Zend\Diactoros\ServerRequest))
#3 /app/src/vendor/mvlabs/ze-content-validation/src/Middleware/ValidationMiddleware.php(53): ZE\ContentValidation\Validator\ValidatorHandler->validate(Object(Zend\Diactoros\ServerRequest))
#4 /app/src/vendor/zendframework/zend-expressive/src/Middleware/LazyLoadingMiddleware.php(60): 

Maybe it would make sense to treath the "null" case as a special case returning an empty array in the BodyExtractor class

U alive?

Is this project alive and kicking or is it ready to be forked and republished?

We have three open PRs with no feedback and four years of no commits.

If you consider this project dead, then please tell me and I will try to start a replacement. If you would be so kind to add an abandonment notice and the reference to the replacement in packagist then, it would be really appreciated.

What do you say? :)

InputFilter is created twice

Please consider the following code from ValidatiorHandler:

    private function getInputFilter($inputFilterService)
    {
        $inputFilter = $this->inputFilterManager->get($inputFilterService);

        if (! $inputFilter instanceof InputFilter) {
            throw new ValidationClassNotExists(
                sprintf(
                    'Listed input filter "%s" does not exist; cannot validate request',
                    $inputFilterService
                )
            );
        }

        return $this->inputFilterManager->get($inputFilterService);
    }

In the return statement, there's no need to get the input filter again from the manager. We can just return $inputFilter.

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.