.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Validation::Class::Whitepaper 3pm" .TH Validation::Class::Whitepaper 3pm "2023-06-11" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Validation::Class::Whitepaper \- Operate with Impunity .SH "VERSION" .IX Header "VERSION" version 7.900059 .SH "INTRODUCTION" .IX Header "INTRODUCTION" This whitepaper will serves as a guide to help readers understand the common data validation issues as well as the the rationale and various usage scenarios for Validation::Class. .PP Data validation is an important aspect of every application yet it is often overlooked or neglected. Data validation should be thought of as your data input firewall, a layer that exist between the user of your application and the application's business objects. .SH "DATA VALIDATION PROBLEMS" .IX Header "DATA VALIDATION PROBLEMS" The most common application security weakness is the failure to properly validate input from the client or environment. Data validation is important because it provides security, it allows you to ensure user supplied data is formatted properly, is within length boundaries, contains permitted characters and adheres to business rules. .PP To understand the problem domain we need to first ask ourselves: .PP .Vb 3 \& * what is data validation? and ... is that what I\*(Aqve been doing? \& * what are the common data validation requirements? \& * what are the common use\-cases where validation becomes tricky? .Ve .PP Data validation is the process of auditing a piece of data to ensure it fits a specific criteria. Standard data validation requirements are: .PP .Vb 7 \& * existence checking \& * range checking \& * type checking \& * list\-lookup checking \& * dependency checking \& * pattern checking \& * custom validation checking (business logic) .Ve .PP Typically when designing an application we tend to name input parameters in an arbitrarily fashion and validate the same data at various stages during a program's execution (duplicating logic and validation routines) in various places in the application stack. This approach is inefficient and prone to bugs, inconsistencies and security problems. .PP Data can be submitted to an application in various formats and it is not always ideal, and the option to pre-format the data is not always ideal or even possible. A few common use-cases were validation is required and often fails (in a big big way) are as follows: .PP .Vb 7 \& * handling arbitrarily and/or dynamically\-named parameters \& * handling input for batch\-processing \& * handling multi\-type parameters (array or scalar depending on context) \& * handling complex conditional validation logic \& * handling multi\-variant parameter names (aliases) \& * handling parameter dependencies \& * handling errors (reporting messages, localization, etc) .Ve .SH "A DATA VALIDATION SOLUTION" .IX Header "A DATA VALIDATION SOLUTION" A better approach to data validation is to first consider each parameter hitting your application as a transmission fitting a very specific criteria and construct a data validation layer that operates with that in mind (e.g. exactly like a network firewall). Your data validation rules should act as filters which will accept or reject and format the transmission for use within your application. .PP A proper validation framework should allow you to model data and construct validation objects with a focus on structuring rules, reusing common declarations, defining input filters and validating data. Its main purpose should be to properly handle data input errors. It's ulterior motive should be to ensure consistency and promote reuse of data validation rules. .SH "WHY VALIDATION::CLASS" .IX Header "WHY VALIDATION::CLASS" Validation::Class was built around the concept of compartmentalization and re-use. That premise gave birth to the idea of persistent data validation rules which exist in a class configuration which is associated with a class which acts as a validation domain for related validation rules. .PP Validation classes derived from Validation::Class are typically configured using the Validation::Class sugar functions (or keywords). Validation classes are typically defined using the following keywords: .PP .Vb 5 \& * field \- a data validation rule that matches an input parameter \& * mixin \- a configuration template which can be merged with a field \& * directive \- a field/mixin rule corresponding to a directive class name \& * filter \- a custom filtering routine which transforms a field value \& * method \- a self\-validating sub\-routine w/ associated validation profile .Ve .PP A data validation framework exists to handle failures, it is its main function and purpose, in-fact, the difference between a validation framework and a type-constraint system is how it responds to errors. .PP There are generally two types of errors that occur in an application, user-errors which are expected and should be handled and reported so that a user can correct the problem, and system-errors which are unexpected and should cause the application to terminate and/or handling the exception. Exception handling is the process of responding to the occurrence, during computation, of exceptions (anomalous or exceptional situations). .PP User errors and system errors are poplar opposites. It is not always desired and/or appropriate to crash from a failure to validate user input. The following examples should clearly display how Validation::Class addresses key pain-points and handles common use-cases were validation is usually quite arduous. .SS "Dynamic Parameters" .IX Subsection "Dynamic Parameters" .Vb 1 \& # handling arbitrary and/or dynamically\-named parameters \& \& package DynamicParameters; \& \& use Validation::Class; \& \& field email => { \& required => 1, \& pattern => qr/\e@localhost$/ \& }; \& \& field login => { \& required => 1, \& min_length => 5, \& alias => [\*(Aquser\*(Aq] \& }; \& \& field password => { \& required => 1, \& min_length => 5, \& min_digits => 1, \& alias => [\*(Aqpass\*(Aq] \& }; \& \& package main; \& \& my $params = { \& user => \*(Aqadmin\*(Aq, # arbitrary \& pass => \*(Aqs3cret\*(Aq, # arbitrary \& email_1 => \*(Aqadmin@localhost\*(Aq, # dynamic created \& email_2 => \*(Aqroot@localhost\*(Aq, # dynamic created \& email_3 => \*(Aqsa@localhost\*(Aq, # dynamic created \& }; \& \& my $dp = DynamicParameters\->new(params => $params); \& \& $dp\->proto\->clone_field(\*(Aqemail\*(Aq, $_) \& for $dp\->params\->grep(qr/^email/)\->keys \& ; \& \& print $dp\->validate ? "OK" : "NOT OK"; \& \& 1; .Ve .SS "Batch-Processing" .IX Subsection "Batch-Processing" .Vb 1 \& # handling input for batch\-processing \& \& package BatchProcessing; \& \& use Validation::Class; \& \& mixin scrub => { \& required => 1, \& filters => [\*(Aqtrim\*(Aq, \*(Aqstrip\*(Aq] \& }; \& \& field header => { \& mixin => \*(Aqscrub\*(Aq, \& options => [\*(Aqname\*(Aq, \*(Aqemail\*(Aq, \*(Aqcontact\*(Aq, \*(Aqdob\*(Aq, \*(Aqcountry\*(Aq], \& multiples => 1 # handle param as a scalar or arrayref \& }; \& \& field name => { \& mixin => \*(Aqscrub\*(Aq, \& filters => [\*(Aqtitlecase\*(Aq], \& min_length => 2 \& }; \& \& field email => { \& mixin => \*(Aqscrub\*(Aq, \& min_length => 3 \& }; \& \& field contact => { \& mixin => \*(Aqscrub\*(Aq, \& length => 10 \& }; \& \& field dob => { \& mixin => \*(Aqscrub\*(Aq, \& length => 8, \& pattern => \*(Aq##/##/##\*(Aq \& }; \& \& field country => { \& mixin => \*(Aqscrub\*(Aq \& }; \& \& package main; \& \& my $params = { \& pasted_data => q{ \& name email contact dob country \& john john@zuzu.com 9849688899 12/05/98 UK \& jim kathy kjim@zuz.com 8788888888 05/07/99 India \& Federar fed@zuzu.com 4484848989 11/21/80 USA \& Micheal micheal@zuzu.com 6665551212 06/10/87 USA \& Kwang Kit kwang@zuzu.com 7775551212 07/09/91 India \& Martin jmartin@zuzu.com 2159995959 02/06/85 India \& Roheeth roheeth@zuzu.com 9596012020 01/10/89 USA \& } \& }; \& \& # ... there are many ways this could be parsed and validated \& # ... but this is simple \& \& my $bpi = my @pasted_lines = map { s/^\es+//; $_ } split /\en/, $params\->{pasted_data}; \& my @headers = split /\et/, shift @pasted_lines; \& \& my $bp = BatchProcessing\->new(params => { header => [@headers] }); \& \& # validate headers first \& \& if ($bp\->validate) { \& \& $bp\->params\->clear; \& \& $bpi\-\-; \& \& # validate each line, halt on first bad line \& \& while (my $line = shift @pasted_lines) { \& \& my @data = split /\et/, $line; \& \& for (my $i=0; $i<@data; $i++) { \& \& $bp\->params\->add($headers[$i], $data[$i]); \& \& } \& \& last unless $bp\->validate; \& \& $bp\->params\->clear; \& \& $bpi\-\-; \& \& } \& \& } \& \& print ! $bpi ? "OK" : "NOT OK"; \& \& 1; .Ve .SS "Multi-Type Parameters" .IX Subsection "Multi-Type Parameters" .Vb 1 \& # handling multi\-type parameters (array or scalar depending on context) \& \& package MultiType; \& \& use Validation::Class; \& \& field letter_type => { \& \& required => 1, \& options => [ \*(AqA\*(Aq .. \*(AqZ\*(Aq ], \& multiples => 1 # turn on multi\-type processing \& \& }; \& \& package main; \& \& my $mt = MultiType\->new; \& my $ok = 0; \& \& $mt\->params\->add(letter_type => \*(AqA\*(Aq); \& \& $ok++ if $mt\->validate; \& \& $mt\->params\->clear\->add(letter_type => [\*(AqA\*(Aq, \*(AqB\*(Aq, \*(AqC\*(Aq]); \& \& $ok++ if $mt\->validate; \& \& print $ok == 2 ? "OK" : "NOT OK"; \& \& 1; .Ve .SS "Complex Conditions" .IX Subsection "Complex Conditions" .Vb 1 \& # handling complex conditional validation logic \& \& package ComplexCondition; \& \& use Validation::Class; \& \& mixin scrub => { \& required => 1, \& filters => [\*(Aqtrim\*(Aq, \*(Aqstrip\*(Aq] \& }; \& \& mixin flag => { \& length => 1, \& options => [0, 1] \& }; \& \& field first_name => { \& mixin => \*(Aqscrub\*(Aq, \& filters => [\*(Aqtitlecase\*(Aq] \& }; \& \& field last_name => { \& mixin => \*(Aqscrub\*(Aq, \& filters => [\*(Aqtitlecase\*(Aq] \& }; \& \& field role => { \& mixin => \*(Aqscrub\*(Aq, \& filters => [\*(Aqtitlecase\*(Aq], \& options => [\*(AqClient\*(Aq, \*(AqEmployee\*(Aq, \*(AqAdministrator\*(Aq], \& default => \*(AqClient\*(Aq \& }; \& \& field address => { \& mixin => \*(Aqscrub\*(Aq, \& required => 0, \& depends_on => [\*(Aqcity\*(Aq, \*(Aqstate\*(Aq, \*(Aqzip\*(Aq] \& }; \& \& field city => { \& mixin => \*(Aqscrub\*(Aq, \& required => 0, \& depends_on => \*(Aqaddress\*(Aq \& }; \& \& field state => { \& mixin => \*(Aqscrub\*(Aq, \& required => 0, \& length => \*(Aq2\*(Aq, \& pattern => \*(AqXX\*(Aq, \& depends_on => \*(Aqaddress\*(Aq \& }; \& \& field zip => { \& mixin => \*(Aqscrub\*(Aq, \& required => 0, \& length => \*(Aq5\*(Aq, \& pattern => \*(Aq#####\*(Aq, \& depends_on => \*(Aqaddress\*(Aq \& }; \& \& field has_mail => { \& mixin => \*(Aqflag\*(Aq \& }; \& \& profile \*(Aqregistration\*(Aq => sub { \& \& my ($self) = @_; \& \& # address info not required unless role is client or has_mail is true \& \& return unless $self\->validate(\*(Aqhas_mail\*(Aq); \& \& $self\->queue(qw/first_name last_name/); \& \& if ($self\->param(\*(Aqhas_mail\*(Aq) || $self\->param(\*(Aqrole\*(Aq) eq \*(AqClient\*(Aq) { \& \& # depends_on directive kinda makes city, state and zip required too \& $self\->queue(qw/+address/); \& \& } \& \& my $ok = $self\->validate; \& \& $self\->clear_queue; \& \& return $ok; \& \& }; \& \& package main; \& \& my $ok = 0; \& my $mt; \& \& $mt = ComplexCondition\->new( \& first_name => \*(AqRachel\*(Aq, \& last_name => \*(AqGreen\*(Aq \& ); \& \& # defaults to client, missing address info \& $ok++ if ! $mt\->validate_profile(\*(Aqregistration\*(Aq); \& \& $mt = ComplexCondition\->new( \& first_name => \*(Aqmonica\*(Aq, \& last_name => \*(Aqgeller\*(Aq, \& role => \*(Aqemployee\*(Aq \& ); \& \& # filters (pre\-process) role and titlecase, as employee no address needed \& $ok++ if $mt\->validate_profile(\*(Aqregistration\*(Aq); \& \& $mt = ComplexCondition\->new( \& first_name => \*(Aqphoebe\*(Aq, \& last_name => \*(Aqbuffay\*(Aq, \& address => \*(Aq123 street road\*(Aq, \& city => \*(Aqnomans land\*(Aq, \& state => \*(Aqzz\*(Aq, \& zip => \*(Aq54321\*(Aq \& ); \& \& $ok++ if $mt\->validate_profile(\*(Aqregistration\*(Aq); \& \& print $ok == 3 ? "OK" : "NOT OK"; \& \& 1; .Ve .SS "Multi-Variant Parameters" .IX Subsection "Multi-Variant Parameters" .Vb 1 \& # handling multi\-variant parameter names (aliases) \& \& package MultiName; \& \& use Validation::Class; \& \& field login => { \& \& required => 1, \& min_length => 5, # must be 5 or more chars \& min_alpha => 1, # must have at\-least 1 alpha char \& min_digits => 1, # must have at\-least 1 digit char \& min_symbols => 1, # must have at\-least 1 non\-alphanumeric char \& alias => [ \& \*(Aqsignin\*(Aq, \& \*(Aqusername\*(Aq, \& \*(Aqemail\*(Aq, \& \*(Aqemail_address\*(Aq \& ] \& \& }; \& \& package main; \& \& my $ok = 0; \& \& # fail \& $ok++ if ! MultiName\->new(login => \*(Aqmiso\*(Aq)\->validate; \& \& # nice \& $ok++ if MultiName\->new(login => \*(Aqm!s0_soup\*(Aq)\->validate; \& \& # no signin field exists, however, the alias directive pre\-processing DWIM \& $ok++ if MultiName\->new(signin => \*(Aqm!s0_soup\*(Aq)\->validate; \& \& # process aliases \& $ok++ if MultiName\->new(params => {signin => \*(Aqm!s0_soup\*(Aq})\->validate; \& $ok++ if MultiName\->new(params => {username => \*(Aqm!s0_soup\*(Aq})\->validate; \& $ok++ if MultiName\->new(params => {email => \*(Aqm!s0_soup\*(Aq})\->validate; \& $ok++ if MultiName\->new(params => {email_address => \*(Aqm!s0_soup\*(Aq})\->validate; \& \& print $ok == 7 ? "OK" : "NOT OK"; \& \& 1; .Ve .SS "Parameter Dependencies" .IX Subsection "Parameter Dependencies" .Vb 1 \& # handling parameter dependencies \& \& package ParamDependencies; \& \& use Validation::Class; \& \& mixin scrub => { \& required => 1, \& filters => [\*(Aqtrim\*(Aq, \*(Aqstrip\*(Aq] \& }; \& \& mixin flag => { \& length => 1, \& options => [0, 1] \& }; \& \& field billing_address => { \& mixin => \*(Aqscrub\*(Aq, \& required => 1, \& depends_on => [\*(Aqbilling_city\*(Aq, \*(Aqbilling_state\*(Aq, \*(Aqbilling_zip\*(Aq] \& }; \& \& field billing_city => { \& mixin => \*(Aqscrub\*(Aq, \& required => 0, \& depends_on => \*(Aqbilling_address\*(Aq \& }; \& \& field billing_state => { \& mixin => \*(Aqscrub\*(Aq, \& required => 0, \& length => \*(Aq2\*(Aq, \& pattern => \*(AqXX\*(Aq, \& depends_on => \*(Aqbilling_address\*(Aq \& }; \& \& field billing_zip => { \& mixin => \*(Aqscrub\*(Aq, \& required => 0, \& length => \*(Aq5\*(Aq, \& pattern => \*(Aq#####\*(Aq, \& depends_on => \*(Aqbilling_address\*(Aq \& }; \& \& field shipping_address => { \& mixin_field => \*(Aqbilling_address\*(Aq, \& depends_on => [\*(Aqshipping_city\*(Aq, \*(Aqshipping_state\*(Aq, \*(Aqshipping_zip\*(Aq] \& }; \& \& field shipping_city => { \& mixin_field => \*(Aqbilling_city\*(Aq, \& depends_on => \*(Aqshipping_address\*(Aq \& }; \& \& field shipping_state => { \& mixin_field => \*(Aqbilling_state\*(Aq, \& depends_on => \*(Aqshipping_address\*(Aq \& }; \& \& field shipping_zip => { \& mixin_field => \*(Aqbilling_zip\*(Aq, \& depends_on => \*(Aqshipping_address\*(Aq \& }; \& \& field same_billing_shipping => { \& mixin => \*(Aqflag\*(Aq \& }; \& \& profile \*(Aqaddresses\*(Aq => sub { \& \& my ($self) = @_; \& \& return unless $self\->validate(\*(Aqsame_billing_shipping\*(Aq); \& \& # billing and shipping address always required \& $self\->validate(qw/+billing_address +shipping_address/); \& \& # address must match if option is selected \& if ($self\->param(\*(Aqsame_billing_shipping\*(Aq)) { \& \& foreach my $param ($self\->params\->grep(qr/^shipping_/)\->keys) { \& \& my ($suffix) = $param =~ /^shipping_(.*)/; \& \& my $billing = $self\->param("billing_$suffix"); \& my $shipping = $self\->param("shipping_$suffix"); \& \& # shipping_* must match billing_* \& unless ($billing eq $shipping) { \& $self\->errors\->add( \& "Billing and shipping addresses do not match" \& ); \& last; \& } \& \& } \& \& } \& \& return $self\->error_count ? 0 : 1; \& \& }; \& \& package main; \& \& my $ok = 0; \& my $pd; \& \& $pd = ParamDependencies\->new( \& billing_address => \*(Aq10 liberty boulevard\*(Aq, \& billing_city => \*(Aqmalvern\*(Aq, \& billing_state => \*(Aqpa\*(Aq, \& billing_zip => \*(Aq19355\*(Aq \& ); \& \& # missing shipping address info \& $ok++ if ! $pd\->validate_profile(\*(Aqaddresses\*(Aq); \& \& $pd = ParamDependencies\->new( \& billing_address => \*(Aq10 liberty boulevard\*(Aq, \& billing_city => \*(Aqmalvern\*(Aq, \& billing_state => \*(Aqpa\*(Aq, \& billing_zip => \*(Aq19355\*(Aq, \& \& shipping_address => \*(Aq301 cherry street\*(Aq, \& shipping_city => \*(Aqpottstown\*(Aq, \& shipping_state => \*(Aqpa\*(Aq, \& shipping_zip => \*(Aq19464\*(Aq \& ); \& \& $ok++ if $pd\->validate_profile(\*(Aqaddresses\*(Aq); \& \& $pd = ParamDependencies\->new( \& billing_address => \*(Aq10 liberty boulevard\*(Aq, \& billing_city => \*(Aqmalvern\*(Aq, \& billing_state => \*(Aqpa\*(Aq, \& billing_zip => \*(Aq19355\*(Aq, \& \& same_billing_shipping => 1, \& \& shipping_address => \*(Aq301 cherry street\*(Aq, \& shipping_city => \*(Aqpottstown\*(Aq, \& shipping_state => \*(Aqpa\*(Aq, \& shipping_zip => \*(Aq19464\*(Aq \& ); \& \& # billing and shipping don\*(Aqt match \& $ok++ if ! $pd\->validate_profile(\*(Aqaddresses\*(Aq); \& \& $pd = ParamDependencies\->new( \& billing_address => \*(Aq10 liberty boulevard\*(Aq, \& billing_city => \*(Aqmalvern\*(Aq, \& billing_state => \*(Aqpa\*(Aq, \& billing_zip => \*(Aq19355\*(Aq, \& \& same_billing_shipping => 1, \& \& shipping_address => \*(Aq10 liberty boulevard\*(Aq, \& shipping_city => \*(Aqmalvern\*(Aq, \& shipping_state => \*(Aqpa\*(Aq, \& shipping_zip => \*(Aq19355\*(Aq \& ); \& \& $ok++ if $pd\->validate_profile(\*(Aqaddresses\*(Aq); \& \& print $ok == 4 ? "OK" : "NOT OK"; \& \& 1; .Ve .SH "GETTING STARTED" .IX Header "GETTING STARTED" If you are looking for a simple way to get started with Validation::Class, please review Validation::Class::Simple. The instructions contained there are also relevant for configuring any class derived from Validation::Class. .SH "ADDITIONAL INSIGHT" .IX Header "ADDITIONAL INSIGHT" The following screencast and/or slideshow explains what Validation::Class is, why it was created, and what it has to offer. Please note that this screencast and slideshow was created many moons ago and some of its content may be a bit outdated. .SH "AUTHOR" .IX Header "AUTHOR" Al Newkirk .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2011 by Al Newkirk. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.