Valiant/Proxy.pm

filter: Statementstmt 100% 27 / 27 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 95.0% 38 / 40 CC 5 SCAR 9.4
CC
5
Cov
97%
BUILD
4.0
validations
2.0
BEGIN
1.0
CRAP 2.6
« Valiant/Validator/Inclusion.pm ↑ Summary Valiant/Validator/Boolean.pm »
1 package Valiant::Proxy;
2  
3 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 with 'Valiant::Validates';
6  
7 4 requires 'read_attribute_for_validation';
8  
9 4 has 'for' => (is=>'ro', init_arg=>undef, weak_ref=>1);
10  
11 has '_validations' => (
12 is => 'rw',
13 init_arg => 'validations',
14 required => 1,
15 1 ▶ default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 sub BUILD {
18 8 ▶ my $self = shift;
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
19 8 my @rules = @{$self->_validations};
20 8 $self->_validations([]);
21 8 foreach my $rules(@rules) {
22 12 ▶ if(ref($rules) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rules eq 'CODE') { } 1 11 12
elsif (ref $rules eq 'ARRAY') { } 11 0 11
23 1 $self->validates_with($rules);
24 } elsif(ref($rules) eq 'ARRAY') {
25 11 $self->validates(@$rules);
26 }
27 }
28 }
29  
30 sub validations {
31 25 ▶ my ($self, $arg) = @_;
Subroutine validations: called CC=2 SCAR=6.9
Pod documented
32 25 ▶ if($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 14 11 25
33 $self->_validations([
34 14 @{$self->_validations},
35 $arg,
36 ]);
37 }
38 25 return @{$self->_validations};
39 }
40  
41 around 'validate', sub {
42 11 ▶ my ($orig, $self, $target, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
43 11 my $new_self = bless +{%$self, for=>$target}, ref($self);
44 11 return $new_self->$orig(@args);
45 4 };
46  
47 4 1;
48  
49 =head1 NAME
50  
51 Valiant::Proxy - Create a validation ruleset dynamically
52  
53 =head1 SYNOPSIS
54  
55 my $validator = Valiant::Class->new(
56 validations => [
57 [ sub { unless($_[0]->is_active) { $_[0]->errors->add(_base=>'Cannot change inactive user') } } ],
58 [ name => length => [2,15], format => qr/[a-zA-Z ]+/ ],
59 [ age => numericality => 'positive_integer' ],
60 ]
61 );
62  
63 You can also call an API to add validation rules
64  
65 $validator
66 ->validates(name => length => [2,15], format => qr/[a-zA-Z ]+/)
67 ->validates(age => numericality => 'positive_integer')
68 ->validates_with('UserValidator'); # Calls Local::Test::User::UserValidator
69  
70 Then runs validation on it with an instance of a concrete class
71 that has no validation rules of its own:
72  
73 package Local::Test::User {
74  
75 use Moo;
76  
77 has ['name', 'age', 'is_active'],
78 is=>'ro',
79 required=>1;
80 }
81  
82 # A user with several validation issues
83 my $user = Local::Test::User->new(
84 name=>'01',
85 age=>-15,
86 is_active=>0);
87  
88 my $result = $validator->validate($user);
89  
90 $result->invalid; # TRUE
91  
92 warn $result->errors->_dump;
93  
94 $VAR = {
95 '_base' => [
96 'Cannot change inactive user'
97 ],
98 'age' => [
99 'Age must be greater than or equal to zero'
100 ],
101 'name' => [
102 'Name does not match the required pattern'
103 ]
104 };
105  
106 =head1 DESCRIPTION
107  
108 Create a validation runner for a given class or role. Useful when you need (or prefer)
109 to build up a validation ruleset in code rather than via the annotations-like approach
110 given in L<Valiant::Validations>. Can also be useful to add validations to a class that
111 isn't Moo/se and can't use L<Valiant::Validations> or is outside your control (such as
112 a third party library). Lastly you may need to build validation sets based on existing
113 metadata, such as via database introspection or from a file containing validation
114 instructions.
115  
116 Please note that the code used to create the validation object is not speed optimized so
117 I recommend you not use this approach in 'hot' code paths. Its probably best if you can
118 create all these during your application startup once (for long lived applications). Maybe
119 not ideal for 'fire and forget' scripts like cron jobs or CGI.
120  
121 =head1 ATTRIBUTES
122  
123 This object has the followed attributes
124  
125 =head2 for
126  
127 The class this validator is for. Used to load locale files and to look for custom
128 validation objects. Should something that ISA or DOES of the class that you are going
129 to run validations on (this currently isnt enforced but please to rely on that).
130  
131 =head2 result_class
132  
133 Defaults to L<Valiant::Result::Object>. Needs to be something that does L<Valiant::Result>.
134 Write your own if you have an object with unusual attribute accessors.
135  
136 =head2 meta_class
137  
138 Defaults to L<Valiant::Meta>. Should be something that is a subclass of that. You
139 probably won't overrride this unless you are doing extremely odd stuff.
140  
141 =head2 validations
142  
143 Should be an arrayref of validation rules, where each rule is an arrayref containing
144 the rules (where the rules are anything you'd pass to C<validates> in L<Valiant::Validations>
145  
146 =head1 METHODS
147  
148 This class does the following methods
149  
150 =head2 validate
151  
152 Given an instance of the object to be validated, return a result objects that wraps it
153 and provides any validation errors.
154  
155 =head2 validates
156  
157 Adds validation rules.
158  
159 =head2 validates_with
160  
161 Adds a validation object.
162  
163 =head1 SEE ALSO
164
165 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
166  
167 =head1 AUTHOR
168
169 See L<Valiant>
170  
171 =head1 COPYRIGHT & LICENSE
172
173 See L<Valiant>
174  
175 =cut
« Valiant/Validator/Inclusion.pm ↑ Summary Valiant/Validator/Boolean.pm »