Skip to content

Commit

Permalink
ValidatedInputInterface::getValidatedInput() throws exception when …
Browse files Browse the repository at this point in the history
…bject is not validated (#17)
  • Loading branch information
vjik committed Mar 6, 2024
1 parent e61b76a commit f4fd174
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Yii Validating Hydrator Change Log

## 1.0.1 under development
## 2.0.0 under development

- no changes in this release.
- Chg #17: Throws `LogicException` on call `ValidatedInputInterface::getValidatedInput()` method when object is not
validated (@vjik)

## 1.0.0 February 02, 2024

- Initial release.
- Initial release.
16 changes: 16 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Upgrading Instructions for Yii Validating Hydrator

## Upgrade from 1.x to 2.x

If you use `ValidatedInputInterface::getValidatedInput()` on non-validated inputs or form models, wrap it with
`try ... catch`:

```php
$result = $input->getValidatedInput();
// ↓
try {
$result = $input->getValidatedInput();
} catch (LogicException) {
$result = null;
}
```
6 changes: 4 additions & 2 deletions src/ValidatedInputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Hydrator\Validator;

use LogicException;
use Yiisoft\Validator\PostValidationHookInterface;
use Yiisoft\Validator\Result;

Expand All @@ -18,7 +19,8 @@ interface ValidatedInputInterface extends PostValidationHookInterface
/**
* Returns validation result.
*
* @return Result|null Validation result.
* @throws LogicException When validation result is not set.
* @return Result Validation result.
*/
public function getValidationResult(): ?Result;
public function getValidationResult(): Result;
}
7 changes: 6 additions & 1 deletion src/ValidatedInputTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Hydrator\Validator;

use LogicException;
use Yiisoft\Hydrator\Attribute\SkipHydration;
use Yiisoft\Validator\Result;

Expand All @@ -23,8 +24,12 @@ public function processValidationResult(Result $result): void
$this->validationResult = $result;
}

public function getValidationResult(): ?Result
public function getValidationResult(): Result
{
if (empty($this->validationResult)) {
throw new LogicException('Validation result is not set.');
}

return $this->validationResult;
}
}
21 changes: 21 additions & 0 deletions tests/ValidatedInputTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Hydrator\Validator\Tests;

use LogicException;
use PHPUnit\Framework\TestCase;
use Yiisoft\Hydrator\Validator\Tests\Support\Object\SimpleInput;

final class ValidatedInputTraitTest extends TestCase
{
public function testNotValidate(): void
{
$input = new SimpleInput();

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Validation result is not set.');
$input->getValidationResult();
}
}

0 comments on commit f4fd174

Please sign in to comment.