2017 © Pedro Peláez
 

library neuron-model-validator

A php input model that can handle Swagger V1 specs.

image

catlabinteractive/neuron-model-validator

A php input model that can handle Swagger V1 specs.

  • Thursday, January 7, 2016
  • by catlab
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Validator

A php input validator that can handle Swagger V1 specs., (*1)

Example usage:

$validator = \CatLab\Validator\Validator::fromSwagger('specs/');

$input = array ('id' => 1, 'name' => 'Example');

if ($validator->validate('ModelId', $input)) {
    echo 'Success!';
}
else {
    echo 'Failure' . "\n";
    foreach ($validator->getErrors() as $v) {
        echo $v . "\n";
    }
}

Without swagger:

Regular models:, (*2)

$specs = array (
    'message' => 'required|string',
    'user' => array (
        'id' => 'numeric|required',
        'company' => array (
            'id' => 'numeric|required',
            'name' => 'string|required'
        )
    )
);

$data = array (
    'message' => 'This is a message',
    'user' => array (
        'id' => 1,
        'company' => array (
            'id' => 1,
            'name' => 'This is a company name'
        )
    )
);

$validator = new Validator();
$validator->addModel(Model::make('test', $specs));

$valid = $validator->validate('test', $data);

Optional attributes: Append a question mark to your property name to mark a property as optional., (*3)

$specs = array (
    'message' => 'required|string',
    'user' => array (
        'id' => 'numeric|required',
        'company?' => array (
            'id' => 'numeric|required',
            'name' => 'string|required'
        )
    )
);

$data = array (
    'message' => 'This is a message',
    'user' => array (
        'id' => 1
    )
);

$validator = new Validator();
$validator->addModel(Model::make('test', $specs));

$correct = $validator->validate('test', $data);

Advanced examples:

Arrays:, (*4)

$validator = new Validator();

$model = Model::make(
    'ArrayModel',
    array (
        'id' => 'required|int',
        'collection' => array (
            'count' => 'int|required',
            'items[]' => 'int'
        )
    )
);

$data = array (
    'id' => 1,
    'collection' => array (
        'count' => 2,
        'items' => array (1, 2, 3)
    )
);

$correct = $validator->validate('ArrayModel', $data);

$validator->addModel($model);

Arrays of models:, (*5)

$validator = new Validator();

$model = Model::make(
    'ArrayModel',
    array (
        'id' => 'required|int',
        'collection' => array (
            'count' => 'int|required',
            'items[]?' => array (
                'id' => 'int|required',
                'name' => 'string'
            )
        )
    )
);

$validator->addModel($model);

$data = array (
    'id' => 1,
    'collection' => array (
        'count' => 2,
        'items' => array (
            array (
                'id' => 15,
                'name' => 'Foo'
            ),

            array (
                'id' => 16,
                'name' => 'Bar'
            )
        )
    )
);

$correct = $validator->validate('ArrayModel', $data);

Put a question mark at the end of your property name to mark the array property as optional., (*6)

$validator = new Validator();

$model = Model::make(
    'ArrayModel',
    array (
        'id' => 'required|int',
        'collection' => array (
            'count' => 'int|required',
            'items[]?' => array (
                'id' => 'int|required',
                'name' => 'string'
            )
        )
    )
);

$validator->addModel($model);

$correctData = array (
    'id' => 1,
    'collection' => array (
        'count' => 2,
        'items' => array (
            array (
                'id' => 15,
                'name' => 'Foo'
            ),

            array (
                'id' => 16,
                'name' => 'Bar'
            )
        )
    )
);

$correct = $validator->validate('ArrayModel', $correctData);

Usage with PHPUnit:

$model = Model::make(
    'ModelWithValues',
    array (
        'id' => 'required|string',
        'name' => 'required|string',
        'value1' => 'required|string'
    )
);

$data = array (
    'id' => 'I am a string',
    'name' => 'My name is Paul',
    'value1' => 'I\'m very happy here'
);

$this->assertThat($data, new IsValidModel($model));

Checking for exact values (with conversion to comparable types):

$validator = new Validator();

$model = Model::make(
    'ModelWithValues',
    array (
        'id' => 'required|string',
        'name' => 'required|string',
        'value1' => 'required|string'
    )
);

$validator->addModel($model);

// Set the expected values
$model->setValues(array (
    'id' => 15,
    'name' => 'Thijs',
    'value1' => 'woop'
));

// Input data
$data = array (
    'id' => 15,
    'name' => 'Thijs',
    'value1' => 'woop'
);

// Check if model match expectations & compare values
$correct = $validator->validate ('ModelWithValues', $data);

The Versions

07/01 2016

dev-master

9999999-dev http://github.com/catlabinteractive/neuron-model-validator

A php input model that can handle Swagger V1 specs.

  Sources   Download

MIT

framework

07/01 2016

1.0.6

1.0.6.0 http://github.com/catlabinteractive/neuron-model-validator

A php input model that can handle Swagger V1 specs.

  Sources   Download

MIT

framework

07/10 2015

1.0.5

1.0.5.0 http://github.com/catlabinteractive/neuron-model-validator

A php input model that can handle Swagger V1 specs.

  Sources   Download

MIT

framework

31/08 2015

1.0.4

1.0.4.0 http://github.com/catlabinteractive/neuron-model-validator

A php input model that can handle Swagger V1 specs.

  Sources   Download

MIT

framework

21/08 2015

1.0.3

1.0.3.0 http://github.com/catlabinteractive/validator

A php input validator that can handle Swagger V1 specs.

  Sources   Download

MIT

framework

21/08 2015

1.0.2

1.0.2.0 http://github.com/catlabinteractive/validator

A php input validator that can handle Swagger V1 specs.

  Sources   Download

MIT

framework

21/08 2015

1.0.1

1.0.1.0 http://github.com/catlabinteractive/validator

A php input validator that can handle Swagger V1 specs.

  Sources   Download

MIT

framework

16/02 2015

1.0.0

1.0.0.0 http://github.com/catlabinteractive/validator

A php input validator that can handle Swagger V1 specs.

  Sources   Download

MIT

framework