PHP code example of catlabinteractive / validator
1. Go to this page and download the library: Download catlabinteractive/validator library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
catlabinteractive / validator example snippets
$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";
}
}
$specs = array (
'message' => ' => 'numeric|> 'numeric|
$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);
$specs = array (
'message' => ' => 'numeric|=> 'numeric|
$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);
$validator = new Validator();
$model = Model::make(
'ArrayModel',
array (
'id' => 't'
)
)
);
$data = array (
'id' => 1,
'collection' => array (
'count' => 2,
'items' => array (1, 2, 3)
)
);
$correct = $validator->validate('ArrayModel', $data);
$validator->addModel($model);
$validator = new Validator();
$model = Model::make(
'ArrayModel',
array (
'id' => 'ray (
'id' => 'int| 'collection' => array (
'count' => 2,
'items' => array (
array (
'id' => 15,
'name' => 'Foo'
),
array (
'id' => 16,
'name' => 'Bar'
)
)
)
);
$correct = $validator->validate('ArrayModel', $data);
$validator = new Validator();
$model = Model::make(
'ArrayModel',
array (
'id' => 'ray (
'id' => 'int| 1,
'collection' => array (
'count' => 2,
'items' => array (
array (
'id' => 15,
'name' => 'Foo'
),
array (
'id' => 16,
'name' => 'Bar'
)
)
)
);
$correct = $validator->validate('ArrayModel', $correctData);
$model = Model::make(
'ModelWithValues',
array (
'id' => ''
)
);
$data = array (
'id' => 'I am a string',
'name' => 'My name is Paul',
'value1' => 'I\'m very happy here'
);
$this->assertThat($data, new IsValidModel($model));
$validator = new Validator();
$model = Model::make(
'ModelWithValues',
array (
'id' => 'l($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);