| 1 | package Validate::Tiny; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use List::MoreUtils 'natatime'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 22 | our @ISA = qw/Exporter/; | |||||||||||||||||||||||||||
| 11 | 22 | our @EXPORT_OK = qw/ | |||||||||||||||||||||||||||
| 12 | validate | ||||||||||||||||||||||||||||
| 13 | filter | ||||||||||||||||||||||||||||
| 14 | is_required | ||||||||||||||||||||||||||||
| 15 | is_required_if | ||||||||||||||||||||||||||||
| 16 | is_existing | ||||||||||||||||||||||||||||
| 17 | is_equal | ||||||||||||||||||||||||||||
| 18 | is_long_between | ||||||||||||||||||||||||||||
| 19 | is_long_at_least | ||||||||||||||||||||||||||||
| 20 | is_long_at_most | ||||||||||||||||||||||||||||
| 21 | is_a | ||||||||||||||||||||||||||||
| 22 | is_like | ||||||||||||||||||||||||||||
| 23 | is_in | ||||||||||||||||||||||||||||
| 24 | /; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 22 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 27 | 'all' => \@EXPORT_OK | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 22 | our $VERSION = '1.6'; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | our %FILTERS = ( | ||||||||||||||||||||||||||||
| 33 | 16 | ▶ | trim => sub { return unless defined $_[0]; $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 10 | ▶ | strip => sub { return unless defined $_[0]; $_[0] =~ s/(\s){2,}/$1/g; $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | lc => sub { return unless defined $_[0]; lc $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 14 | ▶ | uc => sub { return unless defined $_[0]; uc $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | ucfirst => sub { return unless defined $_[0]; ucfirst $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 22 | ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 133 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 133 | my ( $input, $rules ) = @_; | |||||||||||||||||||||||||||
| 42 | 133 | my $error = {}; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Sanity check | ||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||
| 46 | 133 | ▶ | die 'You must define a fields array' unless defined $rules->{fields}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 132 | for (qw/filters checks/) { | |||||||||||||||||||||||||||
| 49 | 262 | ▶ | next unless exists $rules->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 131 | ▶ | if ( ref( $rules->{$_} ) ne 'ARRAY' || @{ $rules->{$_} } % 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2
MC/DC100%2/2
ref $rules->{$_} ne "ARRAY" or @{$rules->{$_};} % 2
ref $rules->{$_} ne "ARRAY"
@{$rules->{$_};} % 2
Truth table100%3/3
A or B
A= ref $rules->{$_} ne "ARRAY"B= @{$rules->{$_};} % 2
| |||||||||||||||||||||||||||||
| 51 | 4 | die "$_ must be an array with an even number of elements"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 128 | for ( keys %$rules ) { | |||||||||||||||||||||||||||
| 56 | 255 | ▶ | /^(fields|filters|checks)$/ or die "Unknown key $_"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 127 | my $param = {}; | |||||||||||||||||||||||||||
| 60 | 127 | ▶ | my @fields = @{ $rules->{fields} } ? @{ $rules->{fields} } : keys(%$input); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Add existing, filtered input to $param | ||||||||||||||||||||||||||||
| 63 | # | ||||||||||||||||||||||||||||
| 64 | 126 | for my $key ( @fields ) { | |||||||||||||||||||||||||||
| 65 | 224 | ▶ | exists $input->{$key} and ($param->{$key} = _process( $rules->{filters}, $input, $key )); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Process all checks for $param | ||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||
| 70 | 125 | for my $key ( @fields ) { | |||||||||||||||||||||||||||
| 71 | 223 | my $err = _process( $rules->{checks}, $param, $key, 1 ); | |||||||||||||||||||||||||||
| 72 | 222 | ▶ | $error->{$key} ||= $err if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$error->{$key} ||= $err
MC/DC0%0/2
$error->{$key} ||= $err
$error->{$key}
$err
Truth table33%1/3
A ||= B
A= $error->{$key}B= $err
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | return { | ||||||||||||||||||||||||||||
| 76 | 124 | ▶ | success => keys %$error ? 0 : 1, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | error => $error, | ||||||||||||||||||||||||||||
| 78 | data => $param | ||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 185 | ▶ | sub _run_code { | ||||||||||||||||||||||||||
|
Subroutine
_run_code: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 84 | 185 | my ( $code, $value, $param, $key ) = @_; | |||||||||||||||||||||||||||
| 85 | 185 | my $result = $value; | |||||||||||||||||||||||||||
| 86 | 185 | my $ref = ref $code; | |||||||||||||||||||||||||||
| 87 | 185 | ▶ | if ( $ref eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | 157 | $result = $code->( $value, $param, $key ); | |||||||||||||||||||||||||||
| 89 | 157 | ▶ | $value = $result unless defined $param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | elsif ( $ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 92 | 26 | for (@$code) { | |||||||||||||||||||||||||||
| 93 | 39 | $result = _run_code( $_, $value, $param, $key ); | |||||||||||||||||||||||||||
| 94 | 39 | ▶ | if ( defined $param ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 19 | ▶ | last if $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 20 | $value = $result; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 2 | die 'Filters and checks must be either sub{} or []'; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 183 | return $result; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 379 | ▶ | sub _process { | ||||||||||||||||||||||||||
|
Subroutine
_process: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 110 | 379 | my ( $pairs, $param, $key, $check ) = @_; | |||||||||||||||||||||||||||
| 111 | 379 | my $value = $param->{$key}; | |||||||||||||||||||||||||||
| 112 | 379 | my $iterator = natatime(2, @$pairs); | |||||||||||||||||||||||||||
| 113 | 379 | ▶ | while ( my ( $match, $code ) = $iterator->() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 249 | ▶ | if ( _match($key, $match) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 146 | ▶ | my $temp = _run_code( $code, $value, $check ? ($param, $key) : undef ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 144 | ▶ | if ( $check ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 97 | ▶ | return $temp if $temp | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 47 | $value = $temp; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 328 | ▶ | return if $check; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 155 | return $value; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 266 | ▶ | sub _match { | ||||||||||||||||||||||||||
|
Subroutine
_match: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 129 | 266 | my ( $a, $b ) = @_; | |||||||||||||||||||||||||||
| 130 | 266 | ▶ | if ( !ref($b) ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 131 | 231 | return $a eq $b; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | elsif ( ref($b) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 134 | 22 | return grep { $a eq $_ } @$b; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | elsif ( ref($b) eq 'Regexp' ) { | ||||||||||||||||||||||||||||
| 137 | 12 | return $a =~ $b; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||
| 140 | 1 | return 0; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 22 | ▶ | sub filter { | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 22 | my @result = (); | |||||||||||||||||||||||||||
| 146 | 22 | for (@_) { | |||||||||||||||||||||||||||
| 147 | 26 | ▶ | if ( exists $FILTERS{$_} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 25 | push @result, $FILTERS{$_}; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | 1 | die "Invalid filter: $_"; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 21 | ▶ | return @result == 1 ? $result[0] : \@result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 10 | ▶ | sub is_required { | ||||||||||||||||||||||||||
|
Subroutine
is_required: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 10 | ▶ | my $err_msg = shift || 'Required'; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 'Required'
MC/DC100%1/1
shift() || 'Required'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 159 | 37 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 160 | 37 | ▶ | return if defined $_[0] && $_[0] ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[0] and $_[0] ne ""
MC/DC100%2/2
defined $_[0] and $_[0] ne ""
defined $_[0]
$_[0] ne ""
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] ne ""
| |||||||||||||||||||||||||||||
| 161 | 20 | return $err_msg; | |||||||||||||||||||||||||||
| 162 | 10 | }; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 3 | ▶ | sub is_required_if { | ||||||||||||||||||||||||||
|
Subroutine
is_required_if: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 3 | my ( $condition, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 167 | 3 | ▶ | $condition = 0 unless defined $condition; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 3 | ▶ | $err_msg ||= 'Required'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Required"
MC/DC100%1/1
$err_msg ||= "Required"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 169 | 3 | ▶ | if ( ref($condition) && ref($condition) ne 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $condition and ref $condition ne "CODE"
MC/DC0%0/2
ref $condition and ref $condition ne "CODE"
ref $condition
ref $condition ne "CODE"
Truth table66%2/3
A and B
A= ref $conditionB= ref $condition ne "CODE"
| |||||||||||||||||||||||||||||
| 170 | 0 | croak "is_required_if condition must be CODE or SCALAR"; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 7 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 173 | 7 | my ( $value, $params ) = @_; | |||||||||||||||||||||||||||
| 174 | 7 | ▶ | my $required = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | ref($condition) eq 'CODE' | ||||||||||||||||||||||||||||
| 176 | ? $condition->($params) | ||||||||||||||||||||||||||||
| 177 | : $condition; | ||||||||||||||||||||||||||||
| 178 | 7 | ▶ | return unless $required; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 4 | ▶ | return if defined $value && $value ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and $value ne ""
MC/DC50%1/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||
| 180 | 2 | return $err_msg; | |||||||||||||||||||||||||||
| 181 | 3 | }; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | sub is_existing { | ||||||||||||||||||||||||||
|
Subroutine
is_existing: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | my $err_msg = shift || 'Must be defined'; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 'Must be defined'
MC/DC100%1/1
shift() || 'Must be defined'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 186 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 187 | 6 | ▶ | return if exists $_[1]->{$_[2]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 2 | return $err_msg; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 2 | } | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 3 | ▶ | sub is_equal { | ||||||||||||||||||||||||||
|
Subroutine
is_equal: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 3 | my ( $other, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 194 | 3 | ▶ | $err_msg ||= 'Invalid value'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Invalid value"
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 195 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 196 | 4 | ▶ | return if !defined($_[0]) || $_[0] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_[0] or $_[0] eq ""
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0]
$_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
| |||||||||||||||||||||||||||||
| 197 | 3 | ▶ | return if defined $_[1]->{$other} && $_[0] eq $_[1]->{$other}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_[1]{$other} and $_[0] eq $_[1]{$other}
MC/DC50%1/2
defined $_[1]{$other} and $_[0] eq $_[1]{$other}
defined $_[1]{$other}
$_[0] eq $_[1]{$other}
Truth table66%2/3
A and B
A= defined $_[1]{$other}B= $_[0] eq $_[1]{$other}
| |||||||||||||||||||||||||||||
| 198 | 2 | return $err_msg; | |||||||||||||||||||||||||||
| 199 | 3 | }; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 2 | ▶ | sub is_long_between { | ||||||||||||||||||||||||||
|
Subroutine
is_long_between: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 2 | my ( $min, $max, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 204 | 2 | ▶ | $err_msg ||= "Must be between $min and $max symbols"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Must be between $min and $max symbols"
MC/DC100%1/1
$err_msg ||= "Must be between $min and $max symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 205 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 206 | 6 | ▶ | return if !defined($_[0]) || $_[0] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_[0] or $_[0] eq ""
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0]
$_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
| |||||||||||||||||||||||||||||
| 207 | 5 | ▶ | return if length( $_[0] ) >= $min && length( $_[0] ) <= $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $_[0] >= $min and length $_[0] <= $max
MC/DC100%2/2
length $_[0] >= $min and length $_[0] <= $max
length $_[0] >= $min
length $_[0] <= $max
Truth table100%3/3
A and B
A= length $_[0] >= $minB= length $_[0] <= $max
| |||||||||||||||||||||||||||||
| 208 | 3 | return $err_msg; | |||||||||||||||||||||||||||
| 209 | 2 | }; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 2 | ▶ | sub is_long_at_least { | ||||||||||||||||||||||||||
|
Subroutine
is_long_at_least: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 213 | 2 | my ( $length, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 214 | 2 | ▶ | $err_msg ||= "Must be at least $length symbols"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Must be at least $length symbols"
MC/DC100%1/1
$err_msg ||= "Must be at least $length symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 215 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 216 | 4 | ▶ | return if !defined($_[0]) || $_[0] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_[0] or $_[0] eq ""
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0]
$_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
| |||||||||||||||||||||||||||||
| 217 | 3 | ▶ | return if length( $_[0] ) >= $length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 2 | return $err_msg; | |||||||||||||||||||||||||||
| 219 | 2 | }; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 2 | ▶ | sub is_long_at_most { | ||||||||||||||||||||||||||
|
Subroutine
is_long_at_most: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 223 | 2 | my ( $length, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 224 | 2 | ▶ | $err_msg ||= "Must be at the most $length symbols"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Must be at the most $length symbols"
MC/DC100%1/1
$err_msg ||= "Must be at the most $length symbols"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 225 | 5 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 226 | 5 | ▶ | return if !defined($_[0]) || $_[0] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_[0] or $_[0] eq ""
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0]
$_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
| |||||||||||||||||||||||||||||
| 227 | 4 | ▶ | return if length( $_[0] ) <= $length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 2 | return $err_msg; | |||||||||||||||||||||||||||
| 229 | 2 | }; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 2 | ▶ | sub is_a { | ||||||||||||||||||||||||||
|
Subroutine
is_a: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 2 | my ( $class, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 234 | 2 | ▶ | $err_msg ||= "Invalid value"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Invalid value"
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 235 | 5 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 236 | 5 | ▶ | return if !defined( $_[0] ) || ref( $_[0] ) eq $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $_[0] or ref $_[0] eq $class
MC/DC100%2/2
not defined $_[0] or ref $_[0] eq $class
not defined $_[0]
ref $_[0] eq $class
Truth table100%3/3
not(A) or B
A= not defined $_[0]B= ref $_[0] eq $class
| |||||||||||||||||||||||||||||
| 237 | 3 | return $err_msg; | |||||||||||||||||||||||||||
| 238 | 2 | }; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 3 | ▶ | sub is_like { | ||||||||||||||||||||||||||
|
Subroutine
is_like: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 242 | 3 | my ( $regexp, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 243 | 3 | ▶ | $err_msg ||= "Invalid value"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Invalid value"
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 244 | 3 | ▶ | croak 'Regexp expected' unless ref($regexp) eq 'Regexp'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 5 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 246 | 5 | ▶ | return if !defined($_[0]) || $_[0] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_[0] or $_[0] eq ""
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0]
$_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
| |||||||||||||||||||||||||||||
| 247 | 4 | ▶ | return if $_[0] =~ $regexp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 3 | return $err_msg; | |||||||||||||||||||||||||||
| 249 | 2 | }; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 3 | ▶ | sub is_in { | ||||||||||||||||||||||||||
|
Subroutine
is_in: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 253 | 3 | my ( $arrayref, $err_msg ) = @_; | |||||||||||||||||||||||||||
| 254 | 3 | ▶ | $err_msg ||= "Invalid value"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$err_msg ||= "Invalid value"
MC/DC100%1/1
$err_msg ||= "Invalid value"
$err_msg
Truth table100%2/2
A
A= $err_msg
| |||||||||||||||||||||||||||||
| 255 | 3 | ▶ | croak 'ArrayRef expected' unless ref($arrayref) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 5 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 257 | 5 | ▶ | return if !defined($_[0]) || $_[0] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $_[0] or $_[0] eq ""
MC/DC50%1/2
not defined $_[0] or $_[0] eq ""
not defined $_[0]
$_[0] eq ""
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
| |||||||||||||||||||||||||||||
| 258 | 4 | ▶ | return if _match( $_[0], $arrayref ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 3 | return $err_msg; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | 2 | } | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 264 | 6 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 265 | 6 | my $filters = $args{filters}; | |||||||||||||||||||||||||||
| 266 | 6 | ▶ | if ( defined $filters && ref $filters eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $filters and ref $filters eq "HASH"
MC/DC50%1/2
defined $filters and ref $filters eq "HASH"
defined $filters
ref $filters eq "HASH"
Truth table66%2/3
A and B
A= defined $filtersB= ref $filters eq "HASH"
| |||||||||||||||||||||||||||||
| 267 | 1 | for my $key ( keys %$filters ) { | |||||||||||||||||||||||||||
| 268 | 1 | ▶ | $FILTERS{$key} = $filters->{$key} if ref $filters->{$key} eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | 6 | bless \%args, $class; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 6 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 275 | 6 | my ( $self, $input, $rules, %args ) = @_; | |||||||||||||||||||||||||||
| 276 | 6 | ▶ | $self = $self->new( %args ) unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 6 | ▶ | if ( ref $input ne 'HASH' || ref $rules ne 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $input ne "HASH" or ref $rules ne "HASH"
MC/DC100%2/2
ref $input ne "HASH" or ref $rules ne "HASH"
ref $input ne "HASH"
ref $rules ne "HASH"
Truth table100%3/3
A or B
A= ref $input ne "HASH"B= ref $rules ne "HASH"
| |||||||||||||||||||||||||||||
| 279 | 2 | confess("Parameters and rules HASH refs are needed"); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 4 | $self->{input} = $input; | |||||||||||||||||||||||||||
| 283 | 4 | $self->{rules} = $rules; | |||||||||||||||||||||||||||
| 284 | 4 | $self->{result} = validate( $input, $rules ); | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 4 | return $self; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 27 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 290 | 27 | my $self = shift; | |||||||||||||||||||||||||||
| 291 | 27 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 292 | 27 | ▶ | my $sub = $AUTOLOAD =~ /::(\w+)$/ ? $1 : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | 27 | ▶ | if ( $sub =~ /(params|rules)/ ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 294 | 0 | return $self->{$sub}; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | elsif ( $sub =~ /(data|error)/ ) { | ||||||||||||||||||||||||||||
| 297 | 21 | ▶ | if ( my $field = shift ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 8 | my $fields = $self->{rules}->{fields}; | |||||||||||||||||||||||||||
| 299 | 8 | ▶ | if ( scalar(@$fields) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 6 | ▶ | croak("Undefined field $sub($field)") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | unless _match( $field, $fields ); | ||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | 5 | return $self->{result}->{$sub}->{ $field }; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | else { | ||||||||||||||||||||||||||||
| 306 | 13 | return {%{$self->{result}->{$sub}}}; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | elsif ( $sub eq 'success' ) { | ||||||||||||||||||||||||||||
| 310 | return $self->{result}->{success} | ||||||||||||||||||||||||||||
| 311 | 3 | } | |||||||||||||||||||||||||||
| 312 | elsif ( $sub eq 'to_hash' ) { | ||||||||||||||||||||||||||||
| 313 | 2 | return {%{$self->{result}}} | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | else { | ||||||||||||||||||||||||||||
| 316 | 1 | confess "Undefined method $AUTOLOAD"; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 22 | 1; | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | __END__ | ||||||||||||||||||||||||||||