VMValidator
Hello, this is simple attribute validation for PHP Models, based on the new features, presented in PHP 8 It works as a standalone and can be use in custom projects or in libraries like Symfony and Laravel.
Use just three rows, alogside some attributes:
Example
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
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
Extras
- Lots of validations
- Supprots also nested object validation
- Supprots also collection item types and collection item validations
- Supprots also custom validations*
- Nullable check
- Repeatable validation attributes
- Works with private properties and methods
- Works with constructor promotion
- Memory and time profiling
- Custom error messages
- Custom property and method names for the exceptions
*The custom validation should be declared as static in a validation class
class Validation {
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:
#[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.
Support
- Request a new validation
- Give a star
- Just say hi !