Validation::Class is designed to provide you with a self-validating data model. The DRY approach the library enforces ensures data integrity through consistency.

This enables you to check incoming data once and ensure it meets a specific criteria then move on throughout various layers in the application stack with a level-of-certainty that the input is as it should be, ... sort've a set-it-and-forget-it approach.

Prior to Validation::Class version 7, the general idea was to have developers create a validation class in addition to an existing data/object model (which is twice the work), this was the reasoning behind having the framework ship with a Moose adapter (no longer available). Although the Moose adapter is no longer supported, Validation::Class provides your classes with a simple object system providing the tools necessary to combine your validation and model layers.

The Validation::Class object system provides automatic generation of attributes and accessors based on field names, and a new method for instantiating your classes. The method keyword creates self-validating routines (like method signatures) which allow you to focus on the handling of data instead of worrying if the data is as it should be.

Simple as it may seem, these features allow developers to easily create data models and objects with verifiable attributes and methods as well as built-in validation and error handling functionality while remaining fast and extensible due to its lean dependency chain.

Validation::Class won't die on instantiation (or anywhere else for that matter) unless you tell it to. The ignore_failure flag when set to false will confess to method validation failures. The ignore_unknown flag when set to false will confess to an attempt to validate a parameter with no matching field definition.

Although the sentiment may not be shared by all, error handling is one of those things I'd just like to be available without worrying too much about how its setup. Validation and error handling are built-in mechanisms you are encouraged to leverage. Validation::Class is extremely extendable for those that wish to roll-their-own-solution.

Your validation class can be thought of as your data-model/input-firewall. The benefits this approach provides might require you to change your perspective on parameter handling and workflow. Typically when designing an application we tend to name parameters arbitrarily and validate the same data at various stages during a program's execution in various places in the application stack. This approach is inefficient and prone to bugs and security problems.

To get the most out of Validation::Class you should consider each parameter hitting your application (individually) as a transmission fitting a very specific criteria, yes, like a field in a data model.

Your validation rules will act as filters which will reject or accept and format the transmission for use within your application, yes, almost exactly like a firewall.

The fields defined will be used to validate the specified input parameters. You specify the input parameters at/after instantiation, parameters should take the form of a hashref of key/value pairs passed to the params attribute, or attribute/value pairs. The following is an example on using your validate class to validate input in various scenarios:

Lazy validation? Have your validation class automatically find the appropriate fields to validate against (params must match field names). This is possible but not necessarily recommended as the execution and validation is dictated by the parameters submitted (which you may or may not have control over).

You can define an alias to automatically map a parameter to a validation field whereby a field definition will have an alias attribute containing an arrayref of alternate names that can be matched against passed-in parameter names.

Validation::Class supports pre/post filtering but is configured to pre-filter incoming data. This means that based upon the filtering options supplied within the individual fields, filtering will happen before validation (technically at instantiation and again just before validation). As expected, this is configurable via the filtering attribute.

A WORD OF CAUTION: Validation::Class is configured to pre-filter incoming data which boosts application security and is best used with passive filtering (e.g. converting character case - filtering which only alters the input in predictable ways), versus aggressive filtering (e.g. formatting a telephone number) which completely and permanently changes the incoming data ... so much so that if the validation still fails ... errors that are reported may not match the data that was submitted.

If you're sure you'd rather employ aggressive filtering, I suggest setting the filtering attribute to 'post' for post-filtering or setting it to null and applying the filters manually by calling the apply_filters() method.

Validation::Class supports automatic serialization and deserialization of parameters with complex data structures which means that you can set a parameter as an arrayref or hashref of nested data structures and validate against them, likewise you can set a parameters using parameter names which are serialized string representations of the keys within the complex structure you wish to set and validate against.

When creating official Validation::Class plugins you should use the namespace Validation::Class::Plugin::YourPluginName. This will allow users of your plugin to simply pass YourPluginName to the plugins option of the load() method. Otherwise you will need to pass the fully-qualified plugin package name prefixed with a "+" symbol. The following is an example of including a plugin.

Most users will never venture beyond the public API, but powerful abilities await the more adventureous developer and this section was written specifically for you. To assist you on along your journey, let me explain exactly what happens when you define and instantiate a validation class.

Classes are defined using keywords (field, mixin, filter, etc) which register rule definitions on a cached class profile (of-sorts) associated with the class which is being constructed. On instantiation, the cached class profile is cloned then merged with any arguments provided to the constructor, this means that even in a persistent environment the original class profile is never altered.

To begin introspection, simply look into the attributes attached to the class prototype, e.g. fields, mixins, filters, etc., the following examples will give you an idea of how to use introspection to extend your application code using Validation::Class.

Please keep in mind that Validation::Class is likely to have most of the functionalty you would need to introspect your codebase. The following is an introspect design template that will work in most cases:

In the context of a web-application, it is often best to perform the initial input validation on the client (web-browser) before submitting data to the server for further validation and processing. In the following code we will generate javascript objects that match our Validation::Class data models which we will then use with jQuery to validate form data, etc.

The load keyword (or set), which can also be used as a method, provides options for extending the current class by attaching other Validation::Class classes as relatives, roles, plugins, etc. The process of applying roles to the current class mainly involve copying the role's methods and configuration.

NOTE: While the load/set functionality is not depreciated and will remain part of this library, its uses are no longer recommended as there are better ways to achieve the desired results. Additionally, the following usage scenarios can be refactored using traditional inheritance.

The load.classes option, can be a constant or arrayref and uses Module::Find to load all child classes (in-all-subdirectories) for convenient access through the class() method. Existing parameters and configuration options are passed to the child class' constructor. All attributes can be easily overwritten using the attribute's accessors on the child class. These child classes are often referred to as relatives. This option accepts a constant or an arrayref of constants.

The load.plugins option is used to load plugins that support Validation::Class. A Validation::Class plugin is little more than a class that implements a "new" method that extends the associated validation class object. As usual, an official Validation::Class plugin can be referred to using shorthand while custom plugins are called by prefixing a plus symbol to the fully-qualified plugin name. Learn more about plugins at Validation::Class::Intro. This option accepts a constant or an arrayref of constants.

The load.roles option is used to load and inherit functionality from child classes, these classes should be used and thought-of as roles. Any validation class can be used as a role with this option. This option accepts a constant or an arrayref of constants.

The object keyword (or obj) registers a class object builder which builds and returns a class object on-demand. The object keyword also creates a method on the calling class which invokes the builder. Unlike class attributes, this method does not cache or otherwise store the returned class object it constructs.

NOTE: While the object/obj functionality is not depreciated and will remain part of this library, its is mostly experimental.

The object keyword takes two arguments, an object builder name and a hashref of key/value pairs which are used to instruct the builder on how to construct the object. The supplied hashref should be configured as follows:

# class to construct
type => 'ClassName',
# optional: constructor name (defaults to new)
init => 'new',
# optional: arrayref of field names or a coderef which returns arguments for
# the constructor (defaults to all fields)
args => sub {}
# optional: if supplying an arrayref of fields to the args option, this
# option determines how the parameters will be supplied to the constructor,
# valid options are 'hash' or 'list' (defaults to list)
list => 'hash'