Skip Menu |

This queue is for tickets about the Type-Tiny CPAN distribution.

Report information
The Basics
Id: 127005
Status: resolved
Priority: 0/
Queue: Type-Tiny

People
Owner: Nobody in particular
Requestors: tonywuxd [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



From: tonywuxd [...] gmail.com
Subject: Type::Util's "message" method does not take effect
OS:Ubuntu 18 Perl:Active State Perl ,V5.26.1 My Code====> BEGIN{ package User { use Class::Tiny qw(name age sex); }; $INC{"User.pm"} = __FILE__; } BEGIN{ package UserType { use Type::Library -base, -declare => qw(UserWrapper Age); use Types::Standard -types; use Type::Utils qw(class_type declare coerce as where message from via); use Type::Tiny; class_type UserWrapper, { class => "User" }; declare Age, as Int, where { $_ >= 10 and $_ < 20 }, message { "abc"; }; coerce UserWrapper, from Age, via {"User"->new(age => $_)}; }; $INC{"UserType.pm"} = __FILE__; } package MyType { use Moose; use User; use UserType qw(UserWrapper Age); has 'User' => (is => 'rw', isa => UserWrapper, coerce => 1); } my $j = MyType->new(User => 4) The Output===> Attribute (User) does not pass the type constraint because: Value "4" did not pass type constraint "UserWrapper" (not isa User) at /opt/scientific/libs/perl5/site/lib/Moose/Object.pm line 24 Moose::Object::new('MyType', 'User', 4) called at /root/PycharmProjects/untitled/info.pl line 107 In other words, the "message" prompt for my custom type "Age" does not take effect. Is this a problem with my use? Still not configured. Please advise, thank you
Show quoted text
> In other words, the "message" prompt for my custom type "Age" does not > take effect. Is this a problem with my use? Still not configured. > Please advise, thank you
The supplied value for the User attribute, 4, is not a UserWrapper, but there is no matching coercion to use, because 4 doesn't match the constraints of Age either. Age is not the type of the attribute, so its error message does not get used.
Yep, as ether said, the message comes from UserWrapper, not from Age. You can do something like this. class_type UserWrapper, { class => 'User', message => sub { ... }, };