blib/lib/DBIx/Class/Validation.pm

filter: Statementstmt 100% 55 / 55 Branchbran 100% 26 / 26 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 98.0% 101 / 103 CC 16 SCAR 12.8
CC
16
Cov
99%
validate
6.0
validation
5.0
validation_module
4.0
CRAP 3.6
↑ Summary
1 # $Id$
2 package DBIx::Class::Validation;
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 our $VERSION = '0.02005';
6  
7 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶ use base qw/DBIx::Class Class::Accessor::Grouped/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶ use English qw/-no_match_vars/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶ use FormValidator::Simple 0.17;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶ use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 __PACKAGE__->mk_group_accessors('inherited', qw/
14 validation_profile
15 validation_auto
16 validation_filter
17 _validation_module_accessor
18 /);
19 };
20 3 __PACKAGE__->validation_auto(1);
21 3 __PACKAGE__->validation_module('FormValidator::Simple');
22  
23 =head1 NAME
24  
25 DBIx::Class::Validation - Validate all data before submitting to your database.
26  
27 =head1 SYNOPSIS
28  
29 In your base L<"DBIx::Class"> package:
30  
31 __PACKAGE__->load_components(qw/... Validation/);
32  
33 And in your subclasses:
34  
35 __PACKAGE__->validation(
36 module => 'FormValidator::Simple',
37 profile => { ... },
38 filter => 0,
39 auto => 1,
40 );
41  
42 And then somewhere else:
43  
44 eval{ $obj->validate() };
45 if( my $results = $EVAL_ERROR ){
46 ...
47 }
48  
49 =head1 METHODS
50  
51 =head2 validation
52  
53 __PACKAGE__->validation(
54 module => 'FormValidator::Simple',
55 profile => { ... },
56 filter => 0,
57 auto => 1,
58 );
59  
60 Calls L</"validation_module">, L</"validation_profile"> and L</"validation_auto">
61 if the corresponding argument is defined.
62  
63 =cut
64  
65 2 ▶sub validation {
Subroutine validation: called CC=5 SCAR=16.1
Pod documented
66 2 my ($self, %args) = @_;
67  
68 2 ▶ $self->validation_module($args{module}) if exists $args{module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"module"} 1 1 2
69 2 ▶ $self->validation_profile($args{profile}) if exists $args{profile};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"profile"} 1 1 2
70 2 ▶ $self->validation_auto($args{auto}) if exists $args{auto};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"auto"} 1 1 2
71 2 ▶ $self->validation_filter($args{filter}) if exists $args{filter};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"filter"} 1 1 2
72 };
73  
74 =head2 validation_module
75  
76 __PACKAGE__->validation_module('Data::FormValidator');
77  
78 Sets the validation module to use. Any module that supports a check() method
79 just like L<"Data::FormValidator">'s can be used here, such as
80 L<"FormValidator::Simple">.
81  
82 Defaults to FormValidator::Simple.
83  
84 =cut
85  
86 21 ▶sub validation_module {
Subroutine validation_module: called CC=4 SCAR=13.9
Pod documented
87 21 my ($self, $class) = @_;
88  
89 21 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 7 14 21
90 7 ▶ if (!eval "require $class") {
Branch: 2 branches
BranchTrueFalseTotal
if (not eval "require $class") { } 1 6 7
elsif (not $class->can('check')) { } 1 5 6
91 1 $self->throw_exception("Unable to load the validation module '$class' because $@");
92 } elsif (!$class->can('check')) {
93 1 $self->throw_exception("The '$class' module does not support the check() method");
94 } else {
95 5 $self->_validation_module_accessor($class->new);
96 };
97 };
98  
99 19 return ref $self->_validation_module_accessor;
100 };
101  
102 =head2 validation_profile
103  
104 __PACKAGE__->validation_profile(
105 { ... }
106 );
107  
108 Sets the profile that will be passed to the validation module. Expects either
109 a HASHREF or a reference to a subroutine. If it's a subref it will be passed
110 the result row object as it's first parameter so that you can perform complex
111 data validation for cases when you'd like to have access to the actual result.
112  
113 For example, you could use the following to return an error if the named field
114 is not unique in the table:
115  
116 my $profile = sub {
117 my $result = shift @_;
118
119 return {
120 required => [qw/email/],
121 constraint_methods => {
122 email => sub {
123 my ($dvf, $val) = @_;
124 return $result->result_source->resultset->find({email=>$val}) ? 0:1;
125 },
126 },
127 };
128 };
129  
130 Please note that the subref needs to return a hashref/arrayref suitable for use
131 in the validation module you have chosen.
132  
133 =head2 validation_auto
134  
135 __PACKAGE__->validation_auto( 1 );
136  
137 Turns on and off auto-validation. This feature makes all UPDATEs and
138 INSERTs call the L</"validate"> method before doing anything.
139  
140 The default is for validation_auto is to be on.
141  
142 =head2 validation_filter
143  
144 __PACKAGE__->validation_filter( 1 );
145  
146 Turns on and off validation filters. When on, this feature will make all
147 UPDATEs and INSERTs modify your data to that of the values returned by
148 your validation modules C<check> method. This is primarily meant for use
149 with L<"Data::FormValidator"> but may be used with any validation module
150 that returns a results object that supports a C<valid()> method just
151 like L<"Data::FormValidator::Results">.
152  
153 B<Filters modify your data, so use them carefully>.
154  
155 The default is for validation_filter is to be off.
156  
157 =head2 validate
158  
159 $obj->validate();
160  
161 Validates all the data in the object against the pre-defined validation
162 module and profile. If there is a problem then a hard error will be
163 thrown. If you put the validation in an eval you can capture whatever
164 the module's check() method returned.
165  
166 =cut
167  
168 12 ▶sub validate {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
169 12 my $self = shift;
170 12 my %data = $self->get_columns;
171 12 my $module = $self->validation_module;
172 12 my $profile = $self->validation_profile;
173  
174 12 ▶ if (ref $profile eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $profile eq "CODE") 2 10 12
175 2 $profile = $profile->($self);
176 };
177 12 my $result = $module->check( \%data => $profile );
178  
179 12 ▶ if ($result->success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->success) { } 6 6 12
180 6 ▶ if ($self->validation_filter && $result->can('valid')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validation_filter and $result->can("valid")) 2 4 6
Condition66%2/3
$self->validation_filter and $result->can("valid")
!ll&&!rl&&r
402
MC/DC50%1/2
$self->validation_filter and $result->can("valid")
$self->validation_filter $result->can("valid")
Truth table66%2/3
A and B
A= $self->validation_filterB= $result->can("valid")
ABresult
0X0
100
111
181 2 $self->$_($result->valid($_)) for ($result->valid);
182 };
183 6 return $result;
184 } else {
185 6 croak $result;
186 };
187 };
188  
189 =head1 EXTENDED METHODS
190  
191 The following L<"DBIx::Class::Row"> methods are extended by this module:-
192  
193 =over 4
194  
195 =item insert
196  
197 =cut
198  
199 11 ▶sub insert {
Subroutine insert: called CC=2 SCAR=6.9
Pod documented
200 11 my $self = shift;
201 11 ▶ $self->validate if $self->validation_auto;
Branch: 1 branch
BranchTrueFalseTotal
if $self->validation_auto 10 1 11
202 6 $self->next::method(@_);
203 }
204  
205 =item update
206  
207 =cut
208  
209 3 ▶sub update {
Subroutine update: called CC=3 SCAR=11.0
Pod documented
210 3 my $self = shift;
211 3 my $columns = shift;
212  
213 3 ▶ $self->set_inflated_columns($columns) if $columns;
Branch: 1 branch
BranchTrueFalseTotal
if $columns 1 2 3
214 3 ▶ $self->validate if $self->validation_auto;
Branch: 1 branch
BranchTrueFalseTotal
if $self->validation_auto 2 1 3
215 2 $self->next::method(@_);
216 };
217  
218 3 1;
219 __END__
↑ Summary