Example #1
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;
     }
 }
 public function testRequiredMessageIncludesType()
 {
     $param = new Parameter(array('name' => 'test', 'type' => array('string', 'boolean'), 'required' => true));
     $value = null;
     $this->assertFalse($this->validator->validate($param, $value));
     $this->assertEquals(array('[test] is a required string or boolean'), $this->validator->getErrors());
 }
 public function testIntegersCastToStringWhenTypeMismatch()
 {
     $param = new Parameter(array('name' => 'test', 'type' => 'string'));
     $value = 12;
     $this->assertTrue($this->validator->validate($param, $value));
     $this->assertEquals('12', $value);
 }
 /**
  * Get the validator used to prepare and validate properties. If no validator has been set on the command, then
  * the default {@see SchemaValidator} will be used.
  *
  * @return ValidatorInterface
  */
 protected function getValidator()
 {
     if (!$this->validator) {
         $this->validator = SchemaValidator::getInstance();
     }
     return $this->validator;
 }