2017 © Pedro Peláez
 

library api-validator

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

image

eleven-labs/api-validator

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  • Wednesday, June 6, 2018
  • by guillemcanal
  • Repository
  • 4 Watchers
  • 11 Stars
  • 245 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 15 Versions
  • 4 % Grown

The README.md

API Validator

Build Status Code Coverage Scrutinizer Quality Score, (*1)

This library provides a set of classes suited to describe a WebService based on the HTTP protocol., (*2)

It can validate PSR-7 Requests against a schema., (*3)

It's design is heavily inspired by the OpenAPI/Swagger2.0 specifications., (*4)

As of now, it only support the OpenAPi/Swagger2.0 specifications but we plan to support RAML 1.0 and API Elements (API Blueprint) in the future., (*5)

Dependencies

We rely on the justinrainbow/json-schema library to parse specification files and to validate requests and responses:, (*6)

  • Request's headers, query, uri and body parts.
  • Response headers and body parts.

Usage

Before you start

You will need to write a valid Swagger 2.0 file in order to use this library. Ensure that this file is valid using the Swagger Editor., (*7)

You can also validate your specifications using the Swagger 2.0 JSONSchema., (*8)

Validate a request

You can validate any PSR-7:, (*9)

  • Request implementing the Psr\Http\Message\RequestInterface interface.
  • Response implementing the Psr\Http\Message\ResponseInterface interface.
<?php

use ElevenLabs\Api\Factory\SwaggerSchemaFactory;
use ElevenLabs\Api\Decoder\Adapter\SymfonyDecoderAdapter;
use ElevenLabs\Api\Validator\MessageValidator;
use JsonSchema\Validator;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\ChainDecoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

// Given a $request implementing the `Psr\Http\Message\RequestInterface`
// For this example we are using the PSR7 Guzzle implementation;
$request = new Request(
    'POST', 
    'http://domain.tld/api/pets',
    ['application/json'],
    '{"id": 1, "name": "puppy"}'
);

$validator = new Validator();

// Here we are using decoders provided by the symfony serializer component
// feel free to use yours if you so desire. You just need to create an adapter that 
// implement the `ElevenLabs\Api\Decoder\DecoderInterface` 
$decoder = new SymfonyDecoderAdapter(
    new ChainDecoder([
        new JsonDecode(),
        new XmlEncoder()
    ])  
);

// Load a JSON swagger 2.0 schema using the SwaggerSchemaFactory class.
// We plan to support RAML 1.0 and API Elements (API Blueprint) in the future.
$schema = (new SwaggerSchemaFactory())->createSchema('file://path/to/your/swagger.json');

// Find the Request Definition in the Schema API
$requestDefinition = $schema->getRequestDefinition(
  $schema->findOperationId($request->getMethod(), $request->getUri()->getPath())  
);

// Validate the Request
$messageValidator = new MessageValidator($validator, $decoder);
$messageValidator->validateRequest($request, $requestDefinition);

// Check if the request has violations
if ($messageValidator->hasViolations()) {
    // Get violations and do something with them
    $violations = $messageValidator->getViolations();
}

// Using the message Validator, you can also validate a Response
// It will find the proper ResponseDefinition from a RequestDefinition
$response = new Response(
    200, 
    ['Content-Type' => 'application/json'],
    '{"id": 1}'
);

$messageValidator->validateResponse($response, $requestDefinition);

// ...

Working with Symfony HTTPFoundation Requests

You will need an adapter in order to validate symfony requests., (*10)

We recommend you to use the symfony/psr-http-message-bridge, (*11)

Using the schema

You can navigate the ElevenLabs\Api\Schema to meet other use cases., (*12)

Example:, (*13)

<?php
use ElevenLabs\Api\Factory\SwaggerSchemaFactory;

$schema = (new SwaggerSchemaFactory())->createSchema('file://path/to/your/swagger.json');

// Find a request definition from an HTTP method and a path.
$requestDefinition = $schema->getRequestDefinition(
    $schema->findOperationId('GET', '/pets/1234')
);

// Get the response definition for the status code 200 (HTTP OK)
$responseDefinition = $requestDefinition->getResponseDefinition(200);

// From here, you can access the JSON Schema describing the expected response
$responseSchema = $responseDefinition->getBodySchema();

The Versions

06/06 2018
02/11 2016

dev-feat/work

dev-feat/work

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

13/09 2016
29/08 2016
24/08 2016
24/08 2016

dev-refact/http-message-validator

dev-refact/http-message-validator

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

24/08 2016
23/08 2016

0.2.2

0.2.2.0

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

22/08 2016

dev-feat/vendor-definitions

dev-feat/vendor-definitions

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

22/08 2016

0.2.1

0.2.1.0

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

22/08 2016

0.2.0

0.2.0.0

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

18/08 2016

0.1.2

0.1.2.0

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

12/08 2016

0.1.1

0.1.1.0

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL

04/08 2016

0.1.0

0.1.0.0

Validate PSR-7 Requests against an OpenAPI/Swagger2 Schema

  Sources   Download

The Requires

 

The Development Requires

by Guillem CANAL