laminas / laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria
https://docs.laminas.dev/laminas-validator/
BSD 3-Clause "New" or "Revised" License
132 stars 57 forks source link

Abstract validator creates warning when an array value contains circular reference #40

Open michalbundyra opened 4 years ago

michalbundyra commented 4 years ago

As the \Zend\Validator\AbstractValidator::createMessage method uses var_export to convert arrays to strings this fails if the array contains a circular reference. The problem line is https://github.com/zendframework/zend-validator/blob/master/src/AbstractValidator.php#L294.

I've provided a quick example of this below which outputs "PHP Warning: var_export does not handle circular references":

class ExampleValidator extends \Zend\Validator\AbstractValidator
{
    protected $messageTemplates = [
        'someError' => 'error'
    ];
    public function isValid($value)
    {
        $this->setValue($value);
        $this->error('someError');
        return false;
    }
}

class Circular {
    public $reference;
}

$test = new Circular();
$test->reference = $test;

$validator = new ExampleValidator();
$validator->isValid([$test]);

Originally posted by @tomp4l at https://github.com/zendframework/zend-validator/issues/82

michalbundyra commented 4 years ago

Can you avoid the circular dependency?

To be honest I don't think we will implement some kind of circular depedency check because it could affect to the performance.


Originally posted by @Maks3w at https://github.com/zendframework/zend-validator/issues/82#issuecomment-221534430

michalbundyra commented 4 years ago

I've worked around it by handling the array to string conversion manually by overriding setValue. This may cause issues with built in validators provided by Zend but it's only a minor inconvenience for my use case.


Originally posted by @tomp4l at https://github.com/zendframework/zend-validator/issues/82#issuecomment-221535134