2017 © Pedro Peláez
 

library simple-validator

Simple validate request data

image

alexpts/simple-validator

Simple validate request data

  • Friday, March 9, 2018
  • by alexpts
  • Repository
  • 1 Watchers
  • 0 Stars
  • 105 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 5 Versions
  • 5 % Grown

The README.md

simple-validator

SensioLabsInsight, (*1)

Build Status Code Coverage Code Climate Scrutinizer Code Quality, (*2)

Simple validator for validate accos arrays., (*3)

Install

composer require alexpts/simple-validator, (*4)

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, (*5)

int

The field under validation must be integer, (*6)

float

The field under validation must be float, (*7)

array

The field under validation must be array, (*8)

required

Required field, (*9)

betweenInt:min:max

Int from range [min, max], (*10)

betweenFloat:min:max

Float from range [min, max], (*11)

bool

The field under validation must be boolean, (*12)

alpha

The field under validation must be entirely alphabetic characters, (*13)

alphaDash

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

alphaNum

The field under validation must be entirely alpha-numeric characters, (*15)

date

The field under validation must be a valid date according to the strtotime PHP function, (*16)

dateTime

Value instance of \DateTime object, (*17)

inArray

Check value in array, (*18)

min

Min length fot string, min value for number, min count for array, (*19)

max

Max length fot string, max value for number, max count for array, (*20)

email

The field under validation must be simple email string, (*21)

Custom validator

You can add custom validator via method, (*22)

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

The Versions

09/03 2018

dev-master

9999999-dev https://github.com/alexpts/simple-validator

Simple validate request data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

validator validate

09/03 2018

1.1.0

1.1.0.0 https://github.com/alexpts/simple-validator

Simple validate request data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

validator validate

09/03 2018

1.0.2

1.0.2.0 https://github.com/alexpts/simple-validator

Simple validate request data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

validator validate

05/09 2017

1.0.1

1.0.1.0 https://github.com/alexpts/simple-validator

Simple validate request data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

validator validate

04/09 2017

1.0.0

1.0.0.0 https://github.com/alexpts/simple-validator

Simple validate request data

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar alexpts

validator validate