Download the PHP package kentaroutakeda/laravel-openapi-validator without Composer
On this page you can find all versions of the php package kentaroutakeda/laravel-openapi-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kentaroutakeda/laravel-openapi-validator
More information about kentaroutakeda/laravel-openapi-validator
Files in kentaroutakeda/laravel-openapi-validator
Package laravel-openapi-validator
Short Description Laravel OpenAPI Validator - Request and response validators based on the OpenAPI Specification.
License MIT
Informations about the package laravel-openapi-validator
Laravel OpenAPI Validator
Request and response validators based on the OpenAPI Specification.
Summary
- Validate any request and response with a pre-prepared OpenAPI Spec.
- Automatically load specs from Laravel OpenAPI or L5 Swagger.
- You can also load your own specs without using these libraries.
- You can customize validation and error logging behavior on a per-route or application-wide basis.
- Can display Swagger UI. You can view the documentation and test the API.
Requirements
- PHP 8.1 or higher
- Laravel 9.0 or higher
Installation
You can install the package via composer:
Usage
-
Configure OpenAPI Specification
If you're using Laravel OpenAPI, you don't need to do anything.
For L5 Swagger, the following settings are required:
How to load your own schema without using these packages will be explained later.
-
Register Middleware
Routes with this setting will be validated for all requests including Feature Tests, and depending on the settings, responses as well.
NOTE:
This repository's ./e2e directory contains working examples for e2e testing. You can see middleware configuration examples in Routing, and actual validations and failures in Tests. -
(Optional) Customize Middleware
If necessary, you can change Middleware behavior for each route.
NOTE:
Response validation for large amounts of data can take a long time. It would be a good idea to switch on/off validation depending on the route andAPP_*
environment variables. -
Deployment
When deploying your application to production, you should make sure that you run the
openapi-validator:cache
Artisan command during your deployment process:This command caches the OpenAPI Spec defined in your application. If you change the definition for development, you need to clear it as follows:
(Optional) Swagger UI support
You can view the Swagger UI just by installing the package. No additional configuration is required.
-
Install package.
- Display
APP_URL/openapi-validator/documents
in browser.
NOTE:
By default, the Swagger UI can only be displayed when APP_DEBUG
is enabled.
(Optional) Customization
Publish Configuration
You can publish the config file to change behavior.
Alternatively, most settings can be changed using environment variables. Check the comments in config/openapi-validator.php for details.
Your own schema providers
-
If you want to use your own schema providers, first publish the config.
-
Next, implement a class to retrieve the schema.
- Finally, set it in your config.
Error responses and customization
By default, it is formatted according to RFC 7807 - Problem Details for HTTP APIs.
Validation errors, stack traces and original response can also be included depending on your settings. For example, it might look like this:
Here's how to change to a different format:
-
First, implement a class to generate a response. For example:
- Next, register the class to the service container.
Events
If a validation error occurs, an event will be fired depending on the type of error.
ValidationFailedInterface
- All errorsRequestValidationFailed
- Request validation errorResponseValidationFailed
- Response validation error
Contributing and Development
Feel free to open an Issue or add a Pull request.
When adding a pull request, please refer to the following setup steps.
License
Laravel OpenAPI Validator is open-sourced software licensed under the MIT license.
All versions of laravel-openapi-validator with dependencies
laravel/framework Version ^9.0|^10.0|^11.0|^12.0
league/openapi-psr7-validator Version ^0.22.0
nyholm/psr7 Version ^1.8
symfony/psr-http-message-bridge Version ^6.0|^7.0