Skip to content

Commit 34d58c0

Browse files
committed
Validators: better exception message
1 parent 8d39ae7 commit 34d58c0

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
@@ -80,14 +80,14 @@ public static function assert($value, string $expected, string $label = 'variabl
8080
{
8181
if (!static::is($value, $expected)) {
8282
$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
83-
if (is_array($value)) {
84-
$type = 'array(' . count($value) . ')';
85-
} elseif (is_object($value)) {
83+
if (is_object($value)) {
8684
$type = 'object ' . get_class($value);
87-
} elseif (is_string($value) && strlen($value) < 40) {
88-
$type = "string '$value'";
8985
} else {
90-
$type = gettype($value);
86+
static $translate = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'NULL' => 'null'];
87+
$type = $translate[gettype($value)] ?? gettype($value);
88+
if (is_int($value) || is_float($value) || (is_string($value) && strlen($value) < 40)) {
89+
$type .= ' ' . var_export($value, true);
90+
}
9191
}
9292
throw new AssertionException("The $label expects to be $expected, $type given.");
9393
}

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)