View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Clayton O'Neill > Config-Validate-0.2.6 > Config::Validate



Annotate this POD


New  5
Open  2
View/Report Bugs
Module Version: 0.2.6   Source  


Config::Validate - Validate data structures generated from configuration files. (Or anywhere else)


Version 0.2.6


This module is for validating configuration data that has been read in already and is in a Perl data structure. It does not handle reading or parsing configuration files since there are a plethora of available modules on CPAN to do that task. Instead it concentrates on verifying that the data read is correct, and providing defaults where appropriate. It also allows you to specify that a given configuration key may be available under several aliases, and have those renamed to the canonical name automatically.

The basic model used is that the caller provides a schema as a perl data structure that describes the constraints to verify against. The caller can then use the Config::Validate object to validate any number of data structures against the configured schema. If the data structure conforms to the schema given, then a new data structure will be returned, otherwise an exception is thrown.

Probably the easiest way to explain the intent is that Config::Validate is trying to be like Params::Validate for configuration files and other data structures.

This module has the following features:


The most complex part of using Config::Validate is defining the schema to validate against. The schema takes the form of set of nested hashes.

Here is an example schema you might use if you were writing something that needs to validate a database connection configuration file.

  my $schema = { db => { 
                    type => 'nested',
                    alias => 'dbinfo',
                    child => { 
                       hostname => { 
                          type => 'hostname'
                          alias => [ qw(host server) ],
                          default => 'localhost,
                       port => { 
                          type => 'integer',
                          max => 64*1024 - 1,
                          min => 1,
                          default => '3306',
                       username => { 
                          type => 'string'
                          optional => 1,
                          alias => 'user',
                       password => { 
                          type => 'string',
                          optional => 1,
                          alias => [ qw(pass passwd) ],
                       database => {
                          type => 'string',
                          alias => 'dbname',
                       column_types => {
                          type => 'hash',
                          keytype => 'string',
                          child => {
                            id => { 
                               type => 'string',
                               default => 'INT',
                 allowed_users => {
                    type => 'array',
                    subtype => 'string',

This is a somewhat long example of what a schema can look like. This uses most of the features available. The basic format is that a schema consists of a hash of hashes. Each of it's children describe a single field in the data structure to be validated. The only required key in the field definition is type, which defines how that element in the data/config hash should be validated.


Below is a list of the built in validation types, and the options they take. There are several global options that any of these can take that are documented below.


The nested type provides a way to validate nested hash references. Valid options are:


The integer type expects a whole number that can be positive or negative. Valid options are:


The float type verifies that the value meets the looks_like_number test from Scalar::Util. Valid options are:


The string type does no validation if no addition restrictions are specified. Valid options are:


The boolean type looks for a number of specific values, and converts them to 0 or 1. The values considered to be true are: 1, y, yes, t, true and on. The values considered to be false are 0, n, no, f, false, off. These values are not case sensitive. The boolean type takes no options.


The directory type verifies that the value is a directory that exists. The directory type takes no options.


The file type verifies that the value is a file, or a symlink that points at a file that exists. The file type takes no options.


The domain type uses the Data::Validate::Domain is_domain function to verify that the value is a validate domain name. This does not look the value up in DNS and verify that it exists. The domain type takes no options.


The hostname type uses the Data::Validate::Domain is_hostname function to verify that the value is a validate hostname name. This does not look the value up in DNS and verify that it exists. The hostname type takes no options.


The array type verifies that the value is an array reference. If the array_allows_scalar option is turned on (it is by default), then if a scalar value is found, then it will automatically be converted to an array reference with a single element.


The hash type validates a hash reference of key/value pairs.


There are a set of options that can be added to any field definition, that provide a common set of functionality to all.



The new method constructs a Config::Validate object, and returns it. It accepts the following arguments:

In addition, any of these can read or changed after the object is created, via an accessor with the same name as the parameter.


The validate sub can be called as either a function, or as a instance method.

If it is called as an instance method, then it expects a single config parameter which should be the data structure/config to be validated.

  my $result = $obj->validate(config => $config)

If it is called as a function, then it accepts two parameters. The config parameter should be the data structure/config to be validated, and the schema parameter should be the schema.

  my $result = validate(config => $config, schema => $schema)

The config parameter above can be a hash reference, or it can be a Config::General object. If it is a Config::General object, then the validate sub will automatically call the getall method on the object.

If any errors are encountered, then the validate sub will call die to throw an exception. In that case the value of $@ contain an error message describing the problem.

There was formerly a one and two argument variant of this sub. It is still supported, but deprecated.


The add_type method allows you to register a validation type on just a single instance of Config::Validate. The parameters are as follows:


The add_default_type method allows you to register a validation type for all new Config::Validate instances. It can be called as a function, class method, or instance method. If it is called as an instance method, then the new type will also be added to that instance. The parameters are the same as add_type.


The reset_default_types method removes all user defined types from the base class. Any instances that are alread created will retain their existing type configuration.


This is a convenience function for people writing callbacks and user defined type validation. It takes either an array or array reference and returns a string that represents the path to a specific item in the configuration. This might be useful if you're interested in having your error messages be consistent with the rest of Config::Validate. This is available for export, but not exported by default. Note: this is a function, not a method.


Clayton O'Neill

Eval for e-mail address: join('@', join('.', qw(cv 20 coneill)), '')


Copyright (C) 2007-2008 by Clayton O'Neill

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: