MediaWiki master
JsonBodyValidator.php
Go to the documentation of this file.
1<?php
2
4
5use FormatJson;
12
17
21 private $bodyParamSettings;
22
26 public function __construct( array $bodyParamSettings ) {
27 $this->bodyParamSettings = $bodyParamSettings;
28 }
29
34 public function validateBody( RequestInterface $request ) {
35 $jsonStream = $request->getBody();
36 $status = FormatJson::parse( "$jsonStream", FormatJson::FORCE_ASSOC );
37
38 if ( !$status->isOK() ) {
39 throw new LocalizedHttpException(
40 new MessageValue( 'rest-json-body-parse-error', [ "$status" ] ),
41 400
42 );
43 }
44
45 $data = $status->value;
46
47 if ( !is_array( $data ) ) {
48 throw new LocalizedHttpException( new MessageValue( 'rest-bad-json-body' ), 400 );
49 }
50
51 $uncheckedBodyKeys = array_fill_keys( array_keys( $data ), true );
52 foreach ( $this->bodyParamSettings as $name => $settings ) {
53 if ( !empty( $settings[ParamValidator::PARAM_REQUIRED] ) && !isset( $data[$name] ) ) {
54 throw new LocalizedHttpException(
55 new MessageValue( 'rest-missing-body-field', [ $name ] ), 400
56 );
57 }
58
59 if ( !isset( $data[$name] ) ) {
60 $data[$name] = $settings[ParamValidator::PARAM_DEFAULT] ?? null;
61 }
62
63 unset( $uncheckedBodyKeys[$name] );
64 // TODO: use a ParamValidator to check field value, etc!
65 }
66 if ( $uncheckedBodyKeys ) {
67 throw new LocalizedHttpException(
68 new MessageValue(
69 'rest-extraneous-body-fields',
70 [ new ListParam( ListType::COMMA, array_keys( $uncheckedBodyKeys ) ) ]
71 ),
72 400
73 );
74 }
75
76 return $data;
77 }
78
88 public function getOpenAPISpec(): array {
89 $body = [];
90 $required = [];
91
92 // XXX: Maybe we want to be able to define a spec file in the route definition?
93 // NOTE: the route definition may not be loaded when this is called before init()!
94
95 foreach ( $this->bodyParamSettings as $name => $paramSetting ) {
97 $name,
98 $paramSetting
99 );
100
101 $body['properties'][$name] = $param['schema'];
102
103 if ( isset( $param['description'] ) ) {
104 $body['properties'][$name]['description'] = $param['description'];
105 }
106
107 if ( $param['required'] ?? false ) {
108 $required[] = $param['name'];
109 }
110 }
111
112 if ( $required ) {
113 $body['required'] = $required;
114 }
115
116 return $body;
117 }
118}
JSON formatter wrapper class.
validateBody(RequestInterface $request)
Validate the body of a request.This may return a data structure representing the parsed body....
getOpenAPISpec()
Returns an OpenAPI Schema Object specification structure as an associative array.
static getParameterSpec(string $name, array $paramSetting)
Convert a param settings array into an OpenAPI Parameter Object specification structure.
Value object representing a message parameter that consists of a list of values.
Definition ListParam.php:12
The constants used to specify list types.
Definition ListType.php:9
Value object representing a message for i18n.
Service for formatting and validating API parameters.
A request interface similar to PSR-7's ServerRequestInterface.
getBody()
Gets the body of the message.
Interface for validating a request body.