hideo55/p5-FormValidator-Lite-Constraint-Moose — pull request #1 by hirobanex (2012-03-07 02:10:09+00:00)
Title: support coercions

Please, support coercions.

diff --git a/lib/FormValidator/Lite/Constraint/Moose.pm b/lib/FormValidator/Lite/Constraint/Moose.pm
index c9c22eb..0a3f911 100644
--- a/lib/FormValidator/Lite/Constraint/Moose.pm
+++ b/lib/FormValidator/Lite/Constraint/Moose.pm
@@ -12,7 +12,15 @@ my @types = any_moose('::Util::TypeConstraints')->list_all_type_constraints();
for my $name (@types) {
my $constraint = _get_constraint($name);
rule $name => sub {
- $constraint->check($_);
+ my $value = $_;
+
+ $constraint->check($value) or do {
+ return unless $constraint->has_coercion;
+
+ $value = $constraint->coerce($value);
+
+ return $constraint->check($value);
+ }
};
}