| 1 | package Validator::LIVR::Rules::Conditional; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Validator::LIVR::Rules::Types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '2.22'; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | # LIVR 2.5: rules that depend on other fields. A reference, such as "type" or "address/country", | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # is a path from the containing object: the hash that a rule gets as $all_values. That is the | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # hash of the validator, of nested_object, list_of_objects and the other metarules for objects. | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # Aliases and "or" give the hash of their caller, and list_of, values and keys an empty hash | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 115 | ▶ | sub required_if { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
required_if: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 115 | my ($conditions) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # Arguments after the object are ignored. The last one is the rule builders | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 115 | ▶ | if ( @_ < 2 || ref($conditions) ne 'HASH' || !%$conditions ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@_ < 2 or ref $conditions ne "HASH" or not %$conditions
@_ < 2 or ref $conditions ne "HASH"
MC/DC100%3/3
@_ < 2 or ref $conditions ne "HASH" or not %$conditions
@_ < 2
ref $conditions ne "HASH"
not %$conditions
Truth table100%4/4
A or B or not(C)
A= @_ < 2B= ref $conditions ne "HASH"C= not %$conditions
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | die "Rule [required_if] takes an object of references and values\n"; | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # A path and the values as strings, so 1 matches "1" and true matches "true" | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 112 | my @entries; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 112 | foreach my $reference ( sort keys %$conditions ) { | |||||||||||||||||||||||||||||||||||||||||
| 26 | 114 | my $path = parse_reference( $reference, 'required_if' ); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 105 | my $expected = $conditions->{$reference}; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # An empty list never matches | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 105 | ▶ | die qq{Rule [required_if] takes a primitive value other than "" or a list of them for "$reference"\n} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $expected eq "ARRAY" and not @$expected
MC/DC100%2/2
ref $expected eq "ARRAY" and not @$expected
ref $expected eq "ARRAY"
not @$expected
Truth table100%3/3
A and not(B)
A= ref $expected eq "ARRAY"B= not @$expected
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | if ref($expected) eq 'ARRAY' && !@$expected; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 104 | my %is_expected; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 104 | ▶ | foreach my $value ( ref($expected) eq 'ARRAY' ? @$expected : $expected ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 112 | my $string = _expected_value($value); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 112 | ▶ | die qq{Rule [required_if] takes a primitive value other than "" or a list of them for "$reference"\n} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $string or $string eq ""
MC/DC100%2/2
not defined $string or $string eq ""
not defined $string
$string eq ""
Truth table100%3/3
not(A) or B
A= not defined $stringB= $string eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | if !defined($string) || $string eq ''; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 106 | $is_expected{$string} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 98 | push @entries, [ $path, \%is_expected ]; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 106 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 106 | my ( $value, $all_values ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 106 | ▶ | return if defined($value) && $value ne ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value ne ""
MC/DC100%2/2
defined $value and $value ne ""
defined $value
$value ne ""
Truth table100%3/3
A and B
A= defined $valueB= $value ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # Required when every referenced value is a non-empty primitive value from its list | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 86 | foreach my $entry (@entries) { | |||||||||||||||||||||||||||||||||||||||||
| 51 | 88 | my $string = _reference_string( $all_values, $entry->[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 88 | ▶ | return if !defined($string) || !$entry->[1]{$string}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $string and $entry->[1]{$string}
MC/DC100%2/2
defined $string and $entry->[1]{$string}
defined $string
$entry->[1]{$string}
Truth table100%3/3
A and B
A= defined $stringB= $entry->[1]{$string}
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 39 | return 'REQUIRED'; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 96 | }; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 112 | ▶ | sub _expected_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expected_value: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 112 | my $value = shift; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 112 | ▶ | return undef if !defined($value) || ( ref($value) && ref($value) !~ /Boolean/ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $value or ref $value and not ref($value) =~ /Boolean/
ref $value and not ref($value) =~ /Boolean/
MC/DC100%3/3
not defined $value or ref $value and not ref($value) =~ /Boolean/
not defined $value
ref $value
not ref($value) =~ /Boolean/
Truth table100%4/4
not(A) or B and not(C)
A= not defined $valueB= ref $valueC= not ref($value) =~ /Boolean/
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 108 | ▶ | return ref($value) ? ( $value ? 'true' : 'false' ) : Validator::LIVR::Rules::Types::string_form($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # when(reference, condition, rules_if_true, [rules_if_false], rule_builders): the condition form | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # when(reference, cases, rule_builders): the switch form | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # Arguments after the 4th are ignored | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 200 | ▶ | sub when { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
when: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 200 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 200 | my $rule_builders = pop @args; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 200 | ▶ | if ( @args < 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | die "Rule [when] takes a reference and cases, or a reference, a condition, rules if true " | |||||||||||||||||||||||||||||||||||||||||
| 74 | . "and optional rules if false\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 198 | ▶ | splice @args, 4 if @args > 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # A reference is a string: Perl tells a number from a string only by how it was created | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 198 | require B if !Validator::LIVR::Rules::Types::HAS_CREATED_AS_NUMBER; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 198 | ▶ | if ( defined( $args[0] ) && !ref( $args[0] ) && Validator::LIVR::Rules::Types::is_number( $args[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $args[0] and not ref $args[0] and Validator::LIVR::Rules::Types::is_number($args[0])
defined $args[0] and not ref $args[0]
MC/DC100%3/3
defined $args[0] and not ref $args[0] and Validator::LIVR::Rules::Types::is_number($args[0])
defined $args[0]
not ref $args[0]
Validator::LIVR::Rules::Types::is_number($args[0])
Truth table100%4/4
A and not(B) and C
A= defined $args[0]B= not ref $args[0]C= Validator::LIVR::Rules::Types::is_number($args[0])
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | die "Rule [when] takes a reference as a non-empty string\n"; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 197 | my $path = parse_reference( $args[0], 'when' ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 187 | ▶ | return _switch( $path, $args[1], $rule_builders ) if @args == 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 103 | my ( $condition, $rules_if_true, $rules_if_false ) = _field_rules( $rule_builders, @args[ 1 .. $#args ] ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 96 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 96 | my ( $value, $all_values, $output_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | # The condition rules see the fields next to the field, their output is discarded | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 96 | my $ref_value = resolve_reference( $all_values, $path ); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 96 | ▶ | my $is_true = defined($ref_value) && $ref_value ne '' | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $ref_value && $ref_value ne '' && !&$condition($ref_value, $all_values, \(my $output = $ref_value))
defined $ref_value and $ref_value ne ""
MC/DC100%3/3
defined $ref_value && $ref_value ne '' && !&$condition($ref_value, $all_values, \(my $output = $ref_value))
defined $ref_value
$ref_value ne ""
!&$condition($ref_value, $all_values, \(my $output = $ref_value))
Truth table100%4/4
A and B && not(C)
A= defined $ref_valueB= $ref_value ne ""C= !&$condition($ref_value, $all_values, \(my $output = $ref_value))
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | && !$condition->( $ref_value, $all_values, \( my $output = $ref_value ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 96 | ▶ | return _run_branch( $is_true ? $rules_if_true : $rules_if_false, $value, $all_values, $output_ref ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 93 | }; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 84 | ▶ | sub _switch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_switch: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 84 | my ( $path, $cases, $rule_builders ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 84 | ▶ | die "Rule [when] takes an object of values and rules as cases\n" if ref($cases) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 81 | ▶ | die "Rule [when] needs at least one case\n" if !%$cases; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 79 | my @case_values = sort keys %$cases; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 79 | my %case_rules; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 79 | @case_rules{@case_values} = _field_rules( $rule_builders, map { $cases->{$_} } @case_values ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 80 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 80 | my ( $value, $all_values, $output_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # An empty or unknown referenced value chooses no case. An empty value passes unchanged | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # (the referenced field reports its own errors), any other value would pass unchecked | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 80 | my $string = _reference_string( $all_values, $path ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 80 | ▶ | my $rules = defined($string) ? $case_rules{$string} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 80 | ▶ | if ( !defined $rules ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 48 | ▶ | return if !defined($value) || $value eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value or $value eq ""
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value
$value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 34 | return 'NOT_ALLOWED_VALUE'; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 32 | return _run_branch( $rules, $value, $all_values, $output_ref ); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 74 | }; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # The rules of a branch get the value as it is, empty values too, and the fields next to the field | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 128 | ▶ | sub _run_branch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_branch: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 128 | my ( $rules, $value, $all_values, $output_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # No rules if false: the value passes and stays in the output unchanged | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 128 | ▶ | return if !$rules; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 124 | my $output = $value; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 124 | my $err_code = $rules->( $value, $all_values, \$output ); | |||||||||||||||||||||||||||||||||||||||||
| 137 | 124 | ▶ | return $err_code if $err_code; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 78 | $$output_ref = $output; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 78 | return; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | # Rule sets in any form that a field takes, each as one rule, from one validator. null and an | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # empty list die: they are almost always a mistake, "any" is a rule set without rules | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 182 | ▶ | sub _field_rules { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_field_rules: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 182 | my ( $rule_builders, @rule_sets ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 182 | my %livr; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 182 | foreach my $i ( 0 .. $#rule_sets ) { | |||||||||||||||||||||||||||||||||||||||||
| 150 | 406 | my $rules = $rule_sets[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 406 | Validator::LIVR::Rules::Types::check_rule_set( 'when', $rules ); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 396 | $livr{"when$i"} = $rules; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 172 | my $validator = Validator::LIVR->_new_with_rules( \%livr, $rule_builders )->prepare(); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 167 | return map { $validator->_field_rule("when$_") } 0 .. $#rule_sets; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # The string form of the referenced value when it is a non-empty primitive value, otherwise undef | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 168 | ▶ | sub _reference_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reference_string: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 168 | my $value = resolve_reference(@_); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 168 | ▶ | return undef if !defined($value) || $value eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value or $value eq ""
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value
$value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 111 | ▶ | if ( ref($value) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 11 | ▶ | return undef if ref($value) !~ /Boolean/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | ▶ | return $value ? 'true' : 'false'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 100 | return Validator::LIVR::Rules::Types::string_form($value); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | # "a/b/0" as [ 'a', 'b', '0' ]. "~1" stands for "/" and "~0" for "~" as in JSON Pointer (RFC 6901). | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # Not a string, an empty reference, a leading or trailing "/", an empty segment ("a//b") and a | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # wrong escape ("~2", a trailing "~") die. A leading "/" is reserved for references from the root | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 311 | ▶ | sub parse_reference { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_reference: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 311 | my ( $reference, $rule_name ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 311 | ▶ | if ( !defined($reference) || ref($reference) || $reference eq '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $reference or ref $reference or $reference eq ""
not defined $reference or ref $reference
MC/DC100%3/3
not defined $reference or ref $reference or $reference eq ""
not defined $reference
ref $reference
$reference eq ""
Truth table100%4/4
not(A) or B or C
A= not defined $referenceB= ref $referenceC= $reference eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 4 | die "Rule [$rule_name] takes a reference as a non-empty string\n"; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # The common case: a field name | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 307 | ▶ | return [$reference] if index( $reference, '/' ) < 0 && index( $reference, '~' ) < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
index($reference, "/") == -1 and index($reference, "~") == -1
MC/DC100%2/2
index($reference, "/") == -1 and index($reference, "~") == -1
index($reference, "/") == -1
index($reference, "~") == -1
Truth table100%3/3
A and B
A= index($reference, "/") == -1B= index($reference, "~") == -1
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 54 | ▶ | if ( substr( $reference, 0, 1 ) eq '/' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | die qq{Rule [$rule_name] got the reference "$reference": a leading "/" is reserved for references from the root\n}; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 51 | my @path = split /\//, $reference, -1; | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 51 | foreach my $segment (@path) { | |||||||||||||||||||||||||||||||||||||||||
| 192 | 97 | ▶ | die qq{Rule [$rule_name] got the reference "$reference" with an empty segment\n} if $segment eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 91 | ▶ | next if index( $segment, '~' ) < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 11 | ▶ | die qq{Rule [$rule_name] got the reference "$reference" with a wrong escape\n} if $segment =~ /~(?![01])/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 5 | $segment =~ s/~1/\//g; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 5 | $segment =~ s/~0/~/g; | |||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 39 | return \@path; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | # The input value at the path, or undef when a segment is missing or a value on the way is not a | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # hash (or a list for an index segment: "0" or a number without leading zeros) | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 264 | ▶ | sub resolve_reference { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_reference: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 264 | my ( $value, $path ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 264 | foreach my $segment (@$path) { | |||||||||||||||||||||||||||||||||||||||||
| 210 | 298 | my $type = ref($value); | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 298 | ▶ | if ( $type eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 280 | ▶ | return undef if !exists $value->{$segment}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 227 | $value = $value->{$segment}; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } elsif ( $type eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 16 | ▶ | return undef if $segment !~ /\A(?:0|[1-9][0-9]*)\z/ || $segment >= @$value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $segment =~ /\A(?:0|[1-9][0-9]*)\z/ or $segment >= @$value
MC/DC100%2/2
not $segment =~ /\A(?:0|[1-9][0-9]*)\z/ or $segment >= @$value
not $segment =~ /\A(?:0|[1-9][0-9]*)\z/
$segment >= @$value
Truth table100%3/3
not(A) or B
A= not $segment =~ /\A(?:0|[1-9][0-9]*)\z/B= $segment >= @$value
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 6 | $value = $value->[$segment]; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 199 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | Validator::LIVR::Rules::Conditional - LIVR 2.5 rules of Validator::LIVR that depend on other fields: required_if and when | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | This module has the code of these rules, which are documented in | ||||||||||||||||||||||||||||||||||||||||||
| 235 | L<Validator::LIVR/LIVR 2.5 rules>. Validator::LIVR loads it when a validator | ||||||||||||||||||||||||||||||||||||||||||
| 236 | uses one of the rules. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | Each sub is the builder of the rule with the same name. | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | =cut | ||||||||||||||||||||||||||||||||||||||||||