DataTypeValidator
DataTypeValidator is a PHP Experts, Inc., Project designed for easy data type validation.
It supports both traditional, fuzzy, PHP data types (e.g., "1.0" can be both a float, int, and string) and strict data type validations ('1' is only a string, 1.0 is only a float, etc.).
Installation
Via Composer
composer require phpexperts/datatype-validator
Usage
// 1. Pick a Type Checker (IsAFuzzyDataType or IsAStrictDataType).
// * IsAFuzzyDataType tries its best to emulate PHP's `==`.
// * IsAStrictDataType observes PHP's `strict_types=1` rules.
$validator = new DataValidator(new IsAStrictDataType());
// There are two powerful mechanisms out of the box:
// 2. It is easy to validate any data type dynamically, without a ton of if statements.
$validator->isType('asdf', 'string'); // true or false.
$validator->assertIsType(1, 'int'); // null or throws InvalidDataTypeException
// 3. You can also validate arrays:
$data = [
'name' => 'Cheyenne',
'age' => 22,
'birthday' => Carbon::parse('1996-12-04 15:15:15'),
'daysOld' => 8194.35,
'today' => Carbon::now(),
'sayHi' => function () { return 'Hi!'; },
];
$rules = [
'name' => 'string',
'age' => 'int',
'birthday' => 'Carbon',
'daysOld' => 'float',
'today' => 'Carbon\Carbon',
'sayHi' => 'callable',
];
$validator->validate($data, $rules);
// 4. DataValidator::validate() will return `true` on success or throw
// an `InvalidDataTypeException` that contains an array of errors:
$data = [
'name' => 'Cheyenne',
'age' => '22',
'birthday' => '1996-12-04 15:15:15',
];
try {
$validator->validate($data, $rules);
} catch (InvalidDataTypeException $e) {
print_r($e->getReasons());
/* Output:
array:2 [
0 => "age is not a valid int"
1 => "birthday is not a valid Carbon"
]
*/
}
// 5. It can validate objects based on their short name or full name.
$data = [
'yesterday' => \Carbon\Carbon::parse('2019-05-11'),
'tomorrow' => \Carbon\Carbon::parse('2019-05-13'),
];
$validator->validate($data, [
'yesterday' => '\Carbon\Carbon',
'tomorrow' => 'Carbon',
]);
Benchmarks
phpbench run --report=aggregate
benchmark | subject | set | revs | its | mem_peak | best | mean | mode | worst | stdev | rstdev | diff |
---|---|---|---|---|---|---|---|---|---|---|---|---|
DataTypeValidatorBench | benchValidator | 0 | 1000 | 5 | 1,421,504b | 52.561Ξs | 53.824Ξs | 54.211Ξs | 54.648Ξs | 0.769Ξs | 1.43% | 3.00x |
DataTypeValidatorBench | benchNative | 0 | 1000 | 5 | 1,357,368b | 20.470Ξs | 24.246Ξs | 21.822Ξs | 30.204Ξs | 3.833Ξs | 15.81% | 1.00x |
Use cases
PHPExperts\DataTypeValidator\DataTypeValidator
â Can bulk validate a data array
â Will return the name of the data validator logic
â Will return an array of invalid keys with explanations
â Will silently ignore data not in the rules
â Will silently ignore nullable rules with no data
â Data cannot be null by default
â Any data type that starts with a '?' is nullable
â Any data type that ends with '[]' is an array of X
â Will allow an empty array of something
â Will allow a nullable array of something
â Will throw a logic exception if a non string rule is given
PHPExperts\DataTypeValidator\DataTypeValidator: Assertions
â Will assert a value is a bool
â Will assert a value is an int
â Will assert a value is a float
â Will assert a value is a string
â Will assert a value is an array
â Will assert a value is an object
â Will assert a value is a callable
â Will assert a value is a resource
â Will assert an array of something
â Will assert an object by its short name
â Will assert an object by its full name
PHPExperts\DataTypeValidator\DataTypeValidator: Data Type Checks
â Will validate bools strictly
â Will validate ints strictly
â Will validate floats strictly
â Will validate strings strictly
â Will validate arrays strictly
â Will validate objects
â Will validate callables
â Will validate resources
â Will validate objects by their short name
â Will validate objects by their full name
â Can validate bools loosely
â Can validate ints loosely
â Can validate floats loosely
â Can validate strings loosely
â Can validate arrays loosely
â Will validate arrays of something
PHPExperts\DataTypeValidator\IsAFuzzyDataType
â Will return true for valid values
â Will return false for invalid values
â Will match short classes
â Will match specific classes
â Will work with an array of something
PHPExperts\DataTypeValidator\IsAStrictDataType
â Will return true for valid values
â Will return false for invalid values
â Will match short classes
â Will match specific classes
â Will work with an array of something
Testing
phpunit
Contributors
Theodore R. Smith theodore@phpexperts.pro
GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
CEO: PHP Experts, Inc.
License
MIT license. Please see the license file for more information.