1MooseX::Params::ValidatUes(e3r)Contributed Perl DocumentMaotoisoenX::Params::Validate(3)
2
3
4
6 MooseX::Params::Validate - an extension of Params::Validate for using
7 Moose's types
8
10 package Foo;
11 use Moose;
12 use MooseX::Params::Validate;
13
14 sub foo {
15 my ( $self, %params ) = validated_hash(
16 \@_,
17 bar => { isa => 'Str', default => 'Moose' },
18 );
19 return "Horray for $params{bar}!";
20 }
21
22 sub bar {
23 my $self = shift;
24 my ( $foo, $baz, $gorch ) = validated_list(
25 \@_,
26 foo => { isa => 'Foo' },
27 baz => { isa => 'ArrayRef | HashRef', optional => 1 },
28 gorch => { isa => 'ArrayRef[Int]', optional => 1 }
29 );
30 [ $foo, $baz, $gorch ];
31 }
32
34 This module fills a gap in Moose by adding method parameter validation
35 to Moose. This is just one of many developing options, it should not be
36 considered the "official" one by any means though.
37
38 You might also want to explore "MooseX::Method::Signatures" and
39 "MooseX::Declare"
40
42 It is not possible to introspect the method parameter specs, they are
43 created as needed when the method is called and cached for subsequent
44 calls.
45
47 validated_hash( \@_, %parameter_spec )
48 This behaves similar to the standard Params::Validate "validate"
49 function and returns the captured values in a HASH. The one
50 exception being that if it spots an instance in the @_, then it
51 will handle it appropriately (unlike Params::Validate which forces
52 you to shift you $self first).
53
54 The %parameter_spec accepts the following options:
55
56 isa The "isa" option can be either; class name, Moose type
57 constraint name or an anon Moose type constraint.
58
59 does
60 The "does" option can be either; role name or an anon Moose
61 type constraint.
62
63 default
64 This is the default value to be used if the value is not
65 supplied.
66
67 optional
68 As with Params::Validate, all options are considered required
69 unless otherwise specified. This option is passed directly to
70 Params::Validate.
71
72 coerce
73 If this is true and the parameter has a type constraint which
74 has coercions, then the coercion will be called for this
75 parameter. If the type does have coercions, then this parameter
76 is ignored.
77
78 This function is also available under its old name, "validate".
79
80 validated_list( \@_, %parameter_spec )
81 The %parameter_spec accepts the same options as above, but returns
82 the parameters as positional values instead of a HASH. This is best
83 explained by example:
84
85 sub foo {
86 my ( $self, $foo, $bar ) = validated_list(
87 \@_,
88 foo => { isa => 'Foo' },
89 bar => { isa => 'Bar' },
90 );
91 $foo->baz($bar);
92 }
93
94 We capture the order in which you defined the parameters and then
95 return them as a list in the same order. If a param is marked
96 optional and not included, then it will be set to "undef".
97
98 Like "validated_hash", if it spots an object instance as the first
99 parameter of @_, it will handle it appropriately, returning it as
100 the first argument.
101
102 This function is also available under its old name, "validatep".
103
104 pos_validated_list( \@_, $spec, $spec, ... )
105 This function validates a list of positional parameters. Each $spec
106 should validate one of the parameters in the list:
107
108 sub foo {
109 my $self = shift;
110 my ( $foo, $bar ) = pos_validated_list(
111 \@_,
112 { isa => 'Foo' },
113 { isa => 'Bar' },
114 );
115
116 ...
117 }
118
119 Unlike the other functions, this function cannot find $self in the
120 argument list. Make sure to shift it off yourself before doing
121 validation.
122
123 If a parameter is marked as optional and is not present, it will
124 simply not be returned.
125
126 If you want to pass in any of the cache control parameters
127 described below, simply pass them after the list of parameter
128 validation specs:
129
130 sub foo {
131 my $self = shift;
132 my ( $foo, $bar ) = pos_validated_list(
133 \@_,
134 { isa => 'Foo' },
135 { isa => 'Bar' },
136 MX_PARAMS_VALIDATE_NO_CACHE => 1,
137 );
138
139 ...
140 }
141
143 By default, this module exports the "validated_hash", "validated_list",
144 and "pos_validated_list".
145
146 If you would prefer to import the now deprecated functions "validate"
147 and "validatep" instead, you can use the ":deprecated" tag to import
148 them.
149
151 When "validate" or "validatep" are called the first time, the parameter
152 spec is prepared and cached to avoid unnecessary regeneration. It uses
153 the fully qualified name of the subroutine (package + subname) as the
154 cache key. In 99.999% of the use cases for this module, that will be
155 the right thing to do.
156
157 However, I have (ab)used this module occasionally to handle dynamic
158 sets of parameters. In this special use case you can do a couple things
159 to better control the caching behavior.
160
161 · Passing in the "MX_PARAMS_VALIDATE_NO_CACHE" flag in the parameter
162 spec this will prevent the parameter spec from being cached.
163
164 sub foo {
165 my ( $self, %params ) = validated_hash(
166 \@_,
167 foo => { isa => 'Foo' },
168 MX_PARAMS_VALIDATE_NO_CACHE => 1,
169 );
170
171 }
172
173 · Passing in "MX_PARAMS_VALIDATE_CACHE_KEY" with a value to be used
174 as the cache key will bypass the normal cache key generation.
175
176 sub foo {
177 my ( $self, %params ) = validated_hash(
178 \@_,
179 foo => { isa => 'Foo' },
180 MX_PARAMS_VALIDATE_CACHE_KEY => 'foo-42',
181 );
182
183 }
184
186 All complex software has bugs lurking in it, and this module is no
187 exception. If you find a bug please either email me, or add the bug to
188 cpan-RT.
189
191 Stevan Little <stevan.little@iinteractive.com>
192
193 Dave Rolsky <autarch@urth.org>
194
196 Copyright 2007-2009 by Infinity Interactive, Inc.
197
198 <http://www.iinteractive.com>
199
200 This library is free software; you can redistribute it and/or modify it
201 under the same terms as Perl itself.
202
203
204
205perl v5.12.0 2010-03-18 MooseX::Params::Validate(3)