If you are looking for a data validation module with an even lower learning curve built using the same tenets and principles as Validation::Class which is as simple and even lazier than this module, please review the tested but experimental Validation::Class::Simple::Streamer.

All input parameters are wrapped by the Validation::Class::Params container which provides generic functionality for managing hashes. Additionally you can declare parameters by using the params object directly:

Finally, any parameter which has corresponding validation rules that has been declared in a validation class derived from Validation::Class will have an accessor which can be used directly or as an argument to the constructor:

Validation::Class comes with a complete standard set of validation rules which allows you to easily describe the constraints and operations that need to be performed per parameter.

Validation rules are referred to as fields, fields are named after the parameters they expect to be matched against. A field is also a hashref whose keys are called directives which correspond with the names of classes in the directives namespace, and whose values are arguments which control how directives carry-out their operations.

A good data validation tool is not simply checking input against constraints, its also providing a means to easily handle different and often complex data input scenarios.

The queue method allows you to designate and defer fields to be validated. It also allows you to set fields that must be validated regardless of what has been passed to the validate method. Additionally it allows you to conditionally specify constraints:

Akin to the queue method is the stash method. At-times it is necessary to break out of the box in order to design constraints that fit your particular use-case. The stash method allows you to share arbitrary objects with routines used by validation classes.

When validation fails, and it will, you need to be able to report what failed and why. Validation::Class give you complete control over error handling and messages. Errors can exist at the field-level and class-level (errors not specific to a particular field). All errors are wrapped in a Validation::Class::Errors container.

Filtering data is one fringe benefits of a good data validation framework. The process is also known as scrubbing or sanitizing data. The process ensures that the data being passed to the business logic will be clean and consistent.

Filtering data is not as simple and straight-forward as it may seem which is why it is necessary to think-through your applications interactions before implementation.

Filtering is the process of applying transformations to the incoming data. The problem with filtering is that it permanently alters the data input and in the event of a failure could report inconsistent error messages:

use Validation::Simple;
my $rules = Validation::Simple->new;
$rules->fields->add(
# even if the input is submitted as lowercase it will fail
# the filter is run as a pre-process by default
username => {
filters => ['uppercase'],
validation => sub {
return 0 if $_[1]->value =~ /[A-Z]/;
return 1;
}
}
);

When designing a system to filter data, it is always necessary to differentiate pre-processing filters from post-processing filters. Validation::Class provides a filtering directive which designates certain fields to run filters in post-processing:

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.

When a type-constraint system finds an error it raises an exception. Exception handling is the process of responding to the occurrence, during computation, of exceptions (anomalous or exceptional situations).

Typically the errors reported when an exception is raised includes a dump of the program's state up until the point of the exception which is apropos as exceptions are unexpected.

A data validation framework can also be thought-of as a type system but one that is specifically designed to expect input errors and report user-friendly error messages.

Once your fields are defined and you have your parameter rules configured as desired you will like use the validate method to perform all required operations. The validation operations occur in the following order:

What gets validated is determined by the state and arguments passed to the validate method. The validate method determines what to validate in the following order:

checks the validation queue for fields
checks arguments for regular expression objects and adds matching fields
validates fields with matching parameters if no fields are specified
validates all fields if no parameters are specified

It is also important to under what it means to declare a field as being required. A field is a data validation rule matching a specific parameter, A required field simply means that if-and-when a parameter is submitted, it is required to have a value. It does not mean that a field is always required to be validated.

Occasionally you may need to temporarily set a field as required or not-required for a specific validation operation. This requirement is referred to as the toggle function. The toggle function is enacted by prefixing a field name with a plus or minus sign (+|-) when passed to the validate method:

use Validation::Simple;
my $rules = Validation::Simple->new(fields => {...});
# meaning, email is always required to have a value
# however password and password2 can be submitted as empty strings
# but if password and password2 have values they will be validated
$rules->validate('+email', '-password', '-password2');

Here are a few examples and explanations of using the validate method: