Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type: allow to use list with PREVENT_MERGING flag #14

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 7 additions & 2 deletions src/Schema/Elements/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ public function complete($value, Context $context)
$value = []; // is unable to distinguish null from array in NEON
}

$cleanValue = $value;
if (is_array($cleanValue) && isset($cleanValue[Helpers::PREVENT_MERGING])) {
unset($cleanValue[Helpers::PREVENT_MERGING]);
}

$expected = $this->type . ($this->range === [null, null] ? '' : ':' . implode('..', $this->range));
if (!$this->doValidate($value, $expected, $context)) {
if (!$this->doValidate($cleanValue, $expected, $context)) {
return;
}
if ($this->pattern !== null && !preg_match("\x01^(?:$this->pattern)$\x01Du", $value)) {
Expand All @@ -150,7 +155,7 @@ public function complete($value, Context $context)

if ($this->items) {
$errCount = count($context->errors);
foreach ($value as $key => $val) {
foreach ($cleanValue as $key => $val) {
$context->path[] = $key;
$value[$key] = $this->items->complete($val, $context);
array_pop($context->path);
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'));
});
5 changes: 5 additions & 0 deletions tests/Schema/Expect.list.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Nette\Schema\Expect;
use Nette\Schema\Helpers;
use Nette\Schema\Processor;
use Tester\Assert;

Expand Down Expand Up @@ -45,6 +46,8 @@ test(function () { // merging
Assert::same([1, 2, 3, 'a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c']));

Assert::same([1, 2, 3], (new Processor)->process($schema, null));

Assert::same(['a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c', Helpers::PREVENT_MERGING => true]));
});


Expand All @@ -64,6 +67,8 @@ test(function () { // merging & other items validation
]);

Assert::same([1, 2, 3], (new Processor)->process($schema, null));

Assert::same(['a', 'b', 'c'], (new Processor)->process($schema, ['a', 'b', 'c', Helpers::PREVENT_MERGING => true]));
});


Expand Down