1package FormValidator::Lite::Constraint::Default; 2use strict; 3use warnings; 4use FormValidator::Lite::Constraint; 5 6rule 'NOT_NULL' => sub { 7 return 0 if not defined($_); 8 return 0 if $_ eq ""; 9 return 0 if ref($_)eq'ARRAY' && @$_ == 0; 10 return 1; 11}; 12rule 'INT' => sub { $_ =~ /\A[+\-]?[0-9]+\z/ }; 13rule 'UINT' => sub { $_ =~ /\A[0-9]+\z/ }; 14alias 'NOT_NULL' => 'NOT_BLANK'; 15alias 'NOT_NULL' => 'REQUIRED'; 16 17rule 'ASCII' => sub { 18 $_ =~ /^[\x21-\x7E]+$/ 19}; 20 21# {mails => [qw/mail1 mail2/]} => ['DUPLICATION'] 22rule 'DUPLICATION' => sub { 23 defined($_->[0]) && defined($_->[1]) && $_->[0] eq $_->[1] 24}; 25alias 'DUPLICATION' => 'DUP'; 26 27# 'name' => [qw/LENGTH 5 20/], 28rule 'LENGTH' => sub { 29 my $length = length($_); 30 my $min = shift; 31 my $max = shift || $min; 32 Carp::croak("missing \$min") unless defined($min); 33 34 ( $min <= $length and $length <= $max ) 35}; 36 37rule 'EQUAL' => sub { 38 Carp::croak("missing \$argument") if @_ == 0; 39 $_ eq $_[0] 40}; 41 42rule 'REGEX' => sub { 43 my $regex = shift; 44 Carp::croak("missing args at REGEX rule") unless defined $regex; 45 $_ =~ /$regex/ 46}; 47alias 'REGEX' => 'REGEXP'; 48 49rule 'CHOICE' => sub { 50 Carp::croak("missing \$choices") if @_ == 0; 51 52 my @choices = @_==1 && ref$_[0]eq'ARRAY' ? @{$_[0]} : @_; 53 54 for my $c (@choices) { 55 if ($c eq $_) { 56 return 1; 57 } 58 } 59 return 0; 60}; 61alias 'CHOICE' => 'IN'; 62 63rule 'NOT_IN' => sub { 64 my @choices = @_==1 && ref$_[0]eq'ARRAY' ? @{$_[0]} : @_; 65 66 for my $c (@choices) { 67 if ($c eq $_) { 68 return 0; 69 } 70 } 71 return 1; 72}; 73 74rule 'MATCH' => sub { 75 my $callback = shift; 76 Carp::croak("missing \$callback") if ref $callback ne 'CODE'; 77 78 $callback->($_); 79}; 80 81our $Filters = { 82 trim => sub { 83 my $value = shift; 84 return $value unless $value; 85 $value =~ s/^\s+|\s+$//g; 86 $value; 87 }, 88}; 89 90rule 'FILTER' => sub { 91 my $filter = shift; 92 Carp::croak("missing \$filter") unless $filter; 93 94 if (not ref $filter) { 95 $filter = $Filters->{$filter} 96 or Carp::croak("$filter is not defined."); 97 } 98 99 Carp::croak("\$filter must be coderef.") if ref $filter ne 'CODE'; 100 101 $_ = $filter->($_); 102 103 1; # always return true 104}; 105 106rule 'FLAG' => sub { $_ =~ /^[01]$/ }; 107 1081; 109__END__ 110 111=head1 NAME 112 113FormValidator::Lite::Constraint::Default - default constraint rules 114 115=head1 DESCRIPTION 116 117This module provides default constraint rules for L<FormValidator::Lite>. 118 119=head1 CONSTRAINTS 120 121=over 4 122 123=item NOT_NULL 124 125The parameter is true value or not. 126 127=item NOT_BLANK, REQUIRED 128 129Synonym of NOT_NULL. 130 131=item INT 132 133The parameter looks like a integer? i.e. It matches /^[+\-]?[0-9]+$/? 134 135=item UINT 136 137The parameter looks like a unsigned integer? i.e. It matches /^[0-9]+$/? 138 139=item ASCII 140 141 $_ =~ /^[\x21-\x7E]+$/ 142 143The parameter is just ASCII? 144 145=item DUPLICATION 146 147 $validator->check( 148 {mails => [qw/mail1 mail2/]} => ['DUPLICATION'] 149 ); 150 151The two parameters have same value? 152 153=item DUP 154 155Synonym of DUPLICATION. 156 157=item LENGTH 158 159 $validator->check( 160 name => [[qw/LENGTH 5 20/]], 161 password => [[qw/LENGTH 5/]], 162 ); 163 164Check the length of data. First argument means $minumum value, second argument is $max. 165$max is optional. 166 167=item EQUAL 168 169 $validator->check( 170 name => [[EQUAL => "foo"]], 171 ); 172 173Check parameter match the argument or not. 174 175=item REGEX 176 177 $validator->check( 178 name => [[REGEXP => qr/^[0-9]$/]], 179 ); 180 181Check regexp matches parameter or not. 182 183=item REGEXP 184 185Synonym of REGEX. 186 187=item CHOICE 188 189 $validator->check( 190 sex => [[CHOICE => qw/male female/]] 191 ); 192 193The parameter is one of choice or not. 194 195=item IN 196 197Synonym of CHOICE. 198 199=item NOT_IN 200 201 $validator->check( 202 new_user => [[NOT_IN => \@existing_users]] 203 ); 204 205The parameter does not belong to the list of values. 206 207=item MATCH 208 209 use MyApp::Util qw/is_foo/; 210 211 $validator->check( 212 foo => [[MATCH => \&is_foo ]], 213 bar => [[MATCH => sub { $_[0] eq 'foo' } ]], 214 ); 215 216Check parameter using callback. Callback takes parameter as first argument, 217should return true/false. 218 219=item FILTER 220 221 $validator->check( 222 foo => [[FILTER => 'trim'], 'INT'], 223 bar => [[FILTER => sub { $_[0] . '@example.com' } ], 'EMAIL'], 224 ); 225 226FILTER is special constraint. It does not check the value and simply filter. 227"trim" is only pre-defined. You can also pass a callback. 228Callback takes parameter as first argument, should return filtered value. 229 230=item FLAG 231 232 $validator->check( 233 is_enabled => [qw/FLAG/] 234 ); 235 236Check parameter is 0 or 1. 237 238=back 239 240