/** * Validates the given data value using the given data type. * * @param DataValue $dataValue * @param string $dataTypeId * * @return Result */ public function validateDataValue(DataValue $dataValue, $dataTypeId) { try { $dataValueType = $this->dataTypeFactory->getType($dataTypeId)->getDataValueType(); } catch (OutOfBoundsException $ex) { return Result::newError(array(Error::newError('Bad data type: ' . $dataTypeId, null, 'bad-data-type', array($dataTypeId)))); } if ($dataValue instanceof UnDeserializableValue) { return Result::newError(array(Error::newError('Bad snak value: ' . $dataValue->getReason(), null, 'bad-value', array($dataValue->getReason())))); } elseif ($dataValueType != $dataValue->getType()) { return Result::newError(array(Error::newError('Bad value type: ' . $dataValue->getType() . ', expected ' . $dataValueType, null, 'bad-value-type', array($dataValue->getType(), $dataValueType)))); } $result = Result::newSuccess(); //XXX: DataTypeValidatorFactory should expose only one validator, which would be a CompositeValidator foreach ($this->validatorFactory->getValidators($dataTypeId) as $validator) { $subResult = $validator->validate($dataValue); //XXX: Some validators should be fatal and cause us to abort the loop. // Others shouldn't. if (!$subResult->isValid()) { //TODO: Don't bail out immediately. Accumulate errors from all validators. // We need Result::merge() for this. return $subResult; } } return $result; }
protected function setUp() { parent::setUp(); $numericValidator = new TestValidator('/^\\d+$/'); $alphabeticValidator = new TestValidator('/^[A-Z]+$/i'); $lengthValidator = new TestValidator('/^.{1,10}$/'); $this->dataTypeFactory = new DataTypeFactory(array('numeric' => 'string', 'alphabetic' => 'string')); $p1 = new PropertyId('p1'); $p2 = new PropertyId('p2'); $this->propertyDataTypeLookup = new InMemoryDataTypeLookup(); $this->propertyDataTypeLookup->setDataTypeForProperty($p1, 'numeric'); $this->propertyDataTypeLookup->setDataTypeForProperty($p2, 'alphabetic'); $this->validatorFactory = $this->getMock('Wikibase\\Repo\\DataTypeValidatorFactory'); $this->validatorFactory->expects($this->any())->method('getValidators')->will($this->returnCallback(function ($dataTypeId) use($numericValidator, $alphabeticValidator, $lengthValidator) { return array($dataTypeId === 'numeric' ? $numericValidator : $alphabeticValidator, $lengthValidator); })); }
/** * @return ValueValidator */ private function getValidator() { $params = $this->extractRequestParams(); $name = $params['datatype']; if (empty($name)) { // 'datatype' parameter is required for validation. $this->errorReporter->dieError('No datatype given', 'param-illegal'); } // Note: For unknown datatype, we'll get an empty list. $validators = $this->dataTypeValidatorFactory->getValidators($name); return $this->wrapValidators($validators); }