78176441c3f70ce1dd55c0fd42e65551f41b8f70
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / Validator.pm
1 package Catalyst::Controller::DBIC::API::Validator;
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'; }
7
8 has '+visitor' => ( 'builder' => '_build_custom_visitor' );
9
10 sub _build_custom_visitor
11 {
12     return Catalyst::Controller::DBIC::API::Visitor->new();
13 }
14
15 Catalyst::Controller::DBIC::API::Validator->meta->make_immutable;
16
17 ###############################################################################
18 package Catalyst::Controller::DBIC::API::Visitor;
19
20 use Moose;
21 use namespace::autoclean;
22
23 BEGIN { extends 'Data::DPath::Validator::Visitor'; }
24
25 use constant DEBUG => $ENV{DATA_DPATH_VALIDATOR_DEBUG} || 0;
26
27 around visit_array => sub
28 {
29     my ($orig, $self, $array) = @_;
30     $self->dive();
31     warn 'ARRAY: '. $self->current_template if DEBUG;
32     if(@$array == 1 && $array->[0] eq '*')
33     {
34         $self->append_text('[reftype eq "HASH" ]');
35         $self->add_template($self->current_template);
36     }
37     else
38     {
39         if($self->current_template =~ /\/$/)
40         {
41             my $temp = $self->current_template;
42             $self->reset_template();
43             $temp =~ s/\/$//;
44             $self->append_text($temp);
45         }
46         $self->$orig($array);
47     }
48     $self->rise();
49 };
50
51 sub visit_array_entry
52 {
53     my ($self, $elem, $index, $array) = @_;
54     $self->dive();
55     warn 'ARRAYENTRY: '. $self->current_template if DEBUG;
56     if(!ref($elem))
57     {
58         $self->append_text($elem . '/*');
59         $self->add_template($self->current_template);
60     }
61     elsif(ref($elem) eq 'HASH')
62     {
63         $self->visit($elem);
64     }
65     $self->rise();
66     $self->value_type('NONE');
67 };
68
69 around visit_hash => sub
70 {
71     my ($orig, $self, $hash) = @_;
72     $self->dive();
73     if($self->current_template =~ /\/$/)
74     {
75         my $temp = $self->current_template;
76         $self->reset_template();
77         $temp =~ s/\/$//;
78         $self->append_text($temp);
79     }
80     warn 'HASH: '. $self->current_template if DEBUG;
81     $self->$orig($hash);
82     $self->rise();
83 };
84
85 around visit_value => sub
86 {
87     my ($orig, $self, $val) = @_;
88     
89     if($self->value_type eq 'NONE')
90     {
91         $self->dive();
92         $self->append_text($val . '/*');
93         $self->add_template($self->current_template);
94         warn 'VALUE: ' . $self->current_template if DEBUG;
95         $self->rise();
96     }
97     elsif($self->value_type eq 'HashKey')
98     {
99         $self->append_text($val);
100         warn 'VALUE: ' . $self->current_template if DEBUG;
101     }    
102     else
103     {
104         $self->$orig($val);
105     }
106
107 };
108
109
110 Catalyst::Controller::DBIC::API::Visitor->meta->make_immutable;
111
112 1;