Skip to content

Commit

Permalink
Allow options to be marked as deprecated [Closes #27]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 25, 2020
1 parent 185dea8 commit e515a0a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ $processor->process($schema, ['additional' => 1]); // OK
$processor->process($schema, ['additional' => true]); // ERROR
```

Deprecations
------------

You can deprecate property using the `deprecated([string $message])` method. Deprecation notices are returned by `$processor->getWarnings()`.

```php
$schema = Expect::structure([
'old' => Expect::int()->deprecated('The option %path% is deprecated'),
]);

$processor->process($schema, ['old' => 1]); // OK
$processor->getWarnings(); // ["The option 'old' is deprecated"]
```

Ranges: min() max()
-------------------

Expand Down
20 changes: 19 additions & 1 deletion src/Schema/Elements/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ trait Base
/** @var string|null */
private $castTo;

/** @var string|null */
private $deprecated;


public function default($value): self
{
Expand Down Expand Up @@ -69,6 +72,14 @@ public function assert(callable $handler, string $description = null): self
}


/** Marks option as deprecated */
public function deprecated(string $message = 'Option %path% is deprecated.'): self
{
$this->deprecated = $message;
return $this;
}


public function completeDefault(Context $context)
{
if ($this->required) {
Expand All @@ -95,7 +106,6 @@ private function doValidate($value, string $expected, Context $context): bool
{
try {
Nette\Utils\Validators::assert($value, $expected, 'option %path%');
return true;
} catch (Nette\Utils\AssertionException $e) {
$context->addError(
$e->getMessage(),
Expand All @@ -104,6 +114,14 @@ private function doValidate($value, string $expected, Context $context): bool
);
return false;
}

if ($this->deprecated !== null) {
$context->addWarning(
$this->deprecated,
Nette\Schema\Message::DEPRECATED
);
}
return true;
}


Expand Down
3 changes: 3 additions & 0 deletions src/Schema/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ final class Message
/** variables: {hint: string} */
public const UNEXPECTED_KEY = 'schema.unexpectedKey';

/** no variables */
public const DEPRECATED = 'schema.deprecated';

/** @var string */
public $message;

Expand Down
15 changes: 15 additions & 0 deletions tests/Schema/Expect.anyOf.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ test('required & nullable', function () {
});


test('deprecated item', function () {
$schema = Expect::anyOf('one', true, Expect::int()->deprecated());

$processor = new Processor;
Assert::same('one', $processor->process($schema, 'one'));
Assert::same([], $processor->getWarnings());

Assert::same(true, $processor->process($schema, true));
Assert::same([], $processor->getWarnings());

Assert::same(123, $processor->process($schema, 123));
Assert::same(['Option is deprecated.'], $processor->getWarnings());
});


test('nullable anyOf', function () {
$schema = Expect::anyOf(Expect::string(), true)->nullable();

Expand Down
28 changes: 28 additions & 0 deletions tests/Schema/Expect.structure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,31 @@ test('processing without default values', function () {
$processor->process($schema, ['d' => 'newval'])
);
});


test('deprecated item', function () {
$schema = Expect::structure([
'b' => Expect::string()->deprecated('depr %path%'),
]);

$processor = new Processor;
Assert::equal(
(object) ['b' => 'val'],
$processor->process($schema, ['b' => 'val'])
);
Assert::same(["depr 'b'"], $processor->getWarnings());
});


test('deprecated other items', function () {
$schema = Expect::structure([
'key' => Expect::string(),
])->otherItems(Expect::string()->deprecated());

$processor = new Processor;
Assert::equal((object) ['key' => null], $processor->process($schema, []));
Assert::same([], $processor->getWarnings());

Assert::equal((object) ['key' => null, 'other' => 'foo'], $processor->process($schema, ['other' => 'foo']));
Assert::same(["Option 'other' is deprecated."], $processor->getWarnings());
});

0 comments on commit e515a0a

Please sign in to comment.