NAME

Data::Validate::XSD - Validate complex structures by definition

SYNOPSIS

use Data::Validate::XSD;

my $validator = Data::Validate::XSD->new( \%definition );

my ($errors, $error) = $validator->validate( \%data );

if($error) {
  carp Dumper($errors);
}

DESCRIPTION

Based on xsd and xml validation, this is an attempt to provide those functions
without either xml or the hidous errors given out by modules like XPath.

The idea behind the error reporting is that the errors can reflect the structure
of the original structure replacing each variable with an error code and message.
It is possible to work out a one dimention error reporting scheme too which I may
work on next.

DEFINITIONS

A definition is a hash containing information like an xml node containing children.

An example definition and the kind of data it validates:

$definition = { root => [ { name => 'input', type => 'newuser' } ],

simpleTypes => [
  confirm  => { base => 'id',   match => '/input/password' },
  rname    => { base => 'name', minLength => 1 },
  password => { base => 'id',   minLength => 6 },
],

complexTypes => {
  newuser => [
    { name => 'username',     type => 'id' },
    { name => 'password',     type => 'password' },
    { name => 'confirm',      type => 'confirm' },
    { name => 'firstName',    type => 'rname' },
    { name => 'familyName',   type => 'name',  minOccurs => 0 },
    { name => 'nickName',     type => 'name',  minOccurs => 0 },
    { name => 'emailAddress', type => 'email', minOccurs => 0 },
  ],
},
};

$data = { input => { username => 'abcdef', password => '1234567', confirm => '1234567', firstName => 'test', familyName => 'user', nickName => 'foobar', emailAddress => 'foo@bar.com', } };

RESULTS

The first result you get is a structure the second is a boolean, the boolean explains the total stuctures pass or fail status.

The structure that is returned is almost a mirror structure of the input:

$error = 0; $errors = { _input => 0, input => { username => 0, password => 0, confirm => 0, firstName => 0, familyName => 0, nickName => 0, emailAddress => 0, } },

METHODS

$class->new( $definition, $debug )

Create a new validation object, debug will cause
All error codes to be replaced by error strings.

$class->new_from_file( $path, $filename, $debug )

Create a new definition from a stored file.

$validator->validate( $data )

Validate a set of data against this validator.
Returns $errors and $error (see synopsys)

$validator->setStrict( $bool )

Should missing data be considered an error.

$validator->setDefinition( $definition )

Set the validators definition, will load it (used internally too)

$validator->_load_definition( $definition )

Internal method for loading a definition into the validator

$validator->_load_definition_from_file( $filename )

Internal method for loading a definition from a file

$validator->_validate_elements( %p )

Internal method for validating a list of elements;
p: definition, data, mode

$validator->_validate_element( %p )

Internal method for validating a single element
p: data, definition, mode

$validator->_validate_type( %p )

Internal method for validating a single data type

$validator->_find_value( %p )

Internal method for finding a value match (basic xpath)

$validator->_push_hash( $dest, $source )

Internal method for copying a hash to another

$validator->_load_file( $file )

Internal method for loading a file, must be valid perl syntax.
Yep that's right, be bloody careful when loading from files.

$validate->objectify( $class, @data )

Create a data based object for this type.

AUTHOR

Copyright, Martin Owens 2007, GPLv3