[Catalyst] Data validation : a proposal (request for comments)

Dami Laurent (PJ) laurent.dami at justice.ge.ch
Tue Oct 17 18:16:14 CEST 2006


Hi Catalysters,

In our organization we are building a Catalyst app where form items have
nested names (i.e. <input name="foo.2.bar.5">) and the whole form data
is then expanded into a Perl tree through CGI::Expand. Next I need to
validate the shape of that tree (type of nodes, absence/presence/size of
subtrees, etc.). As far as I know, existing validation modules like
Data::FormValidator or Html::Widget::Constraint do not go that far. So
attached is a new proposal, still experimental, called Data::Domain (not
yet on CPAN). I'm interested to hear about any
comments/suggestions/collaboration proposals, or just to know if other
people have similar needs. 

Best regards, 
 
Laurent Dami

========================================
Excerpt from the doc
=========================================


=head1 NAME

Data::Domain - Data description and validation

=head1 SYNOPSIS

  use Data::Domain qw/:all/;

  my $domain = Struct(
    anInt      => Int(-min => 3, -max => 18),
    aNum       => Num(-min => 3.33, -max => 18.5),
    aDate      => Date(-max => 'today'),
    aLaterDate => sub {my $context = shift;
                       Date(-min => $context->{flat}{aDate})},
    aString    => String(-min_length => 2, -optional => 1),
    anEnum     => Enum(qw/foo bar buz/),
    anIntList  => List(-min_size => 1, -all => Int),
    aMixedList => List(Integer, String, Int(-min => 0), Date),
    aStruct    => Struct(foo => String, bar => Int(-optional => 1))
  );

  my $messages = $domain->inspect($some_data);
  my_display_error($messages) if $messages;

=head1 DESCRIPTION

A data domain is a description of a set of values, either
scalar, or, more interestingly, structured values (arrays or hashes).
The description can include many constraints, like minimal or maximal
values, regular expressions, required fields, forbidden fields, and
also contextual dependencies. From that description, one can then
check if a given value belongs to the domain. In case of mismatch,
a structured set of error messages is returned.

The motivation for writing this package was to be able to express in a
compact way some possibly complex constraints about structured
data. The data is a Perl tree (nested hashrefs or arrayrefs) that may
come from XML, L<JSON|JSON>, or from postprocessing an HTML form
through L<CGI::Expand|CGI::Expand>. C<Data::Domain> is a kind of tree
parser on that structure, with some facilities for dealing
with dependencies within the structure through lazy 
evaluation of domains. 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Data-Domain-0.01.tar.gz
Type: application/x-gzip
Size: 15228 bytes
Desc: Data-Domain-0.01.tar.gz
Url : http://lists.rawmode.org/pipermail/catalyst/attachments/20061017/a2c203f8/attachment-0001.bin 


More information about the Catalyst mailing list