| 1 | package Data::Validation::Exception; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Unexpected::Functions qw( has_exception ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Unexpected::Types qw( HashRef SimpleStr ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | extends q(Unexpected); | |||||||||
| 10 | 1 | with q(Unexpected::TraitFor::ExceptionClasses); | |||||||||
| 11 | |||||||||||
| 12 | 1 | my $class = __PACKAGE__; | |||||||||
| 13 | |||||||||||
| 14 | 1 | has_exception $class; | |||||||||
| 15 | |||||||||||
| 16 | 1 | has_exception 'InvalidParameter' => parents => [ $class ]; | |||||||||
| 17 | |||||||||||
| 18 | 1 | has_exception 'Allowed' => parents => [ 'InvalidParameter' ], | |||||||||
| 19 | error => 'Parameter [_1] is not in the list of allowed values'; | ||||||||||
| 20 | |||||||||||
| 21 | 1 | has_exception 'BetweenValues' => parents => [ 'InvalidParameter' ], | |||||||||
| 22 | error => 'Parameter [_1] is not in range', | ||||||||||
| 23 | explain => 'Must be greater than {min_value} and less than {max_value}'; | ||||||||||
| 24 | |||||||||||
| 25 | 1 | has_exception 'EqualTo' => parents => [ 'InvalidParameter' ], | |||||||||
| 26 | error => 'Parameter [_1] is not equal to the required value', | ||||||||||
| 27 | explain => 'Must equal {value}'; | ||||||||||
| 28 | |||||||||||
| 29 | 1 | has_exception 'FieldComparison' => parents => [ 'InvalidParameter' ], | |||||||||
| 30 | error => 'Field [_1] does not [_2] field [_3]'; | ||||||||||
| 31 | |||||||||||
| 32 | 1 | has_exception 'Hexadecimal' => parents => [ 'InvalidParameter' ], | |||||||||
| 33 | error => 'Parameter [_1] is not a hexadecimal number', | ||||||||||
| 34 | explain => 'Hexadecimal numbers can only contain the characters 0-9a-f'; | ||||||||||
| 35 | |||||||||||
| 36 | 1 | has_exception 'Mandatory' => parents => [ 'InvalidParameter' ], | |||||||||
| 37 | error => 'Parameter [_1] is mandatory'; | ||||||||||
| 38 | |||||||||||
| 39 | 1 | has_exception 'MatchingRegex' => parents => [ 'InvalidParameter' ], | |||||||||
| 40 | error => 'Parameter [_1] does not match the required regex', | ||||||||||
| 41 | explain => 'Must match the pattern {pattern}'; | ||||||||||
| 42 | |||||||||||
| 43 | 1 | has_exception 'MatchingType' => parents => [ 'InvalidParameter' ], | |||||||||
| 44 | error => 'Parameter [_1] does not of the required type [_3]'; | ||||||||||
| 45 | |||||||||||
| 46 | 1 | has_exception 'Printable' => parents => [ 'InvalidParameter' ], | |||||||||
| 47 | error => 'Parameter [_1] value is not a printable character'; | ||||||||||
| 48 | |||||||||||
| 49 | 1 | has_exception 'SimpleText' => parents => [ 'InvalidParameter' ], | |||||||||
| 50 | error => 'Parameter [_1] is not simple text', | ||||||||||
| 51 | explain => 'Must match the pattern [a-zA-Z0-9_ \-\.]+'; | ||||||||||
| 52 | |||||||||||
| 53 | 1 | has_exception 'ValidHostname' => parents => [ 'InvalidParameter' ], | |||||||||
| 54 | error => 'Parameter [_1] is not a hostname'; | ||||||||||
| 55 | |||||||||||
| 56 | 1 | has_exception 'ValidIdentifier' => parents => [ 'InvalidParameter' ], | |||||||||
| 57 | error => 'Parameter [_1] is not a valid identifier', | ||||||||||
| 58 | explain => 'Must match the pattern [a-zA-Z_] \w*'; | ||||||||||
| 59 | |||||||||||
| 60 | 1 | has_exception 'ValidInteger' => parents => [ 'InvalidParameter' ], | |||||||||
| 61 | error => 'Parameter [_1] is not a valid integer'; | ||||||||||
| 62 | |||||||||||
| 63 | 1 | has_exception 'ValidLength' => parents => [ 'InvalidParameter' ], | |||||||||
| 64 | error => 'Parameter [_1] has an invalid length', | ||||||||||
| 65 | explain => 'Must be greater than {min_length} and less ' | ||||||||||
| 66 | . 'than {max_length} characters long'; | ||||||||||
| 67 | |||||||||||
| 68 | 1 | has_exception 'ValidNumber' => parents => [ 'InvalidParameter' ], | |||||||||
| 69 | error => 'Parameter [_1] is not a valid number'; | ||||||||||
| 70 | |||||||||||
| 71 | 1 | has_exception 'ValidText' => parents => [ 'InvalidParameter' ], | |||||||||
| 72 | error => 'Parameter [_1] is not valid text', | ||||||||||
| 73 | explain => 'Must match the pattern [\t\n !\"#%&\'\(\)\*\+\,\-\./0-9:;=\?@A-Z\[\]_a-z\|\~]+'; | ||||||||||
| 74 | |||||||||||
| 75 | 1 | has_exception 'ValidTime' => parents => [ 'InvalidParameter' ], | |||||||||
| 76 | error => 'Parameter [_1] is not a valid time', | ||||||||||
| 77 | explain => 'Must match the pattern (\d\d ): (\d\d) (?: : (\d\d) )?'; | ||||||||||
| 78 | |||||||||||
| 79 | 1 | has_exception 'KnownType' => parents => [ $class ], | |||||||||
| 80 | error => 'Type constraint [_1] is unknown'; | ||||||||||
| 81 | |||||||||||
| 82 | 1 | has_exception 'ValidationErrors' => parents => [ $class ], | |||||||||
| 83 | error => 'There is at least one data validation error'; | ||||||||||
| 84 | |||||||||||
| 85 | 1 | has '+class' => default => $class; | |||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | has 'constraints' => is => 'lazy', isa => HashRef, default => sub { {} }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 88 | |||||||||||
| 89 | 1 | has '_explain' => is => 'lazy', isa => SimpleStr, default => q(), | |||||||||
| 90 | init_arg => 'explain'; | ||||||||||
| 91 | |||||||||||
| 92 | 3 | ▶ | sub explain { | ||||||||
|
Subroutine
explain: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 93 | 3 | my $self = shift; my $text = $self->_explain; | |||||||||
| 94 | |||||||||||
| 95 | 3 | ▶ | 0 > index $text, '{' and return $text; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | # Expand named parameters of the form {param_name} | ||||||||||
| 98 | 1 | my %args = %{ $self->constraints }; | |||||||||
| 99 | 1 | my $re = join '|', map { quotemeta $_ } keys %args; | |||||||||
| 100 | |||||||||||
| 101 | 1 | ▶ | $text =~ s{ \{($re)\} }{ defined $args{ $1 } ? $args{ $1 } : "{${1}?}" }egmx; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | |||||||||||
| 103 | 1 | return $text; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 1 | 1; | |||||||||
| 107 | |||||||||||
| 108 | __END__ | ||||||||||