ddrv/dresscode

OpenAPI schema validator

v1.1.5 2021-01-29 05:21 UTC

This package is auto-updated.

Last update: 2024-10-29 06:12:17 UTC


README

Install

composer require ddrv/dresscode

Usage

<?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');

Check errors

<?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
*/

Defining rules

<?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');

Register your string formats

<?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');

Strict types

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

Supported types

  • 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

Supported keywords

All types

  • nullable
  • readOnly
  • writeOnly
  • default (only for object properties)

Integer and number types

  • minimum
  • maximum
  • exclusiveMinimum
  • exclusiveMaximum
  • multipleOf

String type

  • pattern
  • minLength
  • maxLength
  • enum

Array type

  • items
  • minItems
  • maxItems
  • uniqueItems

Object type

  • properties
  • required
  • additionalProperties
  • minProperties
  • maxProperties

Support references

  • $ref (only after call setEntity() method)

Supported polymorphism

  • oneOf
  • anyOf
  • allOf
  • not