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