ValidationCompiler.pm

filter: Statementstmt 100% 21 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 100% 30 / 30 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ValidationCompiler/Compiler.pm ↑ Summary ValidationCompiler/Exceptions.pm »
1 package Params::ValidationCompiler;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 our $VERSION = '0.28';
7  
8 17 ▶use Params::ValidationCompiler::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 our @EXPORT_OK = qw( compile source_for validation_for );
13  
14 sub validation_for {
15 57 ▶ return Params::ValidationCompiler::Compiler->new(@_)->subref;
Subroutine validation_for: called CC=1 SCAR=0.0
Pod documented
16 }
17  
18 ## no critic (TestingAndDebugging::ProhibitNoWarnings)
19 17 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 17 *compile = \&validation_for;
21 ## use critic
22  
23 sub source_for {
24 1 ▶ return Params::ValidationCompiler::Compiler->new(@_)->source;
Subroutine source_for: called CC=1 SCAR=0.0
Pod documented
25 }
26  
27 17 1;
28  
29 # ABSTRACT: Build an optimized subroutine parameter validator once, use it forever
30  
31 __END__
32  
33 =pod
34  
35 =encoding UTF-8
36  
37 =head1 NAME
38  
39 Params::ValidationCompiler - Build an optimized subroutine parameter validator once, use it forever
40  
41 =head1 VERSION
42  
43 version 0.28
44  
45 =head1 SYNOPSIS
46  
47 use Types::Standard qw( Int Str );
48 use Params::ValidationCompiler qw( validation_for );
49  
50 {
51 my $validator = validation_for(
52 params => {
53 foo => { type => Int },
54 bar => {
55 type => Str,
56 optional => 1,
57 },
58 baz => {
59 type => Int,
60 default => 42,
61 },
62 },
63 );
64  
65 sub foo {
66 my %args = $validator->(@_);
67 }
68 }
69  
70 {
71 my $validator = validation_for(
72 params => [
73 { type => Int },
74 {
75 type => Str,
76 optional => 1,
77 },
78 ],
79 );
80  
81 sub bar {
82 my ( $int, $str ) = $validator->(@_);
83 }
84 }
85  
86 {
87 my $validator = validation_for(
88 params => [
89 foo => { type => Int },
90 bar => {
91 type => Str,
92 optional => 1,
93 },
94 ],
95 named_to_list => 1,
96 );
97  
98 sub baz {
99 my ( $foo, $bar ) = $validator->(@_);
100 }
101 }
102  
103 =head1 DESCRIPTION
104  
105 This module creates a customized, highly efficient parameter checking
106 subroutine. It can handle named or positional parameters, and can return the
107 parameters as key/value pairs or a list of values.
108  
109 In addition to type checks, it also supports parameter defaults, optional
110 parameters, and extra "slurpy" parameters.
111  
112 =for Pod::Coverage compile
113  
114 =head1 EXPORTS
115  
116 This module has two options exports, C<validation_for> and C<source_for>. Both
117 of these subs accept the same options:
118  
119 =over 4
120  
121 =item * params
122  
123 An arrayref or hashref containing a parameter specification.
124  
125 If you pass a hashref then the generated validator sub will expect named
126 parameters. The C<params> value should be a hashref where the parameter names
127 are keys and the specs are the values.
128  
129 If you pass an arrayref and C<named_to_list> is false, the validator will
130 expect positional params. Each element of the C<params> arrayref should be a
131 parameter spec.
132  
133 If you pass an arrayref and C<named_to_list> is true, the validator will
134 expect named params, but will return a list of values. In this case the
135 arrayref should contain a I<list> of key/value pairs, where parameter names
136 are the keys and the specs are the values.
137  
138 Each spec can contain either a boolean or hashref. If the spec is a boolean,
139 this indicates required (true) or optional (false).
140  
141 The spec hashref accepts the following keys:
142  
143 =over 8
144  
145 =item * type
146  
147 A type object. This can be a L<Moose> type (from L<Moose> or
148 L<MooseX::Types>), a L<Type::Tiny> type, or a L<Specio> type.
149  
150 If the type has coercions, those will always be used.
151  
152 =item * default
153  
154 This can either be a simple (non-reference) scalar or a subroutine
155 reference. The sub ref will be called without any arguments (for now).
156  
157 =item * optional
158  
159 A boolean indicating whether or not the parameter is optional. By default,
160 parameters are required unless you provide a default.
161  
162 =back
163  
164 =item * slurpy
165  
166 If this is a simple true value, then the generated subroutine accepts
167 additional arguments not specified in C<params>. By default, extra arguments
168 cause an exception.
169  
170 You can also pass a type constraint here, in which case all extra arguments
171 must be values of the specified type.
172  
173 =item * named_to_list
174  
175 If this is true, the generated subroutine will expect a list of key-value
176 pairs or a hashref and it will return a list containing only values. The
177 C<params> you pass must be a arrayref of key-value pairs. The order of these
178 pairs determines the order in which values are returned.
179  
180 You cannot combine C<slurpy> with C<named_to_list> as there is no way to know
181 how to order the extra return values.
182  
183 =item * return_object
184  
185 If this is true, the generated subroutine will return an object instead of a
186 hashref. You cannot set this option to true if you set either or C<slurpy> or
187 C<named_to_list>.
188  
189 The object's methods correspond to the parameter names passed to the
190 subroutine. While calling methods on an object is slower than accessing a
191 hashref, the advantage is that if you typo a parameter name you'll get a
192 helpful error.
193  
194 If you have L<Class::XSAccessor> installed then this will be used to create
195 the class's methods, which makes it fairly fast.
196  
197 The returned object is in a generated class. Do not rely on this class name
198 being anything in specific, and don't check this object using C<isa>, C<DOES>,
199 or anything similar.
200  
201 When C<return_object> is true, the parameter spec hashref also accepts to the
202 following additional keys:
203  
204 =over 8
205  
206 =item * getter
207  
208 Use this to set an explicit getter method name for the parameter. By default
209 the method name will be the same as the parameter name. Note that if the
210 parameter name is not a valid sub name, then you will get an error compiling
211 the validation sub unless you specify a getter for the parameter.
212  
213 =item * predicate
214  
215 Use this to ask for a predicate method to be created for this parameter. The
216 predicate method returns true if the parameter was passed and false if it
217 wasn't. Note that this is only useful for optional parameters, but you can ask
218 for a predicate for any parameter.
219  
220 =back
221  
222 =back
223  
224 =head2 validation_for(...)
225  
226 This returns a subroutine that implements the specific parameter
227 checking. This subroutine expects to be given the parameters to validate in
228 C<@_>. If all the parameters are valid, it will return the validated
229 parameters (with defaults as appropriate), either as a list of key-value pairs
230 or as a list of just values. If any of the parameters are invalid it will
231 throw an exception.
232  
233 For validators expected named params, the generated subroutine accepts either
234 a list of key-value pairs or a single hashref. Otherwise the validator expects
235 a list of values.
236  
237 For now, you must shift off the invocant yourself.
238  
239 This subroutine accepts the following additional parameters:
240  
241 =over 4
242  
243 =item * name
244  
245 If this is given, then the generated subroutine will be named using
246 L<Sub::Util>. This is strongly recommended as it makes it possible to
247 distinguish different check subroutines when profiling or in stack traces.
248  
249 This name will also be used in some exception messages, even if L<Sub::Util>
250 is not available.
251  
252 Note that you must install L<Sub::Util> yourself separately, as it is not
253 required by this distribution, in order to avoid requiring a compiler.
254  
255 =item * name_is_optional
256  
257 If this is true, then the name is ignored when C<Sub::Util> is not
258 installed. If this is false, then passing a name when L<Sub::Util> cannot be
259 loaded causes an exception.
260  
261 This is useful for CPAN modules where you want to set a name if you can, but
262 you do not want to add a prerequisite on L<Sub::Util>.
263  
264 =item * debug
265  
266 Sets the C<EVAL_CLOSURE_PRINT_SOURCE> environment variable to true before
267 calling C<Eval::Closure::eval_closure()>. This causes the source of the
268 subroutine to be printed before it's C<eval>'d.
269  
270 =back
271  
272 =head2 source_for(...)
273  
274 This returns a two element list. The first is a string containing the source
275 code for the generated sub. The second is a hashref of "environment" variables
276 to be used when generating the subroutine. These are the arguments that are
277 passed to L<Eval::Closure>.
278  
279 =head1 SUPPORT
280  
281 Bugs may be submitted at L<https://github.com/houseabsolute/Params-ValidationCompiler/issues>.
282  
283 I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
284  
285 =head1 SOURCE
286  
287 The source code repository for Params-ValidationCompiler can be found at L<https://github.com/houseabsolute/Params-ValidationCompiler>.
288  
289 =head1 DONATIONS
290  
291 If you'd like to thank me for the work I've done on this module, please
292 consider making a "donation" to me via PayPal. I spend a lot of free time
293 creating free software, and would appreciate any support you'd care to offer.
294  
295 Please note that B<I am not suggesting that you must do this> in order for me
296 to continue working on this particular software. I will continue to do so,
297 inasmuch as I have in the past, for as long as it interests me.
298  
299 Similarly, a donation made in this way will probably not make me work on this
300 software much more, unless I get so many donations that I can consider working
301 on free software full time (let's all have a chuckle at that together).
302  
303 To donate, log into PayPal and send money to autarch@urth.org, or use the
304 button at L<http://www.urth.org/~autarch/fs-donation.html>.
305  
306 =head1 AUTHOR
307  
308 Dave Rolsky <autarch@urth.org>
309  
310 =head1 CONTRIBUTORS
311  
312 =for stopwords Gregory Oschwald Tomasz Konojacki
313  
314 =over 4
315  
316 =item *
317  
318 Gregory Oschwald <goschwald@maxmind.com>
319  
320 =item *
321  
322 Gregory Oschwald <oschwald@gmail.com>
323  
324 =item *
325  
326 Tomasz Konojacki <me@xenu.pl>
327  
328 =back
329  
330 =head1 COPYRIGHT AND LICENSE
331  
332 This software is Copyright (c) 2016 - 2018 by Dave Rolsky.
333  
334 This is free software, licensed under:
335  
336 The Artistic License 2.0 (GPL Compatible)
337  
338 The full text of the license can be found in the
339 F<LICENSE> file included with this distribution.
340  
341 =cut
« ValidationCompiler/Compiler.pm ↑ Summary ValidationCompiler/Exceptions.pm »