Skip to content

Commit

Permalink
Assert with optinal description nette#7
Browse files Browse the repository at this point in the history
  • Loading branch information
integer committed Jul 11, 2019
1 parent a307303 commit fb99032
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,18 @@ $schema = Expect::arrayOf('string')
$processor->process($schema, ['a', 'b']); // it passes, 2 is even number
$processor->process($schema, ['a', 'b', 'c']); // error, 3 is not even number
```

You can add custom description for every assert. This description will be part of error message.

```php
$schema = Expect::arrayOf('string')
->assert(function ($v) { return count($v) % 2 === 0; }, 'Even items in array'); // count must be even number

$processor->process($schema, ['a', 'b', 'c']); // Failed assertion "Even items in array" for option with value array.
```

PHP build in functions can be used too:

```php
$schema = Expect::string()->assert('is_file');
```
10 changes: 7 additions & 3 deletions src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ public function castTo(string $type): self
}


public function assert(callable $handler): self
public function assert(callable $handler, ?string $description = null): self
{
$this->asserts[] = $handler;
if ($description === null) {
$this->asserts[] = $handler;
} else {
$this->asserts[$description] = $handler;
}
return $this;
}

Expand Down Expand Up @@ -112,7 +116,7 @@ private function doFinalize($value, Context $context)

foreach ($this->asserts as $i => $assert) {
if (!$assert($value)) {
$expected = is_string($assert) ? "$assert()" : "#$i";
$expected = !is_numeric($i) ? "\"$i\"" : (is_string($assert) ? "$assert()" : "#$i");
$context->addError("Failed assertion $expected for option %path% with value " . static::formatValue($value) . '.');
return;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Schema/Expect.assert.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ test(function () { // single assertion
Assert::same(__FILE__, (new Processor)->process($schema, __FILE__));
});

test(function () { // single assertion with custom description
$schema = Expect::string()->assert('is_file', 'File exists');

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, 'hello');
}, ["Failed assertion \"File exists\" for option with value 'hello'."]);

Assert::same(__FILE__, (new Processor)->process($schema, __FILE__));
});

test(function () { // multiple assertions
$schema = Expect::string()->assert('ctype_digit')->assert(function ($s) { return strlen($s) >= 3; });
Expand All @@ -34,3 +43,19 @@ test(function () { // multiple assertions

Assert::same('123', (new Processor)->process($schema, '123'));
});

test(function () { // multiple assertions with custom description
$schema = Expect::string()
->assert('ctype_digit', 'Is number')
->assert(function ($s) { return strlen($s) >= 3; }, 'Minimal lenght');

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, '');
}, ["Failed assertion \"Is number\" for option with value ''."]);

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, '1');
}, ["Failed assertion \"Minimal lenght\" for option with value '1'."]);

Assert::same('123', (new Processor)->process($schema, '123'));
});

0 comments on commit fb99032

Please sign in to comment.