Open
Description
/cc @DaveLiddament
When an object is validated with Symfony Validator, we could narrow down its property types based on constraints.
My idea is:
- Create
class ValidatedObjectType extends ObjectType
- This class could override
ObjectType::getProperty()
, read the validator attributes, and narrow the property type in reflection returned from parent. We'd probably createnew ValidatedObjectPropertyReflection
, inject the original object, and only override the type getters. - Also override what
ValidatedObjectType::isSuperTypeOf
andacceptsWithReason
answer. - In PHPDocs we could typehint something like
symfony-validated<UserDTO>
and return ValidatedObjectType thanks to https://phpstan.org/developing-extensions/custom-phpdoc-types.
The only missing part is to narrow the object type to a validated one after calling the validator:
$errors = $validator->validate($author);
if (count($errors) === 0) { /* here we have a validated object */ }
This interface isn't really static analysis friendly. It'd be a job for a type-specifying extension to do that, but something like $validator->isValid($author)
would be much easier to work with.
Metadata
Metadata
Assignees
Labels
No labels