Skip to content

Commit 8cfb873

Browse files
integerdg
authored andcommitted
Assert with optinal description #7 (#8)
1 parent fbf4951 commit 8cfb873

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,18 @@ $schema = Expect::arrayOf('string')
314314
$processor->process($schema, ['a', 'b']); // it passes, 2 is even number
315315
$processor->process($schema, ['a', 'b', 'c']); // error, 3 is not even number
316316
```
317+
318+
Or
319+
320+
```php
321+
$schema = Expect::string()->assert('is_file'); // file must exist
322+
```
323+
324+
You can add custom description for every assert. This description will be part of error message.
325+
326+
```php
327+
$schema = Expect::arrayOf('string')
328+
->assert(function ($v) { return count($v) % 2 === 0; }, 'Even items in array');
329+
330+
$processor->process($schema, ['a', 'b', 'c']); // Failed assertion "Even items in array" for option with value array.
331+
```

src/Schema/Elements/Base.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ trait Base
2727
/** @var callable|null */
2828
private $before;
2929

30-
/** @var callable[] */
30+
/** @var array[] */
3131
private $asserts = [];
3232

3333
/** @var string|null */
@@ -62,9 +62,9 @@ public function castTo(string $type): self
6262
}
6363

6464

65-
public function assert(callable $handler): self
65+
public function assert(callable $handler, string $description = null): self
6666
{
67-
$this->asserts[] = $handler;
67+
$this->asserts[] = [$handler, $description];
6868
return $this;
6969
}
7070

@@ -110,9 +110,9 @@ private function doFinalize($value, Context $context)
110110
}
111111
}
112112

113-
foreach ($this->asserts as $i => $assert) {
114-
if (!$assert($value)) {
115-
$expected = is_string($assert) ? "$assert()" : "#$i";
113+
foreach ($this->asserts as $i => [$handler, $description]) {
114+
if (!$handler($value)) {
115+
$expected = $description ? ('"' . $description . '"') : (is_string($handler) ? "$handler()" : "#$i");
116116
$context->addError("Failed assertion $expected for option %path% with value " . static::formatValue($value) . '.');
117117
return;
118118
}

tests/Schema/Expect.assert.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,20 @@ test(function () { // multiple assertions
3434

3535
Assert::same('123', (new Processor)->process($schema, '123'));
3636
});
37+
38+
39+
test(function () { // multiple assertions with custom descriptions
40+
$schema = Expect::string()
41+
->assert('ctype_digit', 'Is number')
42+
->assert(function ($s) { return strlen($s) >= 3; }, 'Minimal lenght');
43+
44+
checkValidationErrors(function () use ($schema) {
45+
(new Processor)->process($schema, '');
46+
}, ["Failed assertion \"Is number\" for option with value ''."]);
47+
48+
checkValidationErrors(function () use ($schema) {
49+
(new Processor)->process($schema, '1');
50+
}, ["Failed assertion \"Minimal lenght\" for option with value '1'."]);
51+
52+
Assert::same('123', (new Processor)->process($schema, '123'));
53+
});

0 commit comments

Comments
 (0)