Skip to content

Commit

Permalink
Validators::assert replaced with usual error message
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 15, 2020
1 parent ab3bfc9 commit 21cde84
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 54 deletions.
7 changes: 3 additions & 4 deletions src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ public function doNormalize($value, Context $context)

private function doValidate($value, string $expected, Context $context): bool
{
try {
Nette\Utils\Validators::assert($value, $expected, 'option %path%');
} catch (Nette\Utils\AssertionException $e) {
if (!Nette\Utils\Validators::is($value, $expected)) {
$expected = str_replace(['|', ':'], [' or ', ' in range '], $expected);
$context->addError(
$e->getMessage(),
'The option %path% expects to be %expected%, %value% given.',
Nette\Schema\Message::UNEXPECTED_VALUE,
['value' => $value, 'expected' => $expected]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Schema/Expect.anyOf.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('with complex structure', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, [123]);
}, ["The option '0' expects to be string, int 123 given."]);
}, ["The option '0' expects to be string, 123 given."]);

Assert::same(['foo'], (new Processor)->process($schema, ['foo']));
});
Expand Down
32 changes: 16 additions & 16 deletions tests/Schema/Expect.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ test('without default value', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 'one');
}, ["The option expects to be array, string 'one' given."]);
}, ["The option expects to be array, 'one' given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, true);
}, ['The option expects to be array, bool given.']);
}, ['The option expects to be array, true given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 123);
}, ['The option expects to be array, int 123 given.']);
}, ['The option expects to be array, 123 given.']);

Assert::same([], (new Processor)->process($schema, null));
});
Expand Down Expand Up @@ -95,9 +95,9 @@ test('merging & other items validation', function () {
checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, [1, 2, 3]);
}, [
"The option '0' expects to be string, int 1 given.",
"The option '1' expects to be string, int 2 given.",
"The option '2' expects to be string, int 3 given.",
"The option '0' expects to be string, 1 given.",
"The option '1' expects to be string, 2 given.",
"The option '2' expects to be string, 3 given.",
]);

Assert::same(
Expand Down Expand Up @@ -161,9 +161,9 @@ test('items() & scalar', function () {
checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, [1, 2, 3]);
}, [
"The option '0' expects to be string, int 1 given.",
"The option '1' expects to be string, int 2 given.",
"The option '2' expects to be string, int 3 given.",
"The option '0' expects to be string, 1 given.",
"The option '1' expects to be string, 2 given.",
"The option '2' expects to be string, 3 given.",
]);

Assert::same(['a' => 'val'], (new Processor)->process($schema, ['a' => 'val']));
Expand All @@ -174,7 +174,7 @@ test('items() & scalar', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => 123]);
}, ["The option 'b' expects to be string, int 123 given."]);
}, ["The option 'b' expects to be string, 123 given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => null]);
Expand All @@ -193,19 +193,19 @@ test('items() & structure', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['a' => 'val']);
}, ["The option 'a' expects to be array, string 'val' given."]);
}, ["The option 'a' expects to be array, 'val' given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, [1, 2, 3]);
}, [
"The option '0' expects to be array, int 1 given.",
"The option '1' expects to be array, int 2 given.",
"The option '2' expects to be array, int 3 given.",
"The option '0' expects to be array, 1 given.",
"The option '1' expects to be array, 2 given.",
"The option '2' expects to be array, 3 given.",
]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => 'val']);
}, ["The option 'b' expects to be array, string 'val' given."]);
}, ["The option 'b' expects to be array, 'val' given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => ['a' => 'val']]);
Expand All @@ -229,7 +229,7 @@ test('arrayOf() & scalar', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, [1, 2, false]);
}, ["The option '2' expects to be string or int, bool given."]);
}, ["The option '2' expects to be string or int, false given."]);

Assert::same(['key' => 'val'], (new Processor)->process($schema, ['key' => 'val']));
});
Expand Down
18 changes: 9 additions & 9 deletions tests/Schema/Expect.list.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ test('without default value', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 'one');
}, ["The option expects to be list, string 'one' given."]);
}, ["The option expects to be list, 'one' given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, true);
}, ['The option expects to be list, bool given.']);
}, ['The option expects to be list, true given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 123);
}, ['The option expects to be list, int 123 given.']);
}, ['The option expects to be list, 123 given.']);

Assert::same([], (new Processor)->process($schema, null));
});
Expand All @@ -58,9 +58,9 @@ test('merging & other items validation', function () {
checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, [1, 2, 3]);
}, [
"The option '0' expects to be string, int 1 given.",
"The option '1' expects to be string, int 2 given.",
"The option '2' expects to be string, int 3 given.",
"The option '0' expects to be string, 1 given.",
"The option '1' expects to be string, 2 given.",
"The option '2' expects to be string, 3 given.",
]);

Assert::same([1, 2, 3], (new Processor)->process($schema, null));
Expand All @@ -75,9 +75,9 @@ test('listOf() & scalar', function () {
checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, [1, 2, 3]);
}, [
"The option '0' expects to be string, int 1 given.",
"The option '1' expects to be string, int 2 given.",
"The option '2' expects to be string, int 3 given.",
"The option '0' expects to be string, 1 given.",
"The option '1' expects to be string, 2 given.",
"The option '2' expects to be string, 3 given.",
]);

Assert::same(['val', 'val'], (new Processor)->process($schema, ['val', 'val']));
Expand Down
14 changes: 7 additions & 7 deletions tests/Schema/Expect.minmax.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('int & min', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 9);
}, ['The option expects to be int in range 10.., int 9 given.']);
}, ['The option expects to be int in range 10.., 9 given.']);
});


Expand All @@ -28,7 +28,7 @@ test('int & max', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 21);
}, ['The option expects to be int in range ..20, int 21 given.']);
}, ['The option expects to be int in range ..20, 21 given.']);
});


Expand All @@ -40,11 +40,11 @@ test('int & min & max', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 9);
}, ['The option expects to be int in range 10..20, int 9 given.']);
}, ['The option expects to be int in range 10..20, 9 given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 21);
}, ['The option expects to be int in range 10..20, int 21 given.']);
}, ['The option expects to be int in range 10..20, 21 given.']);
});


Expand All @@ -56,7 +56,7 @@ test('nullable int & min & max', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 9);
}, ['The option expects to be null or int in range 10..20, int 9 given.']);
}, ['The option expects to be null or int in range 10..20, 9 given.']);
});


Expand All @@ -68,11 +68,11 @@ test('string', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, '');
}, ["The option expects to be string in range 1..5, string '' given."]);
}, ["The option expects to be string in range 1..5, '' given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 'foobar');
}, ["The option expects to be string in range 1..5, string 'foobar' given."]);
}, ["The option expects to be string in range 1..5, 'foobar' given."]);
});


Expand Down
8 changes: 4 additions & 4 deletions tests/Schema/Expect.scalars.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ test('', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 123);
}, ['The option expects to be string, int 123 given.']);
}, ['The option expects to be string, 123 given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, null);
}, ['The option expects to be string, null given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, false);
}, ['The option expects to be string, bool given.']);
}, ['The option expects to be string, false given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, []);
Expand All @@ -59,7 +59,7 @@ test('', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 123);
}, ['The option expects to be string or bool, int 123 given.']);
}, ['The option expects to be string or bool, 123 given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, null);
Expand All @@ -78,7 +78,7 @@ test('', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 123);
}, ['The option expects to be null or string, int 123 given.']);
}, ['The option expects to be null or string, 123 given.']);

Assert::same(null, (new Processor)->process($schema, null));
});
26 changes: 13 additions & 13 deletions tests/Schema/Expect.structure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ test('without items', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 'one');
}, ["The option expects to be array, string 'one' given."]);
}, ["The option expects to be array, 'one' given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, true);
}, ['The option expects to be array, bool given.']);
}, ['The option expects to be array, true given.']);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 123);
}, ['The option expects to be array, int 123 given.']);
}, ['The option expects to be array, 123 given.']);

Assert::equal((object) [], (new Processor)->process($schema, null));
});
Expand All @@ -48,7 +48,7 @@ test('accepts object', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, (object) ['a' => 1]);
}, ["The option 'a' expects to be string, int 1 given."]);
}, ["The option 'a' expects to be string, 1 given."]);

$schema = Expect::structure(['a' => Expect::string()->before('strrev')]);

Expand Down Expand Up @@ -143,7 +143,7 @@ test('with indexed item', function () {
}, [
"Unexpected option '1'.",
"Unexpected option '2'.",
"The option '0' expects to be string, int 1 given.",
"The option '0' expects to be string, 1 given.",
]);

Assert::equal(
Expand Down Expand Up @@ -194,7 +194,7 @@ test('with indexed item & otherItems', function () {

checkValidationErrors(function () use ($processor, $schema) {
$processor->process($schema, [1, 2, 3]);
}, ["The option '0' expects to be string, int 1 given."]);
}, ["The option '0' expects to be string, 1 given."]);

Assert::equal(
(object) [
Expand Down Expand Up @@ -238,7 +238,7 @@ test('item with default value', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => 123]);
}, ["The option 'b' expects to be string, int 123 given."]);
}, ["The option 'b' expects to be string, 123 given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => null]);
Expand All @@ -257,7 +257,7 @@ test('item without default value', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => 123]);
}, ["The option 'b' expects to be string, int 123 given."]);
}, ["The option 'b' expects to be string, 123 given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => null]);
Expand Down Expand Up @@ -301,7 +301,7 @@ test('other items', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['other' => 123]);
}, ["The option 'other' expects to be string, int 123 given."]);
}, ["The option 'other' expects to be string, 123 given."]);
});


Expand Down Expand Up @@ -331,7 +331,7 @@ test('structure items', function () {
checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['a' => 'val']);
}, [
"The option 'a' expects to be array, string 'val' given.",
"The option 'a' expects to be array, 'val' given.",
"The mandatory option 'b › y' is missing.",
]);

Expand All @@ -341,15 +341,15 @@ test('structure items', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => 123]);
}, ["The option 'b' expects to be array, int 123 given."]);
}, ["The option 'b' expects to be array, 123 given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => null]);
}, ["The mandatory option 'b › y' is missing."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => 'val']);
}, ["The option 'b' expects to be array, string 'val' given."]);
}, ["The option 'b' expects to be array, 'val' given."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => ['x' => 'val']]);
Expand All @@ -368,7 +368,7 @@ test('structure items', function () {

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, ['b' => ['y' => 123]]);
}, ["The option 'b › y' expects to be string, int 123 given."]);
}, ["The option 'b › y' expects to be string, 123 given."]);

Assert::equal(
(object) ['a' => (object) ['x' => 'defval'], 'b' => (object) ['y' => 'val']],
Expand Down

0 comments on commit 21cde84

Please sign in to comment.