Hi,
I'm using the enum() constraints for a attribute. It works fine like this:
has 'index_type' => (
is => 'ro',
isa => enum($_, qw(for bar baz)),
required => 1,
);
But I'd like to provide a friendlier message then "Attribute
(index_type) does not pass the type constraint because:...5 more lines
of croak"
At this point I got stuck. I cannot add 'message' to the attribute
definition. It looks like I have to create a subtype but I can't seem
to get enum to work correctly within my subtype. This snip below will
allow ANY value.
use Moose;
use Moose::Util::TypeConstraints;
subtype 'IndexType'
=> as 'Str',
=> enum( $_, qw[foo bar baz]);
=> message { "index_type ($_) must be one of ". join
', ', qw[foo bar baz] };
has 'index_type' => (
isa => 'IndexType',
is => 'ro',
required => 1,
);
my $indexer = MyApp::Local::Indexer->new(index_type=>'foobarquin');
print "Index=$index\n";
# Index=MyApp::Local::Indexer=HASH(0x1a8fbc0)
Can anyone point me in the right direction please?
Thanks,
Dp.