diff options
-rw-r--r-- | lib/MooseX/Meta/TypeConstraint/Doctype.pm | 9 | ||||
-rw-r--r-- | t/nested.t | 63 |
2 files changed, 71 insertions, 1 deletions
diff --git a/lib/MooseX/Meta/TypeConstraint/Doctype.pm b/lib/MooseX/Meta/TypeConstraint/Doctype.pm index dbc1289..cc48312 100644 --- a/lib/MooseX/Meta/TypeConstraint/Doctype.pm +++ b/lib/MooseX/Meta/TypeConstraint/Doctype.pm @@ -177,7 +177,14 @@ sub _validate_doctype { 'Str|Moose::Meta::TypeConstraint' => sub { my $tc = Moose::Util::TypeConstraints::find_or_parse_type_constraint($doctype); die "Unknown type $doctype" unless $tc; - if (!$tc->check($data)) { + if ($tc->isa(__PACKAGE__)) { + my $sub_errors = $tc->_validate_doctype($data, undef, $prefix); + if ($sub_errors) { + $errors = $sub_errors->errors; + $extra_data = $sub_errors->extra_data; + } + } + elsif (!$tc->check($data)) { $errors = $self->_format_error($data, $prefix); } }, diff --git a/t/nested.t b/t/nested.t new file mode 100644 index 0000000..67f941b --- /dev/null +++ b/t/nested.t @@ -0,0 +1,63 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use Moose::Util::TypeConstraints 'find_type_constraint'; + +use MooseX::Validation::Doctypes; + +doctype 'Name' => { + first => 'Str', + last => 'Str', +}; + +doctype 'Person' => { + id => 'Str', + name => 'Name', + title => 'Str', +}; + +{ + my $person = find_type_constraint('Person'); + isa_ok($person, 'Moose::Meta::TypeConstraint'); + isa_ok($person, 'MooseX::Meta::TypeConstraint::Doctype'); + + is_deeply( + $person->doctype, + { id => 'Str', name => 'Name', title => 'Str' }, + "got the right doctype" + ); + + { + my $errors = $person->validate({ + id => '17382-QA', + name => { + first => 'Bob', + last => 'Smith', + }, + title => 'CIO' + }); + is($errors, undef, "no errors"); + } + + { + my $errors = $person->validate({ + id => '17382-QA', + name => { + first => [], + last => 'Smith', + }, + title => 'CIO', + }); + isa_ok($errors, 'MooseX::Validation::Doctypes::Errors'); + is_deeply( + $errors->errors, + { name => { first => "invalid value [ ] for 'name.first'" } }, + "got the right errors" + ); + is($errors->extra_data, undef, "no errors"); + } +} + +done_testing; |