public function testCanSetAndRetrieveErrors() { $errors = array('foo', 'bar'); $e = new ValidationException('Foo'); $e->setErrors($errors); $this->assertEquals($errors, $e->getErrors()); }
/** * Validate response model after processing * @throws ValidationException * @override */ protected function process() { parent::process(); if ($this[AbstractCommand::DISABLE_VALIDATION]) { // skipping validation in all cases return; } if (!$this->result instanceof Model) { // result is not a model - no way to validate return; } $errors = array(); $validator = SchemaValidator::getInstance(); // validate parameters present $schema = $this->result->getStructure(); $value = $this->result->toArray(); if (!$validator->validate($schema, $value)) { $errors = $validator->getErrors(); } // @todo validate additional parameters? // $schema->getAdditionalProperties() ); if ($errors) { $e = new ValidationException('Response failed model validation: ' . implode("\n", $errors)); $e->setErrors($errors); throw $e; } }
/** * Validate and prepare the command based on the schema and rules defined by the command's Operation object * * @throws ValidationException when validation errors occur */ protected function validate() { // Do not perform request validation/transformation if it is disable if ($this[self::DISABLE_VALIDATION]) { return; } $errors = array(); $validator = $this->getValidator(); foreach ($this->operation->getParams() as $name => $schema) { $value = $this[$name]; if (!$validator->validate($schema, $value)) { $errors = array_merge($errors, $validator->getErrors()); } elseif ($value !== $this[$name]) { // Update the config value if it changed and no validation errors were encountered $this->data[$name] = $value; } } // Validate additional parameters $hidden = $this[self::HIDDEN_PARAMS]; if ($properties = $this->operation->getAdditionalParameters()) { foreach ($this->toArray() as $name => $value) { // It's only additional if it isn't defined in the schema if (!$this->operation->hasParam($name) && !in_array($name, $hidden)) { // Always set the name so that error messages are useful $properties->setName($name); if (!$validator->validate($properties, $value)) { $errors = array_merge($errors, $validator->getErrors()); } elseif ($value !== $this[$name]) { $this->data[$name] = $value; } } } } if (!empty($errors)) { $e = new ValidationException('Validation errors: ' . implode("\n", $errors)); $e->setErrors($errors); throw $e; } }
/** * Validate and prepare the command based on the schema and rules defined by the command's Operation object * * @throws ValidationException when validation errors occur */ protected function validate() { // Do not perform request validation/transformation if it is disable if ($this->get(self::DISABLE_VALIDATION)) { return; } $errors = array(); $validator = $this->getValidator(); foreach ($this->operation->getParams() as $name => $schema) { $value = $this->get($name); if (!$validator->validate($schema, $value)) { $errors = array_merge($errors, $validator->getErrors()); } elseif ($value !== $this->get($name)) { // Update the config value if it changed and no validation errors were encountered $this->data[$name] = $value; } } if (!empty($errors)) { $e = new ValidationException('Validation errors: ' . implode("\n", $errors)); $e->setErrors($errors); throw $e; } }
protected function validate() { if ($this[self::DISABLE_VALIDATION]) { return; } $errors = array(); $validator = $this->getValidator(); foreach ($this->operation->getParams() as $name => $schema) { $value = $this[$name]; if (!$validator->validate($schema, $value)) { $errors = array_merge($errors, $validator->getErrors()); } elseif ($value !== $this[$name]) { $this->data[$name] = $value; } } $hidden = $this[self::HIDDEN_PARAMS]; if ($properties = $this->operation->getAdditionalParameters()) { foreach ($this->toArray() as $name => $value) { if (!$this->operation->hasParam($name) && !in_array($name, $hidden)) { $properties->setName($name); if (!$validator->validate($properties, $value)) { $errors = array_merge($errors, $validator->getErrors()); } elseif ($value !== $this[$name]) { $this->data[$name] = $value; } } } } if (!empty($errors)) { $e = new ValidationException('Validation errors: ' . implode("\n", $errors)); $e->setErrors($errors); throw $e; } }
/** * {@inheritdoc} * @throws ValidationException when validation errors occur */ public function validate(Collection $config, Inspector $inspector = null) { $inspector = $inspector ?: Inspector::getInstance(); $typeValidation = $inspector->getTypeValidation(); $errors = array(); foreach ($this->params as $name => $arg) { $currentValue = $config->get($name); $configValue = $arg->getValue($currentValue); // Inject configuration information into the config value if ($configValue && is_string($configValue)) { $configValue = $config->inject($configValue); } // Ensure that required arguments are set if ($arg->getRequired() && ($configValue === null || $configValue === '')) { $errors[] = 'Requires that the ' . $name . ' argument be supplied.' . ($arg->getDoc() ? ' (' . $arg->getDoc() . ').' : ''); continue; } // Ensure that the correct data type is being used if ($typeValidation && $configValue !== null && ($argType = $arg->getType())) { $validation = $inspector->validateConstraint($argType, $configValue, $arg->getTypeArgs()); if ($validation !== true) { $errors[] = $name . ': ' . $validation; $config->set($name, $configValue); continue; } } $configValue = $arg->filter($configValue); // Update the config value if it changed if (!$configValue !== $currentValue) { $config->set($name, $configValue); } // Check the length values if validating data $argMinLength = $arg->getMinLength(); if ($argMinLength && strlen($configValue) < $argMinLength) { $errors[] = 'Requires that the ' . $name . ' argument be >= ' . $arg->getMinLength() . ' characters.'; } $argMaxLength = $arg->getMaxLength(); if ($argMaxLength && strlen($configValue) > $argMaxLength) { $errors[] = 'Requires that the ' . $name . ' argument be <= ' . $arg->getMaxLength() . ' characters.'; } } if (!empty($errors)) { $e = new ValidationException('Validation errors: ' . implode("\n", $errors)); $e->setErrors($errors); throw $e; } }