Subject: | Type::Library messages lost when used with named parameters in Type::Params |
Date: | Mon, 29 Jun 2015 12:18:33 -0700 |
To: | bug-Type-Tiny [...] rt.cpan.org |
From: | Nick Tonkin <1nickt [...] gmail.com> |
Hi Toby,
Thanks for your amazing modules! Ran into a little problem:
I've noticed that when I use Types that I make with Type::Library, to
validate *named* incoming arguments, with Type::Params, using a slurpy Dict,
per the docs, I lose the custom error message set in the library.
# in package MyModule::Types
declare DBPrefix, as Optional[StrMatch[ qr/ \w+_ /x ]],
message { 'The table prefix must end in an underscore. ' };
# in package MyModule
use Type::Params qw/ compile /;
use My::Types qw/ DBPrefix /;
state $validate = compile( DBPrefix );
my ($param) = $validate->( @_ );
# in foo.pl
$obj->method( 'bar' );
# output:
# The table prefix must end in an underscore. (in $_[0]) at ./foo.pl line 11
# "DBPrefix" is a subtype of "Optional[StrMatch[(?^x: \w+_ )]]"
# Value "bar" did not pass type constraint "Optional[StrMatch[(?^x:
\w+_ )]]" (in $_[0]+)
# $_[0] exists
# "Optional[StrMatch[(?^x: \w+_ )]]" constrains $_[0] with
"StrMatch[(?^x: \w+_ )]" if +it exists
# Value "bar" did not pass type constraint "StrMatch[(?^x: \w+_ )]"
(in $_[0])
# "StrMatch[(?^x: \w+_ )]" is defined as: do { !ref($_) and $_ =~
$Types::Standard::_St+rMatch{"(?^x: \\w+_ )"} }
[download] <http://www.perlmonks.com/?abspart=1;displaytype=displaycode;node_id=1132514;part=2>
That's a little more than I really need, but it gives me my custom message
at the beginning.
Now, when I want to use named arguments, I follow the manual:
state $validate = compile(slurpy Dict[ prefix => DBPrefix ]); #per the
docs for named args
my ($param) = $validate->( @_ );
# in foo.pl
$obj->method( 'bar' );
# output :
# Reference {"prefix" => "bar"} did not pass type constraint
"Dict[prefix=>DBPrefix]" (in +$SLURPY) at ./foo.pl line 11
[download] <http://www.perlmonks.com/?abspart=1;displaytype=displaycode;node_id=1132514;part=3>
I need to use named args because some of them are completely optional. But
I want to be able to return an error that tells the user how to fix the
error!
Is there a way around this?
Thanks,
Nick