n1215/openapi-http-foundation-validator

0.3.0 2024-11-14 05:06 UTC

This package is auto-updated.

Last update: 2024-12-14 05:24:36 UTC


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
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
);

Usage for Laravel

see https://github.com/n1215/openapi-laravel-validator