You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I oftentimes have the need to validate a complete array of data, for example when handling a POST request. It would be nice to have an easier way of validating the shape of the array.
This could look something like this:
<?php// Can also be implemented as interface with constants for compatibility with php < 8.1
enum Type
{
caseInt;
caseArray;
// ... more cases
}
$data = [
'foo' => 123,
'bar' => [
'baz' => 321
]
];
Assertion::hasShape(
$data,
[
'foo' => Type::Int,
'bar' => Type::Array,
]
); // success// validating the shape recursivelyAssertion::hasShape(
$data,
[
'foo' => Type::Int,
'bar' => [
'baz' => Type::Int,
]
]
); // success// data can have more keys and values, than validatedAssertion::hasShape(
$data,
[
'foo' => Type::Int,
]
); // success// Fails if even one key is not as expectedAssertions::hasShape(
$data,
[
'foo' => Type::Array,
'baz' => Type::Array
]
); // exception
I know that this is basically possible with lazy assertions, but I think this might be easier to read and understand while reading code. What do you think?
The text was updated successfully, but these errors were encountered:
I oftentimes have the need to validate a complete array of data, for example when handling a POST request. It would be nice to have an easier way of validating the shape of the array.
This could look something like this:
I know that this is basically possible with lazy assertions, but I think this might be easier to read and understand while reading code. What do you think?
The text was updated successfully, but these errors were encountered: