empress-php / empress

Empress is a flexible microframework for creating async web applications in PHP 8.1.
MIT License
15 stars 0 forks source link

Introduce validators #19

Closed jakobmats closed 3 years ago

jakobmats commented 3 years ago

Things that can be retrieved from context objects using array access should be exposed as wrapped values that can be validated against some criteria. My suggestion is something along the lines of:

$ctx['name']->as('string')->unwrap();
$ctx['age']->as('int')->unwrapOrNull();
$ctx['age']->as('int')->unwrapOr(0);
$ctx['price']->as('float')->unwrapOrThrow(new \InvalidArgumentException());

This will make path parameters more secure and robust.

jakobmats commented 3 years ago

The following syntax will be used because "as" is a keyword :roll_eyes:

$ctx['name']->to('string')->unwrap();
$ctx['age']->to('int')->unwrapOrNull();
$ctx['age']->to('int')->unwrapOr(0);
$ctx['price']->to('float')->unwrapOrThrow(new \InvalidArgumentException());
jakobmats commented 3 years ago

Also, I'm afraid that context and context injecting will need to be reworked so that validator registry can be stored inside context objects.