PHP code example of hkarlstrom / openapi-validation-middleware
1. Go to this page and download the library: Download hkarlstrom/openapi-validation-middleware library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hkarlstrom / openapi-validation-middleware example snippets
$config = [
'settings' => [
'determineRouteBeforeAppMiddleware' => true,
],
];
$app = new \Slim\App($config);
$app->add(new HKarlstrom\Middleware\OpenApiValidation('/path/to/openapi.json'));
$app = $container->get(\Zend\Expressive\Application::class);
$app->pipe(new HKarlstrom\Middleware\OpenApiValidation('/path/to/openapi.json'));
$app = new Slim\App;
$app->add(new HKarlstrom\Middleware\OpenApiValidation('/path/to/openapi.json'),[
'additionalParameters' => true,
'stripResponse' => true
]);
$options = [
'beforeHandler' => function (\Psr\Http\Message\ServerRequestInterface $request, array $errors) : \Psr\Http\Message\ServerRequestInterface {
// Alter request
return $request
}
];
$options = [
'errorHandler' => function (int $code, string $message, array $errors) : \Psr\Http\Message\ResponseInterface {
// Alter request
return $request
}
];
$options = [
'validateSecurity' => function (ServerRequestInterface $request, string $type, string $token = '', ?array $scopes) : ?\Psr\Http\Message\ResponseInterface {
// if user is authorized
return null;
// create and return error response
$response = new Response( ... );
return $response;
}
];
class MyOwnFormat implements Opis\JsonSchema\Format {
public function validate($data) : bool
{
// Validate data
// $isValid = ...
return $isValid;
}
}
$mw = new HKarlstrom\Middleware\OpenApiValidation('/path/to/openapi.json');
$mw->addFormat('string','my-own-format',new MyOwnFormat());
$app->add($mw);