From c1aed1711db0678b57a17ea125c0b92e204a308c Mon Sep 17 00:00:00 2001 From: Jonathan Rubin Date: Fri, 18 May 2018 17:47:24 -0400 Subject: [PATCH 1/2] Test that type constraint messages can be objects --- .../stringifiable_type_constraint_messages.t | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 t/bugs/stringifiable_type_constraint_messages.t diff --git a/t/bugs/stringifiable_type_constraint_messages.t b/t/bugs/stringifiable_type_constraint_messages.t new file mode 100644 index 000000000..596e49448 --- /dev/null +++ b/t/bugs/stringifiable_type_constraint_messages.t @@ -0,0 +1,53 @@ +use strict; +use warnings; + +# RT 123299 + +use Test::More; +use Test::Fatal; + +{ + package String::Object; + + use overload '""' => \&as_string, fallback => 1; + + sub new { + my $self = bless {}, __PACKAGE__; + } + + sub as_string { 'successful exception' } + + package Bad::Moose::Types; + + use Moose; + use MooseX::Types::Moose qw( Str ); + use MooseX::Types -declare => [ 'AlwaysFail', 'StringObject' ]; + + subtype AlwaysFail, + as Str, + where { 0 }, + message { String::Object->new }; + + class_type StringObject, { class => 'String::Object' }; + coerce Str, from StringObject, via { 'coerced' }; + + package Bad::Moose; + use Moose; + + has validation_fail => ( + is => 'ro', + isa => Bad::Moose::Types::AlwaysFail, + ); + + __PACKAGE__->meta->make_immutable; # This is the key line +} + +package main; + +like( + exception { Bad::Moose->new( validation_fail => '123' ) }, + qr/successful exception/, + "Moose type constraints accept stringifiable type constraint errors" +); + +done_testing(); From eb7aae90a652c20cb0ef8f3869b60c0228457af1 Mon Sep 17 00:00:00 2001 From: Jonathan Rubin Date: Fri, 18 May 2018 17:49:03 -0400 Subject: [PATCH 2/2] Allow string objects in type constraint errors --- ...ValidationFailedForInlineTypeConstraint.pm | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/Moose/Exception/ValidationFailedForInlineTypeConstraint.pm b/lib/Moose/Exception/ValidationFailedForInlineTypeConstraint.pm index ab9c24a4a..bbc043b4d 100644 --- a/lib/Moose/Exception/ValidationFailedForInlineTypeConstraint.pm +++ b/lib/Moose/Exception/ValidationFailedForInlineTypeConstraint.pm @@ -5,9 +5,28 @@ use Moose; extends 'Moose::Exception'; with 'Moose::Exception::Role::Class'; +# An indiscriminate `use` without an import list somehow makes +# type_constraint_message a hash memory address +use Moose::Util::TypeConstraints qw( + duck_type + + coerce + from + via + + subtype + as + ); +subtype '_MooseImmediateStr' => as 'Str'; +subtype '_MooseDucktypeStr' => as duck_type([qw< ("" >]); +coerce '_MooseImmediateStr', + from '_MooseDucktypeStr', + via { "$_" }; + has 'type_constraint_message' => ( is => 'ro', - isa => 'Str', + isa => '_MooseImmediateStr', + coerce => 1, required => 1 );