Lazy/Role/Check/Form.pm

filter: Statementstmt 100% 39 / 39 Branchbran 83.3% 10 / 12 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 93.0% 67 / 72 CC 13 SCAR 20.1
CC
13
Cov
95%
check
11.0
before_check
3.1
BEGIN
1.0
CRAP 7.5
« Lazy/Role/Check/Trim.pm ↑ Summary Lazy/Role/Check.pm »
1 package Validator::Lazy::Role::Check::Form;
2  
3 3 our $VERSION = '0.03';
4  
5 =head1 NAME
6  
7 Validator::Lazy::Role::Check::Form
8  
9  
10 =head1 VERSION
11  
12 Version 0.03
13  
14  
15 =head1 SYNOPSIS
16  
17 use Validator::Lazy;
18 my $v = Validator::Lazy->new( Form => [ simple list of field names ] ); # empty, or list, or scalar with 2letters countrycode
19  
20 BUT! Form validation is a more complicaded construction than simple scalars, so we should use more advansed configuration,
21 so we may use YAML or something like it as a config.
22  
23 Some examples:
24  
25 my $v = Validator::Lazy->new( 'string without \n interprets as a file name. extention will interprets as a file format' );
26 my $v = Validator::Lazy->new( 'string WITH \n's interprets as YAML' );
27 my $v = Validator::Lazy->new( { HASH interprets as a ready for work config } );
28  
29 my $ok = $v->check( { form => { Form => { hash of field-name => value pairs to check } } } );
30  
31 say Dumper $v->errors;
32  
33 Please check more details about forms in Validator::Lazy
34  
35  
36 =head1 DESCRIPTION
37  
38 An internal Role for Validator::Lazy, part of Validator::Lazy package.
39  
40 Provides "Form" type for Validator::Lazy config.
41 Allows to check hashes as a key-value forms.
42  
43  
44 =head1 METHODS
45  
46 =head2 C<check>
47  
48 Called from inside if Validator::Lazy->check process
49  
50 Temporary overrides internal Validator::Lazy::check method like this:
51  
52 $validator->check( $value, $param );
53  
54  
55 =head1 SUPPORT AND DOCUMENTATION
56  
57 After installing, you can find documentation for this module with the perldoc command.
58  
59 perldoc Validator::Lazy
60  
61 You can also look for information at:
62  
63 RT, CPAN's request tracker (report bugs here)
64 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy
65  
66 AnnoCPAN, Annotated CPAN documentation
67 http://annocpan.org/dist/Validator-Lazy
68  
69 CPAN Ratings
70 http://cpanratings.perl.org/d/Validator-Lazy
71  
72 Search CPAN
73 http://search.cpan.org/dist/Validator-Lazy/
74  
75  
76 =head1 AUTHOR
77  
78 ANTONC <antonc@cpan.org>
79  
80 =head1 LICENSE
81  
82 This program is free software; you can redistribute it and/or modify it
83 under the terms of the the Artistic License (2.0). You may obtain a
84 copy of the full license at:
85  
86 L<http://www.perlfoundation.org/artistic_license_2_0>
87  
88 =cut
89  
90 3 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 28 ▶sub before_check {
Subroutine before_check: called CC=3 SCAR=11.4
Pod undocumented
96 28 my ( $self, $value ) = @_;
97  
98 28 ▶ return $value unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 28 28
99  
100 28 ▶ confess 'Form should be a Hashref!' unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 28 28
101  
102 28 return $value;
103 }
104  
105 28 ▶sub check {
Subroutine check: called CC=11 SCAR=24.0
Pod documented
106 28 my ( $self, $value, $param ) = @_;
107  
108 28 ▶ $param = [] unless $param && ref $param eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $param and ref $param eq "ARRAY" 3 25 28
Condition66%2/3
$param and ref $param eq "ARRAY"
!ll&&!rl&&r
0325
MC/DC50%1/2
$param and ref $param eq "ARRAY"
$param ref $param eq "ARRAY"
Truth table66%2/3
A and B
A= $paramB= ref $param eq "ARRAY"
ABresult
0X0
100
111
109  
110 28 my @prefixes;
111 my @fixed_params;
112  
113 28 for my $par ( @$param ) {
114 221 ▶ if ( $par && ref $par eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($par and ref $par eq 'ARRAY') { } 4 217 221
elsif (not $par) { } 4 213 217
elsif (@prefixes) { } 44 169 213
Condition100%3/3
$par and ref $par eq "ARRAY"
!ll&&!rl&&r
42134
MC/DC100%2/2
$par and ref $par eq "ARRAY"
$par ref $par eq "ARRAY"
Truth table100%3/3
A and B
A= $parB= ref $par eq "ARRAY"
ABresult
0X0
100
111
115 4 @prefixes = @$par;
116 }
117 elsif ( !$par ) {
118 4 @prefixes = ();
119 }
120 elsif ( @prefixes ) {
121 44 push @fixed_params, $_ . '_' . $par for @prefixes;
122 }
123 else {
124 169 push @fixed_params, $par;
125 };
126 };
127  
128 # All fields mentioned in Form params should be checked,
129 # because we should check ALL form params, not only passed
130 28 ▶ $value->{$_} //= undef for @fixed_params;
Condition100%2/2
$value->{$_} //= undef
l!l
29352
MC/DC100%1/1
$value->{$_} //= undef
$value->{$_}
Truth table100%2/2
A
A= $value->{$_}
Aresult
11
00
131  
132 28 my $validator = Validator::Lazy->new( $self->config );
133 28 push @{ $validator->form_stack }, ( @{ $self->form_stack }, $self->current_field );
134 28 $validator->check( %$value );
135  
136 28 push @{ $self->errors }, @{ $validator->errors() };
137 28 push @{ $self->warnings }, @{ $validator->warnings() };
138  
139 28 return $value;
140 };
141  
142 3 1;
« Lazy/Role/Check/Trim.pm ↑ Summary Lazy/Role/Check.pm »