| 1 | package Data::TreeValidator::Constraints; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 5 | $Data::TreeValidator::Constraints::VERSION = '0.04'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: A collection of constraints for validating data | ||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Data::TreeValidator::Util qw( fail_constraint ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Set::Object qw( set ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | exports => [ qw( length options required type ) ] | ||||||||||||||||||||||||||||
| 14 | 5 | }; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | sub required { \&_required } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 17 | ▶ | sub _required { | ||||||||||||||||||||||||||
|
Subroutine
_required: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 18 | 17 | local $_ = shift; | |||||||||||||||||||||||||||
| 19 | 17 | ▶ | fail_constraint("Required") unless defined $_ && "$_" ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_ and "$_" ne ""
MC/DC100%2/2
defined $_ and "$_" ne ""
defined $_
"$_" ne ""
Truth table100%3/3
A and B
A= defined $_B= "$_" ne ""
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | sub length { | ||||||||||||||||||||||||||
|
Subroutine
length: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 24 | 2 | my ($min, $max) = @args{qw( min max )}; | |||||||||||||||||||||||||||
| 25 | 8 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 26 | 8 | my ($input) = @_; | |||||||||||||||||||||||||||
| 27 | 8 | ▶ | my $length = defined $input ? length($input) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | fail_constraint("Input must be longer than $min characters") | ||||||||||||||||||||||||||||
| 30 | 8 | ▶ | if exists $args{min} && $length < $min; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"min"} and $length < $min
MC/DC100%2/2
exists $args{"min"} and $length < $min
exists $args{"min"}
$length < $min
Truth table100%3/3
A and B
A= exists $args{"min"}B= $length < $min
| |||||||||||||||||||||||||||||
| 31 | fail_constraint("Input must be shorter than $max characters") | ||||||||||||||||||||||||||||
| 32 | 5 | ▶ | if exists $args{max} && $length > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args{"max"} and $length > $max
MC/DC50%1/2
exists $args{"max"} and $length > $max
exists $args{"max"}
$length > $max
Truth table66%2/3
A and B
A= exists $args{"max"}B= $length > $max
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 2 | } | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | sub options { | ||||||||||||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 2 | my $valid = set(@_); | |||||||||||||||||||||||||||
| 38 | 9 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 9 | my ($input) = @_; | |||||||||||||||||||||||||||
| 40 | 9 | ▶ | fail_constraint("Input must be a valid set member") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | unless $valid->contains($input); | ||||||||||||||||||||||||||||
| 42 | 2 | }; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | sub type { | ||||||||||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 2 | my $type = shift; | |||||||||||||||||||||||||||
| 47 | 13 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 13 | ▶ | fail_constraint('Input must be of type: ' . $type->name . '"') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | unless $type->check(@_); | ||||||||||||||||||||||||||||
| 50 | 2 | }; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 5 | 1; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | __END__ | ||||||||||||||||||||||||||||