composer require ddrv/dresscode
<?php
use Ddrv\DressCode\Action;
use Ddrv\DressCode\DressCode;
require 'vendor/autoload.php';
$validator = new DressCode();
$validator->validate(Action::output(), ['type' => 'string'], 'it is ok');
<?php
use Ddrv\DressCode\Action;
use Ddrv\DressCode\DressCode;
use Ddrv\DressCode\Exception\InvalidValueException;
require 'vendor/autoload.php';
$validator = new DressCode();
$rule = [
'type' => 'object',
'properties' => [
'email' => [
'type' => 'string',
'format' => 'email',
],
'login' => [
'type' => 'string',
'minLength' => 5,
'maxLength' => 32,
'pattern' => '^[a-z\-]+$',
],
'birthday' => [
'type' => 'string',
'format' => 'date',
],
],
'required' => ['email', 'login'],
'additionalProperties' => true,
'nullable' => true,
];
$data = [
'email' => 'ivan',
'login' => 'ddrv',
'birthday' => '2020-02-30',
];
try {
$validator->validate(Action::input(), $rule, $data);
} catch (InvalidValueException $e) {
foreach ($e->getErrors() as $error) {
echo $error->getPath() . ': ' . $error->getMessage() . PHP_EOL;
}
}
/*
email: ivan is not a email
login: string size must be between 5 to 32 symbols
birthday: 2020-02-30 is not a date
*/
<?php
use Ddrv\DressCode\Action;
use Ddrv\DressCode\DressCode;
require 'vendor/autoload.php';
$validator = new DressCode();
$validator->setEntity('#/entities/email', [
'type' => 'string',
'format' => 'email',
]);
$validator->setEntity('#/entities/login', [
'type' => 'string',
'minLength' => 5,
'maxLength' => 32,
'pattern' => '^[a-z\-]+$',
]);
$validator->setEntity('#/entities/date', [
'type' => 'string',
'format' => 'date',
]);
$rule = [
'type' => 'object',
'properties' => [
'email' => [
'$ref' => '#/entities/email',
],
'login' => [
'$ref' => '#/entities/login',
],
'birthday' => [
'$ref' => '#/entities/date',
],
'password' => [
'type' => 'string',
'minLength' => 8,
'maxLength' => 32,
'writeOnly' => true,
],
],
'required' => ['email', 'login'],
'additionalProperties' => true,
'nullable' => true,
];
$data = [
'email' => 'ivan@ddrv.ru',
'login' => 'i-dudarev',
'password' => 'short',
];
$valid = $validator->validate(Action::input(), $rule, $data); // Error password
$valid = $validator->validate(Action::output(), $rule, $data); // No error because password writeOnly
$valid = $validator->validate(Action::input(), ['$ref' => '#/entities/date'], '2020-12-30');
<?php
use Ddrv\DressCode\Action;
use Ddrv\DressCode\Exception\WrongFormatException;
use Ddrv\DressCode\Format\Format;
use Ddrv\DressCode\DressCode;
require 'vendor/autoload.php';
$validator = new DressCode();
$myEmailFormat = new class extends Format
{
public function check(string $value) : void{
if (!filter_var($value, FILTER_VALIDATE_EMAIL) !== false) {
throw new WrongFormatException('email');
}
}
};
$validator->registerFormat('email', $myEmailFormat);
$rule = [
'type' => 'string',
'format' => 'email',
];
$validator->validate(Action::input(), $rule, 'ivan@ddrv.ru');
use Action::input(true)
and Action::output(true)
for strict type checking.
Warning
do not use it if you are checking data
application/x-www-form-urlencoded
- boolean
- number
- number (format
float
) - number (format
double
) - integer
- integer (format
int32
) - integer (format
int64
) - string
- string (format
binary
) - string (format
byte
) - string (format
date
) - string (format
date-time
) - string (format
email
) - string (format
hostname
) - string (format
ip
) - string (format
ipv4
) - string (format
ipv6
) - string (format
uri
) - string (format
uuid
) - array
- object
- nullable
- readOnly
- writeOnly
- default (only for object properties)
- minimum
- maximum
- exclusiveMinimum
- exclusiveMaximum
- multipleOf
- pattern
- minLength
- maxLength
- enum
- items
- minItems
- maxItems
- uniqueItems
- properties
- required
- additionalProperties
- minProperties
- maxProperties
- $ref (only after call
setEntity()
method)
- oneOf
- anyOf
- allOf
- not