CatalystX::Controller::Verifier - Moose Role for verifying request parameters on a per action basis.
version 0.02
package MyApp::Controller::Foo; use Moose; BEGIN { extends 'Catalyst::Controller'; } with 'CatalystX::Controller::Verifier'; __PACKAGE__->config( 'verifiers' => { # The action name 'search' => { # Everything here gets passed to Data::Verifier->new for the scope filters => [ 'trim' ], # Just a plain Data::Verifier profile here: profile => { page => { type => 'Int', post_check => sub { shift->get_value('page') > 0 } }, query => { type => 'Str', required => 1, } } }, }, # Additional options can be passed in: # If verification fails, detach to the 'bad_args' action 'detach_on_failure' => 'bad_args', # If you want to override where the Data::Manager objects get tucked away: '_verifier_stash_key' => 'a secret garden', ); sub search : Local { my ( $self, $c ) = @_; my $results = $self->verify( $c ); $c->model('Search')->search( # If invalid, it will be undef here. $results->get_value('page') || 1, $results->get_value('query') ); }
If you run verify in an action that does not have a profile, this will throw an exception informing you of your transgressions.
verify
But wait, there's more! Data::Verifier allows you to also define coercions.
This stores the verifier configuration, which should be a hash ref of action names to verification profiles.
This attribute is used to instruct the verify method to detach to the action specified. If this is unset, no detaching happens.
$self->verify($c);
The heart of the action, the verify method takes the current context object and verifies based on the profiles supplied in the configuration. If the detach_on_failure attribute is set, it will detach on an unsuccessful verification.
Returns a Data::Verifier::Results object. Note that this can be serialized and tucked away in the flash for later use.
$self->messages($c);
Returns a Message::Stack for the action in question (after verification).
$self->data_manager($c);
Returns a Data::Manager object that is used for this request (specific to the controller and the request).
So, in the above example lets say you wanted to parse your search query using Search::Query. Piece of cake!
use Search::Query; __PACKAGE__->config( 'verifiers' => { # The action name 'search' => { # ... include the rest from synopsis ... query => { type => 'Search::Query', required => 1, coercion => Data::Verifier::coercion( from => 'Str', via => sub { Search::Query->parser->parse($_) } ) } }, } ); sub search : Local { my ( $self, $c ) = @_; my $results = $self->verify( $c ); $results->get_value('query'); # isa Search::Query object now! $results->get_original_value('query'); # Still valid }
Got a validation error? Well, Data::Manager covers that, too.
The messages method will return a Message::Stack specific to that action.
sub search : Local { my ( $self, $c ) = @_; my $results = $self->verify($c); unless ( $results->success ) { # Returns a Message::Stack for the action in question $self->messages($c); # You can also get the Data::Manager object $self->data_manager($c); } }
Each controller gets its own Data::Manager per request. This is probably not blindly fast. It lives in the stash
J. Shirley <jshirley@cpan.org>
This software is copyright (c) 2011 by Cold Hard Code, LLC.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install CatalystX::Controller::Verifier, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CatalystX::Controller::Verifier
CPAN shell
perl -MCPAN -e shell install CatalystX::Controller::Verifier
For more information on module installation, please visit the detailed CPAN module installation guide.