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 9 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
18 9 my $self = shift;
19 9 my @rules = @{$self->_validations};
20 9 $self->_validations([]);
21 9 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 26 ▶sub validations {
Subroutine validations: called CC=2 SCAR=6.9
Pod documented
31 26 my ($self, $arg) = @_;
32 26 ▶ if($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 14 12 26
33 $self->_validations([
34 14 @{$self->_validations},
35 $arg,
36 ]);
37 }
38 26 return @{$self->_validations};
39 }
40  
41 12 ▶around 'validate', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 12 my ($orig, $self, $target, @args) = @_;
43 12 my $new_self = bless +{%$self, for=>$target}, ref($self);
44 12 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::Proxy::Object->new(
56 validations => [
57 sub { unless($_[0]->is_active) { $_[0]->errors->add(undef, '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 '*' => [
96 'Cannot change inactive user'
97 ],
98 'age' => [
99 'Age must be a positive integer'
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 isn't enforced but please don't rely on that).
130  
131 =head2 validations
132  
133 Should be an arrayref of validation rules, where each rule is an arrayref containing
134 the rules (where the rules are anything you'd pass to C<validates> in L<Valiant::Validations>
135  
136 =head1 METHODS
137  
138 This class does the following methods
139  
140 =head2 validate
141  
142 Given an instance of the object to be validated, return a result objects that wraps it
143 and provides any validation errors.
144  
145 =head2 validates
146  
147 Adds validation rules.
148  
149 =head2 validates_with
150  
151 Adds a validation object.
152  
153 =head1 SEE ALSO
154
155 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
156  
157 =head1 AUTHOR
158
159 See L<Valiant>
160  
161 =head1 COPYRIGHT & LICENSE
162
163 See L<Valiant>
164  
165 =cut
« Valiant/Validator/Inclusion.pm ↑ Summary Valiant/Validator/Boolean.pm »