Validator/Type/scalar.pm untested

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