xeronuro
12/6/2013 - 5:40 PM

moosextypesstructured---how-i-detest-thee.pl

#!/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.