Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit d2b5636

Browse files
committed
Provides validation error messages for Between validator type mismatch
Adds two new validation error messages, one for when a numeric value is validated against non-numeric min/max values, and one for when a string value is validated against numeric min/max values.
1 parent 3cda0ad commit d2b5636

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/Between.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Between extends AbstractValidator
1616
{
1717
const NOT_BETWEEN = 'notBetween';
1818
const NOT_BETWEEN_STRICT = 'notBetweenStrict';
19+
const VALUE_NOT_NUMERIC = 'valueNotNumeric';
20+
const VALUE_NOT_STRING = 'valueNotString';
1921

2022
/**
2123
* Retain if min and max are numeric values. Allow to not compare string and numeric types
@@ -31,7 +33,10 @@ class Between extends AbstractValidator
3133
*/
3234
protected $messageTemplates = [
3335
self::NOT_BETWEEN => "The input is not between '%min%' and '%max%', inclusively",
34-
self::NOT_BETWEEN_STRICT => "The input is not strictly between '%min%' and '%max%'"
36+
self::NOT_BETWEEN_STRICT => "The input is not strictly between '%min%' and '%max%'",
37+
self::VALUE_NOT_NUMERIC => "The min ('%min%') and max ('%max%') values are numeric, but the input is not",
38+
self::VALUE_NOT_STRING => "The min ('%min%') and max ('%max%') values are non-numeric strings, "
39+
. "but the input is not a string",
3540
];
3641

3742
/**
@@ -88,7 +93,7 @@ public function __construct($options = null)
8893
if (count($options) !== 2
8994
&& (! array_key_exists('min', $options) || ! array_key_exists('max', $options))
9095
) {
91-
throw new Exception\InvalidArgumentException("Missing option : 'min' and 'max' have to be given");
96+
throw new Exception\InvalidArgumentException("Missing option: 'min' and 'max' have to be given");
9297
}
9398

9499
if (is_numeric($options['min']) && is_numeric($options['max'])) {
@@ -97,7 +102,7 @@ public function __construct($options = null)
97102
$this->numeric = false;
98103
} else {
99104
throw new Exception\InvalidArgumentException(
100-
"Invalid options : 'min' and 'max' should be of the same scalar type"
105+
"Invalid options: 'min' and 'max' should be of the same scalar type"
101106
);
102107
}
103108

@@ -179,15 +184,17 @@ public function setInclusive($inclusive)
179184
*/
180185
public function isValid($value)
181186
{
187+
$this->setValue($value);
188+
182189
if ($this->numeric && ! is_numeric($value)) {
190+
$this->error(self::VALUE_NOT_NUMERIC);
183191
return false;
184192
}
185193
if (! $this->numeric && ! is_string($value)) {
194+
$this->error(self::VALUE_NOT_STRING);
186195
return false;
187196
}
188197

189-
$this->setValue($value);
190-
191198
if ($this->getInclusive()) {
192199
if ($this->getMin() > $value || $value > $this->getMax()) {
193200
$this->error(self::NOT_BETWEEN);

test/BetweenTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function testEqualsMessageVariables()
257257
public function testMissingMinOrMax(array $args)
258258
{
259259
$this->expectException(InvalidArgumentException::class);
260-
$this->expectExceptionMessage("Missing option : 'min' and 'max' have to be given");
260+
$this->expectExceptionMessage("Missing option: 'min' and 'max' have to be given");
261261

262262
new Between($args);
263263
}
@@ -280,12 +280,34 @@ public function testConstructorCanAcceptInclusiveParameter()
280280
$this->assertFalse($validator->getInclusive());
281281
}
282282

283-
public function testConstructWithTravesableOptions()
283+
public function testConstructWithTraversableOptions()
284284
{
285285
$options = new \ArrayObject(['min' => 1, 'max' => 10, 'inclusive' => false]);
286286
$validator = new Between($options);
287287

288288
$this->assertTrue($validator->isValid(5));
289289
$this->assertFalse($validator->isValid(10));
290290
}
291+
292+
public function testStringValidatedAgainstNumericMinAndMaxIsInvalidAndReturnsAFailureMessage()
293+
{
294+
$validator = new Between(['min' => 1, 'max' => 10]);
295+
$this->assertFalse($validator->isValid('a'));
296+
$messages = $validator->getMessages();
297+
$this->assertContains(
298+
'The min (\'1\') and max (\'10\') values are numeric, but the input is not',
299+
$messages
300+
);
301+
}
302+
303+
public function testNumericValidatedAgainstStringMinAndMaxIsInvalidAndReturnsAFailureMessage()
304+
{
305+
$validator = new Between(['min' => 'a', 'max' => 'z']);
306+
$this->assertFalse($validator->isValid(10));
307+
$messages = $validator->getMessages();
308+
$this->assertContains(
309+
'The min (\'a\') and max (\'z\') values are non-numeric strings, but the input is not a string',
310+
$messages
311+
);
312+
}
291313
}

0 commit comments

Comments
 (0)