Skip to content

Instantly share code, notes, and snippets.

@hirose31
Created January 22, 2016 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirose31/1dbb1262ac2cbd071537 to your computer and use it in GitHub Desktop.
Save hirose31/1dbb1262ac2cbd071537 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010_000;
use Data::Validator;
use Time::Piece;
say 'Perl: ', $^V;
say 'Data::Validator: ', $Data::Validator::VERSION;
say 'Mouse: ', $Mouse::VERSION;
sub print_result {
my $rule = shift;
print " ";
if ($rule->has_errors) {
print join("\n ", map {$_->{message}} @{$rule->clear_errors}),"\n";
} else {
print "OK\n";
}
}
{ # OK
say '# Int|Object';
my $rule = Data::Validator->new(
v => { isa => 'Int|Object' },
)->with('NoThrow');
$rule->validate(v => 2);
print_result($rule);
$rule->validate(v => Time::Piece->new);
print_result($rule);
}
{ # error
say '# Int|Time::Piece';
my $rule = Data::Validator->new(
v => { isa => 'Int|Time::Piece' },
)->with('NoThrow');
$rule->validate(v => 2);
print_result($rule);
$rule->validate(v => Time::Piece->new);
print_result($rule);
}
__END__
Perl: v5.18.2
Data::Validator: 1.07
Mouse: v2.4.5
# Int|Object
OK
OK
# Int|Time::Piece
Invalid value for 'v': Validation failed for 'Int|Time::Piece' with value 2
Invalid value for 'v': Validation failed for 'Int|Time::Piece' with value Time::Piece=ARRAY(0x1913bb0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment