MediaWiki master
|
Base definition for ParamValidator types. More...
Inherited by MediaWiki\ParamValidator\TypeDef\ArrayDef, MediaWiki\ParamValidator\TypeDef\TitleDef, MediaWiki\ParamValidator\TypeDef\UserDef, Wikimedia\ParamValidator\TypeDef\BooleanDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\ExpiryDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, and Wikimedia\ParamValidator\TypeDef\UploadDef.
Public Member Functions | |
__construct (Callbacks $callbacks) | |
checkSettings (string $name, $settings, array $options, array $ret) | |
Validate a parameter settings array. | |
getEnumValues ( $name, array $settings, array $options) | |
Get the values for enum-like parameters. | |
getHelpInfo ( $name, array $settings, array $options) | |
Describe parameter settings in human-readable format. | |
getParamInfo ( $name, array $settings, array $options) | |
Describe parameter settings in a machine-readable format. | |
getValue ( $name, array $settings, array $options) | |
Get the value from the request. | |
normalizeSettings (array $settings) | |
Normalize a settings array. | |
stringifyValue ( $name, $value, array $settings, array $options) | |
Convert a value to a string representation. | |
supportsArrays () | |
Whether the value may be an array. | |
validate ( $name, $value, array $settings, array $options) | |
Validate the value. | |
Public Attributes | |
const | OPT_ENFORCE_JSON_TYPES = 'enforce-json-types' |
Option that instructs TypeDefs to enforce the native type of parameter values, instead of allowing string values as input. | |
const | OPT_LOG_BAD_TYPES = 'log-bad-types' |
Protected Member Functions | |
failIfNotString (string $name, $value, array $settings, array $options) | |
Fails if $value is not a string. | |
failure ( $failure, $name, $value, array $settings, array $options, $fatal=true) | |
Record a failure message. | |
failureMessage ( $code, ?array $data=null, $suffix=null) | |
Create a DataMessageValue representing a failure. | |
fatal ( $failure, $name, $value, array $settings, array $options) | |
Throw a ValidationException. | |
Protected Attributes | |
Callbacks | $callbacks |
Base definition for ParamValidator types.
Most methods in this class accept an "options array". This is just the $options
passed to ParamValidator::getValue(), ParamValidator::validateValue(), and the like and is intended for communication of non-global state to the Callbacks.
Definition at line 19 of file TypeDef.php.
Wikimedia\ParamValidator\TypeDef::__construct | ( | Callbacks | $callbacks | ) |
Wikimedia\ParamValidator\TypeDef::checkSettings | ( | string | $name, |
$settings, | |||
array | $options, | ||
array | $ret ) |
Validate a parameter settings array.
This is intended for validation of parameter settings during unit or integration testing, and should implement strict checks.
The rest of the code should generally be more permissive.
string | $name | Parameter name |
array | mixed | $settings | Default value or an array of settings using PARAM_* constants. |
array | $options | Options array, passed through to the TypeDef and Callbacks. |
array | $ret |
|
Reimplemented in MediaWiki\Api\Validator\SubmoduleDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\ExpiryDef, Wikimedia\ParamValidator\TypeDef\LimitDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\PasswordDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\UploadDef, MediaWiki\ParamValidator\TypeDef\NamespaceDef, MediaWiki\ParamValidator\TypeDef\TitleDef, and MediaWiki\ParamValidator\TypeDef\UserDef.
Definition at line 239 of file TypeDef.php.
|
protected |
Fails if $value is not a string.
string | $name | Parameter name being validated. |
mixed | $value | Value being validated. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
Definition at line 68 of file TypeDef.php.
References Wikimedia\ParamValidator\TypeDef\failureMessage(), and Wikimedia\ParamValidator\TypeDef\fatal().
Referenced by Wikimedia\ParamValidator\TypeDef\ExpiryDef\validate(), Wikimedia\ParamValidator\TypeDef\StringDef\validate(), MediaWiki\ParamValidator\TypeDef\TagsDef\validate(), MediaWiki\ParamValidator\TypeDef\TitleDef\validate(), and MediaWiki\ParamValidator\TypeDef\UserDef\validate().
|
protected |
Record a failure message.
Depending on $fatal
, this will either throw a ValidationException or call $this->callbacks->recordCondition().
Note that parameters for $name
and $value
are always added as $1
and $2
.
DataMessageValue | string | $failure | Failure code or message. |
string | $name | Parameter name being validated. |
mixed | $value | Value being validated. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
bool | $fatal | Whether the failure is fatal |
Definition at line 121 of file TypeDef.php.
Referenced by Wikimedia\ParamValidator\TypeDef\NumericDef\checkRange(), Wikimedia\ParamValidator\TypeDef\EnumDef\validate(), Wikimedia\ParamValidator\TypeDef\ExpiryDef\validate(), Wikimedia\ParamValidator\TypeDef\IntegerDef\validate(), Wikimedia\ParamValidator\TypeDef\StringDef\validate(), Wikimedia\ParamValidator\TypeDef\TimestampDef\validate(), Wikimedia\ParamValidator\TypeDef\UploadDef\validate(), MediaWiki\ParamValidator\TypeDef\ArrayDef\validate(), MediaWiki\ParamValidator\TypeDef\TitleDef\validate(), and MediaWiki\ParamValidator\TypeDef\UserDef\validate().
|
protected |
Create a DataMessageValue representing a failure.
The message key will be "paramvalidator-$code" or "paramvalidator-$code-$suffix".
Use DataMessageValue's param mutators to add additional MessageParams. Note that failure()
will prepend parameters for $name
and $value
.
string | $code | Failure code. |
array | null | $data | Failure data. |
string | null | $suffix | Suffix to append when producing the message key |
Definition at line 156 of file TypeDef.php.
Referenced by Wikimedia\ParamValidator\TypeDef\NumericDef\checkRange(), Wikimedia\ParamValidator\TypeDef\failIfNotString(), Wikimedia\ParamValidator\TypeDef\BooleanDef\validate(), Wikimedia\ParamValidator\TypeDef\EnumDef\validate(), Wikimedia\ParamValidator\TypeDef\FloatDef\validate(), Wikimedia\ParamValidator\TypeDef\IntegerDef\validate(), Wikimedia\ParamValidator\TypeDef\StringDef\validate(), Wikimedia\ParamValidator\TypeDef\TimestampDef\validate(), and Wikimedia\ParamValidator\TypeDef\UploadDef\validate().
|
protected |
Throw a ValidationException.
This is a wrapper for failure() which explicitly declares that it never returns, which is useful to static analysis tools like Phan.
Note that parameters for $name
and $value
are always added as $1
and $2
.
DataMessageValue | string | $failure | Failure code or message. |
string | $name | Parameter name being validated. |
mixed | $value | Value being validated. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
ValidationException | always |
Definition at line 99 of file TypeDef.php.
Referenced by Wikimedia\ParamValidator\TypeDef\failIfNotString(), Wikimedia\ParamValidator\TypeDef\BooleanDef\validate(), Wikimedia\ParamValidator\TypeDef\FloatDef\validate(), and Wikimedia\ParamValidator\TypeDef\IntegerDef\validate().
Wikimedia\ParamValidator\TypeDef::getEnumValues | ( | $name, | |
array | $settings, | ||
array | $options ) |
Get the values for enum-like parameters.
This is primarily intended for documentation and implementation of PARAM_ALL; it is the responsibility of the TypeDef to ensure that validate() accepts the values returned here.
string | $name | Parameter name being validated. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
Reimplemented in MediaWiki\Api\Validator\SubmoduleDef, Wikimedia\ParamValidator\TypeDef\EnumDef, MediaWiki\ParamValidator\TypeDef\NamespaceDef, and MediaWiki\ParamValidator\TypeDef\TagsDef.
Definition at line 257 of file TypeDef.php.
Referenced by Wikimedia\ParamValidator\TypeDef\EnumDef\getParamInfo().
Wikimedia\ParamValidator\TypeDef::getHelpInfo | ( | $name, | |
array | $settings, | ||
array | $options ) |
Describe parameter settings in human-readable format.
Keys in the returned array should generally correspond to PARAM constants.
If relevant, a MessageValue describing the type itself should be returned with key ParamValidator::PARAM_TYPE.
The default messages for other ParamValidator-defined PARAM constants may be suppressed by returning null as the value for those constants, or replaced by returning a replacement MessageValue. Normally, however, the default messages should not be changed.
MessageValues describing any other constraints applied via PARAM constants specific to this class should also be returned.
string | $name | Parameter name being described. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
Reimplemented in Wikimedia\ParamValidator\TypeDef\BooleanDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\ExpiryDef, Wikimedia\ParamValidator\TypeDef\FloatDef, Wikimedia\ParamValidator\TypeDef\IntegerDef, Wikimedia\ParamValidator\TypeDef\LimitDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\UploadDef, MediaWiki\ParamValidator\TypeDef\TitleDef, and MediaWiki\ParamValidator\TypeDef\UserDef.
Definition at line 326 of file TypeDef.php.
Wikimedia\ParamValidator\TypeDef::getParamInfo | ( | $name, | |
array | $settings, | ||
array | $options ) |
Describe parameter settings in a machine-readable format.
Keys should be short strings using lowercase ASCII letters. Values should generally be values that could be encoded in JSON or the like.
This is intended to handle PARAM constants specific to this class. It generally shouldn't handle constants defined on ParamValidator itself.
string | $name | Parameter name. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
Reimplemented in MediaWiki\Api\Validator\SubmoduleDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, Wikimedia\ParamValidator\TypeDef\StringDef, MediaWiki\ParamValidator\TypeDef\NamespaceDef, MediaWiki\ParamValidator\TypeDef\TitleDef, and MediaWiki\ParamValidator\TypeDef\UserDef.
Definition at line 299 of file TypeDef.php.
Wikimedia\ParamValidator\TypeDef::getValue | ( | $name, | |
array | $settings, | ||
array | $options ) |
Get the value from the request.
string | $name | Parameter name being fetched. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
Reimplemented in Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, and Wikimedia\ParamValidator\TypeDef\UploadDef.
Definition at line 179 of file TypeDef.php.
Wikimedia\ParamValidator\TypeDef::normalizeSettings | ( | array | $settings | ) |
Normalize a settings array.
array | $settings |
Reimplemented in Wikimedia\ParamValidator\TypeDef\LimitDef, Wikimedia\ParamValidator\TypeDef\NumericDef, Wikimedia\ParamValidator\TypeDef\PasswordDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, MediaWiki\ParamValidator\TypeDef\NamespaceDef, and MediaWiki\ParamValidator\TypeDef\UserDef.
Definition at line 211 of file TypeDef.php.
Wikimedia\ParamValidator\TypeDef::stringifyValue | ( | $name, | |
$value, | |||
array | $settings, | ||
array | $options ) |
Convert a value to a string representation.
This is intended as the inverse of getValue() and validate(): this should accept anything returned by those methods or expected to be used as PARAM_DEFAULT, and if the string from this method is passed in as client input or PARAM_DEFAULT it should give equivalent output from validate().
string | $name | Parameter name being converted. |
mixed | $value | Parameter value being converted. Do not pass null. |
array | $settings | Parameter settings array. |
array | $options | Options array. |
Reimplemented in Wikimedia\ParamValidator\TypeDef\BooleanDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\FloatDef, Wikimedia\ParamValidator\TypeDef\IntegerDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\UploadDef, MediaWiki\ParamValidator\TypeDef\ArrayDef, and MediaWiki\ParamValidator\TypeDef\TitleDef.
Definition at line 276 of file TypeDef.php.
Wikimedia\ParamValidator\TypeDef::supportsArrays | ( | ) |
Whether the value may be an array.
Note that this is different from multi-value. This should only return true if each value can be an array.
Reimplemented in MediaWiki\ParamValidator\TypeDef\ArrayDef.
Definition at line 54 of file TypeDef.php.
|
abstract |
Validate the value.
When ParamValidator is processing a multi-valued parameter, this will be called once for each of the supplied values. Which may mean zero calls.
When getValue() returned null, this will not be called.
string | $name | Parameter name being validated. |
mixed | $value | Value to validate, from getValue(). |
array | $settings | Parameter settings array. |
array | $options | Options array. Note the following values that may be set by ParamValidator:
|
ValidationException | if the value is invalid |
Reimplemented in Wikimedia\ParamValidator\TypeDef\BooleanDef, Wikimedia\ParamValidator\TypeDef\EnumDef, Wikimedia\ParamValidator\TypeDef\ExpiryDef, Wikimedia\ParamValidator\TypeDef\FloatDef, Wikimedia\ParamValidator\TypeDef\IntegerDef, Wikimedia\ParamValidator\TypeDef\LimitDef, Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef, Wikimedia\ParamValidator\TypeDef\StringDef, Wikimedia\ParamValidator\TypeDef\TimestampDef, Wikimedia\ParamValidator\TypeDef\UploadDef, MediaWiki\ParamValidator\TypeDef\ArrayDef, MediaWiki\ParamValidator\TypeDef\NamespaceDef, MediaWiki\ParamValidator\TypeDef\TagsDef, MediaWiki\ParamValidator\TypeDef\TitleDef, and MediaWiki\ParamValidator\TypeDef\UserDef.
|
protected |
Definition at line 35 of file TypeDef.php.
Referenced by Wikimedia\ParamValidator\TypeDef\StringDef\__construct(), Wikimedia\ParamValidator\TypeDef\TimestampDef\__construct(), MediaWiki\ParamValidator\TypeDef\TagsDef\__construct(), MediaWiki\ParamValidator\TypeDef\NamespaceDef\__construct(), MediaWiki\ParamValidator\TypeDef\TitleDef\__construct(), and MediaWiki\ParamValidator\TypeDef\UserDef\__construct().
const Wikimedia\ParamValidator\TypeDef::OPT_ENFORCE_JSON_TYPES = 'enforce-json-types' |
Option that instructs TypeDefs to enforce the native type of parameter values, instead of allowing string values as input.
This is intended for use with values coming from a JSON request body, and may accommodate for differences between the type system of PHP and JSON.
Definition at line 32 of file TypeDef.php.
Referenced by MediaWiki\Rest\Validator\Validator\validateBodyParams().
const Wikimedia\ParamValidator\TypeDef::OPT_LOG_BAD_TYPES = 'log-bad-types' |
Definition at line 24 of file TypeDef.php.