Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Attribute does not pass Type Constraint

by PerlSufi (Friar)
on Apr 03, 2014 at 15:48 UTC ( [id://1080965]=perlquestion: print w/replies, xml ) Need Help??

PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I am having a problem with an attribute which isa DateTime. the error is:
Attribute (created_time) does not pass the type constraint because: Va +lidation failed for 'DateTime' with value undef

here is the code:
class_type 'DateTime'; subtype 'My::Model::Support::Ticket::Str' => as 'Str'; subtype 'My::Model::Support::Ticket::Int' => as 'Int'; coerce 'DateTime', from 'My::Model::Support::Ticket::Str', via { DateTime::Format::DateParse->parse_datetime($_) }; coerce 'DateTime', from 'My::Model::Support::Ticket::Int', via { DateTime->from_epoch( epoch => $_ ) }; ... has created_time => ( is => 'rw', isa => 'DateTime', required => 0, coerce => 1, );
Without giving you a ton of code, what do you recommend that I do to try to debug this problem? I tried:
use MooseX::UndefTolerant::Attribute; ... has created_time => ( traits => [qw(MooseX::UndefTolerant::Attribute)], #here is => 'rw', isa => 'DateTime', required => 0, coerce => 1, );
But I ended up getting the error:
"exception" : "Type of argument to keys on reference must be unblessed + hashref or arrayref at /My/Model/Support/Ticket
Any insight is greatly appreciated :)

Replies are listed 'Best First'.
Re: Attribute does not pass Type Constraint
by tobyink (Canon) on Apr 03, 2014 at 17:59 UTC

    It would be useful if you could provide a small, self-contained script exhibiting the problem.

    The "type of argument to keys on reference" error message comes from doing keys($blessed_object). I can't see anywhere in MooseX::UndefTolerant that does this. (Or indeed anywhere where it uses keys() at all!)

    Are you sure that it's the created_date attribute which is causing the issue? Might it be something else?

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Thanks for the response tobyink.
      I'm starting to see now that maybe the addition of MooseX::UndefTolerant might have helped- not sure yet. I am getting this error from my test file, but the line it is complaining about is in the following subroutine:
      my $class = shift; my (%args) = validated_hash( \@_, id => { isa => 'Int' }, ticket_user => { isa => 'Customer::TicketUser' }, ); my $ticket_response = $class->_ticket_api_request( { method => 'get', route => 'user/' . $args{ticket_user}->user_id . '/ticket/' . $args{id}, data => undef, } ); my $ticket = $class->new( { id => $args{id}, department_name => $ticket_response->{ticket}{department +}, subject => $ticket_response->{ticket}{subject}, status_id => $ticket_response->{ticket}{status}, created_time => $ticket_response->{ticket}{created_ti +me}, ticket_user => $args{ticket_user}, } ); my $posts_response = $class->_ticket_api_request( { method => 'get', route => 'user/' . $args{ticket_user}->user_id . '/ticket/' . $args{id} . '/posts', data => undef, } ); my @posts; for my $post_id ( keys $posts_response ) { # HERE my $post = GBClient::Model::Support::Ticket::Post->from_json +( { ticket => $ticket, json => $posts_response->{$post_id} } ); push @posts, $post; }
        I think I may have figured it out. I made the following changes:
        class_type 'DateTime'; subtype 'DT' => as 'Maybe[DateTime]'; coerce 'DT', from 'Str', via { DateTime::Format::DateParse->parse_datetime($_) }; coerce 'DT', from 'Int', via { DateTime->from_epoch( epoch => $_ ) }; ... has created_time => ( is => 'rw', isa => 'DT', required => 0, coerce => 1, ); # and then in the subroutine I posted earlier: ... for my $post_id ( keys %$post_response ) { # changed to unblessed ...
        But I'm now getting an error about something else in the module. So I will follow up as necessary :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1080965]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-18 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found