Valiant/Validator/ResultSet.pm

filter: Statementstmt 89.1% 33 / 37 Branchbran 70.0% 7 / 10 Conditioncond 42.8% 3 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 71.4% 5 / 7 Podpod n/a totaltotal 75.3% 49 / 65 CC 12 SCAR 20.6
CC
12
Cov
80%
normalize_shortcut
12.0
validate_each
10.0
__ANON__
1.1
CRAP 7.9
« Valiant/Validator/Result.pm ↑ Summary Valiant.pm »
1 package DBIx::Class::Valiant::Validator::ResultSet;
2  
3 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 with 'Valiant::Validator::Each';
8  
9 35 ▶has invalid_msg => (is=>'ro', required=>1, default=>sub {_t 'invalid'});
Subroutine __ANON__: called CC=1 SCAR=0.0
10 0 ▶has validations => (is=>'ro', required=>1, default=>sub {0});
Subroutine __ANON__: not called CC=1 SCAR=1.2
11  
12 0 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: not called CC=3 SCAR=24.8
13 0 my ($class, $arg) = @_;
14 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
15 0 return { validations => 1 };
16 }
17 }
18  
19 108 ▶sub validate_each {
Subroutine validate_each: called CC=10 SCAR=23.1
20 108 my ($self, $record, $attribute, $value, $opts) = @_;
21  
22 # If a row is marked to be deleted then don't bother to validate it
23 108 ▶ my @rows = grep { not $_->is_removed } @{$value->get_cache||[]};
Condition100%2/2
$value->get_cache || []
l!l
7533
MC/DC100%1/1
$value->get_cache || []
$value->get_cache
Truth table100%2/2
A
A= $value->get_cache
Aresult
11
00
24
25 # Ok, now run validations. If we find even one row is invalid, then we need
26 # to mark the attribute as invalid.
27 108 my $found_errors = 0;
28 108 foreach my $row (@rows) {
29 119 $row->validate(%$opts); #unless $row->validated
30 119 ▶ $found_errors = 1 if $row->errors->size;
Branch: 1 branch
BranchTrueFalseTotal
if $row->errors->size 17 102 119
31 }
32  
33 # Ok, next if we are asking to aggregate the nested errors, do that
34 108 ▶ if($self->validations) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validations) 108 0 108
35 108 my $rowidx = 0;
36 108 foreach my $row (@rows) {
37 119 foreach my $importable_error ($row->errors->errors->all) {
38 26 ▶ my $local_attribute = "${attribute}[${rowidx}].@{[ $importable_error->attribute||'*' ]}";
Condition50%1/2
$importable_error->attribute || '*'
l!l
260
MC/DC0%0/1
$importable_error->attribute || '*'
$importable_error->attribute
Truth table50%1/2
A
A= $importable_error->attribute
Aresult
11
00
39 26 $record->errors->import_error($importable_error, +{attribute=>$local_attribute});
40 }
41 119 $rowidx++;
42 }
43 }
44  
45 108 ▶ my $count = scalar(@rows) > 1 ? scalar(@rows) : 2;
Branch: 1 branch
BranchTrueFalseTotal
scalar @rows > 1 ? : 40 68 108
46 108 $opts->{count} = $count; # Even if there are zero or one row since we use the plural form
47 # of the class name we need to set this to 2 so that the error message
48 # is correct grammatically.
49  
50 108 ▶ $record->errors->add($attribute, $self->invalid_msg, $opts) if $found_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $found_errors 15 93 108
51 }
52  
53 8 1;
54  
55 =head1 NAME
56  
57 DBIx::Class::Valiant::Validator::ResultSet - Verify a DBIC related resultset
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__->has_many(
85 credit_cards =>
86 'Example::Schema::Result::CreditCard',
87 { 'foreign.person_id' => 'self.id' }
88 );
89  
90 __PACKAGE__->validates(
91 credit_cards => (
92 result_set=>+{ validations=>1, skip_if_empty=>1, min=>2, max=>4 },
93 )
94 );
95  
96 =head1 DESCRIPTION
97  
98 Validations on related resultsets. Used to apply constraints on the resultset as a whole
99 (such as total number of rows) or to trigger running validations on any related row objects.
100 Any errors from related resultsets will be added as sub errors on the parent result.
101  
102 B<NOTE>: This gets added automatically for you if you setup C<accepts_nested> on the parent
103 object. So you shouldn't really ever need to use this code directly.
104  
105 =head1 ATTRIBUTES
106  
107 This validator supports the following attributes:
108  
109 =head2 validations
110  
111 Boolean. Default is 0 ('false'). Used to trigger validations on row objects found inside the
112 resultset. Please keep in mind this can be expensive if you have a lot of found rows (consider
113 using limits and validating in chunks).
114  
115 Please keep in mind these errors will be localized to the associated object, not on the current
116 object.
117  
118 =head2 invalid_msg
119  
120 Error message returned on the current object if we find any errors inside related objects.
121 defaults to tag 'invalid_msg'.
122  
123 =head1 SHORTCUT FORM
124  
125 This validator supports the follow shortcut forms:
126  
127 validates attribute => ( result_set => 1, ... );
128  
129 Which is the same as:
130  
131 validates attribute => (
132 result_set => {
133 validations => 1,
134 }
135 );
136  
137 Which is a shortcut when you wish to run validations on the related rows
138  
139 =head1 GLOBAL PARAMETERS
140  
141 This validator supports all the standard shared parameters: C<if>, C<unless>,
142 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
143  
144 =head1 SEE ALSO
145
146 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
147  
148 =head1 AUTHOR
149
150 See L<Valiant>
151
152 =head1 COPYRIGHT & LICENSE
153
154 See L<Valiant>
155  
156 =cut
« Valiant/Validator/Result.pm ↑ Summary Valiant.pm »