NAME

Data::DPath::Validator - Validate data based on template data

VERSION

version 0.093411

SYNOPSIS

use strict;
use warnings;
use Data::DPath::Validator;

my $template = { foo => '*' };
my $data = [{ foo => [1,2,3] }, { foo => { bar => 'mtfnpy' } } ];

my $v = Data::DPath::Validator->new();
$v->load($template);

my $ret = $v->validate(@$data);

if($ret->[0] && $ret->[1])
{
    print "Hooray!\n";
}

DESCRIPTION

Data::DPath::Validator is a simple data validator using normal Perl data structures as templates. It accomplishes this by translating your template into a series Data::DPath paths (one for each "branch") using Data::Visitor to traverse the data structure like a SAX parser. Then when calling validate(), each path is then attempted against the supplied data.

NOTES

A template is defined by using a normal Perl data structure like in the synopsis, with the parts where 'any' data is acceptable being replaced with an asterisk ('*').

By default, the validator is in loose validation mode, meaning as long as one path matches, the data structure passes. To instead require strict validation do this:

my $v = Data::DPath::Validator->new(strict_mode => 1);

ATTRIBUTES

visitor

This contains our Data::DPath::Validator::Visitor instance constructed via MooseX::Role::BuildInstanceOf.

It handles the following methods:

load -> visit

load() takes any number of data structures and visit()s via Data::Visitor to generate Data::DPath paths, storing a different path for each "branch" in each data structure.

templates

templates() is the accessor from the Visitor to return an ArrayRef[Str] containing all of the parsed templates.

strict_mode is: ro, isa: Bool, default: 0

strict_mode determines how strict the validation is. By default, only a single template path needs to match for the data structure to be okay. With strict_mode on, all paths must pass on the supplied data or it fails

METHODS

validate

Validate takes any number of data structures and verifies that each matches at least one of the DPaths generated from the template data structures.

Returns ArrayRef[Bool] which each indice corresponding to order in which the data was supplied.

AUTHOR

Nicholas Perez <nperez@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2009 by Infinity Interactive.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.