MediaWiki master
MediaWiki\Rest\Validator\Validator Class Reference

Wrapper for ParamValidator. More...

Public Member Functions

 __construct (ObjectFactory $objectFactory, RequestInterface $request, Authority $authority)
 
 detectExtraneousBodyFields (array $paramSettings, array $parsedBody)
 Throw an HttpException if there are unexpected body fields.
 
 validateBody (RequestInterface $request, Handler $handler)
 Validate the body of a request.
 
 validateBodyParams (array $paramSettings, bool $enforceTypes=true)
 Validate body fields.
 
 validateParams (array $paramSettings)
 Validate parameters.
 

Static Public Member Functions

static getParameterSchema (array $paramSetting)
 Convert a param settings array into an OpenAPI schema structure.
 
static getParameterSpec (string $name, array $paramSetting)
 Convert a param settings array into an OpenAPI Parameter Object specification structure.
 
static getParameterTypeSchemas ()
 Returns JSON Schema description of all known parameter types.
 

Public Attributes

const KNOWN_PARAM_SOURCES = [ 'path', 'query', 'body', 'post' ]
 (array) ParamValidator array to specify the known sources of the parameter.
 
const PARAM_DESCRIPTION = 'rest-param-description'
 Parameter description to use in generated documentation.
 
const PARAM_SOURCE = 'rest-param-source'
 (string) ParamValidator constant for use as a key in a param settings array to specify the source of the parameter.
 

Detailed Description

Wrapper for ParamValidator.

It's intended to be used in the REST API classes by composition.

Since
1.34

Definition at line 37 of file Validator.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Rest\Validator\Validator::__construct ( ObjectFactory $objectFactory,
RequestInterface $request,
Authority $authority )
Parameters
ObjectFactory$objectFactory
RequestInterface$request
Authority$authority
Access: internal

Definition at line 117 of file Validator.php.

Member Function Documentation

◆ detectExtraneousBodyFields()

MediaWiki\Rest\Validator\Validator::detectExtraneousBodyFields ( array $paramSettings,
array $parsedBody )

Throw an HttpException if there are unexpected body fields.

Note that this will ignore all body fields if $paramSettings does not declare any body parameters, to avoid failures when clients send spurious data to handlers that do not support body validation at all. This behavior may change in the future.

Parameters
array$paramSettings
array$parsedBody
Exceptions
LocalizedHttpExceptionif there are unexpected body fields.

Definition at line 180 of file Validator.php.

References $source, and MediaWiki\Rest\Handler\PARAM_SOURCE.

Referenced by MediaWiki\Rest\Handler\detectExtraneousBodyFields().

◆ getParameterSchema()

static MediaWiki\Rest\Validator\Validator::getParameterSchema ( array $paramSetting)
static

Convert a param settings array into an OpenAPI schema structure.

See also
https://swagger.io/specification/#schema-object
Parameters
array$paramSetting
Returns
array

Definition at line 390 of file Validator.php.

◆ getParameterSpec()

static MediaWiki\Rest\Validator\Validator::getParameterSpec ( string $name,
array $paramSetting )
static

Convert a param settings array into an OpenAPI Parameter Object specification structure.

See also
https://swagger.io/specification/#parameter-object
Parameters
string$name
array$paramSetting
Returns
array

Definition at line 362 of file Validator.php.

Referenced by MediaWiki\Rest\Validator\JsonBodyValidator\getOpenAPISpec().

◆ getParameterTypeSchemas()

static MediaWiki\Rest\Validator\Validator::getParameterTypeSchemas ( )
static

Returns JSON Schema description of all known parameter types.

The name of the schema is the name of the parameter type with "-param" appended.

See also
https://swagger.io/specification/#schema-object
self::TYPE_DEFS
Returns
array

Definition at line 349 of file Validator.php.

◆ validateBody()

MediaWiki\Rest\Validator\Validator::validateBody ( RequestInterface $request,
Handler $handler )

Validate the body of a request.

This may return a data structure representing the parsed body. When used in the context of Handler::validateParams(), the returned value will be available to the handler via Handler::getValidatedBody().

Deprecated
since 1.43, use validateBodyParams instead.
Parameters
RequestInterface$request
Handler$handlerUsed to call {
See also
Handler::getBodyValidator}
Returns
mixed|null Return value from {
See also
BodyValidator::validateBody}
Exceptions
HttpExceptionon validation failure

Definition at line 286 of file Validator.php.

References MediaWiki\Rest\RequestInterface\getBody(), MediaWiki\Rest\Handler\getBodyValidator(), MediaWiki\Rest\RequestInterface\getHeaderLine(), MediaWiki\Rest\RequestInterface\getMethod(), and wfDeprecated().

Referenced by MediaWiki\Rest\Handler\validate().

◆ validateBodyParams()

MediaWiki\Rest\Validator\Validator::validateBodyParams ( array $paramSettings,
bool $enforceTypes = true )

Validate body fields.

Only params with the source specified as 'body' will be processed, use validateParams() for parameters coming from the path, from query, etc.

Since
1.42
See also
validateParams
validateBody
Parameters
array[]$paramSettingsParameter settings.
bool$enforceTypes$enforceTypes Whether the types of primitive values should be enforced. If set to false, parameters values are allowed to be strings.
Returns
array Validated parameters
Exceptions
HttpExceptionon validation failure

Definition at line 234 of file Validator.php.

References $source, Wikimedia\ParamValidator\ValidationException\getFailureMessage(), Wikimedia\ParamValidator\ValidationException\getParamName(), Wikimedia\ParamValidator\ValidationException\getParamValue(), Wikimedia\ParamValidator\TypeDef\OPT_ENFORCE_JSON_TYPES, and MediaWiki\Rest\Handler\PARAM_SOURCE.

Referenced by MediaWiki\Rest\Handler\validate().

◆ validateParams()

MediaWiki\Rest\Validator\Validator::validateParams ( array $paramSettings)

Validate parameters.

Params with the source specified as 'body' will be ignored. Use validateBodyParams() for these.

See also
validateBodyParams
Parameters
array[]$paramSettingsParameter settings
Returns
array Validated parameters
Exceptions
HttpExceptionon validation failure

Definition at line 141 of file Validator.php.

References $source, Wikimedia\ParamValidator\ValidationException\getFailureMessage(), Wikimedia\ParamValidator\ValidationException\getParamName(), Wikimedia\ParamValidator\ValidationException\getParamValue(), and MediaWiki\Rest\Handler\PARAM_SOURCE.

Referenced by MediaWiki\Rest\Handler\validate().

Member Data Documentation

◆ KNOWN_PARAM_SOURCES

const MediaWiki\Rest\Validator\Validator::KNOWN_PARAM_SOURCES = [ 'path', 'query', 'body', 'post' ]

(array) ParamValidator array to specify the known sources of the parameter.

'post' refers to application/x-www-form-urlencoded or multipart/form-data encoded parameters in the body of a POST request (in other words, parameters in PHP's $_POST). For other kinds of POST parameters, such as JSON fields, use BodyValidator instead of ParamValidator. This list must correspond to the switch statement in ParamValidatorCallbacks::getParamsFromSource.

Since
1.42

Definition at line 48 of file Validator.php.

◆ PARAM_DESCRIPTION

const MediaWiki\Rest\Validator\Validator::PARAM_DESCRIPTION = 'rest-param-description'

Parameter description to use in generated documentation.

Definition at line 60 of file Validator.php.

◆ PARAM_SOURCE

const MediaWiki\Rest\Validator\Validator::PARAM_SOURCE = 'rest-param-source'

(string) ParamValidator constant for use as a key in a param settings array to specify the source of the parameter.

Value must be one of the values in KNOWN_PARAM_SOURCES.

Definition at line 55 of file Validator.php.


The documentation for this class was generated from the following file: