Skip to content

Instantly share code, notes, and snippets.

@chrisa
Created August 27, 2010 14:50
Show Gist options
  • Select an option

  • Save chrisa/553500 to your computer and use it in GitHub Desktop.

Select an option

Save chrisa/553500 to your computer and use it in GitHub Desktop.
From e7fc713ce41d2b8476980c61b7b61703932b2469 Mon Sep 17 00:00:00 2001
From: Chris Andrews <chrisandrews@venda.com>
Date: Fri, 27 Aug 2010 15:41:28 +0100
Subject: [PATCH] Don't skip validation on coercion fail to a type union including Undef
Given a type union 'SomeType|Undef' with coercions enabled, a value
which couldn't be coerced to SomeType would silently be discarded,
and the attribute would be assigned undef - with no validation failure.
This change ensures validation is applied to reject such values.
---
Changes | 3 +++
lib/Moose/Meta/TypeCoercion/Union.pm | 2 +-
.../009_union_types_and_coercions.t | 16 +++++++++++++++-
3 files changed, 19 insertions(+), 2 deletions(-)
mode change 100644 => 100755 t/040_type_constraints/009_union_types_and_coercions.t
diff --git a/Changes b/Changes
index e1a734a..f325ef6 100644
--- a/Changes
+++ b/Changes
@@ -5,6 +5,9 @@ for, noteworthy changes.
* mro::get_linear_isa is called as a function rather than a method
+ * Type constraint validation is no longer bypassed when failing to
+ coerce to a type union which includes Undef. (Chris Andrews)
+
1.10 Sun, Aug 22, 2010
[API CHANGES]
diff --git a/lib/Moose/Meta/TypeCoercion/Union.pm b/lib/Moose/Meta/TypeCoercion/Union.pm
index e2c4e1d..e76585d 100644
--- a/lib/Moose/Meta/TypeCoercion/Union.pm
+++ b/lib/Moose/Meta/TypeCoercion/Union.pm
@@ -36,7 +36,7 @@ sub compile_type_coercion {
return $temp if $type_constraint->check($temp);
}
}
- return undef;
+ return $value;
});
}
diff --git a/t/040_type_constraints/009_union_types_and_coercions.t b/t/040_type_constraints/009_union_types_and_coercions.t
old mode 100644
new mode 100755
index abedd6c..f242f58
--- a/t/040_type_constraints/009_union_types_and_coercions.t
+++ b/t/040_type_constraints/009_union_types_and_coercions.t
@@ -42,10 +42,12 @@ use Test::Requires {
=> from 'FileHandle'
=> via { bless $_, 'IO::File' };
- # create the alias
+ # create the aliases
subtype 'IO::StringOrFile' => as 'IO::String | IO::File';
+ subtype 'IO::StringOrFileOrUndef' => as 'IO::String | IO::File | Undef';
+
# attributes
has 'raw_body' => (
@@ -55,6 +57,12 @@ use Test::Requires {
default => sub { IO::String->new() },
);
+ has 'rare_body' => (
+ is => 'rw',
+ isa => 'IO::StringOrFileOrUndef',
+ coerce => 1,
+ );
+
sub as_string {
my ($self) = @_;
my $fh = $self->raw_body();
@@ -72,6 +80,12 @@ use Test::Requires {
}
{
+ dies_ok {
+ my $email = Email::Moose->new(rare_body => { });
+ } 'Died for invalid attribute value';
+}
+
+{
my $email = Email::Moose->new(raw_body => '... this is my body ...');
isa_ok($email, 'Email::Moose');
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment