Skip to content

Commit

Permalink
bug #47499 [Uid][Validator] Stop to first ULID format violation (ogiz…
Browse files Browse the repository at this point in the history
…anagi)

This PR was merged into the 5.4 branch.

Discussion
----------

[Uid][Validator] Stop to first ULID format violation

| Q             | A
| ------------- | ---
| Branch?       | 5.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | N/A
| License       | MIT
| Doc PR        | N/A

Unlike the UUID validator, the ULID validator does not stop on the first violation raised, but continues adding a violation for each ULID format infringement.
Which might make sense in some situations, but the same error message is set for each.
In case of a string like `not-even-ulid-like`, you'll get 3 violations with the same message.

IIMHO, 95% of the use-cases will consist of exposing the violation messages directly, so displaying 3 times the same message is unexpected.

(getting different messages per violation type could be added as a new feature if needed)

Commits
-------

fdd73c7 [Validator][UID] Stop to first ULID format violation
  • Loading branch information
nicolas-grekas committed Sep 6, 2022
2 parents 7369bc9 + fdd73c7 commit af1ab9e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/UlidValidator.php
Expand Up @@ -48,13 +48,17 @@ public function validate($value, Constraint $constraint)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(26 > \strlen($value) ? Ulid::TOO_SHORT_ERROR : Ulid::TOO_LONG_ERROR)
->addViolation();

return;
}

if (\strlen($value) !== strspn($value, '0123456789ABCDEFGHJKMNPQRSTVWXYZabcdefghjkmnpqrstvwxyz')) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Ulid::INVALID_CHARACTERS_ERROR)
->addViolation();

return;
}

// Largest valid ULID is '7ZZZZZZZZZZZZZZZZZZZZZZZZZ'
Expand Down
Expand Up @@ -78,6 +78,7 @@ public function getInvalidUlids()
['01ARZ3NDEKTSV4RRFFQ69G5FAVA', Ulid::TOO_LONG_ERROR],
['01ARZ3NDEKTSV4RRFFQ69G5FAO', Ulid::INVALID_CHARACTERS_ERROR],
['Z1ARZ3NDEKTSV4RRFFQ69G5FAV', Ulid::TOO_LARGE_ERROR],
['not-even-ulid-like', Ulid::TOO_SHORT_ERROR],
];
}

Expand Down

0 comments on commit af1ab9e

Please sign in to comment.