@ISA = qw(Exporter);
use Carp qw(confess);
-our $VERSION = "1.005";
+our $VERSION = "1.006";
+
+my $re_real =
+ qr/
+ (
+ [-+]? # optional sign
+ (?:
+ [0-9]+(?:\.[0-9]*)? # either 9 with optional decimal digits
+ |
+ \.[0-9]+ # or . with required digits
+ )
+ (?:[eE][-+]?[0-9]+)? # optional exponent
+ )
+ /x;
my %built_ins =
(
}
}
if ($rule->{integer}) {
- unless ($data =~ /^\s*([-+]?\d+)s*$/) {
+ unless ($data =~ /^\s*([-+]?\d+)\s*$/) {
$errors->{$field} = _make_error($field, $info, $rule,
'$n must be a whole number');
last RULE;
last RULE;
}
}
+ if ($rule->{real}) {
+ unless ($data =~ /^\s*$re_real\s*$/) {
+ $errors->{$field} = _make_error($field, $info, $rule,
+ '$n must be a number');
+ last RULE;
+ }
+ my $num = $1;
+ if (my ($from, $to) = $rule->{real} =~ /^$re_real\s*-\s*$re_real$/) {
+ unless ($from <= $num and $num <= $to) {
+ $errors->{$field} = _make_error($field, $info, $rule,
+ $info->{range_error} ||
+ $rule->{range_error} ||
+ "\$n must be in the range $from to $to");
+ last RULE;
+ }
+ }
+ elsif (my ($from2) = $rule->{real} =~ /^\s*$re_real\s*-$/) {
+ unless ($from2 <= $num) {
+ $errors->{$field} = _make_error($field, $info, $rule,
+ $info->{range_error} ||
+ $rule->{range_error} ||
+ "\$n must be $from2 or higher");
+ last RULE;
+ }
+ }
+ }
if ($rule->{ref}) {
my $method = $rule->{method}
or confess "Missing method in ref rule $rule_name";
If set to a range I<integer>-I<integer> then ensures the value is an
integer in that range.
+=item real
+
+If set to 1, simply ensures the value is an real number.
+
+If set to a range C<< I<real> - I<real> >> then ensures the value is
+a real number in that range.
+
=item date
If set to 1, simply validates the value as a date.
#!perl -w
use strict;
-use Test::More tests => 38;
+use Test::More tests => 82;
BEGIN { use_ok('DevHelp::Validate'); }
ok($val->validate({time => "12pm" }, \%errors), "simple 12pm");
ok(!$val->validate({time => "13pm" }, \%errors), "simple 13pm");
}
+
+# reals
+{
+ my @rules =
+ (
+ {
+ name => "simple",
+ rule =>
+ {
+ real => 1,
+ },
+ tests =>
+ [
+ [ "1", 1, undef ],
+ [ "2.", 1, undef ],
+ [ "100", 1, undef ],
+ [ " 100", 1, undef ],
+ [ " 100 ", 1, undef ],
+ [ "1e10", 1, undef ],
+ [ "1E10", 1, undef ],
+ [ "-10", 1, undef ],
+ [ "+10", 1, undef ],
+ [ "0.1", 1, undef ],
+ [ ".1", 1, undef ],
+ [ "1.1e10", 1, undef ],
+ [ "1.1e+10", 1, undef ],
+ [ "1.1e-10", 1, undef ],
+ [ "1.1e-2", 1, undef ],
+ [ "abc", '', "Value must be a number" ],
+ ],
+ },
+ {
+ name => "0 or more",
+ rule =>
+ {
+ real => "0 -",
+ },
+ tests =>
+ [
+ [ "0", 1, undef ],
+ [ "-1", '', "Value must be 0 or higher" ],
+ ],
+ },
+ {
+ name => "0 to 100",
+ rule =>
+ {
+ real => "0 - 100",
+ },
+ tests =>
+ [
+ [ "0", 1, undef ],
+ [ "1", 1, undef ],
+ [ "-1", "", "Value must be in the range 0 to 100" ],
+ [ "101", "", "Value must be in the range 0 to 100" ],
+ ],
+ },
+ );
+
+ for my $rule (@rules) {
+ for my $test (@{$rule->{tests}}) {
+ my $val = DevHelp::Validate::Hash->new
+ (
+ fields =>
+ {
+ value =>
+ {
+ rules => "test",
+ description => "Value",
+ },
+ },
+ rules =>
+ {
+ test => $rule->{rule},
+ },
+ );
+ my %errors;
+ my $name = "rule '$rule->{name}' value '$test->[0]'";
+ is($val->validate({ value => $test->[0] }, \%errors), $test->[1],
+ "$name: validate");
+ is($errors{value}, $test->[2], "$name: message");
+ }
+ }
+}