QBit/Validator/Type/scalar.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 192 CC 28 SCAR 31.3
CC
28
Cov
0%
eq
56.0
optional
30.0
min
20.0
CRAP 22.8
1 package QBit::Validator::Type::scalar;
2 $QBit::Validator::Type::scalar::VERSION = '0.011';
3 use qbit;
4  
5 use base qw(QBit::Validator::Type);
6  
7 use Exception::Validator;
8  
9 #order is important
10 my $OPTIONS = [
11 {name => 'optional', required => TRUE},
12 {name => 'eq'},
13 {name => 'regexp'},
14 {name => 'min'},
15 {name => 'max'},
16 {name => 'len_min'},
17 {name => 'len'},
18 {name => 'len_max'},
19 {name => 'in'},
20 ];
21  
22 sub _get_options {
23 my ($self) = @_;
24  
25 return clone($OPTIONS);
26 }
27  
28 sub _get_options_name {
29 return map {$_->{'name'}} @$OPTIONS;
30 }
31  
32 sub optional {
33 my ($self, $qv, $data, $template, $option, @path_field) = @_;
34  
35 if (ref($data)) {
36 $qv->_add_error($template, gettext('Data must be SCALAR'), \@path_field);
37  
38 return FALSE;
39 }
40  
41 if ($template->{$option} && !defined($data)) {
42 $qv->_add_ok(\@path_field);
43  
44 return FALSE;
45 } elsif (!$template->{$option} && !defined($data)) {
46 unless (exists($template->{'eq'})) {
47 $qv->_add_error($template, gettext('Data must be defined'), \@path_field);
48  
49 return FALSE;
50 }
51 }
52  
53 return TRUE;
54 }
55  
56 sub eq {
57 my ($self, $qv, $data, $template, $option, @path_field) = @_;
58  
59 if (!defined($template->{$option})) {
60 if (defined($data)) {
61 $qv->_add_error($template, gettext('Data must be null'), \@path_field);
62  
63 return FALSE;
64 } else {
65 return TRUE;
66 }
67 } elsif (!defined($data)) {
68 $qv->_add_error($template, gettext('Data must be defined'), \@path_field);
69  
70 return FALSE;
71 }
72  
73 throw Exception::Validator gettext('Option "%s" must be numeric', $option)
74 unless looks_like_number($template->{$option});
75  
76 unless (looks_like_number($data)) {
77 $qv->_add_error($template, gettext('The data must be numeric, but got "%s"', $data), \@path_field);
78  
79 return FALSE;
80 }
81  
82 unless ($data == $template->{$option}) {
83 $qv->_add_error($template, gettext('Got value "%s" not equal "%s"', $data, $template->{$option}), \@path_field);
84  
85 return FALSE;
86 }
87  
88 return TRUE;
89 }
90  
91 sub regexp {
92 my ($self, $qv, $data, $template, $option, @path_field) = @_;
93  
94 throw Exception::Validator gettext('Option "%s" must be type "Regexp"', $option)
95 if !defined($template->{$option}) || ref($template->{$option}) ne 'Regexp';
96  
97 if ($data !~ $template->{$option}) {
98 $qv->_add_error($template, gettext('Got value "%s" do not fit the regular expression', $data), \@path_field);
99  
100 return FALSE;
101 }
102  
103 return TRUE;
104 }
105  
106 sub min {
107 my ($self, $qv, $data, $template, $option, @path_field) = @_;
108  
109 throw Exception::Validator gettext('Option "%s" must be numeric', $option)
110 unless looks_like_number($template->{$option});
111  
112 unless (looks_like_number($data)) {
113 $qv->_add_error($template, gettext('The data must be numeric, but got "%s"', $data), \@path_field);
114  
115 return FALSE;
116 }
117  
118 if ($data < $template->{$option}) {
119 $qv->_add_error($template, gettext('Got value "%s" less then "%s"', $data, $template->{$option}), \@path_field);
120  
121 return FALSE;
122 }
123  
124 return TRUE;
125 }
126  
127 sub max {
128 my ($self, $qv, $data, $template, $option, @path_field) = @_;
129  
130 throw Exception::Validator gettext('Option "%s" must be numeric', $option)
131 unless looks_like_number($template->{$option});
132  
133 unless (looks_like_number($data)) {
134 $qv->_add_error($template, gettext('The data must be numeric, but got "%s"', $data), \@path_field);
135  
136 return FALSE;
137 }
138  
139 if ($data > $template->{$option}) {
140 $qv->_add_error($template, gettext('Got value "%s" more than "%s"', $data, $template->{$option}), \@path_field);
141  
142 return FALSE;
143 }
144  
145 return TRUE;
146 }
147  
148 sub len_min {
149 my ($self, $qv, $data, $template, $option, @path_field) = @_;
150  
151 throw Exception::Validator gettext('Option "%s" must be positive number', $option)
152 if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/;
153  
154 if (length($data) < $template->{$option}) {
155 $qv->_add_error($template, gettext('Length "%s" less then "%s"', $data, $template->{$option}), \@path_field);
156  
157 return FALSE;
158 }
159  
160 return TRUE;
161 }
162  
163 sub len {
164 my ($self, $qv, $data, $template, $option, @path_field) = @_;
165  
166 throw Exception::Validator gettext('Option "%s" must be positive number', $option)
167 if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/;
168  
169 unless (length($data) == $template->{$option}) {
170 $qv->_add_error($template, gettext('Length "%s" not equal "%s"', $data, $template->{$option}), \@path_field);
171  
172 return FALSE;
173 }
174  
175 return TRUE;
176 }
177  
178 sub len_max {
179 my ($self, $qv, $data, $template, $option, @path_field) = @_;
180  
181 throw Exception::Validator gettext('Key "%s" must be positive number', $option)
182 if !defined($template->{$option}) || $template->{$option} !~ /\A[0-9]+\z/;
183  
184 if (length($data) > $template->{$option}) {
185 $qv->_add_error($template, gettext('Length "%s" more than "%s"', $data, $template->{$option}), \@path_field);
186  
187 return FALSE;
188 }
189  
190 return TRUE;
191 }
192  
193 sub in {
194 my ($self, $qv, $data, $template, $option, @path_field) = @_;
195  
196 throw Exception::Validator gettext('Key "%s" must be defined', $option) unless defined($template->{$option});
197  
198 $template->{$option} = [$template->{$option}] if ref($template->{$option}) ne 'ARRAY';
199  
200 unless (in_array($data, $template->{$option})) {
201 $qv->_add_error($template,
202 gettext('Got value "%s" not in array: %s', $data, join(', ', @{$template->{$option}})),
203 \@path_field);
204  
205 return FALSE;
206 }
207  
208 return TRUE;
209 }
210  
211 TRUE;