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