1 package Catalyst::Controller::DBIC::API::Validator;
2 #ABSTRACT: Provides validation services for inbound requests against whitelisted parameters
4 use namespace::autoclean;
6 BEGIN { extends 'Data::DPath::Validator'; }
8 has '+visitor' => ( 'builder' => '_build_custom_visitor' );
10 sub _build_custom_visitor
12 return Catalyst::Controller::DBIC::API::Visitor->new();
15 Catalyst::Controller::DBIC::API::Validator->meta->make_immutable;
17 ###############################################################################
18 package Catalyst::Controller::DBIC::API::Visitor;
21 use namespace::autoclean;
23 BEGIN { extends 'Data::DPath::Validator::Visitor'; }
25 use constant DEBUG => $ENV{DATA_DPATH_VALIDATOR_DEBUG} || 0;
27 around visit_array => sub
29 my ($orig, $self, $array) = @_;
31 warn 'ARRAY: '. $self->current_template if DEBUG;
32 if(@$array == 1 && $array->[0] eq '*')
34 $self->append_text('[reftype eq "HASH" ]');
35 $self->add_template($self->current_template);
39 if($self->current_template =~ /\/$/)
41 my $temp = $self->current_template;
42 $self->reset_template();
44 $self->append_text($temp);
53 my ($self, $elem, $index, $array) = @_;
55 warn 'ARRAYENTRY: '. $self->current_template if DEBUG;
58 $self->append_text($elem . '/*');
59 $self->add_template($self->current_template);
61 elsif(ref($elem) eq 'HASH')
66 $self->value_type('NONE');
69 around visit_hash => sub
71 my ($orig, $self, $hash) = @_;
73 if($self->current_template =~ /\/$/)
75 my $temp = $self->current_template;
76 $self->reset_template();
78 $self->append_text($temp);
80 warn 'HASH: '. $self->current_template if DEBUG;
85 around visit_value => sub
87 my ($orig, $self, $val) = @_;
89 if($self->value_type eq 'NONE')
92 $self->append_text($val . '/*');
93 $self->add_template($self->current_template);
94 warn 'VALUE: ' . $self->current_template if DEBUG;
97 elsif($self->value_type eq 'HashKey')
99 $self->append_text($val);
100 warn 'VALUE: ' . $self->current_template if DEBUG;
110 Catalyst::Controller::DBIC::API::Visitor->meta->make_immutable;