/usr/share/perl5/Validation/Class/Whitepaper.pod is in libvalidation-class-perl 7.900057-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 | # PODNAME: Validation::Class::Whitepaper
# ABSTRACT: Operate with Impunity
# VERSION
__END__
=pod
=head1 NAME
Validation::Class::Whitepaper - Operate with Impunity
=head1 VERSION
version 7.900057
=head1 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.
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.
=head1 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.
To understand the problem domain we need to first ask ourselves:
* what is data validation? and ... is that what I've been doing?
* what are the common data validation requirements?
* what are the common use-cases where validation becomes tricky?
Data validation is the process of auditing a piece of data to ensure it fits a
specific criteria. Standard data validation requirements are:
* existence checking
* range checking
* type checking
* list-lookup checking
* dependency checking
* pattern checking
* custom validation checking (business logic)
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.
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:
* 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)
=head1 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.
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.
=head1 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.
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:
* 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
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.
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).
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.
=head2 Dynamic Parameters
# handling arbitrary and/or dynamically-named parameters
package DynamicParameters;
use Validation::Class;
field email => {
required => 1,
pattern => qr/\@localhost$/
};
field login => {
required => 1,
min_length => 5,
alias => ['user']
};
field password => {
required => 1,
min_length => 5,
min_digits => 1,
alias => ['pass']
};
package main;
my $params = {
user => 'admin', # arbitrary
pass => 's3cret', # arbitrary
email_1 => 'admin@localhost', # dynamic created
email_2 => 'root@localhost', # dynamic created
email_3 => 'sa@localhost', # dynamic created
};
my $dp = DynamicParameters->new(params => $params);
$dp->proto->clone_field('email', $_)
for $dp->params->grep(qr/^email/)->keys
;
print $dp->validate ? "OK" : "NOT OK";
1;
=head2 Batch-Processing
# handling input for batch-processing
package BatchProcessing;
use Validation::Class;
mixin scrub => {
required => 1,
filters => ['trim', 'strip']
};
field header => {
mixin => 'scrub',
options => ['name', 'email', 'contact', 'dob', 'country'],
multiples => 1 # handle param as a scalar or arrayref
};
field name => {
mixin => 'scrub',
filters => ['titlecase'],
min_length => 2
};
field email => {
mixin => 'scrub',
min_length => 3
};
field contact => {
mixin => 'scrub',
length => 10
};
field dob => {
mixin => 'scrub',
length => 8,
pattern => '##/##/##'
};
field country => {
mixin => 'scrub'
};
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/^\s+//; $_ } split /\n/, $params->{pasted_data};
my @headers = split /\t/, 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 /\t/, $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;
=head2 Multi-Type Parameters
# handling multi-type parameters (array or scalar depending on context)
package MultiType;
use Validation::Class;
field letter_type => {
required => 1,
options => [ 'A' .. 'Z' ],
multiples => 1 # turn on multi-type processing
};
package main;
my $mt = MultiType->new;
my $ok = 0;
$mt->params->add(letter_type => 'A');
$ok++ if $mt->validate;
$mt->params->clear->add(letter_type => ['A', 'B', 'C']);
$ok++ if $mt->validate;
print $ok == 2 ? "OK" : "NOT OK";
1;
=head2 Complex Conditions
# handling complex conditional validation logic
package ComplexCondition;
use Validation::Class;
mixin scrub => {
required => 1,
filters => ['trim', 'strip']
};
mixin flag => {
length => 1,
options => [0, 1]
};
field first_name => {
mixin => 'scrub',
filters => ['titlecase']
};
field last_name => {
mixin => 'scrub',
filters => ['titlecase']
};
field role => {
mixin => 'scrub',
filters => ['titlecase'],
options => ['Client', 'Employee', 'Administrator'],
default => 'Client'
};
field address => {
mixin => 'scrub',
required => 0,
depends_on => ['city', 'state', 'zip']
};
field city => {
mixin => 'scrub',
required => 0,
depends_on => 'address'
};
field state => {
mixin => 'scrub',
required => 0,
length => '2',
pattern => 'XX',
depends_on => 'address'
};
field zip => {
mixin => 'scrub',
required => 0,
length => '5',
pattern => '#####',
depends_on => 'address'
};
field has_mail => {
mixin => 'flag'
};
profile 'registration' => sub {
my ($self) = @_;
# address info not required unless role is client or has_mail is true
return unless $self->validate('has_mail');
$self->queue(qw/first_name last_name/);
if ($self->param('has_mail') || $self->param('role') eq 'Client') {
# 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 => 'Rachel',
last_name => 'Green'
);
# defaults to client, missing address info
$ok++ if ! $mt->validate_profile('registration');
$mt = ComplexCondition->new(
first_name => 'monica',
last_name => 'geller',
role => 'employee'
);
# filters (pre-process) role and titlecase, as employee no address needed
$ok++ if $mt->validate_profile('registration');
$mt = ComplexCondition->new(
first_name => 'phoebe',
last_name => 'buffay',
address => '123 street road',
city => 'nomans land',
state => 'zz',
zip => '54321'
);
$ok++ if $mt->validate_profile('registration');
print $ok == 3 ? "OK" : "NOT OK";
1;
=head2 Multi-Variant Parameters
# 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 => [
'signin',
'username',
'email',
'email_address'
]
};
package main;
my $ok = 0;
# fail
$ok++ if ! MultiName->new(login => 'miso')->validate;
# nice
$ok++ if MultiName->new(login => 'm!s0_soup')->validate;
# no signin field exists, however, the alias directive pre-processing DWIM
$ok++ if MultiName->new(signin => 'm!s0_soup')->validate;
# process aliases
$ok++ if MultiName->new(params => {signin => 'm!s0_soup'})->validate;
$ok++ if MultiName->new(params => {username => 'm!s0_soup'})->validate;
$ok++ if MultiName->new(params => {email => 'm!s0_soup'})->validate;
$ok++ if MultiName->new(params => {email_address => 'm!s0_soup'})->validate;
print $ok == 7 ? "OK" : "NOT OK";
1;
=head2 Parameter Dependencies
# handling parameter dependencies
package ParamDependencies;
use Validation::Class;
mixin scrub => {
required => 1,
filters => ['trim', 'strip']
};
mixin flag => {
length => 1,
options => [0, 1]
};
field billing_address => {
mixin => 'scrub',
required => 1,
depends_on => ['billing_city', 'billing_state', 'billing_zip']
};
field billing_city => {
mixin => 'scrub',
required => 0,
depends_on => 'billing_address'
};
field billing_state => {
mixin => 'scrub',
required => 0,
length => '2',
pattern => 'XX',
depends_on => 'billing_address'
};
field billing_zip => {
mixin => 'scrub',
required => 0,
length => '5',
pattern => '#####',
depends_on => 'billing_address'
};
field shipping_address => {
mixin_field => 'billing_address',
depends_on => ['shipping_city', 'shipping_state', 'shipping_zip']
};
field shipping_city => {
mixin_field => 'billing_city',
depends_on => 'shipping_address'
};
field shipping_state => {
mixin_field => 'billing_state',
depends_on => 'shipping_address'
};
field shipping_zip => {
mixin_field => 'billing_zip',
depends_on => 'shipping_address'
};
field same_billing_shipping => {
mixin => 'flag'
};
profile 'addresses' => sub {
my ($self) = @_;
return unless $self->validate('same_billing_shipping');
# billing and shipping address always required
$self->validate(qw/+billing_address +shipping_address/);
# address must match if option is selected
if ($self->param('same_billing_shipping')) {
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 => '10 liberty boulevard',
billing_city => 'malvern',
billing_state => 'pa',
billing_zip => '19355'
);
# missing shipping address info
$ok++ if ! $pd->validate_profile('addresses');
$pd = ParamDependencies->new(
billing_address => '10 liberty boulevard',
billing_city => 'malvern',
billing_state => 'pa',
billing_zip => '19355',
shipping_address => '301 cherry street',
shipping_city => 'pottstown',
shipping_state => 'pa',
shipping_zip => '19464'
);
$ok++ if $pd->validate_profile('addresses');
$pd = ParamDependencies->new(
billing_address => '10 liberty boulevard',
billing_city => 'malvern',
billing_state => 'pa',
billing_zip => '19355',
same_billing_shipping => 1,
shipping_address => '301 cherry street',
shipping_city => 'pottstown',
shipping_state => 'pa',
shipping_zip => '19464'
);
# billing and shipping don't match
$ok++ if ! $pd->validate_profile('addresses');
$pd = ParamDependencies->new(
billing_address => '10 liberty boulevard',
billing_city => 'malvern',
billing_state => 'pa',
billing_zip => '19355',
same_billing_shipping => 1,
shipping_address => '10 liberty boulevard',
shipping_city => 'malvern',
shipping_state => 'pa',
shipping_zip => '19355'
);
$ok++ if $pd->validate_profile('addresses');
print $ok == 4 ? "OK" : "NOT OK";
1;
=head1 GETTING STARTED
If you are looking for a simple way to get started with L<Validation::Class>,
please review L<Validation::Class::Simple>. The instructions contained there
are also relevant for configuring any class derived from L<Validation::Class>.
=head1 ADDITIONAL INSIGHT
The following L<screencast|http://youtu.be/YCPViiB5jv0> and/or
L<slideshow|http://www.slideshare.net/slideshow/embed_code/9632123> explains
what L<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.
=head1 AUTHOR
Al Newkirk <anewkirk@ana.io>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Al Newkirk.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|