PHPackages                             multidimensional/array-validation - 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. multidimensional/array-validation

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

multidimensional/array-validation
=================================

Validate an array using rules from another array.

1.0.2(7y ago)0112MITPHPPHP &gt;=5.5

Since May 4Pushed 7y ago2 watchersCompare

[ Source](https://github.com/multidimension-al/array-validation)[ Packagist](https://packagist.org/packages/multidimensional/array-validation)[ RSS](/packages/multidimensional-array-validation/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (2)Dependencies (2)Versions (4)Used By (0)

Array Validation Library
========================

[](#array-validation-library)

[![Build Status](https://camo.githubusercontent.com/41f496bdcc63d8c48b9c4b422b3e1d48b7397074d4a4b02b2076327dcda45a8f/68747470733a2f2f7472617669732d63692e6f72672f6d756c746964696d656e73696f6e2d616c2f61727261792d76616c69646174696f6e2e737667)](https://travis-ci.org/multidimension-al/array-validation)[![Latest Stable Version](https://camo.githubusercontent.com/01690f3188d94caeb5a7aa930a12c06a4cd19dda9f9c0f6cdf7278318b8d9764/68747470733a2f2f706f7365722e707567782e6f72672f6d756c746964696d656e73696f6e616c2f61727261792d76616c69646174696f6e2f762f737461626c652e737667)](https://packagist.org/packages/multidimensional/array-validation)[![Code Coverage](https://camo.githubusercontent.com/8cc1ef4576e5cb30072c1d261b859055052a3ec9be0684cbff23bef7140546e3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d756c746964696d656e73696f6e2d616c2f61727261792d76616c69646174696f6e2f6261646765732f636f7665726167652e706e67)](https://scrutinizer-ci.com/g/multidimension-al/array-validation/)[![Minimum PHP Version](https://camo.githubusercontent.com/dd6bad85ee03cf570f4cf82ab69a80396fdbf48050af932f8f23aa551b0d1e5a/687474703a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e352d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/0e8548996e0817c52cff73fab0b5646c0dcedc3a960c419349f43c2a72ac9fc6/68747470733a2f2f706f7365722e707567782e6f72672f6d756c746964696d656e73696f6e616c2f61727261792d76616c69646174696f6e2f6c6963656e73652e737667)](https://packagist.org/packages/multidimensional/array-validation)[![Total Downloads](https://camo.githubusercontent.com/acae7dfa6a0222c3f261cee2ea53f9dc962dfc0aea7b101ee8218a989b2893aa/68747470733a2f2f706f7365722e707567782e6f72672f6d756c746964696d656e73696f6e616c2f61727261792d76616c69646174696f6e2f642f746f74616c2e737667)](https://packagist.org/packages/multidimensional/array-validation)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5dadea74ca8b427187106d3e32d567e66c72c83a5bc1c2a779224ed4e534850c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d756c746964696d656e73696f6e2d616c2f61727261792d76616c69646174696f6e2f6261646765732f7175616c6974792d73636f72652e706e67)](https://scrutinizer-ci.com/g/multidimension-al/array-validation/)

This library allows for validation of an array using another array as a ruleset. Validation rules include requiring specific keys, specific types and specific values. When the validation fails, the library throws a normal *Exception*. For best results, also consider using our [Array Sanitization](https://github.com/multidimension-al/array-sanitization) library which will correct errors and force type corrections prior to validation.

Requirements
------------

[](#requirements)

- PHP 5.5+

Installation
============

[](#installation)

The easiest way to install this library is to use composer. To install, simply include the following in your `composer.json` file:

```
"require": {
    "multidimensional/array-validation": "*"
}

```

Or run the following command from a terminal or shell:

```
composer require --prefer-dist multidimensional/array-validation

```

You can also specify version constraints, with more information available [here](https://getcomposer.org/doc/articles/versions.md).

Usage
=====

[](#usage)

This library utilizes PSR-4 autoloading, so make sure you include the library near the top of your class file:

```
use Multidimensional\ArrayValidation\Validation;
```

How to use in your code:

**Create Ruleset**

```
$rules = ['keyName' => ['type' => 'string', 'required' => true]];
```

**Check an array against that ruleset**

```
$array = ['keyName' => 'text'];
$bool = Validation::validate($array, $rules);
echo $bool; //true
```

The validation process will return true or false and will throw an exception.

Advanced Rulesets
=================

[](#advanced-rulesets)

Many examples of rules can be found in the `ValidationTest.php` file.

### Types

[](#types)

There are several different valid types for the values in the array they are as follows:

- String
- Integer
- Decimal
- Boolean
- Group (Multidimensional array)

### Pattern Matching

[](#pattern-matching)

You can specify basic regular expressions that can be used to access whether a value is valid or not.

Examples of pattern matching:

- \[A-Z\]{2} *(match two capital letters)*
- \\d{5} *(matches 5 digits)*
- \\d{5}-\\d{4} *(matches ZIP+4)*

Built in patterns available for matching:

- URL
- Email
- IP Address (IPv4 or IPv6)
- MAC Address (Dashes, Colons or Dots)
- [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)

### Required Values

[](#required-values)

It is possible to set a key as being required or not, but it is also possible to adjust requirements based on the the values of the other keys. For example, a key could be required if the value of another key is equal to *null* or if another key has a specific value.

Here is an example from our *ValidationTest.php* file:

```
$rules = [
  'a' => ['type' => 'string', 'required' => true],
  'b' => ['type' => 'string',
    'required' => [
	  'a' => 'banana'
	 ]
  ],
  'c' => ['type' => 'string',
    'required' => [
	  [
	    'a' => 'banana',
		'b' => 'orange'
	  ],
	  [
		'a' => 'banana',
		'b' => 'carrot'
	  ],
	  [
		'a' => 'pickle'
	  ],
	  [
		'b' => 'banana'
	  ]
	]
  ]
];
```

Under this example the following are true:

- Key 'a' is always required.
- Key 'b' is required when key 'a' is "banana".
- Key 'c' is required when ONE of the following is true:
    - Key 'a' is "banana" and key 'b' is "orange"
    - Key 'a' is "banana" and key 'b' is "carrot"
    - Key 'a' is "pickle"
    - Key 'b' is "banana"

Here you can see the required functionality can be made to be quite complex.

### Specified Values

[](#specified-values)

In some instances where you would only have a small subset of specific values that would be considered valid, you can specify those values in the ruleset by providing am array of valid values.

```
$rules = ['a' => ['type' => 'string', 'values' => ['cat', 'dog']]];
```

In the above example, key 'a' must be either "cat" or "dog". Note that this string comparison is case-insensitive!

Contributing
============

[](#contributing)

We appreciate all help in improving this library by either adding functions or improving existing functionality. If you do want to add to our library, please ensure you use PSR-2 formatting and add unit testing for all added functions.

Feel free to fork and submit a pull request!

License
=======

[](#license)

```
MIT License

Copyright (c) 2017 - 2019 multidimension.al

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 97.2% 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 ~330 days

Total

3

Last Release

2686d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6588235?v=4)[AJ Quick](/maintainers/ajquick)[@ajquick](https://github.com/ajquick)

---

Top Contributors

[![ajquick](https://avatars.githubusercontent.com/u/6588235?v=4)](https://github.com/ajquick "ajquick (70 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (2 commits)")

---

Tags

arrayphp

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/multidimensional-array-validation/health.svg)

```
[![Health](https://phpackages.com/badges/multidimensional-array-validation/health.svg)](https://phpackages.com/packages/multidimensional-array-validation)
```

PHPackages © 2026

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