Valiant/Validator/Result.pm

filter: Statementstmt 83.7% 31 / 37 Branchbran 66.6% 8 / 12 Conditioncond 40.0% 4 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 75.0% 6 / 8 Podpod n/a totaltotal 69.4% 50 / 72 CC 12 SCAR 20.8
CC
12
Cov
73%
normalize_shortcut
12.0
validate_each
11.0
__ANON__
1.1
CRAP 8.0
« Valiant/Validator/SetSize.pm ↑ Summary Valiant/Validator/ResultSet.pm »
1 package DBIx::Class::Valiant::Validator::Result;
2  
3 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Valiant::Util 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use namespace::autoclean -also => ['debug'];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 with 'Valiant::Validator::Each';
9  
10 71 ▶has invalid_msg => (is=>'ro', required=>1, default=>sub {_t 'invalid'});
Subroutine __ANON__: called CC=1 SCAR=0.0
11 0 ▶has validations => (is=>'ro', required=>1, default=>sub {0});
Subroutine __ANON__: not called CC=1 SCAR=1.2
12  
13 0 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: not called CC=3 SCAR=24.8
14 0 my ($class, $arg) = @_;
15 0 ▶ if(($arg eq '1') || ($arg eq 'nested')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq 1 or $arg eq "nested") 0 0 0
Condition0%0/3
$arg eq 1 or $arg eq "nested"
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg eq 1 or $arg eq "nested"
$arg eq 1 $arg eq "nested"
Truth table0%0/3
A or B
A= $arg eq 1B= $arg eq "nested"
ABresult
1X1
011
000
16 0 return { validations => 1 };
17 }
18 }
19  
20 215 ▶sub validate_each {
Subroutine validate_each: called CC=10 SCAR=23.9
21 215 my ($self, $record, $attribute, $result, $opts) = @_;
22  
23 215 ▶ unless(defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 59 156 215
24 59 my $rel_data = $record->relationship_info($attribute);
25 59 ▶ if($rel_data->{attrs}{accessor} eq 'single' && $rel_data->{attrs}{join_type}||'' eq 'LEFT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_data->{'attrs'}{'accessor'} eq 'single' and $rel_data->{'attrs'}{'join_type'} or !1) { } 59 0 59
Condition40%2/5
$rel_data->{"attrs"}{"accessor"} eq "single" and $rel_data->{"attrs"}{"join_type"} or
l!l
590
$rel_data->{"attrs"}{"accessor"} eq "single" and $rel_data->{"attrs"}{"join_type"}
!ll&&!rl&&r
0059
MC/DC0%0/2
$rel_data->{"attrs"}{"accessor"} eq "single" and $rel_data->{"attrs"}{"join_type"} or
$rel_data->{"attrs"}{"accessor"} eq "single" $rel_data->{"attrs"}{"join_type"}
Truth table33%1/3
A and B
A= $rel_data->{"attrs"}{"accessor"} eq "single"B= $rel_data->{"attrs"}{"join_type"}
ABresult
111
0X0
100
26 # Its an optional relation like 'might have' so its not an error to be undefined.
27 59 return;
28 } else {
29 0 $record->errors->add($attribute, $self->invalid_msg, $opts);
30 0 return;
31 }
32 }
33  
34 # If a row is marked to be deleted then don't bother to validate it.
35 156 ▶ return if $result->is_marked_for_deletion;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_marked_for_deletion 1 155 156
36 155 ▶ return unless $self->validations;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validations 0 155 155
37  
38 155 debug 2, "About to run validations for @{[$result]}";
39 155 $result->validate(%$opts);#;# unless $opts->{Scalar::Util::refaddr $result}; # $result->validated;
40 155 ▶ $record->errors->add($attribute, $self->invalid_msg, $opts) if $result->errors->size;
Branch: 1 branch
BranchTrueFalseTotal
if $result->errors->size 31 124 155
41  
42 # Not sure if this should be default behavior or not...
43 #$result->errors->each(sub {
44 # my ($attr, $message) = @_;
45 # $record->errors->add("${attribute}.${attr}", $message);
46 #});
47  
48 155 foreach my $importable_error ($result->errors->errors->all) {
49 72 ▶ $record->errors->import_error($importable_error, +{attribute=>"${attribute}.@{[ $importable_error->attribute||'*' ]}"});
Condition100%2/2
$importable_error->attribute || '*'
l!l
711
MC/DC100%1/1
$importable_error->attribute || '*'
$importable_error->attribute
Truth table100%2/2
A
A= $importable_error->attribute
Aresult
11
00
50 }
51 }
52  
53 11 1;
54  
55 =head1 NAME
56  
57 DBIx::Class::Valiant::Validator::Result - Verify a DBIC related result
58  
59 =head1 SYNOPSIS
60  
61 package Example::Schema::Result::Person;
62  
63 use base 'Example::Schema::Result';
64  
65 __PACKAGE__->load_components(qw/
66 Valiant::Result
67 Core
68 /);
69  
70 __PACKAGE__->table("person");
71  
72 __PACKAGE__->add_columns(
73 id => { data_type => 'bigint', is_nullable => 0, is_auto_increment => 1 },
74 username => { data_type => 'varchar', is_nullable => 0, size => 48 },
75 first_name => { data_type => 'varchar', is_nullable => 0, size => 24 },
76 last_name => { data_type => 'varchar', is_nullable => 0, size => 48 },
77 password => {
78 data_type => 'varchar',
79 is_nullable => 0,
80 size => 64,
81 },
82 );
83  
84 __PACKAGE__->might_have(
85 profile =>
86 'Example::Schema::Result::Profile',
87 { 'foreign.person_id' => 'self.id' }
88 );
89  
90 __PACKAGE__->validates(profile => (result=>+{validations=>1}, on=>'profile' ));
91  
92  
93 =head1 DESCRIPTION
94  
95 Trigger validations on a related result and aggregates any errors as nested errors
96 on the parent class.
97  
98 B<NOTE>: This gets added automatically for you if you setup C<accepts_nested> on the parent
99 object. So you shouldn't really ever need to use this code directly.
100  
101 =head1 ATTRIBUTES
102  
103 This validator supports the following attributes:
104  
105 =head2 validations
106  
107 Boolean. Default is 0 ('false'). Used to trigger validations on the related result.
108  
109 Please keep in mind these errors will be localized to the associated object, not on the current
110 object.
111  
112 =head2 invalid_msg
113  
114 String or translation tag of the error when the result is not valid. This will be in addition
115 to any errors nested from the related result.
116  
117 =head1 SHORTCUT FORM
118  
119 This validator supports the follow shortcut forms:
120  
121 validates attribute => ( result => 1, ... );
122  
123 Which is the same as:
124  
125 validates attribute => (
126 result => {
127 validations => 1,
128 }
129 );
130  
131 Which is a shortcut when you wish to run validations on the related rows
132  
133 =head1 GLOBAL PARAMETERS
134  
135 This validator supports all the standard shared parameters: C<if>, C<unless>,
136 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
137  
138 =head1 SEE ALSO
139
140 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
141  
142 =head1 AUTHOR
143
144 See L<Valiant>
145
146 =head1 COPYRIGHT & LICENSE
147
148 See L<Valiant>
149  
150 =cut
« Valiant/Validator/SetSize.pm ↑ Summary Valiant/Validator/ResultSet.pm »