n1215 / openapi-http-foundation-validator
Installs: 3 701
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- league/openapi-psr7-validator: ^0.22
- psr/cache: 1.0|^2.0|^3.0
- psr/simple-cache: ^1.0|^2.0|^3.0
- symfony/psr-http-message-bridge: ^6.0|^7.0
Requires (Dev)
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.3
- squizlabs/php_codesniffer: ^3.7
- yiisoft/cache: ^3.0
README
OpenAPI(v3) Validators for Symfony http-foundation, using league/openapi-psr7-validator
and symfony/psr-http-message-bridge
.
Requirements
- PHP >= 8.1
Installation
composer require n1215/openapi-http-foundation-validator
Usage
1. install PSR-17 HTTP Factory implementation.
- You can use any implementation of PSR-17 HTTP Factory.
- ex.
nyholm/psr7
- ex.
composer require nyholm/psr7
2. create http message factory
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory(); /** @var \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface $httpMessageFactory */ $httpMessageFactory = new \Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory( serverRequestFactory: $psr17Factory, streamFactory: $psr17Factory, uploadedFileFactory: $psr17Factory, responseFactory: $psr17Factory );
3. create validator builder
- A builder can be created from YAML file, YAML string, JSON file, or JSON string.
- You can use PSR-16 simple cache instead of PSR-6 Cache.
example1
/** @var \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder $validatorBuilder */ $validatorBuilder = (new \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder($psr17Factory)) ->fromYamlFile('/path/to/openapi.yaml') ->setCache(new YourPsr6Cache(), 86400);
example2
/** @var \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder $validatorBuilder */ $validatorBuilder = (new \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder($psr17Factory)) ->fromJsonFile('/path/to/openapi.json') ->setSimpleCache(new YourPsr16Cache(), 3600);
4. get validators from builder
/** @var \N1215\OpenApiValidation\HttpFoundation\Validators $validators */ $validators = $validatorBuilder->getValidators();
5. validate request
/** @var \Symfony\Component\HttpFoundation\Request $request */ /** @var \N1215\OpenApiValidation\HttpFoundation\RequestValidatorInterface $requestValidator */ $requestValidator = $validators->getRequestValidator(); $requestValidator->validate($request);
6. validate response
/** @var \Symfony\Component\HttpFoundation\Response $response */ /** @var \N1215\OpenApiValidation\HttpFoundation\ResponseValidatorInterface $responseValidator */ $responseValidator = $validators->getResponseValidator(); $responseValidator->validate( new \N1215\OpenApiValidation\OperationAddress('/path', 'GET'), $response );