725f4a18551dfcba86abece3c4da21df4b3e2e31
[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 =attribute_private DEBUG
9
10 Debugging warnings can be enabled by setting the environment variable
11 DATA_DPATH_VALIDATOR_DEBUG to a true value.
12
13 =cut
14
15 use constant DEBUG => $ENV{DATA_DPATH_VALIDATOR_DEBUG} || 0;
16
17 around visit_array => sub
18 {
19     my ($orig, $self, $array) = @_;
20     $self->dive();
21     warn 'ARRAY: '. $self->current_template if DEBUG;
22     if(@$array == 1 && $array->[0] eq '*')
23     {
24         $self->append_text('[reftype eq "HASH" ]');
25         $self->add_template($self->current_template);
26     }
27     else
28     {
29         if($self->current_template =~ /\/$/)
30         {
31             my $temp = $self->current_template;
32             $self->reset_template();
33             $temp =~ s/\/$//;
34             $self->append_text($temp);
35         }
36         $self->$orig($array);
37     }
38     $self->rise();
39 };
40
41 sub visit_array_entry
42 {
43     # to make release-unused-vars.t happy
44     #my ($self, $elem, $index, $array) = @_;
45     my ($self, $elem) = @_;
46     $self->dive();
47     warn 'ARRAYENTRY: '. $self->current_template if DEBUG;
48     if(!ref($elem))
49     {
50         $self->append_text($elem . '/*');
51         $self->add_template($self->current_template);
52     }
53     elsif(ref($elem) eq 'HASH')
54     {
55         $self->visit($elem);
56     }
57     $self->rise();
58     $self->value_type('NONE');
59 };
60
61 around visit_hash => sub
62 {
63     my ($orig, $self, $hash) = @_;
64     $self->dive();
65     if($self->current_template =~ /\/$/)
66     {
67         my $temp = $self->current_template;
68         $self->reset_template();
69         $temp =~ s/\/$//;
70         $self->append_text($temp);
71     }
72     warn 'HASH: '. $self->current_template if DEBUG;
73     $self->$orig($hash);
74     $self->rise();
75 };
76
77 around visit_value => sub
78 {
79     my ($orig, $self, $val) = @_;
80
81     if($self->value_type eq 'NONE')
82     {
83         $self->dive();
84         $self->append_text($val . '/*');
85         $self->add_template($self->current_template);
86         warn 'VALUE: ' . $self->current_template if DEBUG;
87         $self->rise();
88     }
89     elsif($self->value_type eq 'HashKey')
90     {
91         $self->append_text($val);
92         warn 'VALUE: ' . $self->current_template if DEBUG;
93     }
94     else
95     {
96         $self->$orig($val);
97     }
98
99 };
100
101
102 __PACKAGE__->meta->make_immutable;
103
104 1;