https://github.com/yiisoft/hydrator-validator
A hydrator that also does validation, including raw data.
https://github.com/yiisoft/hydrator-validator
hydrator validator yii3
Last synced: 4 months ago
JSON representation
A hydrator that also does validation, including raw data.
- Host: GitHub
- URL: https://github.com/yiisoft/hydrator-validator
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2023-04-27T10:03:16.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-03T23:22:40.000Z (about 1 year ago)
- Last Synced: 2024-04-14T00:18:26.747Z (about 1 year ago)
- Topics: hydrator, validator, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com
- Size: 43 KB
- Stars: 11
- Watchers: 16
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii Validating Hydrator
[](https://packagist.org/packages/yiisoft/hydrator-validator)
[](https://packagist.org/packages/yiisoft/hydrator-validator)
[](https://github.com/yiisoft/hydrator-validator/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/hydrator-validator)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/hydrator-validator/master)
[](https://github.com/yiisoft/hydrator-validator/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/yiisoft/hydrator-validator)
[](https://shepherd.dev/github/yiisoft/hydrator-validator)The package provides a [hydrator](https://github.com/yiisoft/hydrator)
that also does [validation](https://github.com/yiisoft/validator), including raw data.
It's useful when input data comes from a user, and you need to validate it and then put it into DTOs.## Requirements
- PHP 8.0 or higher.
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require yiisoft/hydrator-validator
```## General usage
Validating hydrator is a decorator for [hydrator](https://github.com/yiisoft/hydrator) that allows to validate:
- raw data of properties marked with `Validate` PHP attribute;
- an object after creating or populating it.To use it, the object being validated must implement `ValidatedInputInterface`. You can use `ValidatedInputTrait` to
easily create such object. The validation rules for raw values of the object are defined with `Validate` PHP attribute.Example of object:
```php
use Yiisoft\Hydrator\Validator\Attribute\Validate;
use Yiisoft\Hydrator\Validator\ValidatedInputInterface;
use Yiisoft\Hydrator\Validator\ValidatedInputTrait;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Required;final class InputDto implements ValidatedInputInterface
{
use ValidatedInputTrait;
#[Email]
private string $email;
#[Validate(new Required())]
private string $name;
}
```Validation result could be obtained via `getValidationResult()` method. For further working with result, refer to
corresponding [validator's guide section](https://github.com/yiisoft/validator/blob/master/docs/guide/en/result.md).Validating hydrator usage example:
```php
use Psr\Http\Message\RequestInterface;
use Yiisoft\Hydrator\Validator\ValidatingHydrator;public function actionEdit(RequestInterface $request, ValidatingHydrator $hydrator): ResponseInterface
{
$data = $request->getParsedBody();
$inputDto = $hydrator->create(InputDto::class, $data);
if (!$inputDto->getValidationResult()->isValid()) {
// Validation didn't pass :(
}
// Everything is fine. You can use $inputDto now.
}
```## Documentation
- [Internals](docs/internals.md)
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).## License
The Yii Validating Hydrator is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.Maintained by [Yii Software](https://www.yiiframework.com/).
## Support the project
[](https://opencollective.com/yiisoft)
## Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)