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