8000 feature #15154 [Validator] Added missing error codes and turned codes… · symfony/symfony@bfa28d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfa28d6

Browse files
committed
feature #15154 [Validator] Added missing error codes and turned codes into UUIDs (webmozart)
This PR was merged into the 2.8 branch. Discussion ---------- [Validator] Added missing error codes and turned codes into UUIDs Reopened #12388 on the 2.8 branch. | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - With the current implementation of error codes, checking which error occurred is unnecessarily complex: ```php if ($violation->getConstraint() instanceof Length && Length::TOO_SHORT_ERROR === $violation->getCode()) { // ... } ``` Also, the code is completely missing for some constraints. This is fixed now. By using UUIDs, the check is reduced to: ```php if (Length::TOO_SHORT_ERROR === $violation->getCode()) { // ... } ``` Also, APIs can simply output the error code and the name of the error without needing to point to the constraint as well. Before: ```json [ { "code": "1", "name": "TOO_SHORT_ERROR", "message": "This value is too short. ...", "constraint": "Symfony\\Component\\Validator\\Constraints\\Length" } ] ``` After: ```json [ { "code": "9ff3fdc4-b214-49db-8718-39c315e33d45", "name": "TOO_SHORT_ERROR", "message": "This value is too short. ..." } ] ``` This makes it possible to implement a service on symfony.com which looks up error codes, e.g. symfony.com/error?code=9ff3fdc4-b214-49db-8718-39c315e33d45 Such a URL could redirect directly to the documentation of the appropriate constraint. We could even support user-submitted error codes which redirect to the documentation of that constraint. Commits ------- 8874e88 [Validator] Added missing error codes and turned codes into UUIDs
2 parents 32cbfd4 + 8874e88 commit bfa28d6

File tree

97 files changed

+480
-100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+480
-100
lines changed

src/Symfony/Component/Validator/ConstraintViolationInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function getInvalidValue();
130130
/**
131131
* Returns a machine-digestible error code for the violation.
132132
*
133-
* @return mixed The error code.
133+
* @return string|null The error code.
134134
*/
135135
public function getCode();
136136
}

src/Symfony/Component/Validator/Constraints/AbstractComparison.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Used for the comparison of values.
1919
*
2020
* @author Daniel Holmes <daniel@danielholmes.org>
21+
* @author Bernhard Schussek <bschussek@gmail.com>
2122
*/
2223
abstract class AbstractComparison extends Constraint
2324
{

src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ public function validate($value, Constraint $constraint)
6060
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6161
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6262
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
63+
->setCode($this->getErrorCode())
6364
->addViolation();
6465
} else {
6566
$this->buildViolation($constraint->message)
6667
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6768
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
6869
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
70+
->setCode($this->getErrorCode())
6971
->addViolation();
7072
}
7173
}
@@ -80,4 +82,13 @@ public function validate($value, Constraint $constraint)
8082
* @return bool true if the relationship is valid, false otherwise
8183
*/
8284
abstract protected function compareValues($value1, $value2);
85+
86+
/**
87+
* Returns the error code used if the comparison fails.
88+
*
89+
* @return string|null The error code or `null` if no code should be set
90+
*/
91+
protected function getErrorCode()
92+
{
93+
}
8394
}

src/Symfony/Component/Validator/Constraints/Blank.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@
2323
*/
2424
class Blank extends Constraint
2525
{
26+
const NOT_BLANK_ERROR = '183ad2de-533d-4796-a439-6d3c3852b549';
27+
28+
protected static $errorNames = array(
29+
self::NOT_BLANK_ERROR => 'NOT_BLANK_ERROR',
30+
);
31+
2632
public $message = 'This value should be blank.';
2733
}

src/Symfony/Component/Validator/Constraints/BlankValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public function validate($value, Constraint $constraint)
3636
if ($this->context instanceof ExecutionContextInterface) {
3737
$this->context->buildViolation($constraint->message)
3838
->setParameter('{{ value }}', $this->formatValue($value))
39+
->setCode(Blank::NOT_BLANK_ERROR)
3940
->addViolation();
4041
} else {
4142
$this->buildViolation($constraint->message)
4243
->setParameter('{{ value }}', $this->formatValue($value))
44+
->setCode(Blank::NOT_BLANK_ERROR)
4345
->addViolation();
4446
}
4547
}

src/Symfony/Component/Validator/Constraints/CardScheme.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
class CardScheme extends Constraint
2626
{
27-
const NOT_NUMERIC_ERROR = 1;
28-
const INVALID_FORMAT_ERROR = 2;
27+
const NOT_NUMERIC_ERROR = 'a2ad9231-e827-485f-8a1e-ef4d9a6d5c2e';
28+
const INVALID_FORMAT_ERROR = 'a8faedbf-1c2f-4695-8d22-55783be8efed';
2929

3030
protected static $errorNames = array(
3131
self::NOT_NUMERIC_ERROR => 'NOT_NUMERIC_ERROR',

src/Symfony/Component/Validator/Constraints/Choice.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*/
2424
class Choice extends Constraint
2525
{
26-
const NO_SUCH_CHOICE_ERROR = 1;
27-
const TOO_FEW_ERROR = 2;
28-
const TOO_MANY_ERROR = 3;
26+
const NO_SUCH_CHOICE_ERROR = '8e179f1b-97aa-4560-a02f-2a8b42e49df7';
27+
const TOO_FEW_ERROR = '11edd7eb-5872-4b6e-9f12-89923999fd0e';
28+
const TOO_MANY_ERROR = '9bd98e49-211c-433f-8630-fd1c2d0f08c3';
2929

3030
protected static $errorNames = array(
3131
self::NO_SUCH_CHOICE_ERROR => 'NO_SUCH_CHOICE_ERROR',

src/Symfony/Component/Validator/Constraints/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
class Collection extends Composite
2525
{
26-
const MISSING_FIELD_ERROR = 1;
27-
const NO_SUCH_FIELD_ERROR = 2;
26+
const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
27+
const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
2828

2929
protected static $errorNames = array(
3030
self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',

src/Symfony/Component/Validator/Constraints/Count.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
class Count extends Constraint
2626
{
27-
const TOO_FEW_ERROR = 1;
28-
const TOO_MANY_ERROR = 2;
27+
const TOO_FEW_ERROR = 'bef8e338-6ae5-4caf-b8e2-50e7b0579e69';
28+
const TOO_MANY_ERROR = '756b1212-697c-468d-a9ad-50dd783bb169';
2929

3030
protected static $errorNames = array(
3131
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',

src/Symfony/Component/Validator/Constraints/Country.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@
2323
*/
2424
class Country extends Constraint
2525
{
26+
const NO_SUCH_COUNTRY_ERROR = '8f900c12-61bd-455d-9398-996cd040f7f0';
27+
28+
protected static $errorNames = array(
29+
self::NO_SUCH_COUNTRY_ERROR => 'NO_SUCH_COUNTRY_ERROR',
30+
);
31+
2632
public $message = 'This value is not a valid country.';
2733
}

src/Symfony/Component/Validator/Constraints/CountryValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ public function validate($value, Constraint $constraint)
5050
if ($this->context instanceof ExecutionContextInterface) {
5151
$this->context->buildViolation($constraint->message)
5252
->setParameter('{{ value }}', $this->formatValue($value))
53+
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
5354
->addViolation();
5455
} else {
5556
$this->buildViolation($constraint->message)
5657
->setParameter('{{ value }}', $this->formatValue($value))
58+
->setCode(Country::NO_SUCH_COUNTRY_ERROR)
5759
->addViolation();
5860
}
5961
}

src/Symfony/Component/Validator/Constraints/Currency.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1919
*
2020
* @author Miha Vrhovnik <miha.vrhovnik@pagein.si>
21+
* @author Bernhard Schussek <bschussek@gmail.com>
2122
*
2223
* @api
2324
*/
2425
class Currency extends Constraint
2526
{
27+
const NO_SUCH_CURRENCY_ERROR = '69945ac1-2db4-405f-bec7-d2772f73df52';
28+
29+
protected static $errorNames = array(
30+
self::NO_SUCH_CURRENCY_ERROR => 'NO_SUCH_CURRENCY_ERROR',
31+
);
32+
2633
public $message = 'This value is not a valid currency.';
2734
}

src/Symfony/Component/Validator/Constraints/CurrencyValidator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Validates whether a value is a valid currency.
2222
*
2323
* @author Miha Vrhovnik <miha.vrhovnik@pagein.si>
24+
* @author Bernhard Schussek <bschussek@gmail.com>
2425
*
2526
* @api
2627
*/
@@ -50,10 +51,12 @@ public function validate($value, Constraint $constraint)
5051
if ($this->context instanceof ExecutionContextInterface) {
5152
$this->context->buildViolation($constraint->message)
5253
->setParameter('{{ value }}', $this->formatValue($value))
54+
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
5355
->addViolation();
5456
} else {
5557
$this->buildViolation($constraint->message)
5658
->setParameter('{{ value }}', $this->formatValue($value))
59+
->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
5760
->addViolation();
5861
}
5962
}

src/Symfony/Component/Validator/Constraints/Date.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
class Date extends Constraint
2525
{
26-
const INVALID_FORMAT_ERROR = 1;
27-
const INVALID_DATE_ERROR = 2;
26+
const INVALID_FORMAT_ERROR = '69819696-02ac-4a99-9ff0-14e127c4d1bc';
27+
const INVALID_DATE_ERROR = '3c184ce5-b31d-4de7-8b76-326da7b2be93';
2828

2929
protected static $errorNames = array(
3030
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',

src/Symfony/Component/Validator/Constraints/DateTime.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*/
2424
class DateTime extends Constraint
2525
{
26-
const INVALID_FORMAT_ERROR = 1;
27-
const INVALID_DATE_ERROR = 2;
28-
const INVALID_TIME_ERROR = 3;
26+
const INVALID_FORMAT_ERROR = '1a9da513-2640-4f84-9b6a-4d99dcddc628';
27+
const INVALID_DATE_ERROR = 'd52afa47-620d-4d99-9f08-f4d85b36e33c';
28+
const INVALID_TIME_ERROR = '5e797c9d-74f7-4098-baa3-94390c447b27';
2929

3030
protected static $errorNames = array(
3131
self::INVALID_FORMAT_ERROR => 'INVALID_FORMAT_ERROR',

src/Symfony/Component/Validator/Constraints/Email.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*/
2424
class Email extends Constraint
2525
{
26-
const INVALID_FORMAT_ERROR = 1;
27-
const MX_CHECK_FAILED_ERROR = 2;
28-
const HOST_CHECK_FAILED_ERROR = 3;
26+
const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310';
27+
const MX_CHECK_FAILED_ERROR = 'bf447c1c-0266-4e10-9c6c-573df282e413';
28+
const HOST_CHECK_FAILED_ERROR = '7da53a8b-56f3-4288-bb3e-ee9ede4ef9a1';
2929

3030
protected static $errorNames = array(
3131
self::INVALID_FORMAT_ERROR => 'STRICT_CHECK_FAILED_ERROR',

src/Symfony/Component/Validator/Constraints/EqualTo.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1717
*
1818
* @author Daniel Holmes <daniel@danielholmes.org>
19+
* @author Bernhard Schussek <bschussek@gmail.com>
1920
*/
2021
class EqualTo extends AbstractComparison
2122
{
23+
const NOT_EQUAL_ERROR = '478618a7-95ba-473d-9101-cabd45e49115';
24+
25+
protected static $errorNames = array(
26+
self::NOT_EQUAL_ERROR => 'NOT_EQUAL_ERROR',
27+
);
28+
2229
public $message = 'This value should be equal to {{ compared_value }}.';
2330
}

src/Symfony/Component/Validator/Constraints/EqualToValidator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Validates values are equal (==).
1616
*
1717
* @author Daniel Holmes <daniel@danielholmes.org>
18+
* @author Bernhard Schussek <bschussek@gmail.com>
1819
*/
1920
class EqualToValidator extends AbstractComparisonValidator
2021
{
@@ -25,4 +26,12 @@ protected function compareValues($value1, $value2)
2526
{
2627
return $value1 == $value2;
2728
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function getErrorCode()
34+
{
35+
return EqualTo::NOT_EQUAL_ERROR;
36+
}
2837
}

src/Symfony/Component/Validator/Constraints/Expression.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
*/
2323
class Expression extends Constraint
2424
{
25+
const EXPRESSION_FAILED_ERROR = '6b3befbc-2f01-4ddf-be21-b57898905284';
26+
27+
protected static $errorNames = array(
28+
self::EXPRESSION_FAILED_ERROR => 'EXPRESSION_FAILED_ERROR',
29+
);
30+
2531
public $message = 'This value is not valid.';
2632
public $expression;
2733

src/Symfony/Component/Validator/Constraints/ExpressionValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ public function validate($value, Constraint $constraint)
8888
if ($this->context instanceof ExecutionContextInterface) {
8989
$this->context->buildViolation($constraint->message)
9090
->setParameter('{{ value }}', $this->formatValue($value))
91+
->setCode(Expression::EXPRESSION_FAILED_ERROR)
9192
->addViolation();
9293
} else {
9394
$this->buildViolation($constraint->message)
9495
->setParameter('{{ value }}', $this->formatValue($value))
96+
->setCode(Expression::EXPRESSION_FAILED_ERROR)
9597
->addViolation();
9698
}
9799
}

src/Symfony/Component/Validator/Constraints/File.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class File extends Constraint
2626
{
2727
// Check the Image constraint for clashes if adding new constants here
2828

29-
const NOT_FOUND_ERROR = 1;
30-
const NOT_READABLE_ERROR = 2;
31-
const EMPTY_ERROR = 3;
32-
const TOO_LARGE_ERROR = 4;
33-
const INVALID_MIME_TYPE_ERROR = 5;
29+
const NOT_FOUND_ERROR = 'd2a3fb6e-7ddc-4210-8fbf-2ab345ce1998';
30+
const NOT_READABLE_ERROR = 'c20c92a4-5bfa-4202-9477-28e800e0f6ff';
31+
const EMPTY_ERROR = '5d743385-9775-4aa5-8ff5-495fb1e60137';
32+
const TOO_LARGE_ERROR = 'df8637af-d466-48c6-a59d-e7126250a654';
33+
const INVALID_MIME_TYPE_ERROR = '744f00bc-4389-4c74-92de-9a43cde55534';
3434

3535
protected static $errorNames = array(
3636
self::NOT_FOUND_ERROR => 'NOT_FOUND_ERROR',

src/Symfony/Component/Validator/Constraints/GreaterThan.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1717
*
1818
* @author Daniel Holmes <daniel@danielholmes.org>
19+
* @author Bernhard Schussek <bschussek@gmail.com>
1920
*/
2021
class GreaterThan extends AbstractComparison
2122
{
23+
const TOO_LOW_ERROR = '778b7ae0-84d3-481a-9dec-35fdb64b1d78';
24+
25+
protected static $errorNames = array(
26+
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
27+
);
28+
2229
public $message = 'This value should be greater than {{ compared_value }}.';
2330
}

src/Symfony/Component/Validator/Constraints/GreaterThanOrEqual.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1717
*
1818
* @author Daniel Holmes <daniel@danielholmes.org>
19+
* @author Bernhard Schussek <bschussek@gmail.com>
1920
*/
2021
class GreaterThanOrEqual extends AbstractComparison
2122
{
23+
const TOO_LOW_ERROR = 'ea4e51d1-3342-48bd-87f1-9e672cd90cad';
24+
25+
protected static $errorNames = array(
26+
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
27+
);
28+
2229
public $message = 'This value should be greater than or equal to {{ compared_value }}.';
2330
}

src/Symfony/Component/Validator/Constraints/GreaterThanOrEqualValidator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Validates values are greater than or equal to the previous (>=).
1616
*
1717
* @author Daniel Holmes <daniel@danielholmes.org>
18+
* @author Bernhard Schussek <bschussek@gmail.com>
1819
*/
1920
class GreaterThanOrEqualValidator extends AbstractComparisonValidator
2021
{
@@ -25,4 +26,12 @@ protected function compareValues($value1, $value2)
2526
{
2627
return $value1 >= $value2;
2728
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function getErrorCode()
34+
{
35+
return GreaterThanOrEqual::TOO_LOW_ERROR;
36+
}
2837
}

src/Symfony/Component/Validator/Constraints/GreaterThanValidator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Validates values are greater than the previous (>).
1616
*
1717
* @author Daniel Holmes <daniel@danielholmes.org>
18+
* @author Bernhard Schussek <bschussek@gmail.com>
1819
*/
1920
class GreaterThanValidator extends AbstractComparisonValidator
2021
{
@@ -25,4 +26,12 @@ protected function compareValues($value1, $value2)
2526
{
2627
return $value1 > $value2;
2728
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function getErrorCode()
34+
{
35+
return GreaterThan::TOO_LOW_ERROR;
36+
}
2837
}

src/Symfony/Component/Validator/Constraints/GroupSequenceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*
1717
* @Annotation
1818
* @Target({"CLASS", "ANNOTATION"})
19+
*
20+
* @author Bernhard Schussek <bschussek@gmail.com>
1921
*/
2022
class GroupSequenceProvider
2123
{

src/Symfony/Component/Validator/Constraints/Iban.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
*/
2424
class Iban extends Constraint
2525
{
26-
const TOO_SHORT_ERROR = 1;
27-
const INVALID_COUNTRY_CODE_ERROR = 2;
28-
const INVALID_CHARACTERS_ERROR = 3;
29-
const INVALID_CASE_ERROR = 4;
30-
const CHECKSUM_FAILED_ERROR = 5;
26+
const TOO_SHORT_ERROR = '88e5e319-0aeb-4979-a27e-3d9ce0c16166';
27+
const INVALID_COUNTRY_CODE_ERROR = 'de78ee2c-bd50-44e2-aec8-3d8228aeadb9';
28+
const INVALID_CHARACTERS_ERROR = '8d3d85e4-784f-4719-a5bc-d9e40d45a3a5';
29+
const INVALID_CASE_ERROR = 'f4bf62fe-03ec-42af-a53b-68e21b1e7274';
30+
const CHECKSUM_FAILED_ERROR = 'b9401321-f9bf-4dcb-83c1-f31094440795';
3131

3232
protected static $errorNames = array(
3333
self::TOO_SHORT_ERROR => 'TOO_SHORT_ERROR',

0 commit comments

Comments
 (0)
0