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


version 0.093411


    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();

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

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


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.


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);



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() 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



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.


Nicholas Perez <>


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.