Skip to content

Commit

Permalink
Assert with optinal description #7 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
integer authored and dg committed Sep 15, 2019
1 parent fbf4951 commit 8cfb873
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,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
```

Or

```php
$schema = Expect::string()->assert('is_file'); // file must exist
```

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');

$processor->process($schema, ['a', 'b', 'c']); // Failed assertion "Even items in array" for option with value array.
```
12 changes: 6 additions & 6 deletions src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait Base
/** @var callable|null */
private $before;

/** @var callable[] */
/** @var array[] */
private $asserts = [];

/** @var string|null */
Expand Down Expand Up @@ -62,9 +62,9 @@ public function castTo(string $type): self
}


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

Expand Down Expand Up @@ -110,9 +110,9 @@ private function doFinalize($value, Context $context)
}
}

foreach ($this->asserts as $i => $assert) {
if (!$assert($value)) {
$expected = is_string($assert) ? "$assert()" : "#$i";
foreach ($this->asserts as $i => [$handler, $description]) {
if (!$handler($value)) {
$expected = $description ? ('"' . $description . '"') : (is_string($handler) ? "$handler()" : "#$i");
$context->addError("Failed assertion $expected for option %path% with value " . static::formatValue($value) . '.');
return;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Schema/Expect.assert.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ test(function () { // multiple assertions

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


test(function () { // multiple assertions with custom descriptions
$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 8cfb873

Please sign in to comment.