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

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

everest/validation
==================

Everest - Validation Component

v3.2.0(3y ago)04.8kMITPHPPHP &gt;=8.0

Since Nov 4Pushed 3y ago1 watchersCompare

[ Source](https://github.com/inceddy/everest-validation)[ Packagist](https://packagist.org/packages/everest/validation)[ RSS](/packages/everest-validation/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (32)Used By (0)

Everest - Validation Component
==============================

[](#everest---validation-component)

The Validation Component of Everest is ment to validate user input in a simple and intuitive way.

Installing
----------

[](#installing)

Simply go to your project directory where the `composer.json` file is located and type:

```
	composer require everest/validation
```

Usage
-----

[](#usage)

This package offers two methods of data validation. You can either validate a single value on a specific requirement (type) or you can validate one or more validation chains on each key of an array or array like object.

### Validating single value

[](#validating-single-value)

```
use Everest\Validation\Validation;

$int = Validation::integer('10'); // $int -> 10
$noint = Validation::integer('foo'); // Will throw \Everest\Validation\InvalidValidationException
```

### Validating an array of values using a validation chain

[](#validating-an-array-of-values-using-a-validation-chain)

```
use Everest\Validation\Validate;

$data = [
	'foo' => '10',
	'bar' => 'foo'
];

$data = Validate::lazy($data)
	->that('foo')->integer()->between(0, 20)
	->that('bar')->enum(['bar', 'foo'])->upperCase()
	->execute();

// $data -> ['foo' => 10, 'bar' => 'FOO']
```

You can use additional validation chains by seperating them with `->or()`

```
use Everest\Validation\Validate;

$data = [
	'bar' => 'foo'
];

$data = Validate::lazy($data)
	->that('bar')->integer()->between(0, 20)
	->or()->string()->minLength(2)
	->execute();

// $data -> ['bar' => 'FOO']
```

### Strict and lazy validation

[](#strict-and-lazy-validation)

One can choose between `Validate::strict()` and `Validate::lazy()`. First will throw an `InvalidValidationException` on the first occuring error and the last will collect all occuring errors and will throw a `InvalidLazyValidationException`, which provices a `::getErrors()` and `::getErrorsGroupedByKey()` method to access all bundled `InvalidValidationException` exceptions.

### Validating nested arrays

[](#validating-nested-arrays)

One can use dot-notation to validate nested values.

```
use Everest\Validation\Validate;

$data = [
	'foo' => [
		['bar' => 1, 'name' => 'Some name'],
		['bar' => 2, 'name' => 'Some other name']
	]
];

$data = Validate::lazy($data)
	->that('foo.*.bar')->integer()->between(0, 20)
	->that('foo.*.name')->string()->upperCase()
	->execute();

// $data -> [
//   'foo' => [
//     ['bar' => 1, 'name' => 'SOME NAME'],
//     ['bar' => 2, 'name' => 'SOME OTHER NAME']
//   ]
// ]
```

### Optional parameters

[](#optional-parameters)

Parameters can be marked as optional and as optional with default. If the validation ueses a default value as fallback this value is NOT validated by the validation chain anymore!

```
use Everest\Validation\Validate;

$data = ['foo' => 10];

$result = Validate::lazy($data)
	->that('foo')->integer()
	->that('bar')->optional(/* no default */)->integer()
	->execute();

// $result -> ['foo' => 10]

$result = Validate::lazy($data)
	->that('foo')->integer()
	->that('bar')->optional(null)->integer()
	->execute();

// $result -> ['foo' => 10, 'bar' => null]
```

Types
-----

[](#types)

Types are rules that a supplied value has to fulfill.

#### Array

[](#array)

`Validation::array($value)`, validates that given value is an array.

#### Between

[](#between)

`Validation::between($value, numeric $min, numeric $max)`, validates that given value holds `$min
