public function testCanSetAndRetrieveErrors()
 {
     $errors = array('foo', 'bar');
     $e = new ValidationException('Foo');
     $e->setErrors($errors);
     $this->assertEquals($errors, $e->getErrors());
 }
Ejemplo n.º 2
0
 /**
  * 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;
     }
 }
 /**
  * {@inheritdoc}
  * @todo Needs to recurse over nested errors
  */
 public function bind(FormInterface $form, ValidationException $validationException)
 {
     // Attach exception message to root form
     // todo: general error message is non user-friendly, removing for now
     //$form->addError(new FormError($validationException->getMessage()));
     $errors = $validationException->getErrors();
     // Iterate over errors and bind to form
     foreach ($errors as $field => $message) {
         if ($form->get(self::STEP_FORM_NAME)->has($field)) {
             if (is_array($message)) {
                 foreach ($message as $subField => $subMessage) {
                     if ($form->get(self::STEP_FORM_NAME)->get($field)->has($subField)) {
                         $form->get(self::STEP_FORM_NAME)->get($field)->get($subField)->addError(new FormError($subMessage));
                     }
                 }
             } else {
                 $form->get(self::STEP_FORM_NAME)->get($field)->addError(new FormError($message));
             }
             unset($errors[$field]);
         } elseif (is_array($message)) {
             foreach ($message as $key => $error) {
                 if (is_numeric($key)) {
                     if ($form->get(self::STEP_FORM_NAME)->has($field)) {
                         if ($form->get(self::STEP_FORM_NAME)->get($field)->has($key)) {
                             $this->recurseErrors($form->get(self::STEP_FORM_NAME)->get($field)->get($key), $message);
                         }
                     }
                 }
             }
         } elseif (preg_match('/^\\[([a-z0-9_]+)\\].*$/i', $message, $matches)) {
             if (isset($matches[1]) && $form->get(self::STEP_FORM_NAME)->has($matches[1])) {
                 $form->get(self::STEP_FORM_NAME)->get($matches[1])->addError(new FormError($message));
                 unset($errors[$field]);
             }
         }
     }
     // Iterate over errors and bind remaining errors to form
     foreach ($errors as $message) {
         if (is_string($message)) {
             $form->get(self::STEP_FORM_NAME)->addError(new FormError($message));
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * 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;
     }
 }
Ejemplo n.º 6
0
 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;
     }
 }
Ejemplo n.º 7
0
 /**
  * {@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;
     }
 }