2017-25 © Pedro Peláez
 

library validator

Simple validation library.

image

weew/validator

Simple validation library.

  • Friday, August 26, 2016
  • by weew
  • Repository
  • 1 Watchers
  • 0 Stars
  • 150 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 26 Versions
  • 0 % Grown

The README.md

PHP Validator

Build Status Code Quality Test Coverage Version Licence, (*1)

Table of contents

Installation

composer require weew/validator, (*2)

Available constraints

Additional constraint packs

There are additional constraints that you may load trough composer., (*3)

Constraints

Constraints are small pieces of validation logic. A constraint can be used on its own, without the validator., (*4)

$constraint = new EmailConstraint();
// or
$constraint = new EmailConstraint('Custom error message.');
$check = $constraint->check('foo@bar.baz');

if ($check) {
    // valdiation passed
} else {
    echo $constraint->getMessage();
}

Constraint groups

Constraint groups allows you to configure multiple constraints for a single value., (*5)

$group = new ConstraintGroup('email', [
    new EmailConstraint(),
]);
// or
$group = new ConstraintGroup('email');
$group->addConstraint(new EmailConstraint());

Constraint groups can be used to validate data without the validator. The check method returns a ValidationResult object., (*6)

$result = $group->check('foo@bar.baz');

Validator

The easiest way to use the validator is by creating a new instance and adding constraints inline. Validator will return a ValidationResult object., (*7)

$validator = new Validator();
$data = ['username' => 'foo', 'email' => 'foo@bar.baz'];

$result = $validator->check($data, [
    new ConstraintGroup('email', [
        new EmailConstraint(),
    ]),
]);

Validation result and validation errors

Validation result is used to group occurring validation errors. Validation errors hold information about the validated properties, their values and the applied constraints., (*8)

if ($result->isFailed()) {
    foreach ($result->getErrors() as $error) {
        // $error->getSubject()
        // $error->getValue()
        // $error->getMessage()
        // $error->getConstraint()
    }
}

Composing a custom validator

You can compose a validator with predefined constraints that will be applied on each validation., (*9)

$data = ['username' => 'foo', 'email' => 'foo@bar.baz'];
$validator->addConstraint('email', new EmailConstraint());
$validator->addConstraints('username', [
    new AlphaConstraint(),
    new MinMaxLengthConstraint(3, 20),
]);

$result = $validator->check($data);

Creating a custom validator class

Configuring validators inline is not always the best solution. Sometimes you might want to create dedicated validator classes. With this library this is very easy to achieve., (*10)

class UserProfileValidator extends Validator {
    protected function configure() {
        $this->addConstraint('email', new EmailConstraint());
        $this->addConstraints('username', [
            new AlphaConstraint(),
            new MinMaxLengthConstraint(3, 20),
        ]);
    }
}

$data = ['username' => 'foo', 'email' => 'foo@bar.baz'];
$validator = new UserProfileValidator();
$result = $validator->check($data);

Custom constraints

Creating a new constraint is a fairly easy task. All you have to do is to implement the IConstraint interface. This is an example on how to create a simple constraint that makes sure that a number is within the given range., (*11)

class MinMaxConstraint implements IConstraint {
    protected $min;
    protected $max;
    protected $message;

    public function __construct($min, $max, $message = null) {
        $this->min = $min;
        $this->max = $max;
        $this->message = $message;
    }

    public function check($value, IValidationData $data = null) {
        if (is_numeric($value)) {
            return $value >= $this->min && $value <= $this->max;
        }

        return false;
    }

    public function getMessage() {
        if ($this->message !== null) {
            return $this->message;
        }

        return 'Some default error message.';
    }

    public function getOptions() {
        return [
            'min' => $this->min,
            'max' => $this->max,
        ];
    }
}

Wildcard validation

Imagine you have a similar structure that you want to validate., (*12)

$input = [
    'items' => [
        ['name' => 'name1'],
        ['name' => null],
        ['name' => 'name3'],
    ],
];

In order to validate the name property of every single element inside the items array, you would have to iterate over the items manually. You could also use a wildcard to target all the values. To wildcard array values, you can use this special character *., (*13)

$result = $validator->addConstraint('items.*.name', new NotNullConstraint());

In the example above, result will hold an error with subject items.1.name., (*14)

Array keys can also be validated using wildcards. You'll have to use a different wildcard character #. Be aware that the # wildcard character should always be the last path segment. This is wrong foo.#.bar, this is ok foo.bar.#., (*15)

$input = [
    'items' => [
        'name1' => 'value1',
        '2' => 'value2',
        'name3' => 'value3',
    ],
];

$result = $validator->addConstraint('items.#', new MinMaxLengthConstraint(3, 5));

Result will contain an error with subject #items.1. As you see, there is a # prefix in front of subjects for wildcard keys. This way you can differentiate between subjects for values and subjects for keys., (*16)

Property accessors

Validator comes with support for multiple data types., (*17)

Array accessor

This accessor adds support for array based data sets., (*18)

$data = ['email' => 'foo@bar.baz'];
$validator->check($data);

Object accessor

This accessor adds support for object based data sets., (*19)

$data = new stdClass();
$data->email = 'foo@bar.baz';
$validator->check($data);

Getter accessor

This accessor adds support for objects that only allow to access data over getter methods., (*20)

class User {
    protected $username;
    public function __construct($username) {
        $this->username = $username;
    }
    public function getUsername() {
        return $this->username;
    }
}

$validator->check(new User('foo'));

The Versions

26/08 2016

dev-master

9999999-dev

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

26/08 2016

v3.8.0

3.8.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

17/08 2016

v3.7.0

3.7.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

17/08 2016

v3.6.0

3.6.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

16/08 2016

v3.5.1

3.5.1.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

16/08 2016

v3.5.0

3.5.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

16/08 2016

v3.4.1

3.4.1.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

16/08 2016

v3.4.0

3.4.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

10/08 2016

v3.3.0

3.3.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

27/07 2016

v3.2.0

3.2.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

21/07 2016

v3.1.2

3.1.2.0

Simple validation library.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

22/04 2016

v3.1.1

3.1.1.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

29/02 2016

v3.1.0

3.1.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

26/01 2016

v3.0.1

3.0.1.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

26/01 2016

v3.0.0

3.0.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

21/01 2016

v2.2.1

2.2.1.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

21/01 2016

v2.2.0

2.2.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

21/01 2016

v2.1.0

2.1.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

21/01 2016

v2.0.0

2.0.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

20/01 2016

v1.3.0

1.3.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

18/11 2015

v1.2.0

1.2.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-array ^1.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

17/11 2015

v1.1.0

1.1.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-contracts ^1.1
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

16/11 2015

v1.0.0

1.0.0.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-foundation ^1.0.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

11/11 2015

v0.0.3

0.0.3.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-foundation 0.*
  • weew/php-helpers-string 0.*

 

The Development Requires

by Maxim Kott

25/10 2015

v0.0.2

0.0.2.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-foundation 0.*
  • weew/php-helpers-string 0.*

 

The Development Requires

by Maxim Kott

25/10 2015

v0.0.1

0.0.1.0

Simple validation library.

  Sources   Download

MIT

The Requires

  • weew/php-foundation 0.*
  • weew/php-helpers-string 0.*

 

The Development Requires

by Maxim Kott