OpenAPI(v3) Validators for Symfony http-foundation, using league/openapi-psr7-validator
and symfony/psr-http-message-bridge
.
- PHP >= 8.1
composer require n1215/openapi-http-foundation-validator
- You can use any implementation of PSR-17 HTTP Factory.
- ex.
nyholm/psr7
- ex.
composer require nyholm/psr7
$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
);
- 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.
/** @var \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder $validatorBuilder */
$validatorBuilder = (new \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder($psr17Factory))
->fromYamlFile('/path/to/openapi.yaml')
->setCache(new YourPsr6Cache(), 86400);
/** @var \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder $validatorBuilder */
$validatorBuilder = (new \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder($psr17Factory))
->fromJsonFile('/path/to/openapi.json')
->setSimpleCache(new YourPsr16Cache(), 3600);
/** @var \N1215\OpenApiValidation\HttpFoundation\Validators $validators */
$validators = $validatorBuilder->getValidators();
/** @var \Symfony\Component\HttpFoundation\Request $request */
/** @var \N1215\OpenApiValidation\HttpFoundation\RequestValidatorInterface $requestValidator */
$requestValidator = $validators->getRequestValidator();
$requestValidator->validate($request);
/** @var \Symfony\Component\HttpFoundation\Response $response */
/** @var \N1215\OpenApiValidation\HttpFoundation\ResponseValidatorInterface $responseValidator */
$responseValidator = $validators->getResponseValidator();
$responseValidator->validate(
new \N1215\OpenApiValidation\OperationAddress('/path', 'GET'),
$response
);