Renamed Visitor to Validator::Visitor to conform with Data::DPath::Validator and...
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / Validator / Visitor.pm
1 package Catalyst::Controller::DBIC::API::Validator::Visitor;
2 #ABSTRACT: Provides validation services for inbound requests against whitelisted parameters
3 use Moose;
4 use namespace::autoclean;
5
6 BEGIN { extends 'Data::DPath::Validator::Visitor'; }
7
8 use constant DEBUG => $ENV{DATA_DPATH_VALIDATOR_DEBUG} || 0;
9
10 around visit_array => sub
11 {
12     my ($orig, $self, $array) = @_;
13     $self->dive();
14     warn 'ARRAY: '. $self->current_template if DEBUG;
15     if(@$array == 1 && $array->[0] eq '*')
16     {
17         $self->append_text('[reftype eq "HASH" ]');
18         $self->add_template($self->current_template);
19     }
20     else
21     {
22         if($self->current_template =~ /\/$/)
23         {
24             my $temp = $self->current_template;
25             $self->reset_template();
26             $temp =~ s/\/$//;
27             $self->append_text($temp);
28         }
29         $self->$orig($array);
30     }
31     $self->rise();
32 };
33
34 sub visit_array_entry
35 {
36     my ($self, $elem, $index, $array) = @_;
37     $self->dive();
38     warn 'ARRAYENTRY: '. $self->current_template if DEBUG;
39     if(!ref($elem))
40     {
41         $self->append_text($elem . '/*');
42         $self->add_template($self->current_template);
43     }
44     elsif(ref($elem) eq 'HASH')
45     {
46         $self->visit($elem);
47     }
48     $self->rise();
49     $self->value_type('NONE');
50 };
51
52 around visit_hash => sub
53 {
54     my ($orig, $self, $hash) = @_;
55     $self->dive();
56     if($self->current_template =~ /\/$/)
57     {
58         my $temp = $self->current_template;
59         $self->reset_template();
60         $temp =~ s/\/$//;
61         $self->append_text($temp);
62     }
63     warn 'HASH: '. $self->current_template if DEBUG;
64     $self->$orig($hash);
65     $self->rise();
66 };
67
68 around visit_value => sub
69 {
70     my ($orig, $self, $val) = @_;
71
72     if($self->value_type eq 'NONE')
73     {
74         $self->dive();
75         $self->append_text($val . '/*');
76         $self->add_template($self->current_template);
77         warn 'VALUE: ' . $self->current_template if DEBUG;
78         $self->rise();
79     }
80     elsif($self->value_type eq 'HashKey')
81     {
82         $self->append_text($val);
83         warn 'VALUE: ' . $self->current_template if DEBUG;
84     }
85     else
86     {
87         $self->$orig($val);
88     }
89
90 };
91
92
93 __PACKAGE__->meta->make_immutable;
94
95 1;