Skip to content
Permalink
Browse files

Validators: better exception message

  • Loading branch information...
dg committed Mar 10, 2019
1 parent ac90a28 commit 818237b16678f74ff9022ea0beb4b381e0b1af17
Showing with 41 additions and 9 deletions.
  1. +6 −6 src/Utils/Validators.php
  2. +34 −2 tests/Utils/Validators.assert().phpt
  3. +1 −1 tests/Utils/Validators.assertField().phpt
@@ -90,14 +90,14 @@ public static function assert($value, string $expected, string $label = 'variabl
{
if (!static::is($value, $expected)) {
$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
if (is_array($value)) {
$type = 'array(' . count($value) . ')';
} elseif (is_object($value)) {
if (is_object($value)) {
$type = 'object ' . get_class($value);
} elseif (is_string($value) && strlen($value) < 40) {
$type = "string '$value'";
} else {
$type = gettype($value);
static $translate = ['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'NULL' => 'null'];
$type = $translate[gettype($value)] ?? gettype($value);
if (is_int($value) || is_float($value) || (is_string($value) && strlen($value) < 40)) {
$type .= ' ' . var_export($value, true);
}
}
throw new AssertionException("The $label expects to be $expected, $type given.");
}
@@ -15,12 +15,44 @@ require __DIR__ . '/../bootstrap.php';
Assert::exception(function () {
Validators::assert(true, 'int');
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, boolean given.');
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, bool given.');
Assert::exception(function () {
Validators::assert('', 'int');
}, Nette\Utils\AssertionException::class, "The variable expects to be int, string '' given.");
Assert::exception(function () {
Validators::assert(str_repeat('x', 1000), 'int');
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, string given.');
Assert::exception(function () {
Validators::assert('1.0', 'int|float');
}, Nette\Utils\AssertionException::class, "The variable expects to be int or float, string '1.0' given.");
Assert::exception(function () {
Validators::assert(null, 'int');
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, null given.');
Assert::exception(function () {
Validators::assert(1.0, 'int');
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, float 1.0 given.');
Assert::exception(function () {
Validators::assert(1, 'float');
}, Nette\Utils\AssertionException::class, 'The variable expects to be float, int 1 given.');
Assert::exception(function () {
Validators::assert([], 'int');
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, array given.');
Assert::exception(function () {
Validators::assert(new stdClass, 'int');
}, Nette\Utils\AssertionException::class, 'The variable expects to be int, object stdClass given.');
Assert::exception(function () {
Validators::assert(1, 'string|integer:2..5', 'variable');
}, Nette\Utils\AssertionException::class, 'The variable expects to be string or integer in range 2..5, integer given.');
}, Nette\Utils\AssertionException::class, 'The variable expects to be string or integer in range 2..5, int 1 given.');
Assert::exception(function () {
Validators::assert('x', '?int');
}, Nette\Utils\AssertionException::class, "The variable expects to be ?int, string 'x' given.");
@@ -27,4 +27,4 @@ Validators::assertField($arr, 'first');
Assert::exception(function () use ($arr) {
Validators::assertField($arr, 'first', 'int');
}, Nette\Utils\AssertionException::class, "The item 'first' in array expects to be int, boolean given.");
}, Nette\Utils\AssertionException::class, "The item 'first' in array expects to be int, bool given.");

0 comments on commit 818237b

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.