Fix Between validator behavior
return false when validating a string with a numeric range
return false when validating a numeric value with a string range
MODIFY
src/Between.php
src/Between.php
@@ -17,6 +17,13 @@ class Between extends AbstractValidator
const NOT_BETWEEN = 'notBetween';
const NOT_BETWEEN_STRICT = 'notBetweenStrict';
+ /**
+ * Retain if min and max are numeric values. Allow to not compare string and numeric types
+ *
+ * @var boolean
+ */
+ private $numeric;
+
/**
* Validation failure message template definitions
*
@@ -81,7 +88,17 @@ class Between extends AbstractValidator
if (count($options) !== 2
&& (! array_key_exists('min', $options) || ! array_key_exists('max', $options))
) {
- throw new Exception\InvalidArgumentException("Missing option. 'min' and 'max' have to be given");
+ throw new Exception\InvalidArgumentException("Missing option : 'min' and 'max' have to be given");
+ }
+
+ if (is_numeric($options['min']) && is_numeric($options['max'])) {
+ $this->numeric = true;
+ } elseif (is_string($options['min']) && is_string($options['max'])) {
+ $this->numeric = false;
+ } else {
+ throw new Exception\InvalidArgumentException(
+ "Invalid options : 'min' and 'max' should be of the same scalar type"
+ );
}
parent::__construct($options);
@@ -162,6 +179,13 @@ class Between extends AbstractValidator
*/
public function isValid($value)
{
+ if ($this->numeric && ! is_numeric($value)) {
+ return false;
+ }
+ if (! $this->numeric && ! is_string($value)) {
+ return false;
+ }
+
$this->setValue($value);
if ($this->getInclusive()) {

MODIFY
test/BetweenTest.php
test/BetweenTest.php
@@ -18,39 +18,78 @@ use Zend\Validator\Exception\InvalidArgumentException;
*/
class BetweenTest extends TestCase
{
+ public function providerBasic()
+ {
+ return [
+ [
+ 'min' => 1,
+ 'max' => 100,
+ 'inclusive' => true,
+ 'expected' => true,
+ 'values' => [1, 10, 100],
+ ],
+ [
+ 'min' => 1,
+ 'max' => 100,
+ 'inclusive' => true,
+ 'expected' => false,
+ 'values' => [0, 0.99, 100.01, 101],
+ ],
+ [
+ 'min' => 1,
+ 'max' => 100,
+ 'inclusive' => false,
+ 'expected' => false,
+ 'values' => [0, 1, 100, 101],
+ ],
+ [
+ 'min' => 'a',
+ 'max' => 'z',
+ 'inclusive' => true,
+ 'expected' => true,
+ 'values' => ['a', 'b', 'y', 'z'],
+ ],
+ [
+ 'min' => 'a',
+ 'max' => 'z',
+ 'inclusive' => false,
+ 'expected' => false,
+ 'values' => ['!', 'a', 'z'],
+ ],
+ [
+ 'min' => 0,
+ 'max' => 99999999,
+ 'inclusive' => true,
+ 'expected' => false,
+ 'values' => ['asdasd', 'q'],
+ ],
+ [
+ 'min' => 'a',
+ 'max' => 'zzzzz',
+ 'inclusive' => true,
+ 'expected' => false,
+ 'values' => [0, 10, 548],
+ ],
+ ];
+ }
/**
* Ensures that the validator follows expected behavior
*
+ * @dataProvider providerBasic
* @return void
*/
- public function testBasic()
+ public function testBasic($min, $max, $inclusive, $expected, $values)
{
- /**
- * The elements of each array are, in order:
- * - minimum
- * - maximum
- * - inclusive
- * - expected validation result
- * - array of test input values
- */
- $valuesExpected = [
- [1, 100, true, true, [1, 10, 100]],
- [1, 100, true, false, [0, 0.99, 100.01, 101]],
- [1, 100, false, false, [0, 1, 100, 101]],
- ['a', 'z', true, true, ['a', 'b', 'y', 'z']],
- ['a', 'z', false, false, ['!', 'a', 'z']]
- ];
- foreach ($valuesExpected as $element) {
- $validator = new Between(['min' => $element[0], 'max' => $element[1], 'inclusive' => $element[2]]);
- foreach ($element[4] as $input) {
+ $validator = new Between(['min' => $min, 'max' => $max, 'inclusive' => $inclusive]);
+
+ foreach ($values as $input) {
$this->assertEquals(
- $element[3],
+ $expected,
$validator->isValid($input),
'Failed values: ' . $input . ":" . implode("\n", $validator->getMessages())
);
}
}
- }
/**
* Ensures that getMessages() returns expected default value
@@ -117,7 +156,7 @@ class BetweenTest extends TestCase
public function testMissingMinOrMax(array $args)
{
$this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage("Missing option. 'min' and 'max' have to be given");
+ $this->expectExceptionMessage("Missing option : 'min' and 'max' have to be given");
new Between($args);
}

Fix Between validator behavior

return false when validating a string with a numeric range
return false when validating a numeric value with a string range