I gently disagree :) At least with your first points. AutoValidate isn't controller specific. All it does is looking at the request and leaving it's opinion in the request object by setting "validated" and "validation_error". Granted, validattion is a task which is traditionally done (if you can talk about traditions anyway here), but this module is an attemt to abstract that away from the controllers as much as possible. Also, being it a plugin i can enable it in a single place and have it active everywhere.

I wasn't sure about C3, the Catalyst::Plugin::C3 says it's still experimental. But as it seems it's a simple replacement, when NEXT is ever abandoned.

Defaulting to index is a sensible thing to do, I think. I need a file to put the spec in (here index.vcs). Without index that would be ".vcs" which is much worse.

I agree with the rest. I'll make the paths compatible, turn the keys into accessors and replace $_.

Thanks for your input mum :)

Update: new version as per YourMothers suggestion:
package Catalyst::Plugin::AutoValidate; use warnings; use strict; use YAML qw( LoadFile ); use Catalyst::Request; use Config::Validate; use Data::Dumper; use Path::Class; use NEXT; our $VERSION = 0.02; Catalyst::Request->mk_accessors( qw/validated validation_error/ ); sub prepare { my $class = shift; my $c = $class->NEXT::prepare( @_ ); my ( $config, $rpath, $spec_root, $spec_file, $validation_spec, $v +alidator ); # config for this package $config = $c->config->{'Catalyst::Plugin::AutoValidate'}; # the request path $rpath = $c->request->path || $config->{index_spec} || 'index' +; # root dir to look for specifications $spec_root = $config->{spec_root} || dir($c->{home}, 'cvspec'); # spec file, deduced from path $spec_file = file($spec_root, "${rpath}.".($config->{spec_extensio +n}||"cvs")); # check if there are custom types configured if ( $config->{types} ) { # add to the validation module if ( ref( $config->{types} ) eq "ARRAY" ) { for my $type ( @{$config->{types}} ) { Config::Validate::add_default_type(%$type); } } else { Config::Validate->add_default_type(%{$config->{types}}); } # move the types, so they won't get reprocessed another time $config->{xtypes} = delete $config->{types}; } # there is no matching spec file unless ( -e $spec_file ) { # that's ok or not, depending if we are paranoid unless ( $config->{paranoid} ) { $c->request->validated(1); } else { $c->request->validated(0); $c->request->validation_error('Paranoia! The computer will + kill you.'); } } else { # load spec and validate the request against it $validation_spec = LoadFile( $spec_file ); $validator = Config::Validate->new( schema => $validatio +n_spec ); # will die if validation fails eval { $validator->validate( config => $c->request->{parameter +s} ) }; unless ( $@ ) { $c->request->validated(1); } else { $_ = "$@"; s/^.+?validate\(\): //; s/instead.+//; $c->request->validation_error($_); $c->request->validated(0); } } return $c; } 1; __DATA__ =head1 NAME Catalyst::Plugin::AutoValidate - Catalyst-Plugin for easy automatic va +lidation of Request-Parameters =head1 VERSION Version 0.01 =head1 SYNOPSIS This Plugin validates request parameters against a specification in a +file that directly relates to the requests path (like the .fb files when using Formbuilde +r). For the "heavy lifting" it uses L<http://search.cpan.org/~cmo/Config-V +alidate-0.2.6/lib/Config/Validate.pm>; All validation options from that module are supported, as they get sim +ply passed through. # application code package MyApp; use strict; use warnings; use Catalyst qw(AutoValidate); # MyApp::Controller::Root.pm # check parameters and display error message when # appropriate. Does NOT RUN THE CONTROLLER in that case, # otherwise proceed normally sub begin : Private { my ($self, $c) = @_; $c->response->body( $c->request->validation_error ), $c->detach unless $c->request->validated; } # $c->{home}/cvspec/math/multiply.cvs (YAML) a: type: integer b: type: integer # MyApp::Controller::Math.pm sub multiply : Local { my ($self, $c) = @_; # this is safe because Autovalidate ensures both are integers $s->stash->{result} = $c->req->param('a') / $c->req->param('a') } =head1 CONFIGURATION __PACKAGE__->config( 'Catalyst::Plugin::AutoValidate' => { # root dir for looking up specifications spec_root => '', # when set, all requests without specification die paranoid => 0, # the name of the index spec (for empty paths) index_spec => 'index', # extension for spec-files spec_extension => 'cvs', # custom types types => [ # generator sub that generates a closure, could come in handy + for lookups { name => 'custom', validate => sub { my $lookup = { exists => 1 }; return sub { die "Not in lookup table!" unless $looku +p->{$_[1]} }; }->() }, # or easier sub { die "Doesnt match!" unless $_[1] =~ /[abc]/ }; ] } ); =cut =head1 AUTHOR Markus Holzer, C<< <holli.holzer at googlemmail.com> >> =head1 BUGS Please report any bugs or feature requests to C<bug-catalyst-plugin-Au +toValidate at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue= +Catalyst-Plugin-AutoValidate>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc Catalyst::Plugin::AutoValidate You can also look for information at: =over 4 =item * RT: CPAN's request tracker L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Plugin-AutoValidat +e> =item * AnnoCPAN: Annotated CPAN documentation L<http://annocpan.org/dist/Catalyst-Plugin-AutoValidate> =item * CPAN Ratings L<http://cpanratings.perl.org/d/Catalyst-Plugin-AutoValidate> =item * Search CPAN L<http://search.cpan.org/dist/Catalyst-Plugin-AutoValidate> =back =head1 ACKNOWLEDGEMENTS =head1 COPYRIGHT & LICENSE Copyright 2008 Markus Holzer, all rights reserved. This program is free software; you can redistribute it and/or modify i +t under the same terms as Perl itself. =cut


holli, /regexed monk/

In reply to Re^2: [RFC: Catalyst::Plugin::AutoValidate] Easy request parameter validation with Catalyst by holli
in thread [RFC: Catalyst::Plugin::AutoValidate] Easy request parameter validation with Catalyst by holli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.