Hello monks, this is requests for comments, so if you are interested in this topic, please leave any.
Dancer2 is a lightweight framework that provides us with rich routing and serialization features out of the box. The rest is left to the developer, including working with the database, models and data in general.
When working with data, especially received from a user, you want to be sure of their consistency. In other words, I don't want to get a number where the username should be and the word in the age field (like age => twenty).
Perl is a language with very powerful regular expressions and many hackers use them. However, this approach has the following disadvantages:
Instead of writing if ($age !~ /^\d+$/) {…} else {…} and so for each field received from the user, I want to use a construction like (age => 'integer').
And here you should pay attention to special modules that simplify the validation process.
I've found some useful modules on cpan:
They all have their pros and cons, but none of them met all my criteria.
This means that the code used fields as first class entities:
{ email => qw(required, email), age => qw(integer, min:18), }
And not validators:
{ required => qw(email), email => qw(email), integer => qw(age), min => { filed => ‘age’, num => 18 }, }
Very often you can see:
{ required => … , validators => { … } }
Requred is the same validator and it is not clear to me why to extract it into a separate entity.
Not all modules have built-in validators and you need to implement them yourself.
This feature is provided by most modules.
Because we have a Dancer2 project and maybe we don't use DBIc models at all.
Didn't find it anywhere.
I do not want to constantly get and transfer data to the template like errors or old input.
Based on the above criteria, I decided to write my plugin specifically for Dancer2. Since this is my first big OS project, I ask respected monks to share their opinion on what I got as a result.
use Dancer2; use Dancer2::Plugin::FormValidator; package RegisterForm { use Moo; with 'Dancer2::Plugin::FormValidator::Role::Profile'; sub profile { return { username => [ qw(required alpha_num_ascii length_min:4 + length_max:32) ], email => [ qw(required email length_max:127) ], password => [ qw(required length_max:40) ], password_cnf => [ qw(required same:password) ], confirm => [ qw(required accepted) ], }; } } post '/form' => sub { if (validate profile => RegisterForm->new) { my $valid_hash_ref = validated; save_user_input($valid_hash_ref); redirect '/success_page'; } redirect '/form'; };
Plugin can be found on Metacpan => Dancer2::Plugin::FormValidator.
Github repo => dancer2-plugin-formvalidator.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: Creating Dancer2 validation plugin.
by stevieb (Canon) on May 04, 2022 at 15:32 UTC | |
by choroba (Cardinal) on May 05, 2022 at 08:51 UTC | |
by stevieb (Canon) on May 05, 2022 at 14:51 UTC | |
by AlexP (Pilgrim) on May 05, 2022 at 09:31 UTC | |
|
Re: RFC: Creating Dancer2 validation plugin.
by AlexP (Pilgrim) on May 17, 2022 at 11:19 UTC | |
by davebaker (Pilgrim) on May 18, 2022 at 14:15 UTC | |
|
Re: RFC: Creating Dancer2 validation plugin.
by perlfan (Parson) on May 04, 2022 at 18:10 UTC |