21 private $bodyParamSettings;
27 $this->bodyParamSettings = $bodyParamSettings;
35 $jsonStream = $request->
getBody();
36 $status = FormatJson::parse(
"$jsonStream", FormatJson::FORCE_ASSOC );
38 if ( !$status->isOK() ) {
40 new MessageValue(
'rest-json-body-parse-error', [
"$status" ] ),
45 $data = $status->value;
47 if ( !is_array( $data ) ) {
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] ) ) {
55 new MessageValue(
'rest-missing-body-field', [ $name ] ), 400
59 if ( !isset( $data[$name] ) ) {
60 $data[$name] = $settings[ParamValidator::PARAM_DEFAULT] ??
null;
63 unset( $uncheckedBodyKeys[$name] );
66 if ( $uncheckedBodyKeys ) {
69 'rest-extraneous-body-fields',
70 [
new ListParam( ListType::COMMA, array_keys( $uncheckedBodyKeys ) ) ]
95 foreach ( $this->bodyParamSettings as $name => $paramSetting ) {
101 $body[
'properties'][$name] = $param[
'schema'];
103 if ( isset( $param[
'description'] ) ) {
104 $body[
'properties'][$name][
'description'] = $param[
'description'];
107 if ( $param[
'required'] ??
false ) {
108 $required[] = $param[
'name'];
113 $body[
'required'] = $required;
Do-nothing body validator.
__construct(array $bodyParamSettings)
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.
Interface for validating a request body.