• Download Gist
moosextypesstructured---how-i-detest-thee.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#!/usr/bin/env perl
 
package Thingy;
use Moo;
use Types::Standard qw(Str Int HashRef ArrayRef Dict Tuple Optional);
 
has payload => (
is => 'rw',
isa => ArrayRef[
Dict[
some_id => Int,
another_id => Int,
some_colour => Str,
yet_another_id => Optional[Int],
]
],
);
 
package main;
use Try::Tiny;
use Data::Dumper;
 
my $thingy = Thingy->new;
 
try {
$thingy->payload([
{ some_id => 'Ten' },
]);
}
catch {
warn $_;
warn Dumper( $_->explain );
};
 
warn "Moo: $Moo::VERSION";
warn "Types::Standard: $Types::Standard::VERSION";
 
__END__
[{"some_id" => "Ten"}] did not pass type constraint "ArrayRef[Dict[another_id=>Int,some_colour=>Str,some_id=>Int,yet_another_id=>Optional[Int]]]" at (eval 237) line 12.
$VAR1 = [
'[{"some_id" => "Ten"}] did not pass type constraint "ArrayRef[Dict[another_id=>Int,some_colour=>Str,some_id=>Int,yet_another_id=>Optional[Int]]]"',
'"ArrayRef[Dict[another_id=>Int,some_colour=>Str,some_id=>Int,yet_another_id=>Optional[Int]]]" constrains each value in the array with "Dict[another_id=>Int,some_colour=>Str,some_id=>Int,yet_another_id=>Optional[Int]]"',
'{"some_id" => "Ten"} did not pass type constraint "Dict[another_id=>Int,some_colour=>Str,some_id=>Int,yet_another_id=>Optional[Int]]" (in $_->[0])',
'"Dict[another_id=>Int,some_colour=>Str,some_id=>Int,yet_another_id=>Optional[Int]]" requires key "another_id" to appear in hash'
];
Moo: 1.002 at ./scratch.pl line 35.
Types::Standard: 0.007_05 at ./scratch.pl line 36.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.