File Coverage

lib/Data/Validation/Exception.pm
Criterion Covered Total %
statement 22 22 100.0
branch 3 4 75.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 31 32 96.8


line stmt bran cond sub pod time code
1             package Data::Validation::Exception;
2              
3 1     1   3 use namespace::autoclean;
  1         1  
  1         4  
4              
5 1     1   45 use Unexpected::Functions qw( has_exception );
  1         1  
  1         6  
6 1     1   278 use Unexpected::Types qw( HashRef SimpleStr );
  1         1  
  1         8  
7 1     1   478 use Moo;
  1         1  
  1         5  
8              
9             extends q(Unexpected);
10             with q(Unexpected::TraitFor::ExceptionClasses);
11              
12             my $class = __PACKAGE__;
13              
14             has_exception $class;
15              
16             has_exception 'InvalidParameter' => parents => [ $class ];
17              
18             has_exception 'Allowed' => parents => [ 'InvalidParameter' ],
19             error => 'Parameter [_1] is not in the list of allowed values';
20              
21             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             has_exception 'EqualTo' => parents => [ 'InvalidParameter' ],
26             error => 'Parameter [_1] is not equal to the required value',
27             explain => 'Must equal {value}';
28              
29             has_exception 'FieldComparison' => parents => [ 'InvalidParameter' ],
30             error => 'Field [_1] does not [_2] field [_3]';
31              
32             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             has_exception 'Mandatory' => parents => [ 'InvalidParameter' ],
37             error => 'Parameter [_1] is mandatory';
38              
39             has_exception 'MatchingRegex' => parents => [ 'InvalidParameter' ],
40             error => 'Parameter [_1] does not match the required regex',
41             explain => 'Must match the pattern {pattern}';
42              
43             has_exception 'MatchingType' => parents => [ 'InvalidParameter' ],
44             error => 'Parameter [_1] does not of the required type [_3]';
45              
46             has_exception 'Printable' => parents => [ 'InvalidParameter' ],
47             error => 'Parameter [_1] value is not a printable character';
48              
49             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             has_exception 'ValidHostname' => parents => [ 'InvalidParameter' ],
54             error => 'Parameter [_1] is not a hostname';
55              
56             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             has_exception 'ValidInteger' => parents => [ 'InvalidParameter' ],
61             error => 'Parameter [_1] is not a valid integer';
62              
63             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             has_exception 'ValidNumber' => parents => [ 'InvalidParameter' ],
69             error => 'Parameter [_1] is not a valid number';
70              
71             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             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             has_exception 'KnownType' => parents => [ $class ],
80             error => 'Type constraint [_1] is unknown';
81              
82             has_exception 'ValidationErrors' => parents => [ $class ],
83             error => 'There is at least one data validation error';
84              
85             has '+class' => default => $class;
86              
87             has 'constraints' => is => 'lazy', isa => HashRef, default => sub { {} };
88              
89             has '_explain' => is => 'lazy', isa => SimpleStr, default => q(),
90             init_arg => 'explain';
91              
92             sub explain {
93 3     3 1 2679 my $self = shift; my $text = $self->_explain;
  3         35  
94              
95 3 100       491 0 > index $text, '{' and return $text;
96              
97             # Expand named parameters of the form {param_name}
98 1         2 my %args = %{ $self->constraints };
  1         4  
99 1         636 my $re = join '|', map { quotemeta $_ } keys %args;
  2         6  
100              
101 1 50       35 $text =~ s{ \{($re)\} }{ defined $args{ $1 } ? $args{ $1 } : "{${1}?}" }egmx;
  2         10  
102              
103 1         9 return $text;
104             }
105              
106             1;
107              
108             __END__
109              
110             =pod
111              
112             =encoding utf-8
113              
114             =head1 Name
115              
116             Data::Validation::Exception - Defines the exceptions throw by the distribution
117              
118             =head1 Synopsis
119              
120             use Data::Validation::Exception;
121              
122             =head1 Description
123              
124             Defines the exceptions throw by the distribution
125              
126             =head1 Configuration and Environment
127              
128             Defines the following exceptions;
129              
130             =over 3
131              
132             =item C<InvalidParameter>
133              
134             =item C<BetweenValues>
135              
136             =item C<EqualTo>
137              
138             =item C<FieldComparison>
139              
140             =item C<Hexadecimal>
141              
142             =item C<KnownType>
143              
144             =item C<Mandatory>
145              
146             =item C<MatchingRegex>
147              
148             =item C<MatchingType>
149              
150             =item C<Printable>
151              
152             =item C<SimpleText>
153              
154             =item C<ValidHostname>
155              
156             =item C<ValidIdentifier>
157              
158             =item C<ValidInteger>
159              
160             =item C<ValidLength>
161              
162             =item C<ValidNumber>
163              
164             =item C<ValidationErrors>
165              
166             =back
167              
168             =head1 Subroutines/Methods
169              
170             =head2 C<explain>
171              
172             Returns an explanation of the validation error
173              
174             =head1 Diagnostics
175              
176             None
177              
178             =head1 Dependencies
179              
180             =over 3
181              
182             =item L<Unexpected>
183              
184             =back
185              
186             =head1 Incompatibilities
187              
188             There are no known incompatibilities in this module
189              
190             =head1 Bugs and Limitations
191              
192             There are no known bugs in this module. Please report problems to
193             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Validation.
194             Patches are welcome
195              
196             =head1 Acknowledgements
197              
198             Larry Wall - For the Perl programming language
199              
200             =head1 Author
201              
202             Peter Flanigan, C<< <pjfl@cpan.org> >>
203              
204             =head1 License and Copyright
205              
206             Copyright (c) 2016 Peter Flanigan. All rights reserved
207              
208             This program is free software; you can redistribute it and/or modify it
209             under the same terms as Perl itself. See L<perlartistic>
210              
211             This program is distributed in the hope that it will be useful,
212             but WITHOUT WARRANTY; without even the implied warranty of
213             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
214              
215             =cut
216              
217             # Local Variables:
218             # mode: perl
219             # tab-width: 3
220             # End:
221             # vim: expandtab shiftwidth=3: