https://github.com/ivangrigorov/vmvalidator
Hello, this is simple attribute validation for PHP Models, based on the new features, presented in PHP 8
https://github.com/ivangrigorov/vmvalidator
packagist php php8 profiling validation
Last synced: 8 months ago
JSON representation
Hello, this is simple attribute validation for PHP Models, based on the new features, presented in PHP 8
- Host: GitHub
- URL: https://github.com/ivangrigorov/vmvalidator
- Owner: IvanGrigorov
- License: mit
- Created: 2021-06-28T21:52:27.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-19T02:37:59.000Z (over 3 years ago)
- Last Synced: 2024-12-06T04:11:10.882Z (over 1 year ago)
- Topics: packagist, php, php8, profiling, validation
- Language: PHP
- Homepage:
- Size: 134 KB
- Stars: 97
- Watchers: 7
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# VMValidator
[](https://github.com/IvanGrigorov/VMValidator/actions/workflows/php.yml)

  
[](https://www.buymeacoffee.com/ivangrigorov)
Hello, this is simple attribute validation for PHP Models, based on the new features, presented in [PHP 8](https://www.php.net/releases/8.0/en.php) It works as a standalone and can be use in custom projects or in libraries like Symfony and Laravel.
Use just [three rows](https://github.com/IvanGrigorov/VMValidator/blob/2139877c4ca6ae01f60729db2d83f9c5e087096d/index.php), alogside some [attributes](https://github.com/IvanGrigorov/VMValidator/blob/2139877c4ca6ae01f60729db2d83f9c5e087096d/index.php):
# Example
```php
test = $test;
}
}
$test = new Test(40);
try {
MasterValidator::validate(new UpperTest($test),
new OptionsModel(orderOfValidation: [ValidationOrderEnum::PROPERTIES,
ValidationOrderEnum::METHODS,
ValidationOrderEnum::CONSTANTS],
excludedMethods: ['getFile'],
excludedProperties: ['file']));
} catch(Exception $e) {
var_dump($e);
}
```
# Installation
```bash
composer require ivangrigorov/vmvalidator
```
# Options
In what order to validate the classes (methods or properties first), and what to exclude is directly configurable runtime [here](https://github.com/IvanGrigorov/VMValidator/blob/master/RMValidator/Options/OptionsModel.php)
# Extras
- [x] Lots of validations
- [x] Supports also nested object validation
- [x] Supports also collection item types and collection item validations
- [x] Supports also custom validations*
- [x] Supports also or validations*
- [x] Nullable check
- [x] Repeatable validation attributes
- [x] Works with private properties and methods
- [x] Works with constructor promotion
- [x] Memory and time profiling
- [x] Custom error messages
- [x] Custom property and method names for the exceptions
- [x] Severity levels
- [x] Debug explorer
- [x] Callback execution*
### *The custom validation should be declared as static in a validation class
```php
class Validation
{
public static function validate($valueToTest, $arg1): bool
{
return $valueToTest == $arg1;
}
}
```
The method should always return boolean: ```true``` for valid input and ```false``` for invalid.
In the declaration:
```php
#[CustomAttribute(staticClassName: Validation::class, staticMethodName: 'validate', args: [2])]
```
You can pass additional arguments to use in the validation function, but the first parameter is always the value to be tested.
### *The or validation uses custom attribute names
```
#[RangeAttribute(from:10, to:30, name: "orOne")]
#[RangeAttribute(from:10, to:40, name: "orTwo")]
const tttt = 40;
```
```
MasterValidator::validate($test, new OptionsModel(orAttributes: ['orOne', 'orTwo']));
```
### *Callback execution
```$successCallable``` is executed on successful validation
```$failureCallable``` is executed on unsuccessful validation
```$forcedCallable``` is executed on successful AND unsuccessful validation
### Severity
Add option to make the failed validation throw NOTICE|WARNING|ERROR
```
#[RangeAttribute(from:10, to:30, severity: SeverityEnum::ERROR)]
```
### Debugging
Add option to display all validations per class
```
MasterValidator::debug(::class);
```
# Support
- Request a new validation
- Give a star
- Just say hi !