Mojolicious/Validator/Validation.pm

filter: Statementstmt 28.3% 21 / 74 Branchbran 3.3% 1 / 30 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 33.3% 7 / 21 Podpod 92.3% 12 / 13 totaltotal 24.8% 41 / 165 CC 24 SCAR 29.6
CC
24
Cov
18%
optional
56.0
check
30.0
_csrf_valid
30.0
CRAP 19.4
« Mojo/Server/Morbo/Backend/Poll.pm ↑ Summary Mojolicious/Renderer.pm »
1 package Mojolicious::Validator::Validation;
2 20 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::DynamicMethods -dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Mojo::Util qw(secure_compare);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 has [qw(csrf_token topic validator)];
10 0 ▶has [qw(input output)] => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
11  
12 6 ▶sub BUILD_DYNAMIC {
Subroutine BUILD_DYNAMIC: called CC=1 SCAR=3.9
Pod undocumented
13 6 my ($class, $method, $dyn_methods) = @_;
14  
15 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
16 0 my $self = shift;
17 0 my $dynamic = $dyn_methods->{$self->validator}{$method};
18 0 ▶ return $self->check($method => @_) if $dynamic;
Branch: 1 branch
BranchTrueFalseTotal
if $dynamic 0 0 0
19 0 my $package = ref $self;
20 0 Carp::croak qq{Can't locate object method "$method" via package "$package"};
21 6 };
22 }
23  
24 0 ▶sub check {
Subroutine check: not called CC=5 SCAR=34.0
Pod documented
25 0 my ($self, $check) = (shift, shift);
26  
27 0 ▶ return $self unless $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 0 0
28  
29 0 my $cb = $self->validator->checks->{$check};
30 0 my $name = $self->topic;
31 0 my $values = $self->output->{$name};
32 0 ▶ for my $value (ref $values eq 'ARRAY' ? @$values : $values) {
Branch: 1 branch
BranchTrueFalseTotal
ref $values eq 'ARRAY' ? : 0 0 0
33 0 ▶ next unless my $result = $self->$cb($name, $value, @_);
Branch: 1 branch
BranchTrueFalseTotal
unless my $result = $self->$cb($name, $value, @_) 0 0 0
34 0 return $self->error($name => [$check, $result, @_]);
35 }
36  
37 0 return $self;
38 }
39  
40 0 ▶sub csrf_protect {
Subroutine csrf_protect: not called CC=2 SCAR=17.9
Pod documented
41 0 my $self = shift;
42 0 my $token = $self->input->{csrf_token};
43 0 ▶ $self->error(csrf_token => ['csrf_protect']) unless _csrf_valid($token, $self->csrf_token);
Branch: 1 branch
BranchTrueFalseTotal
unless _csrf_valid($token, $self->csrf_token) 0 0 0
44 0 return $self;
45 }
46  
47 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
48 0 my ($self, $name) = (shift, shift);
49 0 ▶ return $self->{error}{$name} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
50 0 $self->{error}{$name} = shift;
51 0 delete $self->output->{$name};
52 0 return $self;
53 }
54  
55 0 ▶sub every_param {
Subroutine every_param: not called CC=4 SCAR=30.0
Pod documented
56 0 ▶ return [] unless defined(my $value = $_[0]->output->{$_[1] // $_[0]->topic});
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $value = $_[0]->output->{$_[1] // $_[0]->topic}) 0 0 0
Condition0%0/3
$_[1] // $_[0]->topic
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] // $_[0]->topic
$_[1] $_[0]->topic
Truth table0%0/3
A // B
A= $_[1]B= $_[0]->topic
ABresult
1X1
011
000
57 0 ▶ return [ref $value eq 'ARRAY' ? @$value : $value];
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
58 }
59  
60 0 ▶sub failed { [sort keys %{shift->{error}}] }
Subroutine failed: not called CC=1 SCAR=6.9
Pod documented
61  
62 0 ▶sub has_data { !!keys %{shift->input} }
Subroutine has_data: not called CC=1 SCAR=6.9
Pod documented
63  
64 0 ▶sub has_error { $_[1] ? exists $_[0]{error}{$_[1]} : !!keys %{$_[0]{error}} }
Subroutine has_error: called CC=2 SCAR=9.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 11 0 11
65  
66 0 ▶sub is_valid { exists $_[0]->output->{$_[1] // $_[0]->topic} }
Subroutine is_valid: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$_[1] // $_[0]->topic
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] // $_[0]->topic
$_[1] $_[0]->topic
Truth table0%0/3
A // B
A= $_[1]B= $_[0]->topic
ABresult
1X1
011
000
67  
68 0 ▶sub optional {
Subroutine optional: not called CC=7 SCAR=40.3
Pod documented
69 0 my ($self, $name, @filters) = @_;
70  
71 0 ▶ return $self->topic($name) unless defined(my $input = $self->input->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $input = $self->input->{$name}) 0 0 0
72  
73 0 ▶ my @input = ref $input eq 'ARRAY' ? @$input : ($input);
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq 'ARRAY' ? : 0 0 0
74 0 for my $cb (map { $self->validator->filters->{$_} } @filters) {
75 0 @input = map { $self->$cb($name, $_) } @input;
76 }
77 0 ▶ $self->output->{$name} = @input > 1 ? \@input : $input[0] if @input && !grep { !defined } @input;
Branch: 2 branches
BranchTrueFalseTotal
if @input and not grep({not defined $_;} @input) 0 0 0
@input > 1 ? : 0 0 0
Condition0%0/3
@input and not grep({not defined $_;} @input)
!ll&&!rl&&r
000
MC/DC0%0/2
@input and not grep({not defined $_;} @input)
@input not grep({not defined $_;} @input)
Truth table0%0/3
A and not(B)
A= @inputB= not grep({not defined $_;} @input)
ABresult
0X0
100
111
78  
79 0 return $self->topic($name);
80 }
81  
82 0 ▶sub param { shift->every_param(shift)->[-1] }
Subroutine param: not called CC=1 SCAR=6.9
Pod documented
83  
84 0 ▶sub passed { [sort keys %{shift->output}] }
Subroutine passed: not called CC=1 SCAR=6.9
Pod documented
85  
86 0 ▶sub required {
Subroutine required: not called CC=2 SCAR=17.9
Pod documented
87 0 my ($self, $name) = (shift, shift);
88 0 ▶ return $self if $self->optional($name, @_)->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->optional($name, @_)->is_valid 0 0 0
89 0 return $self->error($name => ['required']);
90 }
91  
92 0 ▶sub _csrf_valid {
Subroutine _csrf_valid: not called CC=5 SCAR=34.0
93 0 my ($token, $csrf) = @_;
94 0 ▶ return undef unless defined $token && !ref $token && $token =~ /^[0-9a-f]{80}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and not ref $token and $token =~ /^[0-9a-f]{80}$/u 0 0 0
Condition0%0/6
defined $token and not ref $token and $token =~ /^[0-9a-f]{80}$/u
!ll&&!rl&&r
000
defined $token and not ref $token
!ll&&!rl&&r
000
MC/DC0%0/3
defined $token and not ref $token and $token =~ /^[0-9a-f]{80}$/u
defined $token not ref $token $token =~ /^[0-9a-f]{80}$/u
Truth table0%0/4
A and not(B) and C
A= defined $tokenB= not ref $tokenC= $token =~ /^[0-9a-f]{80}$/u
ABCresult
0XX0
10X0
1100
1111
95  
96 # Unmask token to protect against BREACH attacks
97 0 my $mask = pack 'H*', substr $token, 0, 40;
98 0 my $masked = pack 'H*', substr $token, 40;
99 0 ▶ return secure_compare unpack('H*', $mask ^ $masked), $csrf // '';
Condition0%0/2
$csrf // ""
l!l
00
MC/DC0%0/1
$csrf // ""
$csrf
Truth table0%0/2
A
A= $csrf
Aresult
11
00
100 }
101  
102 20 1;
103  
104 =encoding utf8
105  
106 =head1 NAME
107  
108 Mojolicious::Validator::Validation - Perform validations
109  
110 =head1 SYNOPSIS
111  
112 use Mojolicious::Validator;
113 use Mojolicious::Validator::Validation;
114  
115 my $validator = Mojolicious::Validator->new;
116 my $v = Mojolicious::Validator::Validation->new(validator => $validator);
117 $v->input({foo => 'bar'});
118 $v->required('foo')->in('bar', 'baz');
119 say $v->param('foo');
120  
121 =head1 DESCRIPTION
122  
123 L<Mojolicious::Validator::Validation> performs L<Mojolicious::Validator> validation checks.
124  
125 =head1 ATTRIBUTES
126  
127 L<Mojolicious::Validator::Validation> implements the following attributes.
128  
129 =head2 csrf_token
130  
131 my $token = $v->csrf_token;
132 $v = $v->csrf_token('fa6a08...');
133  
134 CSRF token.
135  
136 =head2 input
137  
138 my $input = $v->input;
139 $v = $v->input({foo => 'bar', baz => [123, 'yada']});
140  
141 Data to be validated.
142  
143 =head2 output
144  
145 my $output = $v->output;
146 $v = $v->output({foo => 'bar', baz => [123, 'yada']});
147  
148 Validated data.
149  
150 =head2 topic
151  
152 my $topic = $v->topic;
153 $v = $v->topic('foo');
154  
155 Name of field currently being validated.
156  
157 =head2 validator
158  
159 my $v = $v->validator;
160 $v = $v->validator(Mojolicious::Validator->new);
161  
162 L<Mojolicious::Validator> object this validation belongs to.
163  
164 =head1 METHODS
165  
166 L<Mojolicious::Validator::Validation> inherits all methods from L<Mojo::Base> and implements the following new ones.
167  
168 =head2 check
169  
170 $v = $v->check('size', 2, 7);
171  
172 Perform validation check on all values of the current L</"topic">, no more checks will be performed on them after the
173 first one failed. All checks from L<Mojolicious::Validator/"CHECKS"> are supported.
174  
175 =head2 csrf_protect
176  
177 $v = $v->csrf_protect;
178  
179 Validate C<csrf_token> and protect from cross-site request forgery. Tokens are masked with a random value to protect
180 against BREACH attacks.
181  
182 =head2 error
183  
184 my $err = $v->error('foo');
185 $v = $v->error(foo => ['custom_check']);
186 $v = $v->error(foo => [$check, $result, @args]);
187  
188 Get or set details for failed validation check, at any given time there can only be one per field.
189  
190 # Details about failed validation
191 my ($check, $result, @args) = @{$v->error('foo')};
192  
193 # Force validation to fail for a field without performing a check
194 $v->error(foo => ['some_made_up_check_name']);
195  
196 =head2 every_param
197  
198 my $values = $v->every_param;
199 my $values = $v->every_param('foo');
200  
201 Similar to L</"param">, but returns all values sharing the same name as an array reference.
202  
203 # Get first value
204 my $first = $v->every_param('foo')->[0];
205  
206 =head2 failed
207  
208 my $names = $v->failed;
209  
210 Return an array reference with all names for values that failed validation.
211  
212 # Names of all values that failed
213 say for @{$v->failed};
214  
215 =head2 has_data
216  
217 my $bool = $v->has_data;
218  
219 Check if L</"input"> is available for validation.
220  
221 =head2 has_error
222  
223 my $bool = $v->has_error;
224 my $bool = $v->has_error('foo');
225  
226 Check if validation resulted in errors, defaults to checking all fields.
227  
228 =head2 is_valid
229  
230 my $bool = $v->is_valid;
231 my $bool = $v->is_valid('foo');
232  
233 Check if validation was successful and field has a value, defaults to checking the current L</"topic">.
234  
235 =head2 optional
236  
237 $v = $v->optional('foo');
238 $v = $v->optional('foo', @filters);
239  
240 Change validation L</"topic"> and apply filters. All filters from L<Mojolicious::Validator/"FILTERS"> are supported.
241  
242 # Trim value and check size
243 $v->optional('user', 'trim')->size(1, 15);
244  
245 =head2 param
246  
247 my $value = $v->param;
248 my $value = $v->param('foo');
249  
250 Access validated values, defaults to the current L</"topic">. If there are multiple values sharing the same name, and
251 you want to access more than just the last one, you can use L</"every_param">.
252  
253 # Get value right away
254 my $user = $v->optional('user')->size(1, 15)->param;
255  
256 =head2 passed
257  
258 my $names = $v->passed;
259  
260 Return an array reference with all names for values that passed validation.
261  
262 # Names of all values that passed
263 say for @{$v->passed};
264  
265 =head2 required
266  
267 $v = $v->required('foo');
268 $v = $v->required('foo', @filters);
269  
270 Change validation L</"topic">, apply filters, and make sure a value is present. All filters from
271 L<Mojolicious::Validator/"FILTERS"> are supported.
272  
273 # Trim value and check size
274 $v->required('user', 'trim')->size(1, 15);
275  
276 =head1 CHECKS
277  
278 In addition to the L</"ATTRIBUTES"> and L</"METHODS"> above, you can also call validation checks provided by
279 L</"validator"> on L<Mojolicious::Validator::Validation> objects, similar to L</"check">.
280  
281 # Call validation checks
282 $v->required('foo')->size(2, 5)->like(qr/^[A-Z]/);
283 $v->optional('bar')->equal_to('foo');
284 $v->optional('baz')->in('test', '123');
285  
286 # Longer version
287 $v->required('foo')->check('size', 2, 5)->check('like', qr/^[A-Z]/);
288  
289 =head1 SEE ALSO
290  
291 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
292  
293 =cut
« Mojo/Server/Morbo/Backend/Poll.pm ↑ Summary Mojolicious/Renderer.pm »