An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

![Banner](docs/images/banner.png)

# PHP Validated Properties

![GitHub](https://img.shields.io/github/license/prinsfrank/php-validated-properties)
[![Build Status](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/badges/build.png?b=main)](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/build-status/main)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/?branch=main)
[![Code Coverage](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/badges/coverage.png?b=main)](https://scrutinizer-ci.com/g/PrinsFrank/php-validated-properties/?branch=main)
![PHP Version Support](https://img.shields.io/packagist/php-v/prinsfrank/php-validated-properties)

**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```;
```php

If 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