4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19 * Validates whether the value is a valid UUID per RFC 4122.
21 * @author Colin O'Dell <colinodell@gmail.com>
22 * @author Bernhard Schussek <bschussek@gmail.com>
24 * @see http://tools.ietf.org/html/rfc4122
25 * @see https://en.wikipedia.org/wiki/Universally_unique_identifier
27 class UuidValidator extends ConstraintValidator
29 // The strict pattern matches UUIDs like this:
30 // xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
33 // x = any hexadecimal character
34 // M = any allowed version {1..5}
35 // N = any allowed variant {8, 9, a, b}
37 const STRICT_LENGTH = 36;
38 const STRICT_FIRST_HYPHEN_POSITION = 8;
39 const STRICT_LAST_HYPHEN_POSITION = 23;
40 const STRICT_VERSION_POSITION = 14;
41 const STRICT_VARIANT_POSITION = 19;
43 // The loose pattern validates similar yet non-compliant UUIDs.
44 // Hyphens are completely optional. If present, they should only appear
45 // between every fourth character:
46 // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
47 // xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx
48 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
50 // The value can also be wrapped with characters like []{}:
51 // {xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx}
53 // Neither the version nor the variant is validated by this pattern.
55 const LOOSE_MAX_LENGTH = 39;
56 const LOOSE_FIRST_HYPHEN_POSITION = 4;
61 public function validate($value, Constraint $constraint)
63 if (null === $value || '' === $value) {
67 if (!$constraint instanceof Uuid) {
68 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Uuid');
71 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
72 throw new UnexpectedTypeException($value, 'string');
75 $value = (string) $value;
77 if ($constraint->strict) {
78 $this->validateStrict($value, $constraint);
83 $this->validateLoose($value, $constraint);
86 private function validateLoose($value, Uuid $constraint)
89 // 1. ERROR_INVALID_CHARACTERS
90 // 2. ERROR_INVALID_HYPHEN_PLACEMENT
91 // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
93 // Trim any wrapping characters like [] or {} used by some legacy systems
94 $trimmed = trim($value, '[]{}');
96 // Position of the next expected hyphen
97 $h = self::LOOSE_FIRST_HYPHEN_POSITION;
100 $l = self::LOOSE_MAX_LENGTH;
102 for ($i = 0; $i < $l; ++$i) {
104 if (!isset($trimmed[$i])) {
105 $this->context->buildViolation($constraint->message)
106 ->setParameter('{{ value }}', $this->formatValue($value))
107 ->setCode(Uuid::TOO_SHORT_ERROR)
113 // Hyphens must occur every fifth position
114 // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
116 if ('-' === $trimmed[$i]) {
118 $this->context->buildViolation($constraint->message)
119 ->setParameter('{{ value }}', $this->formatValue($value))
120 ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
131 // Missing hyphens are ignored
138 if (!ctype_xdigit($trimmed[$i])) {
139 $this->context->buildViolation($constraint->message)
140 ->setParameter('{{ value }}', $this->formatValue($value))
141 ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
148 // Check length again
149 if (isset($trimmed[$i])) {
150 $this->context->buildViolation($constraint->message)
151 ->setParameter('{{ value }}', $this->formatValue($value))
152 ->setCode(Uuid::TOO_LONG_ERROR)
157 private function validateStrict($value, Uuid $constraint)
160 // 1. ERROR_INVALID_CHARACTERS
161 // 2. ERROR_INVALID_HYPHEN_PLACEMENT
162 // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
163 // 4. ERROR_INVALID_VERSION
164 // 5. ERROR_INVALID_VARIANT
166 // Position of the next expected hyphen
167 $h = self::STRICT_FIRST_HYPHEN_POSITION;
169 for ($i = 0; $i < self::STRICT_LENGTH; ++$i) {
171 if (!isset($value[$i])) {
172 $this->context->buildViolation($constraint->message)
173 ->setParameter('{{ value }}', $this->formatValue($value))
174 ->setCode(Uuid::TOO_SHORT_ERROR)
180 // Check hyphen placement
181 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
183 if ('-' === $value[$i]) {
185 $this->context->buildViolation($constraint->message)
186 ->setParameter('{{ value }}', $this->formatValue($value))
187 ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
193 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
195 if ($h < self::STRICT_LAST_HYPHEN_POSITION) {
203 if (!ctype_xdigit($value[$i])) {
204 $this->context->buildViolation($constraint->message)
205 ->setParameter('{{ value }}', $this->formatValue($value))
206 ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
214 $this->context->buildViolation($constraint->message)
215 ->setParameter('{{ value }}', $this->formatValue($value))
216 ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
223 // Check length again
224 if (isset($value[$i])) {
225 $this->context->buildViolation($constraint->message)
226 ->setParameter('{{ value }}', $this->formatValue($value))
227 ->setCode(Uuid::TOO_LONG_ERROR)
232 if (!in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
233 $this->context->buildViolation($constraint->message)
234 ->setParameter('{{ value }}', $this->formatValue($value))
235 ->setCode(Uuid::INVALID_VERSION_ERROR)
239 // Check variant - first two bits must equal "10"
243 if ((hexdec($value[self::STRICT_VARIANT_POSITION]) & 12) !== 8) {
244 $this->context->buildViolation($constraint->message)
245 ->setParameter('{{ value }}', $this->formatValue($value))
246 ->setCode(Uuid::INVALID_VARIANT_ERROR)