mlambley/swagvalidator

Validates data against a Swagger schema

1.0.0 2020-11-25 01:51 UTC

This package is auto-updated.

Last update: 2024-11-29 06:28:54 UTC


README

Validates data against a Swagger schema

How to Install

composer require --dev mlambley/swagvalidator

What is Swagger?

Swagger 2.0 (aka Open API 2.0) defines the structure of your API, including end points and the structure of input and output data. See their website for more information.

What is Swagvalidator?

If you have an existing Swagger 2.0 specification, you can use it to validate data coming in or out of your API using this tool. This library fully takes into account the features of the Swagger 2.0 specification.

Requirements

None. This is a pure PHP tool with no dependencies and will work with PHP 5.6 onwards.

Usage

use Mlambley\Swagvalidator\Validator\Validator;
use Mlambley\Swagvalidator\Exception\ValidationException;

$response = $this->getApi("your/path");
$json = (string)$response->getBody();
$data = json_decode($json);
$schema = json_decode(file_get_contents(__DIR__ . '/swagger.json'));

try {
    (new Validator())
        ->validate($schema->paths->{"your/path"}->get->responses->{"200"}->schema, $data);
} catch (ValidationException $e) {
    //dd($e->getMessage());
}

Issues?

Log a github issue. Your assistance is appreciated.