mercredi 27 avril 2016

Validator constraint containing guid as error code

Background

I'm currently unit testing my validators, and I figured I'd try to grab some pointers by looking at some of the validators bundled with Symfony.

I noticed that in their constraint class, they have a const that stores a guid. Example:

class Url extends Constraint
{
    const INVALID_URL_ERROR = '57c2f299-1154-4870-89bb-ef3b1f5ad229';
    //...

This code is used in the validator like this:

$this->context->buildViolation($constraint->message)
    ->setParameter('', $this->formatValue($value))
    ->setCode(Url::INVALID_URL_ERROR)
    ->addViolation();

And used in the unit tests like this:

$this->buildViolation('myMessage')
    ->setParameter('', '"'.$url.'"')
    ->setCode(Url::INVALID_URL_ERROR)
    ->assertRaised();

Question

This might be a stupid question, but why is this done?

I assume it's too make sure that the correct stuff is passed along, but any explanation I come up with in my head is very weak and hand-wavy. I can't quite grok it.

Aucun commentaire:

Enregistrer un commentaire