Version 2.002003
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / Validator.pm
CommitLineData
d2739840 1package Catalyst::Controller::DBIC::API::Validator;
2#ABSTRACT: Provides validation services for inbound requests against whitelisted parameters
3use Moose;
4use namespace::autoclean;
5
6BEGIN { extends 'Data::DPath::Validator'; }
7
8has '+visitor' => ( 'builder' => '_build_custom_visitor' );
9
10sub _build_custom_visitor
11{
12 return Catalyst::Controller::DBIC::API::Visitor->new();
13}
14
15Catalyst::Controller::DBIC::API::Validator->meta->make_immutable;
16
17###############################################################################
18package Catalyst::Controller::DBIC::API::Visitor;
19
20use Moose;
21use namespace::autoclean;
22
23BEGIN { extends 'Data::DPath::Validator::Visitor'; }
24
25use constant DEBUG => $ENV{DATA_DPATH_VALIDATOR_DEBUG} || 0;
26
27around 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
51sub 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
69around 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
85around visit_value => sub
86{
87 my ($orig, $self, $val) = @_;
406086f3 88
d2739840 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;
406086f3 101 }
d2739840 102 else
103 {
104 $self->$orig($val);
105 }
106
107};
108
109
110Catalyst::Controller::DBIC::API::Visitor->meta->make_immutable;
111
1121;