File Coverage

blib/lib/Catalyst/Plugin/StructuredParameters.pm
Criterion Covered Total %
statement 9 12 75.0
branch 0 4 0.0
condition n/a
subroutine 5 6 83.3
pod 4 4 100.0
total 18 26 69.2


line stmt bran cond sub pod time code
1              
2             use Moose::Role;
3 1     1   3179 use Scalar::Util;
  1         2  
  1         7  
4 1     1   5081  
  1         2  
  1         228  
5              
6 4     4 1 3424 my ($self, $obj) = @_;
7 1     1 1 788 return 0 unless Scalar::Util::blessed($obj);
8 2     2 1 64496 return $obj->isa('Catalyst::Exception::StructuredParameter') ? 1:0;
9             }
10              
11 0     0 1   around request_class_traits => sub {
12 0 0         my ($orig, $self, @args) = @_;
13 0 0         my $traits = $self->$orig(@args);
14             return [ @{$traits||[]}, 'Catalyst::TraitFor::Request::StructuredParameters' ];
15             };
16              
17             1;
18              
19             =head1 NAME
20              
21             Catalyst::Plugin::StructuredParameters - Plug to add the structured parameter request trait plus proxy methods
22              
23             =head1 SYNOPSIS
24              
25             package MyApp;
26             use Catalyst 'StructuredParameters';
27            
28             MyApp->setup;
29              
30             package MyApp::Controller::Root;
31              
32             sub body :Local {
33             my ($self, $c) = @_;
34             my %clean = $c->structured_body
35             ->permitted(['person'], +{'email' => []})
36             ->namespace(['person'])
37             ->permitted(
38             'name',
39             'age',
40             'address' => ['street' => ['number', 'zip'],
41             +{'credit_cards' => [
42             'number',
43             'exp' => [qw/year month day/],
44             ]},
45             )->to_hash;
46              
47             ## Do something with the sanitized body parameters
48             }
49              
50             ## Don't forget to add code to handle any exceptions
51              
52             sub end :Action {
53             my ($self, $c) = @_;
54             if(my $error = $c->last_error) {
55             $c->clear_errors; ## Clear the error stack unless you want the default Catalyst error
56             if($c->isa_strong_parameter_exception($error)) {
57             ## Something here like return a Bad Request 4xx view or similar.
58             }
59             }
60             }
61              
62             ## Alternatively handle with L<CatalystX::Errors> (don't forget to add the plugin to your
63             ## application class.)
64            
65             sub end :Action Does(RenderErrors) { }
66              
67             You should review L<Catalyst::TraitFor::Request::StructuredParameters> for a more detailed SYNOPSIS and
68             explanation of how all this works.
69              
70             =head1 DESCRIPTION
71              
72             This plugin will add in the L<Catalyst::TraitFor::Request::StructuredParameters> request class trait
73             and proxy some of its methods to the context. You might find this a bit less typing.
74              
75             All the main documentation is in L<Catalyst::TraitFor::Request::StructuredParameters>.
76              
77             NOTE: This plugin only works with For L<Catalyst> v5.90090 or greater. If you must use an older
78             version of L<Catalyst> you'll need to use the workaround described in the SYNOPSIS of
79             L<Catalyst::TraitFor::Request::StructuredParameters>.
80              
81             =head1 METHODS
82              
83             This role defines the following methods:
84              
85             =head2 structured_body
86              
87             =head2 structured_data
88              
89             =head2 structured_query
90              
91             These just proxy to the same methods under the L<Catalyst::Request> object.
92              
93             =head2 isa_structured_parameter_exception
94              
95             This is just a convenience method that returns true if a possible exception is both a blessed
96             object and ISA L<Catalyst::Exception::StructuredParameter>. Since you need to add checking for
97             this everytime I added this method to save a bit of trouble.
98              
99             =head1 AUTHOR
100              
101             See L<Catalyst::TraitFor::Request::StructuredParameters>
102              
103             =head1 SEE ALSO
104              
105             L<Catalyst>, L<Catalyst::TraitFor::Request::StructuredParameters>
106              
107             =head1 COPYRIGHT & LICENSE
108              
109             See L<Catalyst::TraitFor::Request::StructuredParameters>
110              
111             =cut