diff options
-rw-r--r-- | lib/MooseX/Meta/TypeConstraint/Doctype.pm | 13 | ||||
-rw-r--r-- | t/complex.t | 27 |
2 files changed, 37 insertions, 3 deletions
diff --git a/lib/MooseX/Meta/TypeConstraint/Doctype.pm b/lib/MooseX/Meta/TypeConstraint/Doctype.pm index f18da4c..dbc1289 100644 --- a/lib/MooseX/Meta/TypeConstraint/Doctype.pm +++ b/lib/MooseX/Meta/TypeConstraint/Doctype.pm @@ -117,7 +117,7 @@ sub _validate_doctype { match_on_type $doctype => ( 'HashRef' => sub { if (!find_type_constraint('HashRef')->check($data)) { - $errors = $data; + $errors = $self->_format_error($data, $prefix); } else { for my $key (keys %$doctype) { @@ -147,7 +147,7 @@ sub _validate_doctype { }, 'ArrayRef' => sub { if (!find_type_constraint('ArrayRef')->check($data)) { - $errors = $data; + $errors = $self->_format_error($data, $prefix); } else { for my $i (0..$#$doctype) { @@ -178,7 +178,7 @@ sub _validate_doctype { my $tc = Moose::Util::TypeConstraints::find_or_parse_type_constraint($doctype); die "Unknown type $doctype" unless $tc; if (!$tc->check($data)) { - $errors = "invalid value " . dump($data) . " for '$prefix'"; + $errors = $self->_format_error($data, $prefix); } }, => sub { @@ -194,6 +194,13 @@ sub _validate_doctype { ); } +sub _format_error { + my $self = shift; + my ($data, $prefix) = @_; + + return "invalid value " . dump($data) . " for '$prefix'"; +} + no Moose; 1; diff --git a/t/complex.t b/t/complex.t index 04dbe60..a11ecd0 100644 --- a/t/complex.t +++ b/t/complex.t @@ -185,6 +185,33 @@ doctype 'Location' => { ); ok(!$errors->has_extra_data, "no extra data"); } + + { + my $errors = $location->validate({ + id => '14931-FL-53', + name => 'My House', + contact => { + phone => '867-5309', + support => 'anelson@cpan.org', + web => URI->new('https://metacpan.org/author/ANELSON'), + email => 'anelson@cpan.org', + }, + i18n => { + default_currency => 'USD', + default_locale => 'en', + available_currencies => [ 'CAD', 'EUR' ], + available_locales => [ 'en' ] + } + }); + is_deeply( + $errors->errors, + { + location => "invalid value undef for 'location'", + }, + "got the right errors" + ); + ok(!$errors->has_extra_data, "no extra data"); + } } done_testing; |