| 1 | package Validator::Lazy::Role::Check::MinMax; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Validator::Lazy::Role::Check::MinMax | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Version 0.03 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use Validator::Lazy; | ||||||||||||||||||||||||||||
| 18 | my $v = Validator::Lazy->new( { minmax => { MinMax => { min => 2, max => 4, type => 'Str' } } } ); | ||||||||||||||||||||||||||||
| 19 | my $v = Validator::Lazy->new( { minmax => { MinMax => [ $min, $max, $type ] } } ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my $ok = $v->check( minmax => 'xxxxx' ); # ok is false | ||||||||||||||||||||||||||||
| 22 | say Dumper $v->errors; # [ { code => 'TOO_BIG', field => 'minmax', data => { min => 2, max => 4 } } ] | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | An internal Role for Validator::Lazy, part of Validator::Lazy package. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Provides "MinMax" type for Validator::Lazy config. | ||||||||||||||||||||||||||||
| 30 | Allows to check value for predefined range (from-to-valuetype). | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 METHODS | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head2 C<check> | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Called from inside if Validator::Lazy->check process | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Temporary overrides internal Validator::Lazy::check method like this: | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | $validator->check( $value, $param ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | $param - is a list = [ minvalue, maxvalue, valuetype ] | ||||||||||||||||||||||||||||
| 44 | OR | ||||||||||||||||||||||||||||
| 45 | $param - is a hash = { min => minvalue, max => maxvalue, type => Str or Int ] | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $value - your value to check | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 SUPPORT AND DOCUMENTATION | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | After installing, you can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | perldoc Validator::Lazy | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | You can also look for information at: | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | RT, CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 59 | http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | AnnoCPAN, Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 62 | http://annocpan.org/dist/Validator-Lazy | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | CPAN Ratings | ||||||||||||||||||||||||||||
| 65 | http://cpanratings.perl.org/d/Validator-Lazy | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Search CPAN | ||||||||||||||||||||||||||||
| 68 | http://search.cpan.org/dist/Validator-Lazy/ | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | ANTONC <antonc@cpan.org> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 78 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 79 | copy of the full license at: | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | use v5.14.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # min => ?, max => ?, type => ? | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 209 | ▶ | sub before_check { | ||||||||||||||||||||||||||
|
Subroutine
before_check: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 209 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 209 | ▶ | return $value unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 201 | chomp $value; | |||||||||||||||||||||||||||
| 98 | 201 | $value =~ s/(^\s+|\s+$)//ag; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 201 | return $value; | |||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 209 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=18 SCAR=29.7
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 209 | my ( $self, $value, $param ) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 209 | ▶ | return $value unless defined $value && $param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and $param
MC/DC50%1/2
defined $value and $param
defined $value
$param
Truth table66%2/3
A and B
A= defined $valueB= $param
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 201 | ▶ | if ( $param && ref $param eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$param and ref $param eq "ARRAY"
MC/DC50%1/2
$param and ref $param eq "ARRAY"
$param
ref $param eq "ARRAY"
Truth table66%2/3
A and B
A= $paramB= ref $param eq "ARRAY"
| |||||||||||||||||||||||||||||
| 109 | 171 | $param = { | |||||||||||||||||||||||||||
| 110 | min => $param->[0], | ||||||||||||||||||||||||||||
| 111 | max => $param->[1], | ||||||||||||||||||||||||||||
| 112 | type => $param->[2], | ||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 201 | ▶ | my $type = $param->{type} || 'Str'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$param->{'type'} || 'Str'
MC/DC100%1/1
$param->{'type'} || 'Str'
$param->{'type'}
Truth table100%2/2
A
A= $param->{'type'}
| |||||||||||||||||||||||||||||
| 117 | # $type ||= $value =~ /^\-?\d+(\.\d)?/ ? 'Int' : 'Str'; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 201 | my ( $min, $max ) = ( @$param{ qw/ min max / } ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 201 | ▶ | if ( $type eq 'Int' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | 24 | ▶ | $min //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$min //= 0
MC/DC100%1/1
$min //= 0
$min
Truth table100%2/2
A
A= $min
| |||||||||||||||||||||||||||||
| 123 | 24 | ▶ | $max //= 1000; | ||||||||||||||||||||||||||
|
Condition100%2/2
$max //= 1000
MC/DC100%1/1
$max //= 1000
$max
Truth table100%2/2
A
A= $max
| |||||||||||||||||||||||||||||
| 124 | 24 | ▶ | $self->add_error( 'TOO_SMALL', { min => $min, max => $max } ) if $value < $min; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 24 | ▶ | $self->add_error( 'TOO_BIG', { min => $min, max => $max } ) if $value > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | elsif ( $type eq 'Str' ) { | ||||||||||||||||||||||||||||
| 128 | 177 | ▶ | $min //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$min //= 0
MC/DC100%1/1
$min //= 0
$min
Truth table100%2/2
A
A= $min
| |||||||||||||||||||||||||||||
| 129 | 177 | ▶ | $max //= 64; | ||||||||||||||||||||||||||
|
Condition100%2/2
$max //= 64
MC/DC100%1/1
$max //= 64
$max
Truth table100%2/2
A
A= $max
| |||||||||||||||||||||||||||||
| 130 | 177 | ▶ | $self->add_error( 'TOO_SMALL', { min => $min, max => $max } ) if length( $value ) < $min; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 177 | ▶ | $self->add_error( 'TOO_BIG', { min => $min, max => $max } ) if length( $value ) > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | confess 'Unknown type. Avaliable values are: Str,Int' unless $type && $type =~ /^(Int|Str)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type and $type =~ /^(Int|Str)$/u
MC/DC0%0/2
$type and $type =~ /^(Int|Str)$/u
$type
$type =~ /^(Int|Str)$/u
Truth table0%0/3
A and B
A= $typeB= $type =~ /^(Int|Str)$/u
| |||||||||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 201 | return $value; | |||||||||||||||||||||||||||
| 138 | }; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 3 | 1; | |||||||||||||||||||||||||||