PHPackages                             fitbug/guzzle-swagger-validation-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. fitbug/guzzle-swagger-validation-middleware

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

fitbug/guzzle-swagger-validation-middleware
===========================================

A guzzle middleware that can be used to validate if requests and responses match what is defined in the schema

v0.1.0(9y ago)1241MITPHP

Since Oct 15Pushed 9y ago5 watchersCompare

[ Source](https://github.com/fitbug/guzzle-swagger-validation-middleware)[ Packagist](https://packagist.org/packages/fitbug/guzzle-swagger-validation-middleware)[ RSS](/packages/fitbug-guzzle-swagger-validation-middleware/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Guzzle Middleware: Swagger Validation Middleware
================================================

[](#guzzle-middleware-swagger-validation-middleware)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/70f364bd0555d9b55d9221a1439a40846b1573c2b895fa91714477e5d487f6d8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6669746275672f67757a7a6c652d737761676765722d76616c69646174696f6e2d6d6964646c65776172652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/fitbug/guzzle-swagger-validation-middleware/?branch=master)[![Build Status](https://camo.githubusercontent.com/3b0261ae64b8d0df3f19749e096983bbf3b3702a3fe728e84c90b7926e7c1239/68747470733a2f2f7472617669732d63692e6f72672f6669746275672f67757a7a6c652d737761676765722d76616c69646174696f6e2d6d6964646c65776172652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/fitbug/guzzle-swagger-validation-middleware)[![StyleCI Status](https://camo.githubusercontent.com/82d44f4ea6e0a385c023b9c0cda83f1b9e65f713f725632e7339f4a1960fd252/68747470733a2f2f7374796c6563692e696f2f7265706f732f37303937373835392f736869656c64)](https://styleci.io/repos/70977859)[![Latest Stable Version](https://camo.githubusercontent.com/3dcfb8ac3d8a5357110080d1342161db4c0865e801152268531add4efeee55fc/68747470733a2f2f706f7365722e707567782e6f72672f6669746275672f67757a7a6c652d737761676765722d76616c69646174696f6e2d6d6964646c65776172652f762f737461626c65)](https://packagist.org/packages/fitbug/guzzle-swagger-validation-middleware)[![Coverage Status](https://camo.githubusercontent.com/9e7863f3b64be9074194bfabdf64244b7016c23b199e329c55b424075ba89545/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6669746275672f67757a7a6c652d737761676765722d76616c69646174696f6e2d6d6964646c65776172652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/fitbug/guzzle-swagger-validation-middleware?branch=master)

A guzzle middleware that can be used to validate if requests and responses match what is defined in the schema

Getting Started
---------------

[](#getting-started)

### Prerequisities

[](#prerequisities)

You'll need to install:

- PHP (Minimum 5.6)

### Installing

[](#installing)

```
composer require fitbug/guzzle-swagger-validation-middleware
```

Usage
-----

[](#usage)

Simply add it to the guzzle you use as middleware.

```
use Fitbug\Guzzle\SwaggerValidation\SwaggerSchemaValidationHandler;
use GuzzleHttp\HandlerStack;
use Http\Adapter\Guzzle6\Client as GuzzleClientFactory;
use Http\Message\MessageFactory\GuzzleMessageFactory;

$this->messageFactory = new GuzzleMessageFactory();
$swaggerFile = 'file://';
$swaggerFile .= '/path/to/your/swagger.json';
$this->swaggerValidation = new SwaggerSchemaValidationHandler($swaggerFile);
$stack = new HandlerStack();
$stack->setHandler(GuzzleHttp\choose_handler());
$stack->push($this->swaggerValidation);
$this->httpClient = GuzzleClientFactory::createWithConfig(
    [
        'base_uri' => $apiEndpoint,
        'handler' => $stack,
    ]
);
```

You get output like the request or response doesn't match the specification

```
 PHPUnit_Framework_ExpectationFailedException: Failed asserting that 'application/json, application/json' is an allowed media type (application/json). in vendor/phpunit/phpunit/src/Framework/Constraint.php:115
      Stack trace:
      #0 vendor/phpunit/phpunit/src/Framework/Constraint.php(58): PHPUnit_Framework_Constraint->fail('application/jso...', '')
      #1 vendor/phpunit/phpunit/src/Framework/Assert.php(1980): PHPUnit_Framework_Constraint->evaluate('application/jso...', '')
      #2 vendor/fr3d/swagger-assertions/src/PhpUnit/AssertsTrait.php(128): PHPUnit_Framework_Assert::assertThat('application/jso...', Object(FR3D\SwaggerAssertions\PhpUnit\MediaTypeConstraint), '')
      #3 vendor/fr3d/swagger-assertions/src/PhpUnit/Psr7AssertsTrait.php(89): Fitbug\BackOfficeApi\Features\Support\Guzzle\SwaggerSchemaValidationHandler->assertRequestMediaTypeMatch('application/jso...', Object(FR3D\SwaggerAssertions\SchemaManager), '/v0/company', 'POST', '')
...snip...
      #45 vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php(256): Behat\Testwork\Cli\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #46 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(818): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #47 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Behat\Testwork\Cli\Command), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #48 vendor/behat/behat/src/Behat/Testwork/Cli/Application.php(121): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #49 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(117): Behat\Testwork\Cli\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #50 vendor/behat/behat/bin/behat(32): Symfony\Component\Console\Application->run()
      #51 {main}

      Next Exception: Failed asserting that 'application/json, application/json' is an allowed media type (application/json).

      POST /v0/company HTTP/1.1
      User-Agent: GuzzleHttp/6.2.1 curl/7.35.0 PHP/7.0.7
      Host: localhost
      Accept: application/json
      Accept: application/json
      Content-Type: application/json
      Content-Type: application/json
      Authorization: FITBUG-INTERNAL client-id=behat

      {"name":"starhealth","description":"some description","contact_email":"some support information"}

      HTTP/1.1 500 Internal Server Error
      Date: Thu, 08 Sep 2016 10:47:15 GMT
      Server: Apache/2.4.10 (Debian)
      Vary: Authorization
      X-Powered-By: PHP/7.0.10
      Set-Cookie: PHPSESSID=93257eb031eb0a7b84e4a2af7fef1495; path=/; HttpOnly
      Cache-Control: no-cache
      Content-Length: 195
      Connection: close
      Content-Type: application/json

      {
          "meta": {
              "error": {
                  "number": 2004,
                  "message": "Unknown error",
                  "user_message": "An unknown error has occured"
              }
          },
          "data": {}
      }
       in features/support/Guzzle/SwaggerSchemaValidationHandler.php:99
      Stack trace:
      #0 vendor/guzzlehttp/promises/src/Promise.php(203): Fitbug\BackOfficeApi\Features\Support\Guzzle\SwaggerSchemaValidationHandler->Fitbug\BackOfficeApi\Features\Support\Guzzle\{closure}(Object(GuzzleHttp\Psr7\Response))
      #1 vendor/guzzlehttp/promises/src/Promise.php(169): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
      #2 vendor/guzzlehttp/promises/src/FulfilledPromise.php(39): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}(Object(GuzzleHttp\Psr7\Response))
      #3 vendor/guzzlehttp/promises/src/TaskQueue.php(61): GuzzleHttp\Promise\FulfilledPromise::GuzzleHttp\Promise\{closure}()
...snip...
      #41 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(186): Symfony\Component\Console\Application->doRunCommand(Object(Behat\Testwork\Cli\Command), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #42 vendor/behat/behat/src/Behat/Testwork/Cli/Application.php(121): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #43 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(117): Behat\Testwork\Cli\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #44 vendor/behat/behat/bin/behat(32): Symfony\Component\Console\Application->run()
      #45 {main}

      Next RuntimeException: Invalid exception returned from Guzzle6 in vendor/php-http/guzzle6-adapter/src/Promise.php:65
      Stack trace:
      #0 vendor/guzzlehttp/promises/src/Promise.php(203): Http\Adapter\Guzzle6\Promise->Http\Adapter\Guzzle6\{closure}(Object(Exception))
      #1 vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(2, Object(Exception), Array)
      #2 vendor/guzzlehttp/promises/src/TaskQueue.php(61): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
      #3 vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): GuzzleHttp\Promise\TaskQueue->run()
...snip...
      #41 vendor/behat/behat/src/Behat/Testwork/Cli/Application.php(121): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #42 vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(117): Behat\Testwork\Cli\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      #43 vendor/behat/behat/bin/behat(32): Symfony\Component\Console\Application->run()
      #44 {main}

```

Running the tests
-----------------

[](#running-the-tests)

First checkout the library, then run

```
composer install
```

### Coding Style

[](#coding-style)

We follow PSR2, and also enforce PHPDocs on all functions. To run the tests for coding style violations

```
vendor/bin/php-cs-fixer fix -v
```

### Unit tests

[](#unit-tests)

We use PHPSpec for unit tests. To run the unit tests

```
vendor/bin/phpspec run
```

Contributing
------------

[](#contributing)

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.

Versioning
----------

[](#versioning)

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/fitbug/guzzle-swagger-validation-middleware/tags).

Authors
-------

[](#authors)

See the list of [contributors](https://github.com/fitbug/guzzle-swagger-validation-middleware/contributors) who participated in this project.

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3493d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a24e38d9fccad1b559a52a88d2d851e65021a53e9bb408ccf329d65a18bb066b?d=identicon)[fitbug](/maintainers/fitbug)

---

Top Contributors

[![PurpleBooth](https://avatars.githubusercontent.com/u/133327?v=4)](https://github.com/PurpleBooth "PurpleBooth (11 commits)")[![memsb](https://avatars.githubusercontent.com/u/1275738?v=4)](https://github.com/memsb "memsb (1 commits)")

### Embed Badge

![Health badge](/badges/fitbug-guzzle-swagger-validation-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/fitbug-guzzle-swagger-validation-middleware/health.svg)](https://phpackages.com/packages/fitbug-guzzle-swagger-validation-middleware)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
