diff --git a/CHANGELOG.md b/CHANGELOG.md index 7487d3ea9..43685ca46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,13 @@ All notable changes to this project will be documented in this file, in reverse zend-session requirement (during development, and in the suggestions) to 2.8+, to ensure compatibility with the upcoming PHP 7.2 release. +- [#187](https://github.com/zendframework/zend-validator/pull/187) updates the + `Between` validator to **require** that both a `min` and a `max` value are + provided to the constructor, and that both are of the same type (both + integer/float values and/or both string values). This fixes issues that could + previously occur when one or the other was not set, but means an exception + will now be raised during instantiation (versus runtime during `isValid()`). + ### Deprecated - Nothing. diff --git a/src/Between.php b/src/Between.php index 05a74bcca..28dcd6065 100644 --- a/src/Between.php +++ b/src/Between.php @@ -90,15 +90,17 @@ public function __construct($options = null) $options = $temp; } - if (count($options) !== 2 - && (! array_key_exists('min', $options) || ! array_key_exists('max', $options)) - ) { + if (! array_key_exists('min', $options) || ! array_key_exists('max', $options)) { throw new Exception\InvalidArgumentException("Missing option: 'min' and 'max' have to be given"); } - if (is_numeric($options['min']) && is_numeric($options['max'])) { + if ((isset($options['min']) && is_numeric($options['min'])) + && (isset($options['max']) && is_numeric($options['max'])) + ) { $this->numeric = true; - } elseif (is_string($options['min']) && is_string($options['max'])) { + } elseif ((isset($options['min']) && is_string($options['min'])) + && (isset($options['max']) && is_string($options['max'])) + ) { $this->numeric = false; } else { throw new Exception\InvalidArgumentException( diff --git a/test/BetweenTest.php b/test/BetweenTest.php index 478c12c96..55ee4e7ce 100644 --- a/test/BetweenTest.php +++ b/test/BetweenTest.php @@ -265,12 +265,13 @@ public function testMissingMinOrMax(array $args) public function constructBetweenValidatorInvalidDataProvider() { return [ - [ - ['min' => 1], - ], - [ - ['max' => 5], - ], + 'only-min' => [['min' => 1]], + 'only-max' => [['max' => 5]], + 'min-inclusive' => [['min' => 0, 'inclusive' => true]], + 'max-inclusive' => [['max' => 5, 'inclusive' => true]], + 'min-undefined' => [['min' => 0, 'foo' => 'bar']], + 'max-undefined' => [['max' => 5, 'foo' => 'bar']], + 'no-min-or-max' => [['bar' => 'foo', 'foo' => 'bar']], ]; }