PHPackages                             inok/slim-validation4 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. inok/slim-validation4

ActiveLibrary[Framework](/categories/framework)

inok/slim-validation4
=====================

A slim middleware for validation based on Respect/Validation

1.0.2(3y ago)01491GPL-2.0+PHPPHP &gt;=7.4.0

Since Jul 12Pushed 3y agoCompare

[ Source](https://github.com/nchizhov/Slim-Validation4)[ Packagist](https://packagist.org/packages/inok/slim-validation4)[ RSS](/packages/inok-slim-validation4/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (8)Versions (4)Used By (0)

Slim Framework Validation
=========================

[](#slim-framework-validation)

[![ico-license](https://camo.githubusercontent.com/0c8383317e078ea54081afe5e0cf54fbf6ff6418c08a991748abc5a69a0c074d/68747470733a2f2f706f7365722e707567782e6f72672f696e6f6b2f736c696d2d76616c69646174696f6e342f6c6963656e73653f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/0c8383317e078ea54081afe5e0cf54fbf6ff6418c08a991748abc5a69a0c074d/68747470733a2f2f706f7365722e707567782e6f72672f696e6f6b2f736c696d2d76616c69646174696f6e342f6c6963656e73653f7374796c653d666c61742d737175617265)[![Latest version](https://camo.githubusercontent.com/77d1bcb10644b1557f66e06ed3c1b8b273008d6f136d503104a9e9833366486b/687474703a2f2f706f7365722e707567782e6f72672f696e6f6b2f736c696d2d76616c69646174696f6e342f763f7374796c653d666c61742d737175617265)](https://packagist.org/packages/inok/slim-validation4)[![Total Downloads](https://camo.githubusercontent.com/44061166148f2f0f6018b84a9ed89b1137f165427bc6f8c666ef591dd55f5cbf/687474703a2f2f706f7365722e707567782e6f72672f696e6f6b2f736c696d2d76616c69646174696f6e342f642f6d6f6e74686c793f7374796c653d666c61742d737175617265)](https://packagist.org/packages/inok/slim-validation4)[![Build Status](https://camo.githubusercontent.com/65cb12126b3db92ac1c95f5fd2c3816797c615288e502b255fb665e460475b67/68747470733a2f2f636f6465636f762e696f2f67682f6e6368697a686f762f536c696d2d56616c69646174696f6e342f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d525651454135494d5558)](https://codecov.io/gh/nchizhov/Slim-Validation4)[![PSR2 Conformance](https://camo.githubusercontent.com/f1450b8c07873315e9780657471e7ea5cabd6e46ec8aa632175a15efe1716a06/68747470733a2f2f7374796c6563692e696f2f7265706f732f3531303436373531322f736869656c64)](https://styleci.io/repos/510467512/)

A validation library for the Slim Framework. It internally uses [Respect/Validation](https://github.com/Respect/Validation).

Table of contents
-----------------

[](#table-of-contents)

- [Install](#install)
- [Usage](#usage)
    - [Register per route](#register-per-route)
    - [Register for all routes](#register-for-all-routes)
    - [Route parameters](#route-parameters)
    - [JSON requests](#json-requests)
    - [XML requests](#xml-requests)
    - [Translate errors](#translate-errors)
- [Testing](#testing)
- [Contributing](#contributing)
- [Credits](#credits)

Install
-------

[](#install)

Via Composer

```
$ composer require inok/slim-validation4
```

Requires Slim 4.0.0 or newer.

Usage
-----

[](#usage)

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

### Register per route

[](#register-per-route)

```
use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numericVal()->positive()->between(1, 20);
$validators = [
  '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:
      [
        'username' => [
          'length' => '"davidepastore" must have a length between 1 and 10',
        ],
        'age' => [
          'between' => '"89" must be between 1 and 20',
        ],
      ];
      */
    } else {
      //No errors
    }

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

$app->run();
```

### Register for all routes

[](#register-for-all-routes)

```
use Respect\Validation\Validator as v;

$app = new \Slim\App();

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

// Register middleware for all routes
// If you are implementing per-route checks you must not add this
$app->add(new \Inok\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:
    [
      'username' => [
        'length' => '"davidepastore" must have a length between 1 and 10',
      ],
      'age' => [
        'between' => '"89" must be between 1 and 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

[](#route-parameters)

```
use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$routeParamValidator = v::numericVal()->positive();
$validators = [
  '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:
    [
        'param' => [
          'numeric' => '"wrong" must be numeric',
        ],
    ];
    */
  } else {
    //No errors
  }
})->add(new \Inok\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

[](#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;

$app = new \Slim\App();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
$validators = [
  'type' => $typeValidator,
  'email' => [
    '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
        (
            'length' => "rq3r" must have a length between 1 and 2
        )

)
*/
```

### XML requests

[](#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:

```

   emails
   1

     1
     1
     rq3r
     2016-08-23 13:36:29
     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;

$app = new \Slim\App();

//Create the validators
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
$validators = [
  'type' => $typeValidator,
  'email' => [
    '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
        (
            'length' => "rq3r" must have a length between 1 and 2
        )

)
*/
```

### Translate errors

[](#translate-errors)

You can provide a callable function to translate the errors.

```
use Respect\Validation\Validator as v;

$app = new \Slim\App();

//Create the validators
$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
$ageValidator = v::numericVal()->positive()->between(1, 20);
$validators = [
  '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 \Inok\Slim\Validation\Validation($validators, $translator);

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

$app->run();
```

Testing
-------

[](#testing)

```
$ vendor\bin\phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Davide Pastore](https://github.com/davidepastore)
- [Nikolay Chizhov](https://github.com/nchizhov)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~139 days

Total

3

Last Release

1128d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce0f496c0c04c90c29675b08f834ad58707bc8e3498e1693e198e23e0b3e5871?d=identicon)[nchizhov](/maintainers/nchizhov)

---

Top Contributors

[![DavidePastore](https://avatars.githubusercontent.com/u/1949364?v=4)](https://github.com/DavidePastore "DavidePastore (58 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![iHmD](https://avatars.githubusercontent.com/u/6934421?v=4)](https://github.com/iHmD "iHmD (1 commits)")[![joy2fun](https://avatars.githubusercontent.com/u/7264066?v=4)](https://github.com/joy2fun "joy2fun (1 commits)")

---

Tags

middlewareframeworkvalidatorvalidationslim

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/inok-slim-validation4/health.svg)

```
[![Health](https://phpackages.com/badges/inok-slim-validation4/health.svg)](https://phpackages.com/packages/inok-slim-validation4)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[davidepastore/slim-validation

A slim middleware for validation based on Respect/Validation

171223.7k3](/packages/davidepastore-slim-validation)[slim/csrf

Slim Framework 4 CSRF protection PSR-15 middleware

3512.1M94](/packages/slim-csrf)[slim/http-cache

Slim Framework HTTP cache middleware and service provider

1242.9M27](/packages/slim-http-cache)[yiisoft/yii-middleware

Yii Middleware

21151.3k1](/packages/yiisoft-yii-middleware)[bnf/slim3-psr15

PSR-15 middleware support for Slim Framework v3

10177.0k5](/packages/bnf-slim3-psr15)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
