PHPackages                             serkin/volan - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. serkin/volan

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

serkin/volan
============

PHP array schema validator

4427.1k↓48.4%102PHP

Since Jun 19Pushed 8y ago4 watchersCompare

[ Source](https://github.com/serkin/volan)[ Packagist](https://packagist.org/packages/serkin/volan)[ RSS](/packages/serkin-volan/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (2)

Light PHP validation library
============================

[](#light-php-validation-library)

For everyone who uses MongoDB or other NoSQL solution and cares about what client sends to his/her database and looking for validation library written in PHP. `Volan` validates array against given shema and provides you with full information about invalid nodes. Can be used with logging so you can see the whole validation process.

[![Build Status](https://camo.githubusercontent.com/99d9f1a3f374c98f989397e08fed3a21c2bb1fabc0ff52f09c53eebe3cb9cdd1/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7365726b696e2f766f6c616e2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/serkin/parser)[![Coverage Status](https://camo.githubusercontent.com/0153645212d9cb26e4736bc3a2e0449aa578b4d4256acf2bb2717f9e5fc4d5c0/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7365726b696e2f766f6c616e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/serkin/volan?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/52b763a72fa10198f8bf0179a8b14edf26bfc407c9babc7179b2e28f314f69ce/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7365726b696e2f766f6c616e2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/serkin/volan/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/610d9e5e28d00fd109eab32e7dd1b7f1e633f3374a865e7ce9a379c8b82aee9b/68747470733a2f2f706f7365722e707567782e6f72672f7365726b696e2f766f6c616e2f762f737461626c65)](https://packagist.org/packages/serkin/volan)[![Total Downloads](https://camo.githubusercontent.com/30ac14346ac3d79e8635485a9a3dd0a0fecdd55b5d36eea0035471ace31596ff/68747470733a2f2f706f7365722e707567782e6f72672f7365726b696e2f766f6c616e2f646f776e6c6f616473)](https://packagist.org/packages/serkin/volan)[![Latest Unstable Version](https://camo.githubusercontent.com/857d8e07bfc1df4966aef6ec6997b2e599555523c78014b2f073f27cad7f0d01/68747470733a2f2f706f7365722e707567782e6f72672f7365726b696e2f766f6c616e2f762f756e737461626c65)](https://packagist.org/packages/serkin/volan)[![License](https://camo.githubusercontent.com/0fe19d3d5292b48154674f15760ea7c80cd24136d3515fbe29e52919c6f24454/68747470733a2f2f706f7365722e707567782e6f72672f7365726b696e2f766f6c616e2f6c6963656e7365)](https://packagist.org/packages/serkin/volan)[![SensioLabsInsight](https://camo.githubusercontent.com/ccab7d206bf7310668a04531b7bc4eb5c53fc5f20e92c226217555f26860425a/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f63313565363531612d346539312d343462332d386238662d3339336130613461633730622f736d616c6c2e706e67)](https://insight.sensiolabs.com/projects/c15e651a-4e91-44b3-8b8f-393a0a4ac70b)

- [Volan](#light-php-validation-library)
    - [Installation](#installation)
    - [Usage](#usage)
    - [Predefined validatorse](#predefined-validators)
    - [Custom validators](#custom-validators)
    - [Usage with other libraries](#usage-with-other-libraries)
    - [Tips](#tips)
        - [Allow extra keys in data](#allow-extra-keys-in-data)
        - [Allow required fields be omitted](#allow-required-fields-be-omitted)
        - [Logging](#logging)
        - [PSR compatible class names](#psr-compatible-class-names)
        - [Relational structure](#relational-structure)
    - [Dependencies](#dependencies)
    - [Contribution](#contribution)
    - [Licence](#licence)
    - [Tests](#tests)

Installation
------------

[](#installation)

---

via Composer:

```
composer require serkin/volan dev-master
```

Usage
-----

[](#usage)

---

All you have to do is to specify `_type` field for each node. `_type` is a reference to a validation class

```
include 'vendor/autoload.php';

$schema = [
    'root' => [ // Schema must begins with 'root'
        'title' => [
            '_type' => 'required_string'
        ],
        'price' => [
            '_type' => 'number'
        ],
        'author' => [
            '_type' => 'string'
        ],
        'instock' => [
            '_type' => 'required_boolean'
        ],
        'info' =>  [
            '_type' => 'array',
            'isbn' => [
                '_type' => 'string'
            ],
            'pages' => [
                '_type' => 'number'
            ]
        ],
        'comments' => [
            '_type' => 'nested_array',
            'user_id' => [
                '_type' => 'required_number'
            ],
            'comment' => [
                '_type' => 'required_string'
            ]
        ]
    ]
];

$book = [
    'title' => 'The Idiot', // Cannot be omitted
    'instock' => true, // Cannot be omitted and has to be bool type
    'info' => ['isbn' => '978-0451531520'],
    //  'price' can be omitted but if present has to be numeric type
    'comments' => [ // Nested array check nested elements
        [
            'user_id' => 1,
            'comment' => 'Good book',
            // 'extra_field' => 'bad field'
            // By default if key not present in schema validation stops and returns false
        ],
        [
            'user_id' => 2,
            'comment' => 'I like it'
        ]
    ]
];

$validator = new \Volan\Volan($schema);
$result = $validator->validate($book);

// if $result->isValid() === false you can get full information about invalid node
var_dump($result->getErrorInfo());
```

Predefined validators
---------------------

[](#predefined-validators)

### Strings

[](#strings)

- `string`: string
- `required_string`: string that has to be present

### Arrays

[](#arrays)

- `array`: array
- `required_array`: array that has to be present
- `nested_array`: array with nested arrays
- `required_nested_array`: array with nested arrays has to be present

### Bool

[](#bool)

- `boolean`: boolean
- `required_boolean`: boolean that has to be present

### Numbers

[](#numbers)

- `number`: int or float
- `required_number`: int or float that has to be present

Custom validators
-----------------

[](#custom-validators)

If you need extra validators you can create them extending `\Volan\Validator\AbstractValidator` class

- Create folder `src/Volan/Validator` in your library
- Add your custom validator `src/Volan/Validator/mongoid_validator.php`. Example for `mongoid` validator:

```
namespace Volan\Validator;
class MongoidValidator extends AbstractValidator
{
    public function isValid($nodeData)
    {
        return ($nodeData instanceof \MongoId);
    }
}
```

- Add autoload to composer.json

```
"autoload": {
        "psr-4": {
            "Volan\\Validator\\": "src/Volan/Validator/"
        }
    }
```

Usage with other libraries
--------------------------

[](#usage-with-other-libraries)

If you want to use other validation libraries with `Volan` it is easy. Let's take a look how it works with [Respect validation engine](https://github.com/Respect/Validation)

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

class IntBetween10And20Validator extends AbstractValidator
{
    public function isValid($nodeData)
    {
        return v::int()->between(10, 20)->validate($nodeData);

    }
}
```

Tips
----

[](#tips)

### Allow extra keys in data

[](#allow-extra-keys-in-data)

If you want allow extra keys in array you can define it in constructor

```
$validator = new \Volan\Volan($schema, $strictMode = false);
```

### Allow `required` fields be omitted

[](#allow-required-fields-be-omitted)

In mongoDB when you update just several fields in collection you cannot pass validation cause required fields may be missing. You can tell validator consider all required validation as optional.

```
$validator = new \Volan\Volan($schema);
$validator->setRequiredMode(false);
$result = $validator->validate($book);
```

### Logging

[](#logging)

If you want see validation process set logger

```
$validator = new \Volan\Volan($schema);

$result = $validator->validate($book);
$result->getLog();
```

### PSR compatible class names

[](#psr-compatible-class-names)

You can use PSR compatible names for validation classes. Previous example with `mongoid` validation class can be rewritten like:

```
namespace Volan\Validator;

class MongoidValidator extends AbstractValidator
{
    public function isValid($nodeData)
    {
        return ($nodeData instanceof \MongoId);
    }
}
```

Here we changed `mongoid_validator` to `MongoidValidator`. Example with `int_between_10_and_20_validator` be rewritten to `IntBetween10And20Validator`

### Relational structure

[](#relational-structure)

Let's imagine we have field in our book data

```
...
'categories' => [new \MongoId('111111111111111111111111'),new \MongoId('111111111111111111111112')]
...
```

and we want ensure that all elements not only instances of `MongoId` but actually present in our database. It is easy. Our validator will be look like: namespace Volan\\Validator;

```
class ArrayOfMongoids extends AbstractValidator
{
    public function isValid($nodeData)
    {
        foreach($nodeData as $id) {
            if($id !instanceof \MongoId) || !$this->presentInCategoryCollection($id))
                return false;
            }
        }

        return true;
    }

    public function presentInCategoryCollection($id)
    {
        // Getting connection and so on

        $collection = $db->selectCollection('categories');
        return (bool)$collection->findOne(['_id' => $id]);
    }

}
```

now in schema we add

```
...
'categories' => ['_type' => 'array_of_mongoids']
...
```

Dependencies
------------

[](#dependencies)

- PHP: &gt;= 5.4

Contribution
------------

[](#contribution)

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

Licence
-------

[](#licence)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Tests
-----

[](#tests)

```
phpunit

```

Or with Docker

```
docker run --rm -v "$PWD":/var/src/ serkin/php7 vendor/bin/phpunit --debug

```

Code style

```
docker run --rm -v "$PWD":/var/src/ serkin/php7 vendor/bin/php-cs-fixer fix src

```

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.6% 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.

### Community

Maintainers

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

---

Top Contributors

[![serkin](https://avatars.githubusercontent.com/u/1589686?v=4)](https://github.com/serkin "serkin (160 commits)")[![fichitiu](https://avatars.githubusercontent.com/u/14952106?v=4)](https://github.com/fichitiu "fichitiu (2 commits)")[![senadir](https://avatars.githubusercontent.com/u/6165348?v=4)](https://github.com/senadir "senadir (1 commits)")[![XavRsl](https://avatars.githubusercontent.com/u/1185840?v=4)](https://github.com/XavRsl "XavRsl (1 commits)")

### Embed Badge

![Health badge](/badges/serkin-volan/health.svg)

```
[![Health](https://phpackages.com/badges/serkin-volan/health.svg)](https://phpackages.com/packages/serkin-volan)
```

###  Alternatives

[marcosh/php-validation-dsl

A DSL for validating data in a functional fashion

483.9k](/packages/marcosh-php-validation-dsl)

PHPackages © 2026

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