Improve type constraint stuff
[gitmo/Mouse.git] / lib / Mouse / Meta / TypeConstraint.pm
index 538e3b2..51b0867 100644 (file)
@@ -1,6 +1,8 @@
 package Mouse::Meta::TypeConstraint;
 use strict;
 use warnings;
+use Carp ();
+
 use overload '""'     => sub { shift->{name} },   # stringify to tc name
              fallback => 1;
 
@@ -28,6 +30,26 @@ sub check {
     $self->{_compiled_type_constraint}->(@_);
 }
 
+sub validate {
+    my ($self, $value) = @_;\r
+    if ($self->{_compiled_type_constraint}->($value)) {\r
+        return undef;\r
+    }\r
+    else {\r
+        $self->get_message($value);\r
+    }\r
+}
+
+sub assert_valid {\r
+    my ($self, $value) = @_;\r
+\r
+    my $error = $self->validate($value);\r
+    return 1 if ! defined $error;\r
+
+    Carp::confess($error);\r
+}\r
+
+
 sub message {
     return $_[0]->{message};
 }