wiki.techinc.nl/includes/libs/ParamValidator
Brad Jorsch a04633f678 ParamValidator: Default PresenceBooleanDef to false rather than implicitly null
PresenceBooleanDef is causing ParamValidator::getValue() to return null,
while historical Action API behavior had returned false instead.

While it generally shouldn't make a difference since PHP considers both
falsey, and good arguments could be made either way, we can restore the
historical behavior easily enough by having normalizeSettings()
default PARAM_DEFAULT to false.

Bug: T244440
Change-Id: Iee1d8e5753407674adc3f7384989841bc9b44c54
2020-02-06 09:44:06 -05:00
..
i18n ParamValidator: Adjust message usage 2020-01-14 17:24:20 -05:00
TypeDef ParamValidator: Default PresenceBooleanDef to false rather than implicitly null 2020-02-06 09:44:06 -05:00
Util ParamValidator: Flag as unstable for 1.34 2019-11-01 13:52:00 -04:00
Callbacks.php ParamValidator: Use MessageValue! 2019-11-01 15:49:31 -04:00
ParamValidator.php Move some validation logic from ApiStructureTest to ParamValidator 2020-02-04 20:29:35 +00:00
README.md ParamValidator: Use MessageValue! 2019-11-01 15:49:31 -04:00
SimpleCallbacks.php ParamValidator: Use MessageValue! 2019-11-01 15:49:31 -04:00
TypeDef.php Move some validation logic from ApiStructureTest to ParamValidator 2020-02-04 20:29:35 +00:00
ValidationException.php ParamValidator: Use MessageValue! 2019-11-01 15:49:31 -04:00

Wikimedia API Parameter Validator

This library implements a system for processing and validating parameters to an API from data like that in PHP's $_GET, $_POST, and $_FILES arrays, based on a declarative definition of available parameters.

Usage

use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
use Wikimedia\ParamValidator\SimpleCallbacks as ParamValidatorCallbacks;
use Wikimedia\ParamValidator\ValidationException;

// We assume these are available from your environment in some manner.
/* @var Wikimedia\ObjectFactory $objectFactory */
$objectFactory = ...;
/* @var Wikimedia\Message\MessageFormatterFactory $messageFormatterFactory */
$messageFormatterFactory = ...;

$validator = new ParamValidator(
	new ParamValidatorCallbacks( $_POST + $_GET, $_FILES ),
	$objectFactory
);

try {
	$intValue = $validator->getValue( 'intParam', [
			ParamValidator::PARAM_TYPE => 'integer',
			ParamValidator::PARAM_DEFAULT => 0,
			IntegerDef::PARAM_MIN => 0,
			IntegerDef::PARAM_MAX => 5,
	] );
} catch ( ValidationException $ex ) {
	$error = $messageFormatterFactory->getTextFormatter( 'en' )->format( $ex->getFailureMessage() );
	echo "Validation error: $error\n";
}

Failure reporting

This library uses Wikimedia\Message\DataMessageValue objects to report errors in both human-readable and machine-readable formats. Basic i18n for all errors generated by the library is included.

For possible failure codes and their parameters, see the documentation of the relevant PARAM_* constants and TypeDef classes.

Running tests

composer install --prefer-dist
composer test