Skip to content

Commit

Permalink
allow parameterized types to customize their messages
Browse files Browse the repository at this point in the history
The same arguments that are passed to the where block are also passed to
the message block to allow the error message to be customized.
  • Loading branch information
Brian Phillips committed Aug 2, 2011
1 parent 3782156 commit fd19723
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/MooseX/Meta/TypeConstraint/Parameterizable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ sub parameterize {
constraining_value => $args,
parent_type_constraint=>$self->parent_type_constraint,
constraining_value_type_constraint => $self->constraining_value_type_constraint,
($self->has_message ? (message => $self->message) : ()),
($self->has_message ? (message => sub { $self->message->( @_, $args ) } ) : ()),
);
}
}
Expand Down
24 changes: 24 additions & 0 deletions t/08-custom-messages.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use strict;
use warnings;

use Test::More;
use MooseX::Types -declare=>[qw( SizedArray )];
use MooseX::Types::Parameterizable qw(Parameterizable);
use MooseX::Types::Moose qw( Int ArrayRef );

ok subtype(
SizedArray,
as Parameterizable[ArrayRef,Int],
where {
my ($value, $max) = @_;
@$value > $max
},
message {
my($value, $max) = @_;
return sprintf('%d > %d', scalar(@$value), $max);
}
), 'Created parameterized type';

is SizedArray([3])->get_message([1..4]), q{4 > 3}, 'custom message';

done_testing;

0 comments on commit fd19723

Please sign in to comment.