Download the PHP package codeasashu/openapi-validator without Composer
On this page you can find all versions of the php package codeasashu/openapi-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codeasashu/openapi-validator
More information about codeasashu/openapi-validator
Files in codeasashu/openapi-validator
Package openapi-validator
Short Description Validates data against provide openapi spec
License proprietary
Informations about the package openapi-validator
What is this?
Validate data against openapi v3 spec
Features
- Checks for required fields
- Checks types
- Supports nested structures
- Supports discriminator
- Supports allOf, anyOf
- Supports nullable
- Resolves local references (components)
- Supports additionalProperties
Unlike Dredd it does not require examples and does not check that data matches examples
Installation
Requirements
Your openapi spec has to be valid, You can use Speccy to check Your schema first
This library assumes that each operation has operationId
Examples
Given we have api described by following OpenAPI specification
We have server implementation (this is of course example - normally You would fetch data from some storage)
How to verify that server implementation works as described? Use openapi-validator with any http client:
- Load Your spec to validator
- Make request with any http client
- Pass request uri, request method, response code and response body to validator (and optionally media type, default is application/json)
-
Validator will find figure out that for method 'GET', path '/cards' and response code 200, required response schema is:
- Actual response body is validated against that schema
- Result object is produced, if response is invalid then result object will contain errors
In this case response is valid:
Lets now introduce some errors:
Required id field for second item is missing:
Lets break something else
power field should be integer (second item):
Other libraries
- Dredd - currently supports only swagger/openapi v2, support for v3 is not yet there
- Swagger - support for v2 only
TODO
- Support all openapi formats
- Support for not keyword
How this works?
Transform openapi spec into json schema and then uses justinrainbow/json-schema to validate it