From: Dave Rolsky Date: Mon, 2 Feb 2009 22:52:21 +0000 (+0000) Subject: Do some hackish validation of type names in MUTC, though unfortunately X-Git-Tag: 0.66~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eee1a213503b598ae220d18514bc43dd84232f64;hp=4a8035cfddad21c3cb2fa01fb6c640f607fedab7;p=gitmo%2FMoose.git Do some hackish validation of type names in MUTC, though unfortunately I can't find a clean way to put this into the metaclasses. Also make sure to allow "." in type names. --- diff --git a/Changes b/Changes index d267697..837b04b 100644 --- a/Changes +++ b/Changes @@ -17,6 +17,13 @@ Revision history for Perl extension Moose cause very unhelpful errors when it tried to throw an error before Moose was loaded. (Dave Rolsky) + * Moose::Util::TypeConstraints + - You could declare a name with subtype such as "Foo!Bar" that + would be allowed, but if you used it in a parameterized type + such as "ArrayRef[Foo!Bar]" it wouldn't work. We now do some + vetting on names created via the sugar functions, so that they + can only contain alphanumerics, ":", and ".". + 0.65 Thu, January 22, 2008 * Moose and Moose::Meta::Method::Overridden - If an overridden method called super(), and then the diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index eb253d9..5b1db5d 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -381,6 +381,10 @@ sub _create_type_constraint ($$$;$$) { . " and cannot be created again in " . $pkg_defined_in ) if defined $type; + + $name =~ /^[\w:\.]+$/ + or die qq{$name contains invalid characters for a type name.} + . qq{Names can contain alphanumeric character, ":", and "."\n}; } my %opts = ( @@ -439,7 +443,7 @@ sub _install_type_coercions ($$) { use re "eval"; - my $valid_chars = qr{[\w:]}; + my $valid_chars = qr{[\w:\.]}; my $type_atom = qr{ $valid_chars+ }; my $any; @@ -788,6 +792,9 @@ but it is a saner restriction than most others. =head2 Type Constraint Naming +Type name declared via this module can only contain alphanumeric +characters, colons (:), and periods (.). + Since the types created by this module are global, it is suggested that you namespace your types just as you would namespace your modules. So instead of creating a I type for your B diff --git a/t/040_type_constraints/033_type_names.t b/t/040_type_constraints/033_type_names.t new file mode 100644 index 0000000..e719866 --- /dev/null +++ b/t/040_type_constraints/033_type_names.t @@ -0,0 +1,36 @@ +use strict; +use warnings; + +use Test::More tests => 6; +use Test::Exception; + +use Moose::Meta::TypeConstraint; +use Moose::Util::TypeConstraints; + + +TODO: +{ + local $TODO = 'type names are not validated in the TC metaclass'; + + throws_ok { Moose::Meta::TypeConstraint->new( name => 'Foo-Bar' ) } + qr/contains invalid characters/, + 'Type names cannot contain a dash'; +} + +lives_ok { Moose::Meta::TypeConstraint->new( name => 'Foo.Bar::Baz' ) } +'Type names can contain periods and colons'; + +throws_ok { subtype 'Foo-Baz' => as 'Item' } +qr/contains invalid characters/, + 'Type names cannot contain a dash (via subtype sugar)'; + +lives_ok { subtype 'Foo.Bar::Baz' => as 'Item' } +'Type names can contain periods and colons (via subtype sugar)'; + +is( Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[In-valid]'), + undef, + 'find_or_parse_type_constraint returns undef on an invalid name' ); + +is( Moose::Util::TypeConstraints::find_or_parse_type_constraint('ArrayRef[Va.lid]'), + 'ArrayRef[Va.lid]', + 'find_or_parse_type_constraint returns name for valid name' );