alexpts/simple-validator

Simple validate request data

1.1.0 2018-03-09 12:20 UTC

This package is auto-updated.

Last update: 2024-04-15 01:46:14 UTC


README

SensioLabsInsight

Build Status Code Coverage Code Climate Scrutinizer Code Quality

Simple validator for validate accos arrays.

Install

composer require alexpts/simple-validator

Example

$body = (array)$this->request->getParsedBody();

$validator = new Validator;

// shot format
$errors = $validator->validate($body, [
    'parentId' => ['int', 'betweenInt:0:99999'],
    'name' => ['string', 'min:3'],
    'slug' => ['string', 'min:3', 'max:120'],
    'sort' => ['int']
]);

// full format
$errors = $validator->validate($body, [
    'parentId' => ['int', ['betweenInt' => [0, 99999]] ],
    'name' => ['string', ['min' => [3]] ],
    'slug' => ['string', ['min' => [3]], ['max' => [120]] ],
    'sort' => ['int']
]);

// validate if exists
$errors = $validator->validateIfExists($body, [
    'name' => ['string', ['min' => [3]] ],
    'sort' => ['int'] // field not passed, not validate
]);

// deep attributes
$errors = $validator->validate($body, [
    'user' => ['array', 'min:3'],
    'user.name' => ['string', 'min:3'],
    'user.age' => ['int'],
]);

$errors = $validator->validateIfExists($body, [
    'user' => ['array', 'min:3'],
    'user.name' => ['string', 'min:3'],
    'user.age' => ['int'],
]);

Validators:

string

The field under validation must be string

int

The field under validation must be integer

float

The field under validation must be float

array

The field under validation must be array

required

Required field

betweenInt:min:max

Int from range [min, max]

betweenFloat:min:max

Float from range [min, max]

bool

The field under validation must be boolean

alpha

The field under validation must be entirely alphabetic characters

alphaDash

The field under validation may have alpha-numeric characters, as well as dashes and underscores

alphaNum

The field under validation must be entirely alpha-numeric characters

date

The field under validation must be a valid date according to the strtotime PHP function

dateTime

Value instance of \DateTime object

inArray

Check value in array

min

Min length fot string, min value for number, min count for array

max

Max length fot string, max value for number, max count for array

email

The field under validation must be simple email string

Custom validator

You can add custom validator via method

public function registerRule(string $name, callable $handler): self