wiki.techinc.nl/includes/libs/ParamValidator
Translation updater bot e1b9bb63f2
Localisation updates from https://translatewiki.net.
Change-Id: I79b13b07747b9e7f28af709e0adab7e2ff571dad
2025-10-07 07:33:48 +02:00
..
i18n Localisation updates from https://translatewiki.net. 2025-10-07 07:33:48 +02:00
TypeDef Merge "libs: Add missing documentation to class properties" 2024-09-14 02:38:58 +00:00
Util
Callbacks.php
ParamValidator.php Use const keyword for constant list of strings or ints 2024-09-11 23:16:24 +02:00
README.md
SimpleCallbacks.php
TypeDef.php Use explicit nullable type on parameter arguments 2024-10-16 20:58:33 +02:00
ValidationException.php Use explicit nullable type on parameter arguments 2024-10-16 20:58:33 +02: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 */
$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