2017 © Pedro Peláez
 

project validator

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

image

chmielewskitomasz/validator

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  • Tuesday, July 17, 2018
  • by chmielewskitomasz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 507 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 26 Versions
  • 61 % Grown

The README.md

Simple, lightweight input validation lib for PHP

Build Status Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version License, (*1)

Donate on PayPal, (*2)

How to install it?

composer require chmielewskitomasz/validator

How to use it?

require_once __FILE__ . '/vendor/autoload.php';

use Hop\Strategy\Strategy;
use Hop\Strategy\Field;
use Hop\StdValidator;

// first, create your input validation strategy
class AlbumInputValidator implements Strategy
{
    /**
     * @inheritdoc
     */
    public function getFields(): array
    {
        $nameField = new Field(
            'name',
            true,
            null
        );

        $phoneField = new Field(
            'phone'
            false,
            null
        );

        $phoneField->registerValidator('Digits');
        $phoneField->registerValidator('Length', ['min' => 9, 'max' => 9]);

        return [
            $nameField,
            $phoneField
        ];
    }
}

// second, create stdValidator
$validator = StdValidator::fromConfig(include __DIR__ . '/config/validators.php');


// three - validate your input
$myInput = [
    'name' => 'Tom',
    'phone' => '999888000'
];
$validator->isValid($myInput, new AlbumInputValidator());  // true
$validator->getMessages($myInput, new AlbumInputValidator());  // null


$myWrongInput = [
    'name' => 'Tom',
    'phone' => 'thisShouldBeA9DigitsNumber'
];
$validator->isValid($myWrongInput, new AlbumInputValidator()) // false
$validator->getMessages($myWrongInput, new AlbumInputValidato()) 
//  ['phone' => Message()]

Ok, I want my field to be optional, but if it is passed I want to validate it

//    In strategy: 
//    ...
    public function getFields(): array
    {
        $field = new Field(
            'phone',
            false, // set second parameter 'required' as false,
            null
        );

        $field->registerValidator('Digits', null);

        return [$field];
    }
//    ...


// and then
$validator->isValid([], new AlbumInputValidator()); // true
$validator->isValid(['phone' => 'abcdefg'], new AlbumInputValidator()); // false

I want a single field to be conditional. I mean, if eg street name is not passed, I don't want house number to be required.

Have a look:, (*3)

//    In strategy: 
//    ...
    public function getFields(): array
    {
        $streetField = new Field(
            'street',
            false,
            null
        );

        $houseNumberField = new Field(
            'houseNumber',
            true,
            function (array $inputData) {
                return !isset($inputData['street']);
            }
        );

        return [$streetField, $houseNumberField];
    }
//    ...


// and then
$validator->isValid(['street' => 'High st.'], new AlbumInputValidator()); // false
$validator->isValid([], new AlbumInputValidator()); // true
$validator->isValid(['street' => 'High st.', 'houseNumber' => '12'], new AlbumInputValidator()); // true

How can I implement my own validators?

Easily. You can help me to contribute this project. Or if you don't want to, just create a class implementing Hop\Validator\Strategy\Strategy and pass it to StdValidator::fromConfig() as array, eg, (*4)

$myValidators = [
    'MyValidator' => 'Path\To\Validator\Class'
];

$validator = StdValidator::fromConfig(
    \array_merge(include __DIR__ . '/config/validators.php', $myValidators)
);

© Chmielewski Tomasz, (*5)

The Versions

17/07 2018

dev-master

9999999-dev

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

17/07 2018

1.2.7

1.2.7.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

28/06 2018

1.2.6

1.2.6.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

05/06 2018

1.2.5

1.2.5.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

05/06 2018

1.2.4

1.2.4.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

04/06 2018

1.2.3

1.2.3.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

04/06 2018

1.2.2

1.2.2.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

02/06 2018

1.2.1

1.2.1.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

02/06 2018

dev-feature/nested-validators

dev-feature/nested-validators

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

The Requires

  • php ^7.1

 

The Development Requires

02/06 2018

1.2.0

1.2.0.0

Simple validation tool, allows you to validate both simple and nested structures, introduces conditional fields. All you need is here. Perfect for REST endpoint validation. No dependencies required.

  Sources   Download

The Requires

  • php ^7.1

 

The Development Requires

30/05 2018

1.1.6

1.1.6.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

23/05 2018

1.1.5

1.1.5.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

05/05 2018

1.1.4

1.1.4.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

05/05 2018

1.1.3

1.1.3.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

31/03 2018

1.1.2

1.1.2.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

26/02 2018

1.1.1

1.1.1.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

20/02 2018

1.1.0

1.1.0.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

13/02 2018

1.0.8

1.0.8.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

13/02 2018

1.0.7

1.0.7.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

08/02 2018

1.0.6

1.0.6.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

06/02 2018

1.0.5

1.0.5.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

28/01 2018

1.0.4

1.0.4.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

28/01 2018

1.0.3

1.0.3.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

28/01 2018

1.0.2

1.0.2.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

28/01 2018

1.0.1

1.0.1.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires

28/01 2018

1.0.0

1.0.0.0

Simple validation tool

  Sources   Download

The Requires

 

The Development Requires