summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Bread/Board/Declare/Role/Service.pm11
-rw-r--r--t/30-type-checks.t19
2 files changed, 27 insertions, 3 deletions
diff --git a/lib/Bread/Board/Declare/Role/Service.pm b/lib/Bread/Board/Declare/Role/Service.pm
index e0b36f9..96bdaf7 100644
--- a/lib/Bread/Board/Declare/Role/Service.pm
+++ b/lib/Bread/Board/Declare/Role/Service.pm
@@ -29,12 +29,17 @@ around get => sub {
my $self = shift;
my $container = $self->parent_container;
+ my $attr = $self->associated_attribute;
- if ($self->associated_attribute->has_value($container)) {
- return $self->associated_attribute->get_value($container);
+ if ($attr->has_value($container)) {
+ return $attr->get_value($container);
}
- return $self->$orig(@_);
+ my $val = $self->$orig(@_);
+ $attr->verify_against_type_constraint($val, instance => $container)
+ if $attr->has_type_constraint;
+
+ return $val;
};
=method parent_container
diff --git a/t/30-type-checks.t b/t/30-type-checks.t
index 9133da3..b7718ac 100644
--- a/t/30-type-checks.t
+++ b/t/30-type-checks.t
@@ -15,6 +15,19 @@ use Test::Moose;
isa => 'Ref',
value => 'FOO',
);
+
+ has bar => (
+ is => 'ro',
+ isa => 'Str',
+ block => sub { {} },
+ );
+
+ has baz => (
+ is => 'ro',
+ isa => 'HashRef',
+ block => sub { shift->param('bar') },
+ dependencies => ['bar'],
+ );
}
with_immutable {
@@ -22,6 +35,12 @@ with_immutable {
like(exception { $foo->foo },
qr/^Attribute \(foo\) does not pass the type constraint because: Validation failed for 'Ref' with value FOO/,
"error when service returns invalid value");
+ like(exception { $foo->bar },
+ qr/^Attribute \(bar\) does not pass the type constraint because: Validation failed for 'Str' with value HASH/,
+ "error when service returns invalid value");
+ like(exception { $foo->baz },
+ qr/^Attribute \(bar\) does not pass the type constraint because: Validation failed for 'Str' with value HASH/,
+ "error when service returns invalid value, even as a dependency");
} 'Foo';
done_testing;