sub verify_type_constraint_error {
my($self, $name, $value, $type) = @_;
- $type = ref($type) eq 'ARRAY' ? join '|', map { $_->name } @{ $type } : $type->name;
- my $display = defined($value) ? overload::StrVal($value) : 'undef';
- Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display");
+ Carp::confess("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
}
sub coerce_constraint { ## my($self, $value) = @_;
$check = $check->{_compiled_type_constraint};
}
- bless +{ name => $name, _compiled_type_constraint => $check }, $class;
+ bless +{
+ name => $name,
+ _compiled_type_constraint => $check,
+ message => $args{message}
+ }, $class;
}
sub name { shift->{name} }
$self->{_compiled_type_constraint}->(@_);
}
+sub message {
+ return $_[0]->{message};
+}
+
+sub get_message {
+ my ($self, $value) = @_;
+ if ( my $msg = $self->message ) {
+ local $_ = $value;
+ return $msg->($value);
+ }
+ else {
+ $value = ( defined $value ? overload::StrVal($value) : 'undef' );
+ return
+ "Validation failed for '"
+ . $self->name
+ . "' failed with value $value";
+ }
+}
+
1;
__END__
if ($TYPE{$name} && $TYPE_SOURCE{$name} ne $pkg) {
Carp::croak "The type constraint '$name' has already been created in $TYPE_SOURCE{$name} and cannot be created again in $pkg";
};
- my $constraint = $conf{where};
- my $as_constraint = find_or_create_isa_type_constraint($conf{as} || 'Any');
+ my $constraint = delete $conf{where};
+ my $as_constraint = find_or_create_isa_type_constraint(delete $conf{as} || 'Any');
$TYPE_SOURCE{$name} = $pkg;
$TYPE{$name} = Mouse::Meta::TypeConstraint->new(
$as_constraint->check($_[0]);
}
),
+ %conf
);
return $name;
ok(My::Class->new(name => 'foo'));
-TODO: {
- local $TODO = "message is not used";
- throws_ok { My::Class->new(name => '') } qr/^Attribute \(name\) does not pass the type constraint because: The string is empty!/;
-};
+throws_ok { My::Class->new(name => '') } qr/^Attribute \(name\) does not pass the type constraint because: The string is empty!/;