Validation logic is more complex than you expect

I am creating Validator::Custom from 2009. This is a validation module.

I add many features to customize validation but I can't solve many problems because Validation logic is more complex than I expect.

Mojolicious remove filter feature of validation module. It is maybe impossible to do three things(require, checking, filtering) at once.

And validation logic is complex because we want to see multiple values at once. for example, "high_price" param + "low_price" param.

Next, In html validation, we must think about fore cases.

1. name=foo
2. name=foo&name=bar
3. name=
4. (not exists)

And We want do filtering for blank containing value, for example, " Kimoto ".

And We maybe want to prepare default value when validation fail.

Above combination is very very complex.

Conclusion is that simple is best

At first programmer must to choice a value or values. By this, we don't need to thing "require" checking.

  # A value
  my $age = $c->param('age');

  # Values
  my $features = $c->every_param('features');

After that, you do filtering and checking.

  $age = $vc->filter($age, 'trim'); # "  12 " to "12"
  my $age_is_valid = $vc->check($age, 'int');

  $features = $vc->filter($features, 'remove_blank'); # ['', '001', '002'] to ['001', '002'] 
  my $features_is_valid = $vc->check_each($features, 'in', ['001', '002', '003']]);

2 Comments

To be fair, Mojo never had filtering; it was being considered and is on hold again

Leave a comment

About Yuki Kimoto

user-pic I'm Yuki Kimoto, Japanese Perl programmer.I create Perl modules and applications. GitPrep, SPVM, DBIx::Custom, Validator::Custom, Test::Mojo, Mojolicious::Plugin::AutoRoute, etc.