blib/lib/Dancer/Plugin/KossyValidator.pm

filter: Statementstmt 19.2% 26 / 135 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 35.0% 7 / 20 Podpod n/a totaltotal 13.9% 33 / 237 CC 43 SCAR 60.2
CC
43
Cov
12%
__ANON__
870.0
valid
20.0
__ANON__
12.0
CRAP 411.1
↑ Summary
1 package Dancer::Plugin::KossyValidator;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 our $VERSION = '0.04';
12  
13 our %VALIDATOR = (
14 0 ▶ NOT_NULL => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
15 0 my ($req,$val) = @_;
16 0 ▶ return if not defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
17 0 ▶ return if $val eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" 0 0 0
18 0 return 1;
19 },
20 0 ▶ CHOICE => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
21 0 my ($req, $val, @args) = @_;
22 0 for my $c (@args) {
23 0 ▶ if ($c eq $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq $val) 0 0 0
24 0 return 1;
25 }
26 }
27 0 return;
28 },
29 0 ▶ INT => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
30 0 my ($req,$val) = @_;
31 0 ▶ return if not defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
32 0 $val =~ /^\-?[\d]+$/;
33 },
34 0 ▶ UINT => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
35 0 my ($req,$val) = @_;
36 0 ▶ return if not defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
37 0 $val =~ /^\d+$/;
38 },
39 0 ▶ NATURAL => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
40 0 my ($req,$val) = @_;
41 0 ▶ return if not defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
42 0 ▶ $val =~ /^\d+$/ && $val > 0;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /^\d+$/ and $val > 0 0 0 0
43 },
44 0 ▶ '@SELECTED_NUM' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
45 0 my ($req,$vals,@args) = @_;
46 0 my ($min,$max) = @args;
47 0 ▶ scalar(@$vals) >= $min && scalar(@$vals) <= $max
Branch: 1 branch
BranchTrueFalseTotal
scalar @$vals >= $min and scalar @$vals <= $max 0 0 0
48 },
49 0 ▶ '@SELECTED_UNIQ' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
50 0 my ($req,$vals) = @_;
51 0 my %vals;
52 0 $vals{$_} = 1 for @$vals;
53 0 scalar(@$vals) == scalar keys %vals;
54 },
55 2 );
56  
57 0 ▶register validator => sub {
Subroutine __ANON__: not called CC=29 SCAR=67.7
58 0 ▶ my $rule = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
59  
60 0 my @errors;
61 0 my $valid = Hash::MultiValue->new;
62 0 my $req = request;
63  
64 0 ▶ for ( my $i=0; $i < @$rule; $i = $i+2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rule) 0 0 0
65 0 my $param = $rule->[$i];
66 0 my $constraints;
67 0 my $param_name = $param;
68 0 $param_name =~ s!^@!!;
69 0 my @vals = param($param_name);
70 0 ▶ my $vals = ( $param =~ m!^@! ) ? \@vals : [$vals[-1]];
Branch: 1 branch
BranchTrueFalseTotal
$param =~ /^@/ ? : 0 0 0
71  
72 0 ▶ if ( ref($rule->[$i+1]) && ref($rule->[$i+1]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$rule[$i + 1] and ref $$rule[$i + 1] eq 'HASH') { } 0 0 0
Condition0%0/3
ref $$rule[$i + 1] and ref $$rule[$i + 1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $$rule[$i + 1] and ref $$rule[$i + 1] eq "HASH"
ref $$rule[$i + 1] ref $$rule[$i + 1] eq "HASH"
Truth table0%0/3
A and B
A= ref $$rule[$i + 1]B= ref $$rule[$i + 1] eq "HASH"
ABresult
0X0
100
111
73 0 ▶ if ( $param !~ m!^@! && !$VALIDATOR{NOT_NULL}->($req,$vals->[0]) && exists $rule->[$i+1]->{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $param =~ /^@/ || $Dancer::Plugin::KossyValidator::VALIDATOR{"NOT_NULL"}->($req, $vals->[0]) and exists $$rule[$i + 1]{"default"}) 0 0 0
Condition0%0/6
not $param =~ /^@/ || $Dancer::Plugin::KossyValidator::VALIDATOR{"NOT_NULL"}->($req, $vals->[0]) and exists $$rule[$i + 1]{"default"}
!ll&&!rl&&r
000
$param =~ /^@/ || $Dancer::Plugin::KossyValidator::VALIDATOR{'NOT_NULL'}->($req, $vals->[0])
l!l&&r!l&&!r
000
MC/DC0%0/3
not $param =~ /^@/ || $Dancer::Plugin::KossyValidator::VALIDATOR{"NOT_NULL"}->($req, $vals->[0]) and exists $$rule[$i + 1]{"default"}
$param =~ /^@/ $Dancer::Plugin::KossyValidator::VALIDATOR{'NOT_NULL'}->($req, $vals->[0]) exists $$rule[$i + 1]{"default"}
Truth table0%0/4
not(A || B) and C
A= $param =~ /^@/B= $Dancer::Plugin::KossyValidator::VALIDATOR{'NOT_NULL'}->($req, $vals->[0])C= exists $$rule[$i + 1]{"default"}
ABCresult
1XX0
01X0
0000
0011
74 0 my $default = $rule->[$i+1]->{default};
75 0 $vals = [$default];
76 }
77 0 $constraints = $rule->[$i+1]->{rule};
78 }
79 else {
80 0 $constraints = $rule->[$i+1];
81 }
82  
83 0 my $error;
84 0 PARAM_CONSTRAINT: for my $constraint ( @$constraints ) {
85 0 ▶ if ( ref($constraint->[0]) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $constraint->[0] eq 'ARRAY') { } 0 0 0
elsif (ref $constraint->[0] eq 'CODE') { } 0 0 0
86 0 my @constraint = @{$constraint->[0]};
87 0 my $constraint_name = shift @constraint;
88 0 ▶ if ( ref($constraint_name) && ref($constraint_name) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $constraint_name and ref $constraint_name eq "CODE") 0 0 0
Condition0%0/3
ref $constraint_name and ref $constraint_name eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $constraint_name and ref $constraint_name eq "CODE"
ref $constraint_name ref $constraint_name eq "CODE"
Truth table0%0/3
A and B
A= ref $constraint_nameB= ref $constraint_name eq "CODE"
ABresult
0X0
100
111
89 0 for my $val ( @$vals ) {
90 0 ▶ if ( !$constraint_name->($req, $val, @constraint) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$constraint_name($req, $val, @constraint)) 0 0 0
91 0 push @errors, { param => $param_name, message => $constraint->[1] };
92 0 $error=1;
93 0 last PARAM_CONSTRAINT;
94 }
95 }
96 0 next PARAM_CONSTRAINT;
97 }
98 0 ▶ die "constraint:$constraint_name not found" if ! exists $VALIDATOR{$constraint_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Dancer::Plugin::KossyValidator::VALIDATOR{$constraint_name} 0 0 0
99 0 ▶ if ( $constraint_name =~ m!^@! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint_name =~ /^@/) { } 0 0 0
100 0 ▶ if ( !$VALIDATOR{$constraint_name}->($req,$vals,@constraint) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Dancer::Plugin::KossyValidator::VALIDATOR{$constraint_name}->($req, $vals, @constraint)) 0 0 0
101 0 push @errors, { param => $param_name, message => $constraint->[1] };
102 0 $error=1;
103 0 last PARAM_CONSTRAINT;
104 }
105 }
106 else {
107 0 for my $val ( @$vals ) {
108 0 ▶ if ( !$VALIDATOR{$constraint_name}->($req,$val,@constraint) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Dancer::Plugin::KossyValidator::VALIDATOR{$constraint_name}->($req, $val, @constraint)) 0 0 0
109 0 push @errors, { param => $param_name, message => $constraint->[1] };
110 0 $error=1;
111 0 last PARAM_CONSTRAINT;
112 }
113 }
114 }
115 }
116 elsif ( ref($constraint->[0]) eq 'CODE' ) {
117 0 for my $val ( @$vals ) {
118 0 ▶ if ( !$constraint->[0]->($req, $val) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($constraint->[0]->($req, $val)) 0 0 0
119 0 push @errors, { param => $param_name, message => $constraint->[1] };
120 0 $error=1;
121 0 last PARAM_CONSTRAINT;
122 }
123 }
124 }
125 else {
126 0 ▶ die "constraint:".$constraint->[0]." not found" if ! exists $VALIDATOR{$constraint->[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Dancer::Plugin::KossyValidator::VALIDATOR{$constraint->[0]} 0 0 0
127 0 ▶ if ( $constraint->[0] =~ m!^@! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint->[0] =~ /^@/) { } 0 0 0
128 0 ▶ if ( !$VALIDATOR{$constraint->[0]}->($req,$vals) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Dancer::Plugin::KossyValidator::VALIDATOR{$constraint->[0]}($req, $vals)) 0 0 0
129 0 push @errors, { param => $param_name, message => $constraint->[1] };
130 0 $error=1;
131 0 last PARAM_CONSTRAINT;
132 }
133 }
134 else {
135 0 for my $val ( @$vals ) {
136 0 ▶ if ( !$VALIDATOR{$constraint->[0]}->($req, $val) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Dancer::Plugin::KossyValidator::VALIDATOR{$constraint->[0]}($req, $val)) 0 0 0
137 0 push @errors, { param => $param_name, message => $constraint->[1] };
138 0 $error=1;
139 0 last PARAM_CONSTRAINT;
140 }
141 }
142 }
143 }
144 }
145 0 ▶ $valid->add($param_name,@$vals) unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 0 0
146 }
147
148 0 Kossy::Validator::Result->new(\@errors,$valid);
149 2 };
150  
151 2 register_plugin;
152  
153 package Kossy::Validator::Result;
154  
155 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
159 0 my $class = shift;
160 0 my $errors = shift;
161 0 my $valid = shift;
162 0 bless {errors=>$errors,valid=>$valid}, $class;
163 }
164  
165 0 ▶sub has_error {
Subroutine has_error: not called CC=2 SCAR=17.9
166 0 my $self = shift;
167 0 ▶ return 1 if @{$self->{errors}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"errors"};} 0 0 0
168 0 return;
169 }
170  
171 0 ▶sub messages {
Subroutine messages: not called CC=1 SCAR=6.9
172 0 my $self = shift;
173 0 my @errors = map { $_->{message} } @{$self->{errors}};
174 0 \@errors;
175 }
176  
177 0 ▶sub errors {
Subroutine errors: not called CC=1 SCAR=6.9
178 0 my $self = shift;
179 0 my %errors = map { $_->{param} => $_->{message} } @{$self->{errors}};
180 0 \%errors;
181 }
182  
183 0 ▶sub valid {
Subroutine valid: not called CC=4 SCAR=30.0
184 0 my $self = shift;
185 0 ▶ if ( @_ == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
elsif (@_ == 1) { } 0 0 0
186 0 $self->{valid}->add(@_);
187 0 return $_[1];
188 }
189 elsif ( @_ == 1 ) {
190 0 ▶ return $self->{valid}->get($_[0]) if ! wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
191 0 return $self->{valid}->get_all($_[0]);
192 }
193 0 $self->{valid};
194 }
195  
196 2 1;
197  
198 =head1 NAME
199  
200 Dancer::Plugin::KossyValidator - 根据 Kossy 中的 Validator 移植过来的模块
201  
202 =head1 SYNOPSIS
203  
204 use Dancer ':syntax';
205 use Dancer::Plugin::KossyValidator;
206  
207 any ['post', 'put'] => '/isp' => sub {
208 my $result = validator([
209 'name' => {
210 rule => [
211 ['NOT_NULL', '运营商名不能为空'],
212 ],
213 },
214 'description' => {
215 default => '无',
216 rule => [],
217 },
218 ]);
219
220 return {
221 result => 'false',
222 messages => $result->errors
223 } if $result->has_error;
224  
225 $result->has_error:Flag
226 $result->messages:ArrayRef[`Str]
227
228 my $val = $result->valid('name'); # 注意取请求过来的参数时原函数 param 替换为 valid 了
229 my @val = $result->valid('description');
230
231 my $hash = $result->valid:Hash::MultiValue;
232 # ...
233 };
234  
235 dancer;
236  
237 =head1 DESCRIPTION
238  
239 Kossy 是 Perl 中另一个迷你框架,这个模块根据 Kossy 中的 Validator 移植过来支持 Dancer 的模块。
240  
241 =head1 VALIDATORS
242  
243 =over 4
244  
245 =item NOT_NULL
246  
247 =item CHOICE
248  
249 ['CHOICE',qw/dog cat/]
250  
251 =item INT
252  
253 int
254  
255 =item UINT
256  
257 unsigned int
258  
259 =item NATURAL
260  
261 natural number
262  
263 =item @SELECTED_NUM
264  
265 ['@SELECTED_NUM',min,max]
266  
267 =item @SELECTED_UNIQ
268  
269 all selected values are unique
270  
271 =back
272  
273 =head1 CODEref VALIDATOR
274  
275 my $result = validator([
276 'q' => [
277 [sub{
278 my ($req,$val) = @_;
279 },'invalid']
280 ],
281 ]);
282
283 my $result = validator([
284 'q' => [
285 [[sub{
286 my ($req,$val,@args) = @_;
287 },0,1],'invalid']
288 ],
289 ]);
290  
291  
292 =head1 AUTHOR
293  
294 原模块作者 Masahiro Nagano E<lt>kazeburo {at} gmail.comE<gt> 移植人: 扶凯 iakuf {at} 163.com
295  
296 =head1 SEE ALSO
297  
298 L<Kossy::Validator>
299  
300 =head1 LICENSE
301  
302 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
303  
304 =cut
305  
↑ Summary