2017 © Pedro Peláez
 

library params

Converts user input into correctly typed parameters

image

danack/params

Converts user input into correctly typed parameters

  • Sunday, July 22, 2018
  • by Danack
  • Repository
  • 1 Watchers
  • 1 Stars
  • 105 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 4 Open issues
  • 13 Versions
  • 950 % Grown

The README.md

Library renamed to TypeSpec

So won't be any activity here., (*1)

Params

A framework agnostic library for validating input parameters., (*2)

Build Status, (*3)

Actions Status, (*4)

Installation

composer require danack/params, (*5)

TL:DR - Using in an application

This library allows you to define a set of rules that define the expected input parameters, and then validate them., (*6)

As an example, this is what the code looks like in a controller for retrieving a list of articles:, (*7)

function getArticles(Request $request)
{
    $getArticlesParams = GetArticlesParams::createFromRequest($request);

    echo "After Id: " . $articleGetIndexParams->getAfterId() . PHP_EOL;
    echo "Limit:    " . $articleGetIndexParams->getLimit() . PHP_EOL;
}

The above example will throw a ValidationException with a list of all the validation problems if there are any., (*8)

Alternatively you can have the parameters and list of errors returned as tuple., (*9)

function getArticles(Request $request)
{
    [$getArticlesParams, $errors] = GetArticlesParams::createOrErrorFromVarMap($request);

    if ($errors !== null) {
        // do something about those errors.
    }

    echo "After Id: " . $articleGetIndexParams->getAfterId() . PHP_EOL;
    echo "Limit:    " . $articleGetIndexParams->getLimit() . PHP_EOL;
}

Under the hood, basic usage

Given a set of rules, the library will extract the appropriate values from a 'variable map' and validate that the values meet the defined rules:, (*10)

$rules = [
  'limit' => [
    new CheckSetOrDefault(10, $variableMap),
    new IntegerInput(),
    new MinIntValue(0),
    new MaxIntValue(100),
  ],
  'offset' => [
    new CheckSetOrDefault(null, $variableMap),
    new SkipIfNull(),
    new MinIntValue(0),
    new MaxIntValue(1000000),
  ],
];

list($limit, $offset) = Params::validate($params);

That code will extract the 'limit' and 'offset values from the variable map and check that the limit is an integer between 0 and 100, and that offset is either not set, or must be an integer between 0 and 1,000,000., (*11)

If there are any validation problems a ValidationException will be thrown. The validation problems can be retrieved from ValidationException::getValidationProblems., (*12)

Under the hood, basic usage without exceptions

Alternatively, you can avoid using exceptions for flow control:, (*13)


$validator = new ParamsValidator(); $limit = $validator->validate('limit', [ new CheckSetOrDefault(10, $variableMap), new IntegerInput(), new MinIntValue(0), new MaxIntValue(100), ]); $offset = $validator->validate('offset', [ new CheckSetOrDefault(null, $variableMap), new SkipIfNull(), new MinIntValue(0), new MaxIntValue(1000000), ]); $errors = $validator->getValidationProblems(); if (count($errors) !== 0) { // return an error return [null, $errors]; } // return an object with null return [new GetArticlesParams($order, $limit, $offset), null];

Tests

We have several tools that are run to improve code quality. Please run sh runTests.sh to run them all., (*14)

Pull requests should have full unit test coverage. Preferably also full mutation coverage through infection., (*15)

Related info

Json pointers - https://tools.ietf.org/html/rfc6901 JSON Patch - https://tools.ietf.org/html/rfc6902, (*16)

Future work

Support Uri fragment encoding on paths

    #            // the whole document
    #/foo        ["bar", "baz"]
    #/foo/0      "bar"
    #/           0
    #/a~1b       1
    #/c%25d      2
    #/e%5Ef      3
    #/g%7Ch      4
    #/i%5Cj      5
    #/k%22l      6
    #/%20        7
    #/m~0n       8

Parameter location

Some people care whether a parameter is in the query string or body. This library currently doesn't support differentiating them., (*17)

TODO

  • colors., (*18)

  • Error on unknown/invalid names. e.g. when the input include 'namme', and so the 'name' param uses default value, that sounds like an error., (*19)

  • Generate OpenAPI stuff better., (*20)

The Versions

22/07 2018

dev-master

9999999-dev

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

11/07 2018

0.3.6

0.3.6.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

11/07 2018

0.3.5

0.3.5.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

10/07 2018

0.3.4

0.3.4.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

28/05 2018

0.3.3

0.3.3.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

28/05 2018

0.3.2

0.3.2.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

28/05 2018

0.3.1

0.3.1.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

28/05 2018

0.3.0

0.3.0.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

17/05 2018

0.2.0

0.2.0.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

16/05 2018

0.1.3

0.1.3.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

16/05 2018

0.1.2

0.1.2.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires

15/05 2018

0.1.1

0.1.1.0

Converts user input into correctly typed parameters

  Sources   Download

MIT

The Requires

 

The Development Requires