• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    PHP
  • Created almost 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A validation library for the Slim Framework. It internally uses Respect/Validation.

Slim Framework Validation

Latest version Build Status Coverage Status Quality Score Total Downloads PSR2 Conformance

A validation library for the Slim Framework. It internally uses Respect/Validation.

Table of contents

Install

Via Composer

$ composer require davidepastore/slim-validation

Requires Slim 4.0.0 or newer.

Usage

In most cases you want to register DavidePastore\Slim\Validation for a single route, however, as it is middleware, you can also register it for all routes.

Register per route

use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numeric()->positive()->between(1, 20);
$validators = array(
  'username' => $usernameValidator,
  'age' => $ageValidator
);

$app->get('/api/myEndPoint',function ($req, $res, $args) {
    //Here you expect 'username' and 'age' parameters
    if($req->getAttribute('has_errors')){
      //There are errors, read them
      $errors = $req->getAttribute('errors');

      /* $errors contains:
      array(
        'username' => array(
          '"davidepastore" must have a length between 1 and 10',
        ),
        'age' => array(
          '"89" must be lower than or equals 20',
        ),
      );
      */
    } else {
      //No errors
    }

})->add(new \DavidePastore\Slim\Validation\Validation($validators));

$app->run();

Register for all routes

use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numeric()->positive()->between(1, 20);
$validators = array(
  'username' => $usernameValidator,
  'age' => $ageValidator
);

// Register middleware for all routes
// If you are implementing per-route checks you must not add this
$app->add(new \DavidePastore\Slim\Validation\Validation($validators));

$app->get('/foo', function ($req, $res, $args) {
  //Here you expect 'username' and 'age' parameters
  if($req->getAttribute('has_errors')){
    //There are errors, read them
    $errors = $req->getAttribute('errors');

    /* $errors contains:
    array(
      'username' => array(
        '"davidepastore" must have a length between 1 and 10',
      ),
      'age' => array(
        '"89" must be lower than or equals 20',
      ),
    );
    */
  } else {
    //No errors
  }
});

$app->post('/bar', function ($req, $res, $args) {
  //Here you expect 'username' and 'age' parameters
  if($req->getAttribute('has_errors')){
    //There are errors, read them
    $errors = $req->getAttribute('errors');
  } else {
    //No errors
  }
});

$app->run();

Route parameters

use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$routeParamValidator = v::numeric()->positive();
$validators = array(
  'param' => $routeParamValidator,
);

$app->get('/foo/{param}', function ($req, $res, $args) {
  //Here you expect 'param' route parameter
  if($req->getAttribute('has_errors')){
    //There are errors, read them
    $errors = $req->getAttribute('errors');

    /* $errors contains:
    array(
        'param' => array(
          '"wrong" must be numeric',
        ),
    );
    */
  } else {
    //No errors
  }
})->add(new \DavidePastore\Slim\Validation\Validation($validators));

$app->run();

Note that requests parameters take priority over route parameters, so if you use the same name for a route and request parameter, the last will win and it will be considered for validation.

JSON requests

You can also validate a JSON request. Let's say your body request is:

{
	"type": "emails",
	"objectid": "1",
	"email": {
		"id": 1,
		"enable_mapping": "1",
		"name": "rq3r",
		"created_at": "2016-08-23 13:36:29",
		"updated_at": "2016-08-23 14:36:47"
	}
}

and you want to validate the email.name key. You can do it in this way:

use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
$validators = array(
  'type' => $typeValidator,
  'email' => array(
    'name' => $emailNameValidator,
  ),
);

If you'll have an error, the result would be:

//In your route
$errors = $req->getAttribute('errors');

print_r($errors);
/*
Array
(
    [email.name] => Array
        (
            [0] => "rq3r" must have a length between 1 and 2
        )

)
*/

XML requests

You can also validate a XML request. Let's say your body request is:

Let's say you have a POST request with a XML in its body:

<person>
   <type>emails</type>
   <objectid>1</objectid>
   <email>
     <id>1</id>
     <enable_mapping>1</enable_mapping>
     <name>rq3r</name>
     <created_at>2016-08-23 13:36:29</created_at>
     <updated_at>2016-08-23 14:36:47</updated_at>
    </email>
</person>

and you want to validate the email.name key. You can do it in this way:

use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
$validators = array(
  'type' => $typeValidator,
  'email' => array(
    'name' => $emailNameValidator,
  ),
);

If you'll have an error, the result would be:

//In your route
$errors = $req->getAttribute('errors');

print_r($errors);
/*
Array
(
    [email.name] => Array
        (
            [0] => "rq3r" must have a length between 1 and 2
        )

)
*/

Translate errors

You can provide a callable function to translate the errors.

use Respect\Validation\Validator as v;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numeric()->positive()->between(1, 20);
$validators = array(
  'username' => $usernameValidator,
  'age' => $ageValidator
);

$translator = function($message){
  $messages = [
      'These rules must pass for {{name}}' => 'Queste regole devono passare per {{name}}',
      '{{name}} must be a string' => '{{name}} deve essere una stringa',
      '{{name}} must have a length between {{minValue}} and {{maxValue}}' => '{{name}} deve avere una dimensione di caratteri compresa tra {{minValue}} e {{maxValue}}',
  ];
  return $messages[$message];
};

$middleware = new \DavidePastore\Slim\Validation\Validation($validators, $translator);

// Register middleware for all routes or only for one...

$app->run();

Testing

$ vendor\bin\phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

More Repositories

1

codice-fiscale

A PHP library to calculate and check the italian tax code (codice fiscale).
PHP
96
star
2

ipinfo

A wrapper around the ipinfo.io services
PHP
51
star
3

Slim-Config

A file configuration loader that supports PHP, INI, XML, JSON, and YML files for the Slim Framework. It internally uses https://github.com/hassankhan/config.
PHP
34
star
4

easy-mac-torrent-download

A simple way to download torrents from https://mac-torrent-download.net/
JavaScript
27
star
5

Slim-Restrict-Route

A Slim middleware to restrict ip addresses that will access to your routes
PHP
22
star
6

composer-audit

A composer plugin that checks if your application uses dependencies with known security vulnerabilities (it uses SensioLabs Security Checker)
PHP
10
star
7

liferay-journal-article-converter

From a Liferay Journal Article to a Custom Java Object
Java
6
star
8

codice-fiscale-rest

A simple codice fiscale rest website based on Symfony.
PHP
6
star
9

JBugmenot

Java Bugmenot library to find account and password for various sites.
Java
6
star
10

AndroidCF

Applicazione Android che calcola il codice fiscale
Java
6
star
11

Slim-Markdown-View

A renderer for rendering Markdown into a PSR-7 Response object. It works well with Slim Framework 3.
PHP
5
star
12

LoremPixel-Twig-Extension

This library integrates lorempixel service into Twig. You can use it to generate random image urls that you can use as placeholders in your application
PHP
3
star
13

gandalf-sax-guy

Gandalf Sax Guy ๐Ÿง™๐ŸŽท
HTML
3
star
14

jquery-cssalias

A jQuery Plugin to aliasify your CSS properties.
JavaScript
2
star
15

paris-model-generator

Paris model generator from database
PHP
1
star
16

bruttoesporc

Front page generator for the best Italian sport newspaper
JavaScript
1
star
17

lotteria

Simulation of a lottery
Java
1
star
18

JGuerrillaMail

Java implementation of the API of GuerrillaMail
Java
1
star
19

JRaiDownloader

Un downloader per scaricare i video dal sito della RAI.
Java
1
star
20

WhatsApp4Java

This API helps you to work with WhatsApp using Java.
Python
1
star