An open API service indexing awesome lists of open source software.

https://github.com/kubinyete/assertation

Assertation is a free and simple to use validation library, it's built to allow a more expressive syntax while applying data validation and mantaining an assert-oriented approach.
https://github.com/kubinyete/assertation

Last synced: 5 months ago
JSON representation

Assertation is a free and simple to use validation library, it's built to allow a more expressive syntax while applying data validation and mantaining an assert-oriented approach.

Awesome Lists containing this project

README

          

# Assertation

Assertation is a free and simple to use validation library, it's built to allow a more expressive syntax while applying
data validation and mantaining an assert-oriented approach.

# Getting started

To get started, please use our own package directly with Composer:

```sh
composer require kubinyete/assertation
```

# Usage

First of all, we can start understanding the objective of this approach by looking at some examples and comparisons,
below are some equivalent validations with and without `assertation`.

```php
$data = ' Spaced text ';

// This validation:
$validated = ($data = trim($data)) && strlen($data) > 2;

if (!$validated) {
throw new UnexpectedValueException('Data is required with at least 2 characters');
}

// Is equivalent to:
$data = Assert::value($data)->asTrim()->lgt(2)->validate()->get();

// And also equivalent to:
$data = Assert::value($data)->rules('asTrim;lgt,2')->get();
```

The following examples are a more complex validation, for simple ones it's easier to do natively, but when our validation
rules become more complex, `assertation` can easily validate and modify our data in a more expressive way.

```php
$data = Assert::value($data)->rules('null|str;asTrim;asUppercase;asTruncate,100;lgt,1')->get();
// Data can either be null or a string that has a minimum of 1 character and has every character in uppercase
// while limiting the maximum size to 100 characters with a ellipsis cutoff.

$data = Assert::value($data)->rules('null|float|asDecimal')->get();
// Data can either be null, a float or a string with a decimal number (Ex: 123.23).

$data = Assert::value($data)->null()->or()->asUppercase()->asTrim()->in(['HELLO', 'WORLD'])->get();
// Data can either be null or be 'hello' or 'world' with case insensitivity, resulting only in a upper case result.
```