1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Usox\JsonSchemaApi\Dispatch; |
6
|
|
|
|
7
|
|
|
use Opis\JsonSchema\Errors\ErrorFormatter; |
8
|
|
|
use Opis\JsonSchema\Helper; |
9
|
|
|
use Opis\JsonSchema\Validator; |
10
|
|
|
use stdClass; |
11
|
|
|
use Teapot\StatusCode\Http; |
12
|
|
|
use Usox\JsonSchemaApi\Exception\RequestMalformedException; |
13
|
|
|
use Usox\JsonSchemaApi\Exception\ResponseMalformedException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Validates input and output against the json schema to ensure valid requests/responses |
17
|
|
|
*/ |
18
|
|
|
final readonly class MethodValidator implements MethodValidatorInterface |
|
|
|
|
19
|
|
|
{ |
20
|
6 |
|
public function __construct( |
21
|
|
|
private Validator $schemaValidator, |
22
|
|
|
private ErrorFormatter $errorFormatter |
23
|
|
|
) { |
24
|
6 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @throws RequestMalformedException |
28
|
|
|
*/ |
29
|
2 |
|
public function validateInput( |
30
|
|
|
stdClass $methodSchemaContent, |
31
|
|
|
stdClass $input |
32
|
|
|
): void { |
33
|
|
|
// Validate the input parameter against the parameter definition in method schema |
34
|
2 |
|
$validationResult = $this->schemaValidator->validate( |
35
|
2 |
|
$input->parameter, |
36
|
2 |
|
$methodSchemaContent->properties->parameter |
37
|
2 |
|
); |
38
|
|
|
|
39
|
|
|
// Throw exception if the input does not validate against the basic request schema |
40
|
2 |
|
if (!$validationResult->isValid()) { |
41
|
1 |
|
throw new RequestMalformedException( |
42
|
1 |
|
'Bad Request', |
43
|
1 |
|
Http::BAD_REQUEST |
44
|
1 |
|
); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array<mixed> $output |
50
|
|
|
* |
51
|
|
|
* @throws ResponseMalformedException |
52
|
|
|
*/ |
53
|
2 |
|
public function validateOutput( |
54
|
|
|
stdClass $methodSchemaContent, |
55
|
|
|
array $output |
56
|
|
|
): void { |
57
|
2 |
|
if (property_exists($methodSchemaContent->properties, 'response')) { |
58
|
2 |
|
$data = new stdClass(); |
59
|
2 |
|
$data->data = Helper::toJSON($output); |
60
|
|
|
|
61
|
|
|
// Wrap the response schema |
62
|
2 |
|
$response = (object) [ |
63
|
2 |
|
'type' => 'object', |
64
|
2 |
|
'properties' => (object) [ |
65
|
2 |
|
'data' => $methodSchemaContent->properties->response, |
66
|
2 |
|
], |
67
|
2 |
|
'required' => ['data'] |
68
|
2 |
|
]; |
69
|
|
|
|
70
|
|
|
// Validate the response against the response definition in method schema |
71
|
2 |
|
$validationResult = $this->schemaValidator->validate( |
72
|
2 |
|
$data, |
73
|
2 |
|
$response |
74
|
2 |
|
); |
75
|
|
|
|
76
|
2 |
|
$error = $validationResult->error(); |
77
|
|
|
// Throw exception if the input does not validate against the basic request schema |
78
|
2 |
|
if ($error !== null) { |
79
|
1 |
|
throw new ResponseMalformedException( |
80
|
1 |
|
'Internal Server Error', |
81
|
1 |
|
Http::INTERNAL_SERVER_ERROR, |
82
|
1 |
|
null, |
83
|
1 |
|
$this->errorFormatter->format($error) |
84
|
1 |
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|