Valiant/Validator/Numericality.pm

filter: Statementstmt 75.0% 66 / 88 Branchbran 52.6% 40 / 76 Conditioncond 61.1% 11 / 18 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 78.9% 15 / 19 Podpod 0.0% 0 / 2 totaltotal 63.2% 134 / 212 CC 47 SCAR 29.3
CC
47
Cov
64%
normalize_shortcut
41.5
__ANON__
34.0
validate_each
9.1
CRAP 18.8
« Valiant/HTML/FormBuilder/Checkbox.pm ↑ Summary Valiant/I18N.pm »
1 package Valiant::Validator::Numericality;
2  
3 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 with 'Valiant::Validator::Each';
7  
8 # TODO add postive, negative /postive_or_zero, negative_or_zero
9  
10 # ($value_to_test, ?$constraint_value)
11 our %CHECKS = (
12 4 ▶ greater_than => sub { $_[0] > $_[1] ? 1:0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 2 2 4
13 23 ▶ greater_than_or_equal_to => sub { $_[0] >= $_[1] ? 1:0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= $_[1] ? : 13 10 23
14 3 ▶ equal_to => sub { $_[0] == $_[1] ? 1:0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == $_[1] ? : 0 3 3
15 15 ▶ less_than => sub { $_[0] < $_[1] ? 1:0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 9 6 15
16 13 ▶ less_than_or_equal_to => sub { $_[0] <= $_[1] ? 1:0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] <= $_[1] ? : 8 5 13
17 0 ▶ other_than => sub { $_[0] != $_[1] ? 1:0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] != $_[1] ? : 0 0 0
18 0 ▶ even => sub { $_[0] % 2 ? 0:1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] % 2 ? : 0 0 0
19 0 ▶ odd => sub { $_[0] % 2 ? 1:0 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] % 2 ? : 0 0 0
20 0 ▶ divisible_by => sub { $_[0] % $_[1] ? 0:1 },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] % $_[1] ? : 0 0 0
21 1 ▶ decimals => sub { length((($_[0] =~ /\.(\d*)/)[0]) // '') == $_[1] ? 1:0 },
Subroutine __ANON__: called CC=3 SCAR=12.7
Branch: 1 branch
BranchTrueFalseTotal
length(($_[0] =~ /\.(\d*)/)[0] // '') == $_[1] ? : 0 1 1
Condition50%1/2
($_[0] =~ /\.(\d*)/)[0] // ""
l!l
01
MC/DC0%0/1
($_[0] =~ /\.(\d*)/)[0] // ""
($_[0] =~ /\.(\d*)/)[0]
Truth table50%1/2
A
A= ($_[0] =~ /\.(\d*)/)[0]
Aresult
11
00
22 13 ▶ is_integer => sub { $_[0]=~/\A-?[0-9]+\z/ }, # Taken from Types::Standard
Subroutine __ANON__: called CC=1 SCAR=0.0
23 33 ▶ is_number => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
24 33 my $val = shift;
25 33 ▶ ($val =~ /\A[+-]?[0-9]+\z/) || # Taken from Types::Standard
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A[+-]?[0-9]+\z/ or $val =~ /\A(?:[+-]?) # matches optional +- in the beginning (?=[0-9]|\.[0-9]) # matches previous +- only if there is something like 3 or .3 [0-9]* # matches 0-9 zero or more times (?:\.[0-9]+)? # matches optional .89 or nothing (?:[Ee](?:[+-]?[0-9]+))? # matches E1 or e1 or e-1 or e+1 etc \z/x 33 0 33
26 ( $val =~ /\A(?:[+-]?) # matches optional +- in the beginning
27 (?=[0-9]|\.[0-9]) # matches previous +- only if there is something like 3 or .3
28 [0-9]* # matches 0-9 zero or more times
29 (?:\.[0-9]+)? # matches optional .89 or nothing
30 (?:[Ee](?:[+-]?[0-9]+))? # matches E1 or e1 or e-1 or e+1 etc
31 \z/x );
32 },
33 12 );
34  
35 # Run these first and fail early if the choosen one fails.
36 12 my @INIT = (qw(is_integer is_number));
37 12 my %INIT; @INIT{@INIT} = delete @CHECKS{@INIT};
38  
39 # Add the init_args to set the various check constraints and to allow
40 # someone to override individual error messages.
41 12 foreach my $attr (keys %CHECKS) {
42 120 has $attr => (is=>'ro', predicate=>"has_${attr}");
43 210 ▶ has "${attr}_err" => (is=>'ro', required=>1, default=>sub { _t "${attr}_err" });
Subroutine __ANON__: called CC=1 SCAR=0.0
44 }
45  
46 12 foreach my $attr (keys %INIT) {
47 42 ▶ has "${attr}_err" => (is=>'ro', required=>1, default=>sub { _t "${attr}_err" });
Subroutine __ANON__: called CC=1 SCAR=0.0
48 }
49  
50 12 has only_integer => (is=>'ro', required=>1, default=>0);
51  
52 21 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=15 SCAR=35.3
53 21 my ( $orig, $class, @args ) = @_;
54 21 my $args = $class->$orig(@args);
55  
56 # TODO Am thinking we shoud allow 'gt' for greater_than', 'gte' for 'greater_than_or_equal_to'
57 # and so on.
58  
59 21 ▶ if(my $integer = $args->{only_integer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $integer = $args->{"only_integer"}) 5 16 21
60  
61 5 ▶ if($integer eq 'positive_integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer eq "positive_integer") 2 3 5
62 2 $args->{greater_than_or_equal_to} = 0;
63 2 ▶ $args->{message} = _t("positive_integer_err") unless defined $args->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"message"} 2 0 2
64 }
65 5 ▶ if($integer eq 'negative_integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer eq "negative_integer") 0 5 5
66 0 $args->{less_than} = 0;
67 0 ▶ $args->{message} = _t("negative_integer_err") unless defined $args->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"message"} 0 0 0
68 }
69  
70 5 ▶ if($integer eq 'pg_serial') {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer eq "pg_serial") 1 4 5
71 1 $args->{greater_than_or_equal_to} = 1;
72 1 $args->{less_than_or_equal_to} = 2147483647;
73 1 ▶ $args->{message} = _t("pg_serial_err") unless defined $args->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"message"} 1 0 1
74 }
75 5 ▶ if($integer eq 'pg_bigserial') {
Branch: 1 branch
BranchTrueFalseTotal
if ($integer eq "pg_bigserial") 0 5 5
76 0 $args->{greater_than_or_equal_to} = 1;
77 0 $args->{less_than_or_equal_to} = 9223372036854775807;
78 0 ▶ $args->{message} = _t("pg_bigserial_err") unless defined $args->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"message"} 0 0 0
79 }
80 }
81  
82 21 ▶ if(my $between = delete $args->{between}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $between = delete $args->{"between"}) 1 20 21
83 1 $args->{greater_than_or_equal_to} = $between->[0];
84 1 $args->{less_than_or_equal_to} = $between->[1];
85 }
86  
87 21 ▶ if($args->{positive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"positive"}) 0 21 21
88 0 delete $args->{positive};
89 0 $args->{greater_than_or_equal_to} = 0;
90 0 ▶ $args->{message} = _t("positive_err") unless defined $args->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"message"} 0 0 0
91 }
92  
93 21 ▶ if($args->{negative}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"negative"}) 0 21 21
94 0 delete $args->{negative};
95 0 $args->{less_than} = 0;
96 0 ▶ $args->{message} = _t("negative_err") unless defined $args->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"message"} 0 0 0
97 }
98  
99 21 return $args;
100 12 };
101  
102 5 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=13 SCAR=37.3
Pod undocumented
103 5 my ($class, $arg) = @_;
104  
105 # TODO document this and add a few more (int16, int32, etc)
106 5 ▶ if((ref($arg)||'') eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $arg || '') eq 'ARRAY') { } 2 3 5
Condition100%2/2
ref $arg || ''
l!l
23
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
107 return +{
108 2 greater_than_or_equal_to => $arg->[0],
109 less_than_or_equal_to => $arg->[1],
110 };
111 } else {
112 3 ▶ if( ($arg eq 'only_integer') || ($arg eq 'integer') ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($arg eq 'only_integer' or $arg eq 'integer') { } 0 3 3
elsif ($arg eq 'positive_integer' or $arg eq 'negative_integer' or $arg eq 'pg_serial' or $arg eq 'pg_bigserial') { } 3 0 3
elsif ($arg eq 'positive') { } 0 0 0
elsif ($arg eq 'negative') { } 0 0 0
elsif ($arg eq 'even') { } 0 0 0
elsif ($arg eq 'odd') { } 0 0 0
Condition33%1/3
$arg eq "only_integer" or $arg eq "integer"
l!l&&r!l&&!r
003
MC/DC0%0/2
$arg eq "only_integer" or $arg eq "integer"
$arg eq "only_integer" $arg eq "integer"
Truth table33%1/3
A or B
A= $arg eq "only_integer"B= $arg eq "integer"
ABresult
1X1
011
000
113 return +{
114 0 only_integer => 1,
115 }
116 ▶ } elsif(
Condition55%5/9
$arg eq "positive_integer" or $arg eq "negative_integer" or $arg eq "pg_serial" or $arg eq "pg_bigserial"
l!l&&r!l&&!r
300
$arg eq "positive_integer" or $arg eq "negative_integer" or $arg eq "pg_serial"
l!l&&r!l&&!r
210
$arg eq "positive_integer" or $arg eq "negative_integer"
l!l&&r!l&&!r
201
MC/DC0%0/4
$arg eq "positive_integer" or $arg eq "negative_integer" or $arg eq "pg_serial" or $arg eq "pg_bigserial"
$arg eq "positive_integer" $arg eq "negative_integer" $arg eq "pg_serial" $arg eq "pg_bigserial"
Truth table40%2/5
A or B or C or D
A= $arg eq "positive_integer"B= $arg eq "negative_integer"C= $arg eq "pg_serial"D= $arg eq "pg_bigserial"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
117 ($arg eq 'positive_integer')
118 || ($arg eq 'negative_integer')
119 || ($arg eq 'pg_serial')
120 || ($arg eq 'pg_bigserial')
121 ) {
122 return +{
123 3 only_integer => $arg,
124 }
125 } elsif( $arg eq 'positive') {
126 0 return + { positive => 1};
127 } elsif( $arg eq 'negative') {
128 0 return + { negative => 1};
129 } elsif( $arg eq 'even') {
130 0 return + { even => 1};
131 } elsif( $arg eq 'odd') {
132 0 return + { odd => 1};
133 }
134 }
135 }
136  
137 46 ▶sub validate_each {
Subroutine validate_each: called CC=9 SCAR=22.1
Pod undocumented
138 46 my ($self, $record, $attr, $value, $options) = @_;
139 46 ▶ if($self->only_integer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->only_integer) { } 13 33 46
140 13 ▶ unless($INIT{is_integer}->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INIT{"is_integer"}->($value)) 3 10 13
141 3 $record->errors->add($attr, $self->is_integer_err, $options);
142 3 return;
143 }
144 } else {
145 33 ▶ unless($INIT{is_number}->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INIT{"is_number"}->($value)) 0 33 33
146 0 $record->errors->add($attr, $self->is_number_err, $options);
147 0 return;
148 }
149 }
150  
151 43 foreach my $key (sort keys %CHECKS) {
152 420 ▶ next unless $self->${\"has_${key}"};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->${\("has_$key");} 361 59 420
153 59 my $constraint_value = $self->$key;
154 59 ▶ $constraint_value = $constraint_value->($record)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $constraint_value || "") eq "CODE" 3 56 59
Condition100%2/2
ref $constraint_value || ''
l!l
356
MC/DC100%1/1
ref $constraint_value || ''
ref $constraint_value
Truth table100%2/2
A
A= ref $constraint_value
Aresult
11
00
155 if((ref($constraint_value)||'') eq 'CODE');
156 27 $record->errors->add($attr, $self->${\"${key}_err"}, +{%$options, count=>$constraint_value})
157 59 ▶ unless $CHECKS{$key}->($value, $constraint_value);
Branch: 1 branch
BranchTrueFalseTotal
unless $Valiant::Validator::Numericality::CHECKS{$key}->($value, $constraint_value) 27 32 59
158 }
159 }
160  
161 12 1;
162  
163 =head1 NAME
164  
165 Valiant::Validator::Numericality - Validate numeric attributes
166  
167 =head1 SYNOPSIS
168  
169 package Local::Test::Numericality;
170  
171 use Moo;
172 use Valiant::Validations;
173  
174 has age => (is => 'ro');
175 has equals => (is => 'ro', default => 33);
176  
177 validates age => (
178 numericality => {
179 only_integer => 1,
180 less_than => 200,
181 less_than_or_equal_to => 199,
182 greater_than => 10,
183 greater_than_or_equal_to => 9,
184 equal_to => \&equals,
185 },
186 );
187  
188 validates equals => (numericality => [5, 100]);
189  
190 my $object = Local::Test::Numericality->new(age=>8, equals=>40);
191 $object->validate;
192  
193 warn $object->errors->_dump;
194  
195 $VAR1 = {
196 age => [
197 "Age must be equal to 40",
198 "Age must be greater than 10",
199 "Age must be greater than or equal to 9",
200 ],
201 };
202  
203 =head1 DESCRIPTION
204  
205 Validates that your attributes have only numeric values. By default, it will
206 match an optional sign followed by an integral or floating point number. To
207 specify that only integral numbers are allowed set C<only_integer> to true.
208  
209 There's several parameters you can set to place different type of numeric
210 limits on the value. There's no checks on creating non sense rules (you can
211 set a C<greater_than> of 10 and a C<less_than> of 5, for example) so pay
212 attention.
213  
214 All parameter values can be either a constant or a coderef (which will get
215 C<$self> as as argument). The coderef option
216 exists to make it easier to write dynamic checks without resorting to writing
217 your own custom validators. Each value also defines a translation tag which
218 folows the pattern "${rule}_err" (for example the C<greater_than> rules has a
219 translation tag C<greater_than_err>). You can use the C<message> parameter to
220 set a custom message (either a string value or a translation tag).
221  
222 =head1 CONSTRAINTS
223  
224 Besides an overall test for either floating point or integer numericality this
225 validator supports the following constraints:
226  
227 =over
228  
229 =item only_integer
230  
231 When set to a true value will require the value to be some sort of integer. If
232 you set this to 1 then the value must be generally an integer. However you can
233 also set it to the following to get more limited integer types:
234  
235 validates attribute => ( numericality => { only_integer => 'positive_integer' }, ... );
236 validates attribute => ( numericality => { only_integer => 'negative_integer' }, ... );
237  
238 # Lets you require the integer to conform to Postgresql Serial or Bigserial types
239 validates attribute => ( numericality => { only_integer => 'pg_serial' }, ... );
240 validates attribute => ( numericality => { only_integer => 'pg_bigserial' }, ... );
241  
242 =item greater_than
243  
244 Accepts numeric value or coderef. Returns error message tag C<greater_than_err> if
245 the attribute value isn't greater.
246  
247 =item greater_than_or_equal_to
248  
249 Accepts numeric value or coderef. Returns error message tag C<greater_than_or_equal_to_err> if
250 the attribute value isn't equal or greater.
251  
252 =item equal_to
253  
254 Accepts numeric value or coderef. Returns error message tag C<equal_to_err> if
255 the attribute value isn't equal.
256  
257 =item other_than
258  
259 Accepts numeric value or coderef. Returns error message tag C<other_than_err> if
260 the attribute value isn't different.
261  
262 =item less_than
263  
264 Accepts numeric value or coderef. Returns error message tag C<less_than_err> if
265 the attribute value isn't less than.
266  
267 =item less_than_or_equal_to
268  
269 Accepts numeric value or coderef. Returns error message tag C<less_than_or_equal_to_err> if
270 the attribute value isn't less than or equal.
271  
272 =item between
273  
274 Accepts a two item arrayref, where the first is an inclusive lower number bound and the
275 second is an inclusive upper number bound.
276  
277 =item even
278  
279 Accepts numeric value or coderef. Returns error message tag C<even_err> if
280 the attribute value isn't an even number.
281  
282 =item odd
283  
284 Accepts numeric value or coderef. Returns error message tag C<odd_err> if
285 the attribute value isn't an odd number.
286  
287 =item divisible_by
288  
289 Accepts numeric value or coderef. Returns error message C<divisible_by_err> if the
290 attribute value is not evenly divisible by the value. For example if the attribute
291 value is 15 and the divisible value is 5 that is true (its divisible) but of the
292 divisible value was 4 that woule be false and generate an error message.
293  
294 =item decimals
295  
296 Accepts numeric value or coderef. Returns error message tag C<decimals_err> if
297 the attribute value doesn't contain exactly the requird number of places after
298 the decimal point.
299  
300 =item positive
301  
302 A number greater or equal to zero
303  
304 =item negative
305  
306 A number less than zero
307  
308 =back
309  
310 =head2 Cross-attribute comparison
311  
312 Any comparison constraint value may be a coderef instead of a constant. The
313 coderef receives the object being validated as its first argument, so you can
314 compare one attribute against another. Valiant ships no separate C<comparison>
315 validator because this covers the need:
316  
317 validates max_price => (
318 numericality => { greater_than_or_equal_to => sub { shift->min_price } },
319 );
320  
321 =head1 SHORTCUT FORM
322  
323 This validator supports the follow shortcut forms:
324  
325 validates attribute => ( numericality => [1,10], ... );
326  
327 Which is the same as:
328  
329 validates attribute => (
330 numericality => {
331 greater_than_or_equal_to => 1,
332 less_than_or_equal_to => 10,
333 },
334 );
335  
336 If you merely wish to test for overall numericality you can use:
337  
338 validates attribute => ( numericality => +{}, ... );
339  
340 You can require various integer types as well:
341  
342 validates attribute => ( numericality => 'integer', ... );
343 validates attribute => ( numericality => 'positive_integer', ... );
344 validates attribute => ( numericality => 'negative_integer' ... );
345 validates attribute => ( numericality => 'pg_serial' ... ); # Postgresql Serial
346 validates attribute => ( numericality => 'pg_bigserial' ... ); # Postgresql Bigserial
347  
348 Misc shortcuts:
349  
350 validates attribute => ( numericality => 'positive' ... ); # a positive number
351 validates attribute => ( numericality => 'negative' ... ); # a negative number
352 validates attribute => ( numericality => 'even' ... ); # an even number
353 validates attribute => ( numericality => 'odd' ... ); # an odd number
354  
355
356 =head1 GLOBAL PARAMETERS
357  
358 This validator supports all the standard shared parameters: C<if>, C<unless>,
359 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
360  
361 =head1 SEE ALSO
362
363 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
364  
365 =head1 AUTHOR
366
367 See L<Valiant>
368
369 =head1 COPYRIGHT & LICENSE
370
371 See L<Valiant>
372  
373 =cut
« Valiant/HTML/FormBuilder/Checkbox.pm ↑ Summary Valiant/I18N.pm »