MediaWiki
master
Toggle main menu visibility
JsonBodyValidator.php
Go to the documentation of this file.
1
<?php
2
3
namespace
MediaWiki\Rest\Validator;
4
5
use MediaWiki\Json\FormatJson;
6
use MediaWiki\Rest\LocalizedHttpException;
7
use MediaWiki\Rest\RequestInterface;
8
use Wikimedia\Message\ListParam;
9
use Wikimedia\Message\ListType;
10
use Wikimedia\Message\MessageValue;
11
use Wikimedia\ParamValidator\ParamValidator;
12
16
class
JsonBodyValidator
implements
BodyValidator
{
17
21
private
$bodyParamSettings;
22
27
public
function
__construct
( array $bodyParamSettings ) {
28
wfDeprecatedMsg
(
29
__CLASS__ .
' is deprecated.'
,
30
'1.43'
31
);
32
$this->bodyParamSettings = $bodyParamSettings;
33
}
34
39
public
function
validateBody
(
RequestInterface
$request
) {
40
$jsonStream =
$request
->getBody();
41
$status = FormatJson::parse(
"$jsonStream"
, FormatJson::FORCE_ASSOC );
42
43
if
( !$status->isOK() ) {
44
throw
new
LocalizedHttpException
(
45
new
MessageValue
(
'rest-json-body-parse-error'
, [
"$status"
] ),
46
400
47
);
48
}
49
50
$data = $status->value;
51
52
if
( !is_array( $data ) ) {
53
throw
new
LocalizedHttpException
(
new
MessageValue
(
'rest-bad-json-body'
), 400 );
54
}
55
56
$uncheckedBodyKeys = array_fill_keys( array_keys( $data ),
true
);
57
foreach
( $this->bodyParamSettings as $name => $settings ) {
58
if
( !empty( $settings[ParamValidator::PARAM_REQUIRED] ) && !isset( $data[$name] ) ) {
59
throw
new
LocalizedHttpException
(
60
new
MessageValue
(
'rest-missing-body-field'
, [ $name ] ), 400
61
);
62
}
63
64
if
( !isset( $data[$name] ) ) {
65
$data[$name] = $settings[ParamValidator::PARAM_DEFAULT] ??
null
;
66
}
67
68
unset( $uncheckedBodyKeys[$name] );
69
// TODO: use a ParamValidator to check field value, etc!
70
}
71
if
( $uncheckedBodyKeys ) {
72
throw
new
LocalizedHttpException
(
73
new
MessageValue
(
74
'rest-extraneous-body-fields'
,
75
[
new
ListParam
( ListType::COMMA, array_keys( $uncheckedBodyKeys ) ) ]
76
),
77
400
78
);
79
}
80
81
return
$data;
82
}
83
93
public
function
getOpenAPISpec
(): array {
94
$body = [];
95
$required = [];
96
97
// XXX: Maybe we want to be able to define a spec file in the route definition?
98
// NOTE: the route definition may not be loaded when this is called before init()!
99
100
foreach
( $this->bodyParamSettings as $name => $paramSetting ) {
101
$param =
Validator::getParameterSpec
(
102
$name,
103
$paramSetting
104
);
105
106
$body[
'properties'
][$name] = $param[
'schema'
];
107
108
if
( isset( $param[
'description'
] ) ) {
109
$body[
'properties'
][$name][
'description'
] = $param[
'description'
];
110
}
111
112
if
( $param[
'required'
] ??
false
) {
113
$required[] = $param[
'name'
];
114
}
115
}
116
117
if
( $required ) {
118
$body[
'required'
] = $required;
119
}
120
121
return
$body;
122
}
123
}
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition
GlobalFunctions.php:494
MediaWiki\Rest\LocalizedHttpException
Definition
LocalizedHttpException.php:12
MediaWiki\Rest\Validator\JsonBodyValidator
Definition
JsonBodyValidator.php:16
MediaWiki\Rest\Validator\JsonBodyValidator\__construct
__construct(array $bodyParamSettings)
Definition
JsonBodyValidator.php:27
MediaWiki\Rest\Validator\JsonBodyValidator\validateBody
validateBody(RequestInterface $request)
Validate the body of a request.This may return a data structure representing the parsed body....
Definition
JsonBodyValidator.php:39
MediaWiki\Rest\Validator\JsonBodyValidator\getOpenAPISpec
getOpenAPISpec()
Returns an OpenAPI Schema Object specification structure as an associative array.
Definition
JsonBodyValidator.php:93
MediaWiki\Rest\Validator\Validator\getParameterSpec
static getParameterSpec(string $name, array $paramSetting)
Convert a param settings array into an OpenAPI Parameter Object specification structure.
Definition
Validator.php:383
Wikimedia\Message\ListParam
Value object representing a message parameter that consists of a list of values.
Definition
ListParam.php:15
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition
MessageValue.php:21
MediaWiki\Rest\RequestInterface
A request interface similar to PSR-7's ServerRequestInterface.
Definition
RequestInterface.php:39
MediaWiki\Rest\Validator\BodyValidator
Interface for validating a request body.
Definition
BodyValidator.php:16
$request
$request
Definition
opensearch_desc.php:26
includes
Rest
Validator
JsonBodyValidator.php
Generated on
for MediaWiki by
1.18.0