12 use MooseX::Params::Validate;
18 my ( $self, %params ) = validated_hash(
20 bar => { isa => 'Str', default => 'Moose' },
22 return "Horray for $params{bar}!";
27 use Moose::Util::TypeConstraints;
28 use MooseX::Params::Validate;
34 my %params = validated_hash(
36 foo => { isa => 'Foo' },
37 baz => { isa => 'ArrayRef | HashRef', optional => 1 },
38 gorch => { isa => 'ArrayRef[Int]', optional => 1 },
40 [ $params{foo}, $params{baz}, $params{gorch} ];
45 my %params = validated_hash(
48 isa => subtype( 'Object' => where { $_->isa('Foo') } ),
51 bar => { does => 'Roles::Blah', optional => 1 },
53 does => role_type('Roles::Blah'),
57 return $params{foo} || $params{bar} || $params{boo};
62 my %params = validated_hash(
67 'some random callback' => sub { !ref($_[0]) || @{ $_[0] } <= 2 },
77 isa_ok( $foo, 'Foo' );
79 is( $foo->foo, 'Horray for Moose!', '... got the right return value' );
81 $foo->foo( bar => 'Rolsky' ), 'Horray for Rolsky!',
82 '... got the right return value'
85 is( $foo->baz( foo => $foo ), $foo, '... foo param must be a Foo instance' );
88 exception { $foo->baz( foo => 10 ) }, qr/\QThe 'foo' parameter ("10")/,
89 '... the foo param in &baz must be a Foo instance'
92 exception { $foo->baz( foo => "foo" ) },
93 qr/\QThe 'foo' parameter ("foo")/,
94 '... the foo param in &baz must be a Foo instance'
97 exception { $foo->baz( foo => [] ) }, qr/\QThe 'foo' parameter/,
98 '... the foo param in &baz must be a Foo instance'
101 is( $foo->baz( bar => $foo ), $foo, '... bar param must do Roles::Blah' );
104 exception { $foo->baz( bar => 10 ) }, qr/\QThe 'bar' parameter ("10")/,
105 '... the bar param in &baz must be do Roles::Blah'
108 exception { $foo->baz( bar => "foo" ) },
109 qr/\QThe 'bar' parameter ("foo")/,
110 '... the bar param in &baz must be do Roles::Blah'
113 exception { $foo->baz( bar => [] ) }, qr/\QThe 'bar' parameter/,
114 '... the bar param in &baz must be do Roles::Blah'
117 is( $foo->baz( boo => $foo ), $foo, '... boo param must do Roles::Blah' );
120 exception { $foo->baz( boo => 10 ) }, qr/\QThe 'boo' parameter ("10")/,
121 '... the boo param in &baz must be do Roles::Blah'
124 exception { $foo->baz( boo => "foo" ) },
125 qr/\QThe 'boo' parameter ("foo")/,
126 '... the boo param in &baz must be do Roles::Blah'
129 exception { $foo->baz( boo => [] ) }, qr/\QThe 'boo' parameter/,
130 '... the boo param in &baz must be do Roles::Blah'
134 exception { $foo->bar }, qr/\QMandatory parameter 'foo'/,
135 '... bar has a required param'
138 exception { $foo->bar( foo => 10 ) }, qr/\QThe 'foo' parameter ("10")/,
139 '... the foo param in &bar must be a Foo instance'
142 exception { $foo->bar( foo => "foo" ) },
143 qr/\QThe 'foo' parameter ("foo")/,
144 '... the foo param in &bar must be a Foo instance'
147 exception { $foo->bar( foo => [] ) }, qr/\QThe 'foo' parameter/,
148 '... the foo param in &bar must be a Foo instance'
150 like( exception { $foo->bar( baz => [] ) }, qr/\QMandatory parameter 'foo'/ );
153 $foo->bar( foo => $foo ),
154 [ $foo, undef, undef ],
155 '... the foo param in &bar got a Foo instance'
159 $foo->bar( foo => $foo, baz => [] ),
161 '... the foo param and baz param in &bar got a correct args'
165 $foo->bar( foo => $foo, baz => {} ),
167 '... the foo param and baz param in &bar got a correct args'
171 exception { $foo->bar( foo => $foo, baz => undef ) },
172 qr/\QThe 'baz' parameter (undef)/,
173 '... baz requires a ArrayRef | HashRef'
176 exception { $foo->bar( foo => $foo, baz => 10 ) },
177 qr/\QThe 'baz' parameter ("10")/,
178 '... baz requires a ArrayRef | HashRef'
181 exception { $foo->bar( foo => $foo, baz => 'Foo' ) },
182 qr/\QThe 'baz' parameter ("Foo")/,
183 '... baz requires a ArrayRef | HashRef'
186 exception { $foo->bar( foo => $foo, baz => \( my $var ) ) },
187 qr/\QThe 'baz' parameter/, '... baz requires a ArrayRef | HashRef'
191 $foo->bar( foo => $foo, gorch => [ 1, 2, 3 ] ),
192 [ $foo, undef, [ 1, 2, 3 ] ],
193 '... the foo param in &bar got a Foo instance'
197 exception { $foo->bar( foo => $foo, gorch => undef ) },
198 qr/\QThe 'gorch' parameter (undef)/,
199 '... gorch requires a ArrayRef[Int]'
202 exception { $foo->bar( foo => $foo, gorch => 10 ) },
203 qr/\QThe 'gorch' parameter ("10")/,
204 '... gorch requires a ArrayRef[Int]'
207 exception { $foo->bar( foo => $foo, gorch => 'Foo' ) },
208 qr/\QThe 'gorch' parameter ("Foo")/,
209 '... gorch requires a ArrayRef[Int]'
212 exception { $foo->bar( foo => $foo, gorch => \( my $var ) ) },
213 qr/\QThe 'gorch' parameter/, '... gorch requires a ArrayRef[Int]'
216 exception { $foo->bar( foo => $foo, gorch => [qw/one two three/] ) },
217 qr/\QThe 'gorch' parameter/, '... gorch requires a ArrayRef[Int]'
221 exception { $foo->quux( foo => '123456790' ) },
222 qr/\QThe 'foo' parameter\E.+\Qchecking type constraint/,
223 '... foo parameter must be an ArrayRef'
227 exception { $foo->quux( foo => [ 1, 2, 3, 4 ] ) },
228 qr/\QThe 'foo' parameter\E.+\Qsome random callback/,
229 '... foo parameter additional callback requires that arrayref be 0-2 elements'