Skip to content

Commit 818237b

Browse files
committed
Validators: better exception message
1 parent ac90a28 commit 818237b

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

src/Utils/Validators.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ public static function assert($value, string $expected, string $label = 'variabl
9090
{
9191
if (!static::is($value, $expected)) {
9292
$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
93-
if (is_array($value)) {
94-
$type = 'array(' . count($value) . ')';
95-
} elseif (is_object($value)) {
93+
if (is_object($value)) {
9694
$type = 'object ' . get_class($value);
97-
} elseif (is_string($value) && strlen($value) < 40) {
98-
$type = "string '$value'";
9995
} else {
100-
$type = gettype($value);
96+
static $translate = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'NULL' => 'null'];
97+
$type = $translate[gettype($value)] ?? gettype($value);
98+
if (is_int($value) || is_float($value) || (is_string($value) && strlen($value) < 40)) {
99+
$type .= ' ' . var_export($value, true);
100+
}
101101
}
102102
throw new AssertionException("The $label expects to be $expected, $type given.");
103103
}

tests/Utils/Validators.assert().phpt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,44 @@ require __DIR__ . '/../bootstrap.php';
1515

1616
Assert::exception(function () {
1717
Validators::assert(true, 'int');
18-
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, boolean given.');
18+
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, bool given.');
19+
20+
Assert::exception(function () {
21+
Validators::assert('', 'int');
22+
}, Nette\Utils\AssertionException::class, "The variable expects to be int, string '' given.");
23+
24+
Assert::exception(function () {
25+
Validators::assert(str_repeat('x', 1000), 'int');
26+
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, string given.');
1927

2028
Assert::exception(function () {
2129
Validators::assert('1.0', 'int|float');
2230
}, Nette\Utils\AssertionException::class, "The variable expects to be int or float, string '1.0' given.");
2331

32+
Assert::exception(function () {
33+
Validators::assert(null, 'int');
34+
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, null given.');
35+
36+
Assert::exception(function () {
37+
Validators::assert(1.0, 'int');
38+
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, float 1.0 given.');
39+
40+
Assert::exception(function () {
41+
Validators::assert(1, 'float');
42+
}, Nette\Utils\AssertionException::class, 'The variable expects to be float, int 1 given.');
43+
44+
Assert::exception(function () {
45+
Validators::assert([], 'int');
46+
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, array given.');
47+
48+
Assert::exception(function () {
49+
Validators::assert(new stdClass, 'int');
50+
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, object stdClass given.');
51+
2452
Assert::exception(function () {
2553
Validators::assert(1, 'string|integer:2..5', 'variable');
26-
}, Nette\Utils\AssertionException::class, 'The variable expects to be string or integer in range 2..5, integer given.');
54+
}, Nette\Utils\AssertionException::class, 'The variable expects to be string or integer in range 2..5, int 1 given.');
55+
56+
Assert::exception(function () {
57+
Validators::assert('x', '?int');
58+
}, Nette\Utils\AssertionException::class, "The variable expects to be ?int, string 'x' given.");

tests/Utils/Validators.assertField().phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ Validators::assertField($arr, 'first');
2727

2828
Assert::exception(function () use ($arr) {
2929
Validators::assertField($arr, 'first', 'int');
30-
}, Nette\Utils\AssertionException::class, "The item 'first' in array expects to be int, boolean given.");
30+
}, Nette\Utils\AssertionException::class, "The item 'first' in array expects to be int, bool given.");

0 commit comments

Comments
 (0)