/** * @test */ public function isValidIfExtraItemsPresentAndValidate() { $validator = new Type(); $validator->value = 'number'; $this->validator->setSpecification(['items' => [$validator, new NotEmpty()], 'additionalItems' => $validator]); $this->assertTrue($this->validator->validate([2.3, '', 0])); }
/** * @test */ public function failWithPatternPropertiesAndPropertiesWithInvalidValuesAndExtraParamsValidated() { $validator = new Type(); $validator->value = 'number'; $this->validator->setSpecification(['patternProperties' => ['/a/' => $validator], 'properties' => ['otherLetters' => $validator], 'additionalProperties' => $validator]); $this->assertFalse($this->validator->validate(['a' => 1, 'hasAnA' => 2, 'otherLetters' => 3.4, 'plus' => 'fail'])); }
/** * @test */ public function isNotValidIfItemMissingInObject() { $obj = new \stdClass(); $obj->a = 1; $obj->c = 3; $this->validator->setSpecification(['a', 'b']); $this->assertFalse($this->validator->validate($obj)); }
/** * {@inheritdoc} * * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() */ public function validate($value) { return !$this->validator->validate($value); }
/** * Validates an array against a specific validator. * * @param array $array * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator */ private function validateArray(array $array, ValidatorInterface $validator) { foreach ($array as $item) { if (!$validator->validate($item)) { return false; } } return true; }
/** * @param array $value * @param array $rest * @param string $pattern * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator * * @return bool */ private function validateByPatternUsingValidator(array $value, array &$rest, $pattern, ValidatorInterface $validator) { foreach ($value as $propertyName => $propertyValue) { if (preg_match($pattern, $propertyName)) { unset($rest[$propertyName]); if (!$validator->validate($propertyValue)) { return false; } } } return true; }