MediaWiki  master
JsonBodyValidator.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use FormatJson;
10 
14 class JsonBodyValidator implements BodyValidator {
15 
20 
24  public function __construct( array $bodyParamSettings ) {
25  $this->bodyParamSettings = $bodyParamSettings;
26  }
27 
28  public function validateBody( RequestInterface $request ) {
29  $jsonStream = $request->getBody();
30  $status = FormatJson::parse( "$jsonStream", FormatJson::FORCE_ASSOC );
31 
32  if ( !$status->isOK() ) {
33  throw new LocalizedHttpException(
34  new MessageValue( 'rest-json-body-parse-error', [ "$status" ] ),
35  400
36  );
37  }
38 
39  $data = $status->value;
40 
41  if ( !is_array( $data ) ) {
42  throw new LocalizedHttpException( new MessageValue( 'rest-bad-json-body' ), 400 );
43  }
44 
45  foreach ( $this->bodyParamSettings as $name => $settings ) {
46  if ( !empty( $settings[ParamValidator::PARAM_REQUIRED] ) && !isset( $data[$name] ) ) {
47  throw new LocalizedHttpException(
48  new MessageValue( 'rest-missing-body-field', [ $name ] ), 400
49  );
50  }
51 
52  if ( !isset( $data[$name] ) ) {
53  $data[$name] = $settings[ParamValidator::PARAM_DEFAULT] ?? null;
54  }
55 
56  // TODO: use a ParamValidator to check field value, etc!
57  }
58 
59  return $data;
60  }
61 
62 }
MediaWiki\Rest\Validator\JsonBodyValidator
Do-nothing body validator.
Definition: JsonBodyValidator.php:14
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
MediaWiki\Rest\Validator\BodyValidator
Interface for validating a request body.
Definition: BodyValidator.php:13
MediaWiki\Rest\RequestInterface\getBody
getBody()
Gets the body of the message.
FormatJson
JSON formatter wrapper class.
Definition: FormatJson.php:26
Wikimedia\ParamValidator\ParamValidator\PARAM_REQUIRED
const PARAM_REQUIRED
(bool) Indicate that the parameter is required.
Definition: ParamValidator.php:84
MediaWiki\Rest\Validator\JsonBodyValidator\__construct
__construct(array $bodyParamSettings)
Definition: JsonBodyValidator.php:24
FormatJson\FORCE_ASSOC
const FORCE_ASSOC
If set, treat JSON objects '{...}' as associative arrays.
Definition: FormatJson.php:63
MediaWiki\Rest\Validator\JsonBodyValidator\validateBody
validateBody(RequestInterface $request)
Validate the body of a request.
Definition: JsonBodyValidator.php:28
MediaWiki\Rest\RequestInterface
A request interface similar to PSR-7's ServerRequestInterface.
Definition: RequestInterface.php:39
FormatJson\parse
static parse( $value, $options=0)
Decodes a JSON string.
Definition: FormatJson.php:188
MediaWiki\Rest\Validator
Definition: BodyValidator.php:3
MediaWiki\Rest\Validator\JsonBodyValidator\$bodyParamSettings
array[] $bodyParamSettings
Definition: JsonBodyValidator.php:19
Wikimedia\ParamValidator\ParamValidator\PARAM_DEFAULT
const PARAM_DEFAULT
(mixed) Default value of the parameter.
Definition: ParamValidator.php:68
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42
MediaWiki\Rest\LocalizedHttpException
@newable
Definition: LocalizedHttpException.php:10