| 1 | # Version 0 module | ||||||||||||||||||||
| 2 | package Validator::Custom::Rule; | ||||||||||||||||||||
| 3 | 5 | ▶ | use Object::Simple -base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | ▶ | use Carp 'croak'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 0 | ▶ | has 'topic_info' => sub { {} }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 7 | 14 | ▶ | has 'rule' => sub { [] }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 5 | has 'validator'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | sub default { | ||||||||||||||||||
|
Subroutine
default: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 11 | 1 | my ($self, $default) = @_; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | $self->topic_info->{default} = $default; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | return $self; | |||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 0 | ▶ | sub name { | ||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 19 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | $self->topic_info->{name} = $name; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 0 | return $self; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 10 | ▶ | sub filter { | ||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 27 | 10 | my $self = shift; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 10 | return $self->check(@_) | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 41 | ▶ | sub check { | ||||||||||||||||||
|
Subroutine
check: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 33 | 41 | my $self = shift; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 41 | my @constraints = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 41 | my $constraints_h = []; | |||||||||||||||||||
| 38 | 41 | for my $constraint (@constraints) { | |||||||||||||||||||
| 39 | 41 | my $constraint_h = {}; | |||||||||||||||||||
| 40 | 41 | ▶ | if (ref $constraint eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 6 | $constraint_h->{constraint} = $constraint->[0]; | |||||||||||||||||||
| 42 | 6 | $constraint_h->{message} = $constraint->[1]; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | else { | ||||||||||||||||||||
| 45 | 35 | $constraint_h->{constraint} = $constraint; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | 41 | my $cinfo = $self->validator->_parse_constraint($constraint_h); | |||||||||||||||||||
| 48 | 41 | $cinfo->{each} = $self->topic_info->{each}; | |||||||||||||||||||
| 49 | 41 | push @$constraints_h, $cinfo; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 41 | ▶ | $self->topic_info->{constraints} ||= []; | ||||||||||||||||||
|
Condition100%2/2
$self->topic_info->{"constraints"} ||= []
MC/DC100%1/1
$self->topic_info->{"constraints"} ||= []
$self->topic_info->{"constraints"}
Truth table100%2/2
A
A= $self->topic_info->{"constraints"}
| |||||||||||||||||||||
| 53 | 41 | $self->topic_info->{constraints} = [@{$self->topic_info->{constraints}}, @{$constraints_h}]; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 41 | return $self; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 11 | ▶ | sub message { | ||||||||||||||||||
|
Subroutine
message: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||
| 59 | 11 | my ($self, $message) = @_; | |||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 11 | ▶ | my $constraints = $self->topic_info->{constraints} || []; | ||||||||||||||||||
|
Condition50%1/2
$self->topic_info->{'constraints'} || []
MC/DC0%0/1
$self->topic_info->{'constraints'} || []
$self->topic_info->{'constraints'}
Truth table50%1/2
A
A= $self->topic_info->{'constraints'}
| |||||||||||||||||||||
| 62 | 11 | for my $constraint (@$constraints) { | |||||||||||||||||||
| 63 | 23 | ▶ | $constraint->{message} ||= $message; | ||||||||||||||||||
|
Condition66%2/3
$constraint->{"message"} ||= $message
MC/DC0%0/2
$constraint->{"message"} ||= $message
$constraint->{"message"}
$message
Truth table66%2/3
A ||= B
A= $constraint->{"message"}B= $message
| |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 11 | return $self; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 36 | ▶ | sub topic { | ||||||||||||||||||
|
Subroutine
topic: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 70 | 36 | my ($self, $key) = @_; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # Create topic | ||||||||||||||||||||
| 73 | 36 | my $topic_info = {}; | |||||||||||||||||||
| 74 | 36 | $topic_info->{key} = $key; | |||||||||||||||||||
| 75 | 36 | $self->topic_info($topic_info); | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # Add topic to rule | ||||||||||||||||||||
| 78 | 36 | push @{$self->rule}, $self->topic_info; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 36 | return $self; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 5 | ▶ | sub each { | ||||||||||||||||||
|
Subroutine
each: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||
| 84 | 5 | my $self = shift; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 5 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 5 | $self->topic_info->{each} = $_[0]; | |||||||||||||||||||
| 88 | 5 | return $self; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||
| 91 | 0 | return $self->topic_info->{each}; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | return $self; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1 | ▶ | sub optional { | ||||||||||||||||||
|
Subroutine
optional: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 98 | 1 | my ($self, $key) = @_; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1 | ▶ | if (defined $key) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | # Create topic | ||||||||||||||||||||
| 102 | 1 | $self->topic($key); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | # Value is optional | ||||||||||||||||||||
| 106 | 1 | $self->rule->[-1]{option}{optional} = 1; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 1 | return $self; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 35 | ▶ | sub require { | ||||||||||||||||||
|
Subroutine
require: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 112 | 35 | my ($self, $key) = @_; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # Create topic | ||||||||||||||||||||
| 115 | 35 | ▶ | if (defined $key) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 35 | $self->topic($key); | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 35 | return $self; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 159 | ▶ | sub parse { | ||||||||||||||||||
|
Subroutine
parse: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 123 | 159 | my ($self, $rule, $shared_rule) = @_; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 159 | ▶ | $shared_rule ||= []; | ||||||||||||||||||
|
Condition100%2/2
$shared_rule ||= []
MC/DC100%1/1
$shared_rule ||= []
$shared_rule
Truth table100%2/2
A
A= $shared_rule
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 159 | my $normalized_rule = []; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 159 | ▶ | for (my $i = 0; $i < @{$rule}; $i += 2) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 373 | my $r = {}; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # Key, options, and constraints | ||||||||||||||||||||
| 134 | 373 | my $key = $rule->[$i]; | |||||||||||||||||||
| 135 | 373 | my $option = $rule->[$i + 1]; | |||||||||||||||||||
| 136 | 373 | my $constraints; | |||||||||||||||||||
| 137 | 373 | ▶ | if (ref $option eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 22 | $constraints = $rule->[$i + 2]; | |||||||||||||||||||
| 139 | 22 | $i++; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||
| 142 | 351 | $constraints = $option; | |||||||||||||||||||
| 143 | 351 | $option = {}; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 373 | my $constraints_h = []; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 373 | ▶ | if (ref $constraints eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | 371 | for my $constraint (@$constraints, @$shared_rule) { | |||||||||||||||||||
| 149 | 405 | my $constraint_h = {}; | |||||||||||||||||||
| 150 | 405 | ▶ | if (ref $constraint eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 86 | $constraint_h->{constraint} = $constraint->[0]; | |||||||||||||||||||
| 152 | 86 | $constraint_h->{message} = $constraint->[1]; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||
| 155 | 319 | $constraint_h->{constraint} = $constraint; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | 405 | push @$constraints_h, $self->validator->_parse_constraint($constraint_h); | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||
| 160 | 2 | $constraints_h = { | |||||||||||||||||||
| 161 | 'ERROR' => { | ||||||||||||||||||||
| 162 | value => $constraints, | ||||||||||||||||||||
| 163 | message => 'Constraints must be array reference' | ||||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 371 | $r->{key} = $key; | |||||||||||||||||||
| 169 | 371 | $r->{constraints} = $constraints_h; | |||||||||||||||||||
| 170 | 371 | $r->{option} = $option; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 371 | push @$normalized_rule, $r; | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 157 | $self->rule($normalized_rule); | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 157 | return $self; | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 1 | ▶ | sub copy { | ||||||||||||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 181 | 1 | my ($self, $copy) = @_; | |||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 1 | $self->topic_info->{option}{copy} = $copy; | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 1 | return $self; | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 6 | ▶ | sub check_or { | ||||||||||||||||||
|
Subroutine
check_or: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 189 | 6 | my ($self, @constraints) = @_; | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 6 | my $constraint_h = {}; | |||||||||||||||||||
| 192 | 6 | $constraint_h->{constraint} = \@constraints; | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 6 | my $cinfo = $self->validator->_parse_constraint($constraint_h); | |||||||||||||||||||
| 195 | 6 | $cinfo->{each} = $self->topic_info->{each}; | |||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 6 | ▶ | $self->topic_info->{constraints} ||= []; | ||||||||||||||||||
|
Condition50%1/2
$self->topic_info->{"constraints"} ||= []
MC/DC0%0/1
$self->topic_info->{"constraints"} ||= []
$self->topic_info->{"constraints"}
Truth table50%1/2
A
A= $self->topic_info->{"constraints"}
| |||||||||||||||||||||
| 198 | 6 | push @{$self->topic_info->{constraints}}, $cinfo; | |||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 6 | return $self; | |||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 5 | 1; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head1 NAME | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | Validator::Custom::Rule - Rule object | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head1 SYNOPSYS | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | use Validator::Custom; | ||||||||||||||||||||
| 212 | my $vc = Validator::Custom->new; | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | # Create rule object | ||||||||||||||||||||
| 215 | my $rule = $vc->create_rule; | ||||||||||||||||||||
| 216 | $rule->require('id')->check( | ||||||||||||||||||||
| 217 | 'ascii' | ||||||||||||||||||||
| 218 | ); | ||||||||||||||||||||
| 219 | $rule->optional('name')->check( | ||||||||||||||||||||
| 220 | 'not_blank' | ||||||||||||||||||||
| 221 | ); | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | # Validate | ||||||||||||||||||||
| 224 | my $data = {id => '001', name => 'kimoto'}; | ||||||||||||||||||||
| 225 | my $result = $vc->validate($data, $rule); | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | # Option | ||||||||||||||||||||
| 228 | $rule->require('id')->default(4)->copy(0)->message('Error')->check( | ||||||||||||||||||||
| 229 | 'not_blank' | ||||||||||||||||||||
| 230 | ); | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =head1 DESCRIPTION | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | Validator::Custom::Rule is the class to parse rule and store it as object. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | =head2 rule | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | my $content = $rule_obj->rule; | ||||||||||||||||||||
| 241 | $rule_obj = $rule->rule($content); | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | Content of rule object. | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | =head1 METHODS | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | =head2 each | ||||||||||||||||||||
| 248 | |||||||||||||||||||||
| 249 | $rule->each(1); | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | Tell checke each element. | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | =head2 check | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | $rule->check('not_blank')->check('ascii'); | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | Add constraints to current topic. | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | =head2 check_or | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | $rule->check_or('not_blank', 'ascii'); | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | Add "or" condition constraints to current topic. | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | =head2 copy | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | $rule->copy(0); | ||||||||||||||||||||
| 268 | |||||||||||||||||||||
| 269 | Set copy option | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | =head2 default | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | $rule->default(0); | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | Set default option | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | =head2 filter | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | $rule->filter('trim'); | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | This is C<check> method alias for readability. | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | =head2 message | ||||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | $rule->require('name') | ||||||||||||||||||||
| 286 | ->check('not_blank')->message('should be not blank') | ||||||||||||||||||||
| 287 | ->check('int')->message('should be int'); | ||||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | Set message for each check. | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | Message is fallback to before check | ||||||||||||||||||||
| 292 | so you can write the following way. | ||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | $rule->require('name') | ||||||||||||||||||||
| 295 | ->check('not_blank') | ||||||||||||||||||||
| 296 | ->check('int')->message('should be not blank and int'); | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | =head2 name | ||||||||||||||||||||
| 299 | |||||||||||||||||||||
| 300 | $rule->name('key1'); | ||||||||||||||||||||
| 301 | |||||||||||||||||||||
| 302 | Set result key name | ||||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | =head2 optional | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | $rule->optional('id'); | ||||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | Set key and set require option to 0. | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | =head2 require | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | $rule->require('id'); | ||||||||||||||||||||
| 313 | $rule->require(['id1', 'id2']); | ||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | Set key. | ||||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | =head2 parse | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | $rule_obj = $rule_obj->parse($rule); | ||||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | Parse rule and store it to C<rule> attribute. | ||||||||||||||||||||