| 1 | package DBIx::Class::Valiant::Validator::ResultSet; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Valiant::I18N; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | with 'Valiant::Validator::Each'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | 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 | sub normalize_shortcut { | ||||||||||||||||||||||||||||
| 13 | 0 | ▶ | my ($class, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
normalize_shortcut: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | if(($arg eq '1') || ($arg eq 'nested')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$arg eq 1 or $arg eq "nested"
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"
| |||||||||||||||||||||||||||||
| 15 | 0 | return { validations => 1 }; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub validate_each { | ||||||||||||||||||||||||||||
| 20 | 76 | ▶ | my ($self, $record, $attribute, $value, $opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_each: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # If a row is marked to be deleted then don't bother to validate it | ||||||||||||||||||||||||||||
| 23 | 76 | ▶ | my @rows = grep { not $_->is_removed } @{$value->get_cache||[]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 76 | my $found_errors = 0; | |||||||||||||||||||||||||||
| 28 | 76 | foreach my $row (@rows) { | |||||||||||||||||||||||||||
| 29 | 67 | $row->validate(%$opts); #unless $row->validated | |||||||||||||||||||||||||||
| 30 | 67 | ▶ | $found_errors = 1 if $row->errors->size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Ok, next if we are asking to aggregate the nested errors, do that | ||||||||||||||||||||||||||||
| 34 | 76 | ▶ | if($self->validations) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 76 | my $rowidx = 0; | |||||||||||||||||||||||||||
| 36 | 76 | foreach my $row (@rows) { | |||||||||||||||||||||||||||
| 37 | 67 | foreach my $importable_error ($row->errors->errors->all) { | |||||||||||||||||||||||||||
| 38 | 24 | ▶ | my $local_attribute = "${attribute}.${rowidx}.@{[ $importable_error->attribute||'*' ]}"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$importable_error->attribute || '*'
MC/DC0%0/1
$importable_error->attribute || '*'
$importable_error->attribute
Truth table50%1/2
A
A= $importable_error->attribute
| |||||||||||||||||||||||||||||
| 39 | 24 | $record->errors->import_error($importable_error, +{attribute=>$local_attribute}); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 67 | $rowidx++; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 76 | ▶ | $record->errors->add($attribute, $self->invalid_msg, $opts) if $found_errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | 1; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 NAME | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | DBIx::Class::Valiant::Validator::ResultSet - Verify a DBIC related resultset | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | package Example::Schema::Result::Person; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | use base 'Example::Schema::Result'; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | __PACKAGE__->load_components(qw/ | ||||||||||||||||||||||||||||
| 61 | Valiant::Result | ||||||||||||||||||||||||||||
| 62 | Core | ||||||||||||||||||||||||||||
| 63 | /); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | __PACKAGE__->table("person"); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | __PACKAGE__->add_columns( | ||||||||||||||||||||||||||||
| 68 | id => { data_type => 'bigint', is_nullable => 0, is_auto_increment => 1 }, | ||||||||||||||||||||||||||||
| 69 | username => { data_type => 'varchar', is_nullable => 0, size => 48 }, | ||||||||||||||||||||||||||||
| 70 | first_name => { data_type => 'varchar', is_nullable => 0, size => 24 }, | ||||||||||||||||||||||||||||
| 71 | last_name => { data_type => 'varchar', is_nullable => 0, size => 48 }, | ||||||||||||||||||||||||||||
| 72 | password => { | ||||||||||||||||||||||||||||
| 73 | data_type => 'varchar', | ||||||||||||||||||||||||||||
| 74 | is_nullable => 0, | ||||||||||||||||||||||||||||
| 75 | size => 64, | ||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | __PACKAGE__->has_many( | ||||||||||||||||||||||||||||
| 80 | credit_cards => | ||||||||||||||||||||||||||||
| 81 | 'Example::Schema::Result::CreditCard', | ||||||||||||||||||||||||||||
| 82 | { 'foreign.person_id' => 'self.id' } | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | __PACKAGE__->validates( | ||||||||||||||||||||||||||||
| 86 | credit_cards => ( | ||||||||||||||||||||||||||||
| 87 | result_set=>+{ validations=>1, skip_if_empty=>1, min=>2, max=>4 }, | ||||||||||||||||||||||||||||
| 88 | ) | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Validations on related resultsets. Used to apply constraints on the resultset as a whole | ||||||||||||||||||||||||||||
| 94 | (such as total number of rows) or to trigger running validations on any related row objects. | ||||||||||||||||||||||||||||
| 95 | Any errors from related resultsets will be added as sub errors on the parent result. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | B<NOTE>: This gets added automatically for you if you setup C<accepts_nested> on the parent | ||||||||||||||||||||||||||||
| 98 | object. So you shouldn't really ever need to use this code directly. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | This validator supports the following attributes: | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 validations | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Boolean. Default is 0 ('false'). Used to trigger validations on row objects found inside the | ||||||||||||||||||||||||||||
| 107 | resultset. Please keep in mind this can be expensive if you have a lot of found rows (consider | ||||||||||||||||||||||||||||
| 108 | using limits and validating in chunks). | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Please keep in mind these errors will be localized to the associated object, not on the current | ||||||||||||||||||||||||||||
| 111 | object. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head2 invalid_msg | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Error message returned on the current object if we find any errors inside related objects. | ||||||||||||||||||||||||||||
| 116 | defaults to tag 'invalid_msg'. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 SHORTCUT FORM | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | This validator supports the follow shortcut forms: | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | validates attribute => ( result_set => 1, ... ); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Which is the same as: | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | validates attribute => ( | ||||||||||||||||||||||||||||
| 127 | result_set => { | ||||||||||||||||||||||||||||
| 128 | validations => 1, | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Which is a shortcut when you wish to run validations on the related rows | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 GLOBAL PARAMETERS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | This validator supports all the standard shared parameters: C<if>, C<unless>, | ||||||||||||||||||||||||||||
| 137 | C<message>, C<strict>, C<allow_undef>, C<allow_blank>. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | See L<Valiant> | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | See L<Valiant> | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||