Skip to content

Commit

Permalink
Move template definitions to the rules
Browse files Browse the repository at this point in the history
It's easier to identify the reason for choosing a specific message in
the rule than in the exception. The same goes for the key we use to
determine the templates.

This change will simplify the `ValidationException` because it will
already receive the template it needs to use. As a consequence, the
`Factory` also becomes more straightforward.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Jan 29, 2024
1 parent 3334529 commit dd896bb
Show file tree
Hide file tree
Showing 181 changed files with 1,042 additions and 680 deletions.
5 changes: 0 additions & 5 deletions library/Exceptions/AllOfException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,4 @@ class AllOfException extends GroupedValidationException
self::SOME => 'These rules must not pass for {{name}}',
],
];

protected function chooseTemplate(): string
{
return self::SOME;
}
}
11 changes: 7 additions & 4 deletions library/Exceptions/AlnumException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Rules\AbstractFilterRule as Filter;
use Respect\Validation\Validatable;

final class AlnumException extends FilteredValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)',
self::EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and {{additionalChars}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)',
Filter::TEMPLATE_EXTRA => '{{name}} must contain only letters (a-z), digits (0-9) and {{additionalChars}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not contain letters (a-z) or digits (0-9)',
self::EXTRA => '{{name}} must not contain letters (a-z), digits (0-9) or {{additionalChars}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must not contain letters (a-z) or digits (0-9)',
Filter::TEMPLATE_EXTRA => '{{name}} must not contain letters (a-z), digits (0-9) or {{additionalChars}}',
],
];
}
11 changes: 7 additions & 4 deletions library/Exceptions/AlphaException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Rules\AbstractFilterRule;
use Respect\Validation\Validatable;

final class AlphaException extends FilteredValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must contain only letters (a-z)',
self::EXTRA => '{{name}} must contain only letters (a-z) and {{additionalChars}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must contain only letters (a-z)',
AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must contain only letters (a-z) and {{additionalChars}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not contain letters (a-z)',
self::EXTRA => '{{name}} must not contain letters (a-z) or {{additionalChars}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must not contain letters (a-z)',
AbstractFilterRule::TEMPLATE_EXTRA => '{{name}} must not contain letters (a-z) or {{additionalChars}}',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/AlwaysInvalidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class AlwaysInvalidException extends ValidationException
{
public const SIMPLE = 'simple';
Expand All @@ -18,11 +20,11 @@ final class AlwaysInvalidException extends ValidationException
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} is always invalid',
Validatable::TEMPLATE_STANDARD => '{{name}} is always invalid',
self::SIMPLE => '{{name}} is not valid',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} is always valid',
Validatable::TEMPLATE_STANDARD => '{{name}} is always valid',
self::SIMPLE => '{{name}} is valid',
],
];
Expand Down
6 changes: 4 additions & 2 deletions library/Exceptions/AlwaysValidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class AlwaysValidException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} is always valid',
Validatable::TEMPLATE_STANDARD => '{{name}} is always valid',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} is always invalid',
Validatable::TEMPLATE_STANDARD => '{{name}} is always invalid',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/AnyOfException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class AnyOfException extends NestedValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => 'At least one of these rules must pass for {{name}}',
Validatable::TEMPLATE_STANDARD => 'At least one of these rules must pass for {{name}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => 'At least one of these rules must not pass for {{name}}',
Validatable::TEMPLATE_STANDARD => 'At least one of these rules must not pass for {{name}}',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/ArrayTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class ArrayTypeException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be of type array',
Validatable::TEMPLATE_STANDARD => '{{name}} must be of type array',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be of type array',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type array',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/ArrayValException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class ArrayValException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be an array value',
Validatable::TEMPLATE_STANDARD => '{{name}} must be an array value',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be an array value',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be an array value',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/Base64Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class Base64Exception extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be Base64-encoded',
Validatable::TEMPLATE_STANDARD => '{{name}} must be Base64-encoded',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be Base64-encoded',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be Base64-encoded',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class BaseException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a number in the base {{base}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must be a number in the base {{base}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a number in the base {{base}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be a number in the base {{base}}',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/BetweenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class BetweenException extends NestedValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be between {{minValue}} and {{maxValue}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must be between {{minValue}} and {{maxValue}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be between {{minValue}} and {{maxValue}}',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be between {{minValue}} and {{maxValue}}',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/BoolTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class BoolTypeException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be of type boolean',
Validatable::TEMPLATE_STANDARD => '{{name}} must be of type boolean',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be of type boolean',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be of type boolean',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/BoolValException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class BoolValException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a boolean value',
Validatable::TEMPLATE_STANDARD => '{{name}} must be a boolean value',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a boolean value',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be a boolean value',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/BsnException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class BsnException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a BSN',
Validatable::TEMPLATE_STANDARD => '{{name}} must be a BSN',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a BSN',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be a BSN',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/CallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class CallException extends NestedValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{input}} must be valid when executed with {{callable}}',
Validatable::TEMPLATE_STANDARD => '{{input}} must be valid when executed with {{callable}}',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{input}} must not be valid when executed with {{callable}}',
Validatable::TEMPLATE_STANDARD => '{{input}} must not be valid when executed with {{callable}}',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/CallableTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class CallableTypeException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be callable',
Validatable::TEMPLATE_STANDARD => '{{name}} must be callable',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be callable',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be callable',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/CharsetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class CharsetException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be in the {{charset}} charset',
Validatable::TEMPLATE_STANDARD => '{{name}} must be in the {{charset}} charset',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be in the {{charset}} charset',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be in the {{charset}} charset',
],
];
}
6 changes: 4 additions & 2 deletions library/Exceptions/CnhException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace Respect\Validation\Exceptions;

use Respect\Validation\Validatable;

final class CnhException extends ValidationException
{
/**
* @var array<string, array<string, string>>
*/
protected array $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must be a valid CNH number',
Validatable::TEMPLATE_STANDARD => '{{name}} must be a valid CNH number',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not be a valid CNH number',
Validatable::TEMPLATE_STANDARD => '{{name}} must not be a valid CNH number',
],
];
}
Loading

0 comments on commit dd896bb

Please sign in to comment.