MooseX::Types::Structured - how I detest thee!
Yeah, thanks, “something failed validation”. You’re the damn computer, how about you tell me which field you didn’t like?!
Error: Attribute (payload) does not pass the type constraint because:
Validation failed for '
ArrayRef[
MooseX::Types::Structured::Dict[
some_id,
Int,
another_id,
MooseX::Types::Structured::Optional[Int],
some_code,
MooseX::Types::Structured::Optional[Str],
some_colour,
MooseX::Types::Structured::Optional[Str],
some_source,
MooseX::Types::Structured::Optional[__ANON__],
different_code,
MooseX::Types::Structured::Optional[Int],
restriction,
MooseX::Types::Structured::Optional[
MooseX::Types::Structured::Dict[
add,
MooseX::Types::Structured::Optional[ArrayRef[Str]],
remove,
MooseX::Types::Structured::Optional[ArrayRef[Str]]
]
],
a_term,
MooseX::Types::Structured::Optional[Str],
yet_another_id,
MooseX::Types::Structured::Optional[Str],
# snip many more fields #
# snip many more fields #
' failed with value ARRAY(0x1d263760)
You really aren’t being very helpful!
EDIT: Sample code and output
I put together this quick script:
#!/usr/bin/env perl
package Thingy;
use Moose;
use MooseX::Types::Moose qw(Str Int HashRef ArrayRef);
use MooseX::Types::Structured qw(Dict Tuple Optional);
has payload => (
is => 'rw',
isa=>ArrayRef[
MooseX::Types::Structured::Dict[
some_id => Int,
another_id => Int,
some_colour => Str,
yet_another_id => MooseX::Types::Structured::Optional[Int],
]
],
);
package main;
use TryCatch;
use Data::Dump qw(pp);
my $thingy = Thingy->new;
try {
$thingy->payload([
{ some_id => 'Ten' },
]);
}
catch($err) {
warn $err;
}
warn "Moose: $Moose::VERSION";
warn "MooseX::Types::Structured: $MooseX::Types::Structured::VERSION";
I ran the script on two different servers, with different Moose-y versions:
$ perl structured.pl
Attribute (payload) does not pass the type constraint because: Validation failed for 'ArrayRef[MooseX::Types::Structured::Dict[some_id,Int,another_id,Int,some_colour,Str,yet_another_id,MooseX::Types::Structured::Optional[Int]]]' failed with value ARRAY(0x1be8f990) at structured.pl line 27
eval {...} called at structured.pl line 31
Moose: 0.93 at structured.pl line 35.
MooseX::Types::Structured: 0.21 at structured.pl line 36.
Newer versions:
$ perl structured.pl
Attribute (payload) does not pass the type constraint because: Validation failed for 'ArrayRef[MooseX::Types::Structured::Dict[some_id,Int,another_id,Int,some_colour,Str,yet_another_id,MooseX::Types::Structured::Optional[Int]]]' with value ARRAY(0x89c4688) at structured.pl line 27
eval {...} called at structured.pl line 26
Moose: 1.21 at structured.pl line 35.
MooseX::Types::Structured: 0.26 at structured.pl line 36.
I then made sure I had the latest version of ::Structured, and re-ran the script:
$ perl structured.pl
Attribute (payload) does not pass the type constraint because: Validation failed for 'ArrayRef[MooseX::Types::Structured::Dict[some_id,Int,another_id,Int,some_colour,Str,yet_another_id,MooseX::Types::Structured::Optional[Int]]]' with value ARRAY(0xc07b350) at structured.pl line 27
eval {...} called at structured.pl line 26
Moose: 1.21 at structured.pl line 35.
MooseX::Types::Structured: 0.27 at structured.pl line 36.
That's not the latest version is it? I think the more recent versions have somewhat improved error messaging. Try again and let me know what you see.
yeah, i am also found that!
I've updated the original post with a script I tested with and the output for 0.21, 0.26 and 0.27.
Not sure if I'm doing something wrong, or stupid; or expecting the wrong things from the module(s).å
It seems you haven't initialized full type structure. For eg.:
$thingy->payload([
{ some_id => 10,
another_id => 1,
some_colour => 'blue',
yet_another_id => 1
}]);
This works :)
Darko, I know what causes the error in my simple example. I created the simple example to demonstrate that the error message (still) isn't as helpful as it could be for larger/more complex structures.
Ideally I'd like something that told me what field is invalid, and maybe the incorrect value passed to it; or a list of fields that are missing data and are not optional.
Late response, but see Types::Standard's errors.
(I used Moo instead of Moose for the example because throwing exception objects for type constraint violations breaks in Moose; Moose stringifies the exception object, which means we can't call the lovely "explain" method on it.)