Skip to content

Validator - validateProperty() and validatePropertyValue() give no feedback if the property does not exist #62198

@pauljura

Description

@pauljura

Symfony version(s) affected

7.3.5

Description

Hi folks,

I have tests that execute code like this:

$violations = $validator->validatePropertyValue($object, 'someProperty', $value);

and I noticed that if someProperty does not exist, there is no warning or error raised. This leads me to mistakenly believe my test is working, when actually it's not performing any checks at all.

The same issue exists with the validateProperty() method:

$violations = $validator->validateProperty($object, 'someProperty');

If a property is removed or renamed, and associated tests are not updated, then I would like to see a warning, error, violation, or something.

Thanks

How to reproduce

$object = new stdClass();
$violations = $validator->validatePropertyValue($object, 'propertyThatDoesNotExist', 'test');
$violations = $validator->validateProperty($object, 'propertyThatDoesNotExist');

Possible Solution

In RecursiveContextualValidator.php for both the validateProperty and validatePropertyValue methods, add a check for the property. For example:

if (!$classMetadata->hasPropertyMetadata($propertyName)) {
    throw new ValidatorException(\sprintf('The property "%s" does not exist.', $propertyName));
}

I would also be happy with a trigger_error() or any other appropriate notification.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions