« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Util.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Facebook.pm »
| 1 | package Dwarf::Validator::Constraint::Default; | ||
| 2 | use Dwarf::Validator::Constraint; | ||
| 3 | use Dwarf::Validator::NullValue; | ||
| 4 | use Email::Valid; | ||
| 5 | use Email::Valid::Loose; | ||
| 6 | use Dwarf::Util qw/encode_utf8 decode_utf8/; | ||
| 7 | use Image::Info qw/image_type/; | ||
| 8 | use JSON; | ||
| 9 | use MIME::Base64 qw(decode_base64 decoded_base64_length); | ||
| 10 | use Scalar::Util qw/looks_like_number/; | ||
| 11 | use UNIVERSAL::require; | ||
| 12 | |||
| 13 | |||
| 14 | rule NOT_NULL => sub { | ||
| 15 | return 0 if not defined($_); | ||
| 16 | return 0 if ref $_ eq 'ARRAY' && @$_ == 0; | ||
| 17 | return 1; | ||
| 18 | }; | ||
| 19 | alias NOT_NULL => 'REQUIRED'; | ||
| 20 | |||
| 21 | rule NOT_BLANK => sub { | ||
| 22 | return 0 if not defined($_); | ||
| 23 | return 0 if ref $_ eq 'ARRAY' && @$_ == 0; | ||
| 24 | $_ ne ""; | ||
| 25 | }; | ||
| 26 | |||
| 27 | rule INT => sub { | ||
| 28 | return 0 unless $_ =~ /\A[+\-]?[0-9]+\z/; | ||
| 29 | return 0 unless $_ < 2_147_483_647; | ||
| 30 | return 0 unless $_ > -2_147_483_648; | ||
| 31 | return 1; | ||
| 32 | }; | ||
| 33 | |||
| 34 | rule UINT => sub { | ||
| 35 | return 0 unless $_ =~ /\A[0-9]+\z/; | ||
| 36 | return 0 unless $_ < 2_147_483_647; | ||
| 37 | return 1; | ||
| 38 | }; | ||
| 39 | |||
| 40 | rule BIGINT => sub { | ||
| 41 | return 0 unless $_ =~ /\A[+\-]?[0-9]+\z/; | ||
| 42 | return 1 if $_ =~ /\A[+\-]?[0-9]{1,18}\z/; | ||
| 43 | return 0 if $_ =~ /\A[+]?[0-9]{20,}\z/; | ||
| 44 | return 0 if $_ =~ /\A[\-]?[0-9]{20,}\z/; | ||
| 45 | Math::BigInt->use or die; | ||
| 46 | my $MIN = Math::BigInt->new("-9223372036854775808"); | ||
| 47 | my $MAX = Math::BigInt->new("9223372036854775807"); | ||
| 48 | my $val = Math::BigInt->new($_); | ||
| 49 | return 0 if $MIN->bcmp($val) > 0; | ||
| 50 | return 0 if $val->bcmp($MAX) > 0; | ||
| 51 | return 1; | ||
| 52 | }; | ||
| 53 | |||
| 54 | rule BIGUINT => sub { | ||
| 55 | return 0 unless $_ =~ /\A[0-9]+\z/; | ||
| 56 | return 1 if $_ =~ /\A[0-9]{1,18}\z/; | ||
| 57 | return 0 if $_ =~ /\A[0-9]{20,}\z/; | ||
| 58 | Math::BigInt->use or die; | ||
| 59 | my $MAX = Math::BigInt->new("9223372036854775807"); | ||
| 60 | my $val = Math::BigInt->new($_); | ||
| 61 | return 0 if $val->bcmp($MAX) > 0; | ||
| 62 | return 1; | ||
| 63 | }; | ||
| 64 | |||
| 65 | rule NUMBER => sub { | ||
| 66 | my $value = $_; | ||
| 67 | return 1 unless defined $value; | ||
| 68 | return looks_like_number $value; | ||
| 69 | }; | ||
| 70 | |||
| 71 | rule EQUAL => sub { | ||
| 72 | Carp::croak("missing \$argument") if @_ == 0; | ||
| 73 | $_ eq $_[0] | ||
| 74 | }; | ||
| 75 | |||
| 76 | rule BETWEEN => sub { | ||
| 77 | $_[0] <= $_ && $_ <= $_[1]; | ||
| 78 | }; | ||
| 79 | |||
| 80 | rule LESS_THAN => sub { | ||
| 81 | $_ < $_[0]; | ||
| 82 | }; | ||
| 83 | |||
| 84 | rule LESS_EQUAL => sub { | ||
| 85 | $_ <= $_[0]; | ||
| 86 | }; | ||
| 87 | |||
| 88 | rule MORE_THAN => sub { | ||
| 89 | $_[0] < $_; | ||
| 90 | }; | ||
| 91 | |||
| 92 | rule MORE_EQUAL => sub { | ||
| 93 | $_[0] <= $_; | ||
| 94 | }; | ||
| 95 | |||
| 96 | rule ASCII => sub { | ||
| 97 | $_ =~ /^[\x21-\x7E]+$/ | ||
| 98 | }; | ||
| 99 | |||
| 100 | # 'name' => [qw/LENGTH 5 20/], | ||
| 101 | rule LENGTH => sub { | ||
| 102 | my $length = length($_); | ||
| 103 | my $min = shift; | ||
| 104 | my $max = shift || $min; | ||
| 105 | Carp::croak("missing \$min") unless defined($min); | ||
| 106 | |||
| 107 | ( $min <= $length and $length <= $max ) | ||
| 108 | }; | ||
| 109 | |||
| 110 | rule DATE => sub { | ||
| 111 | if (ref $_) { | ||
| 112 | # query: y=2009&m=09&d=02 | ||
| 113 | # rule: {date => [qw/y m d/]} => ['DATE'] | ||
| 114 | return 0 unless scalar(@{$_}) == 3; | ||
| 115 | _date(@{$_}); | ||
| 116 | } else { | ||
| 117 | # query: date=2009-09-02 | ||
| 118 | # rule: date => ['DATE'] | ||
| 119 | _date(split /-/, $_); | ||
| 120 | } | ||
| 121 | }; | ||
| 122 | |||
| 123 | sub _date { | ||
| 124 | my ($y, $m, $d) = @_; | ||
| 125 | |||
| 126 | return 0 if ( !$y or !$m or !$d ); | ||
| 127 | |||
| 128 | if ($d > 31 or $d < 1 or $m > 12 or $m < 1 or $y == 0) { | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | if ($d > 30 and ($m == 4 or $m == 6 or $m == 9 or $m == 11)) { | ||
| 132 | return 0; | ||
| 133 | } | ||
| 134 | if ($d > 29 and $m == 2) { | ||
| 135 | return 0; | ||
| 136 | } | ||
| 137 | if ($m == 2 and $d > 28 and !($y % 4 == 0 and ($y % 100 != 0 or $y % 400 == 0))){ | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | return 1; | ||
| 141 | } | ||
| 142 | |||
| 143 | rule TIME => sub { | ||
| 144 | if (ref $_) { | ||
| 145 | # query: h=12&m=00&d=60 | ||
| 146 | # rule: {time => [qw/h m s/]} => ['TIME'] | ||
| 147 | _time(@{$_}); | ||
| 148 | } else { | ||
| 149 | # query: time=12:00:30 | ||
| 150 | # rule: time => ['time'] | ||
| 151 | _time(split /:/, $_); | ||
| 152 | } | ||
| 153 | }; | ||
| 154 | |||
| 155 | sub _time { | ||
| 156 | my ($h, $m, $s) = @_; | ||
| 157 | |||
| 158 | return 0 if (!defined($h) or !defined($m)); | ||
| 159 | return 0 if ("$h" eq "" or "$m" eq ""); | ||
| 160 | $s ||= 0; # optional | ||
| 161 | |||
| 162 | if ( $h > 23 or $h < 0 or $m > 59 or $m < 0 or $s > 59 or $s < 0 ) { | ||
| 163 | return 0; | ||
| 164 | } | ||
| 165 | |||
| 166 | return 1; | ||
| 167 | } | ||
| 168 | |||
| 169 | # this regexp is taken from http://www.din.or.jp/~ohzaki/perl.htm#httpURL | ||
| 170 | # thanks to ohzaki++ | ||
| 171 | rule HTTP_URL => sub { | ||
| 172 | $_ =~ /^s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+$/ | ||
| 173 | }; | ||
| 174 | |||
| 175 | rule EMAIL => sub { Email::Valid->address(encode_utf8 $_) }; | ||
| 176 | rule EMAIL_LOOSE => sub { Email::Valid::Loose->address(encode_utf8 $_) }; | ||
| 177 | |||
| 178 | rule HIRAGANA => sub { delsp($_) =~ /^\p{InHiragana}+$/ }; | ||
| 179 | rule KATAKANA => sub { delsp($_) =~ /^\p{InKatakana}+$/ }; | ||
| 180 | rule JTEL => sub { $_ =~ /^0\d+\-?\d+\-?\d+$/ }; | ||
| 181 | rule JZIP => sub { $_ =~ /^\d{3}\-\d{4}$/ }; | ||
| 182 | |||
| 183 | # {mails => [qw/mail1 mail2/]} => ['DUPLICATION'] | ||
| 184 | rule DUPLICATION => sub { | ||
| 185 | defined($_->[0]) && defined($_->[1]) && $_->[0] eq $_->[1] | ||
| 186 | }; | ||
| 187 | alias DUPLICATION => 'DUP'; | ||
| 188 | |||
| 189 | rule REGEX => sub { | ||
| 190 | my $regex = shift; | ||
| 191 | Carp::croak("missing args at REGEX rule") unless defined $regex; | ||
| 192 | $_ =~ /$regex/ | ||
| 193 | }; | ||
| 194 | alias REGEX => 'REGEXP'; | ||
| 195 | |||
| 196 | rule CHOICE => sub { | ||
| 197 | Carp::croak("missing \$choices") if @_ == 0; | ||
| 198 | |||
| 199 | my @choices = @_==1 && ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_; | ||
| 200 | |||
| 201 | for my $c (@choices) { | ||
| 202 | if ($c eq $_) { | ||
| 203 | return 1; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | return 0; | ||
| 207 | }; | ||
| 208 | alias CHOICE => 'IN'; | ||
| 209 | |||
| 210 | rule NOT_IN => sub { | ||
| 211 | my @choices = @_==1 && ref$_[0]eq'ARRAY' ? @{$_[0]} : @_; | ||
| 212 | |||
| 213 | for my $c (@choices) { | ||
| 214 | if ($c eq $_) { | ||
| 215 | return 0; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | return 1; | ||
| 219 | }; | ||
| 220 | |||
| 221 | rule MATCH => sub { | ||
| 222 | my $callback = shift; | ||
| 223 | Carp::croak("missing \$callback") if ref $callback ne 'CODE'; | ||
| 224 | |||
| 225 | $callback->($_); | ||
| 226 | }; | ||
| 227 | |||
| 228 | rule JSON => sub { | ||
| 229 | my $value = $_; | ||
| 230 | return 1 unless defined $value; | ||
| 231 | my $data = eval { decode_json encode_utf8 $value }; | ||
| 232 | if ($@) { | ||
| 233 | warn $@; | ||
| 234 | warn $value; | ||
| 235 | return 0; | ||
| 236 | } | ||
| 237 | return 1; | ||
| 238 | }; | ||
| 239 | |||
| 240 | rule CREDITCARD_NUMBER => sub { $_ =~ /\A[0-9]{14,16}\z/ }; | ||
| 241 | rule CREDITCARD_EXPIRE => sub { $_ =~ /^\d{2}\/\d{2}$/ }; | ||
| 242 | rule CREDITCARD_SECURITY => sub { $_ =~ /\A[0-9]{3,4}\z/ }; | ||
| 243 | |||
| 244 | rule BASE64_TYPE => sub { | ||
| 245 | Carp::croak('missing args. usage: ["BASE64_TYPE", "(jpeg|png|gif)"]') unless @_; | ||
| 246 | my $expected = $_[0]; | ||
| 247 | my $filetype = ''; | ||
| 248 | |||
| 249 | my $decoded = decode_base64($_); | ||
| 250 | my $type = image_type(\$decoded); | ||
| 251 | |||
| 252 | if (my $error = $type->{error}) { | ||
| 253 | return 0; | ||
| 254 | } | ||
| 255 | $filetype = lc $type->{file_type}; | ||
| 256 | return $filetype =~ /^$expected$/i; | ||
| 257 | }; | ||
| 258 | |||
| 259 | rule BASE64_SIZE => sub { | ||
| 260 | Carp::croak('missing args. usage: ["BASE64_SIZE", "10000"]') unless @_; | ||
| 261 | my $expected = $_[0]; | ||
| 262 | my $length = decoded_base64_length($_); | ||
| 263 | return $length < $expected; | ||
| 264 | }; | ||
| 265 | |||
| 266 | |||
| 267 | file_rule FILE_NOT_NULL => sub { | ||
| 268 | return 0 if not defined($_); | ||
| 269 | return 0 if $_ eq ""; | ||
| 270 | return 0 if ref($_) eq 'ARRAY' && @$_ == 0; | ||
| 271 | return 1; | ||
| 272 | }; | ||
| 273 | |||
| 274 | file_rule FILE_MIME => sub { | ||
| 275 | Carp::croak('missing args. usage: ["FILE_MIME", "text/plain"]') unless @_; | ||
| 276 | my $expected = $_[0]; | ||
| 277 | return $_->type =~ /^$expected$/; | ||
| 278 | }; | ||
| 279 | |||
| 280 | file_rule FILE_EXT => sub { | ||
| 281 | Carp::croak('missing args. usage: ["FILE_MIME", "text/plain"]') unless @_; | ||
| 282 | my $expected = $_[0]; | ||
| 283 | my $ext = ''; | ||
| 284 | if ($_->filename =~ /\.([^\.]+)$/) { | ||
| 285 | $ext = lc $1; | ||
| 286 | } | ||
| 287 | return $ext =~ /^$expected$/; | ||
| 288 | }; | ||
| 289 | |||
| 290 | # 予約 | ||
| 291 | rule ARRAY => sub { 1 }; | ||
| 292 | |||
| 293 | rule FILTER => sub { | ||
| 294 | my ($filter, @args) = @_; | ||
| 295 | Carp::croak("missing \$filter") unless $filter; | ||
| 296 | |||
| 297 | my $opts = { | ||
| 298 | override_param => 1, | ||
| 299 | }; | ||
| 300 | |||
| 301 | if (not ref $filter) { | ||
| 302 | $filter = $Dwarf::Validator::Filters->{uc $filter} | ||
| 303 | or Carp::croak("$filter is not defined."); | ||
| 304 | } | ||
| 305 | |||
| 306 | Carp::croak("\$filter must be coderef.") if ref $filter ne 'CODE'; | ||
| 307 | |||
| 308 | $_ = $filter->($_, \@args, $opts); | ||
| 309 | |||
| 310 | # パラメータを上書きしない場合は null を返す | ||
| 311 | unless ($opts->{override_param}) { | ||
| 312 | return Dwarf::Validator::NullValue->new; | ||
| 313 | } | ||
| 314 | |||
| 315 | $_; | ||
| 316 | }; | ||
| 317 | |||
| 318 | filter TRIM => sub { | ||
| 319 | my ($value, $args, $opts) = @_; | ||
| 320 | return $value unless $value; | ||
| 321 | $value =~ s/^\s+|\s+$//g; | ||
| 322 | $value; | ||
| 323 | }; | ||
| 324 | |||
| 325 | # remove control character other than LF or CR | ||
| 326 | filter RCC => sub { | ||
| 327 | my ($value, $args, $opts) = @_; | ||
| 328 | return $value unless $value; | ||
| 329 | $value =~ s/[\x00-\x09\x0b\x0c\x0e-\x1f]//g; | ||
| 330 | $value; | ||
| 331 | }; | ||
| 332 | |||
| 333 | filter DEFAULT => sub { | ||
| 334 | my ($value, $args, $opts) = @_; | ||
| 335 | unless (defined $value) { | ||
| 336 | $value = $args->[0]; | ||
| 337 | } | ||
| 338 | $value; | ||
| 339 | }; | ||
| 340 | |||
| 341 | filter BLANK_TO_NULL => sub { | ||
| 342 | my ($value, $args, $opts) = @_; | ||
| 343 | return undef unless defined $value; | ||
| 344 | return undef if $value eq ''; | ||
| 345 | return $value; | ||
| 346 | }; | ||
| 347 | |||
| 348 | filter DECODE_UTF8 => sub { | ||
| 349 | my ($value, $args, $opts) = @_; | ||
| 350 | return $value unless $value; | ||
| 351 | $value = decode_utf8($value); | ||
| 352 | $value; | ||
| 353 | }; | ||
| 354 | |||
| 355 | filter ENCODE_UTF8 => sub { | ||
| 356 | my ($value, $args, $opts) = @_; | ||
| 357 | return $value unless $value; | ||
| 358 | $value = encode_utf8($value); | ||
| 359 | $value; | ||
| 360 | }; | ||
| 361 | |||
| 362 | # normalize_line_endings | ||
| 363 | filter NLE => sub { | ||
| 364 | my ($value, $opts, @args) = @_; | ||
| 365 | return $value unless $value; | ||
| 366 | $value =~ s/\x0D\x0A|\x0D|\x0A/\n/g; | ||
| 367 | $value; | ||
| 368 | }; | ||
| 369 | |||
| 370 | 1; |