https://github.com/prinsfrank/php-validated-properties
Validated properties in PHP8.1 and above using attribute rules
https://github.com/prinsfrank/php-validated-properties
attributes composer-package php property validation validation-library
Last synced: 6 months ago
JSON representation
Validated properties in PHP8.1 and above using attribute rules
- Host: GitHub
- URL: https://github.com/prinsfrank/php-validated-properties
- Owner: PrinsFrank
- License: mit
- Created: 2022-01-25T17:55:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-07-10T21:04:47.000Z (about 3 years ago)
- Last Synced: 2025-03-26T07:36:34.451Z (6 months ago)
- Topics: attributes, composer-package, php, property, validation, validation-library
- Language: PHP
- Homepage:
- Size: 138 KB
- Stars: 24
- Watchers: 2
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README

# PHP Validated Properties

[](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/build-status/main)
[](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/?branch=main)
[](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/?branch=main)
**Add Rule attributes to your model properties to make sure they are valid.**
## Why this package?
When validating external data coming from either a Request, an import or an API, common PHP packages allow you to validate that incoming data and give you that data back in an unstructured way. With this package you can directly add validation rules to your structured models instead;
```php
#[Url]
protected string $url;#[Between(1, 100)]
protected int $nrOfItems;#[Email]
protected string $email;
```When a property is set to a value that doesn't adhere to these rules, a ValidationException will be thrown. (Which can be handled application specifically)
## Setup
To get up and running, simply run;
```shell
composer require prinsfrank/php-validated-properties
```## Creating a validated model
And extend the base model in ```PrinsFrank\PhpStrictModels\Model```;
```phpIf your models already extend another base model, you can also use the WithValidatedProperties trait instead
```php
## Adding validated properties
To add validation rules to properties, add them as attributes to protected and private properties:
```php