StructuredParameters.pm

filter: Statementstmt 92.3% 157 / 170 Branchbran 81.4% 70 / 86 Conditioncond 62.5% 35 / 56 MC/DCmcdc 33.3% 9 / 27 Subroutinesub 95.6% 22 / 23 Podpod 100% 9 / 9 totaltotal 81.4% 302 / 371 CC 74 SCAR 30.8
CC
74
Cov
84%
_parse_data
42.4
_parse_formlike
28.5
new
7.3
CRAP 21.8
↑ Summary StructuredParameters/Exception/MissingParameter.pm »
1 package PAGI::StructuredParameters;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use PAGI::StructuredParameters::Exception::MissingParameter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use PAGI::StructuredParameters::Exception::InvalidArrayLength;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use PAGI::StructuredParameters::Exception::InvalidArrayPointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.001001';
12  
13 5 our $MAX_ARRAY_DEPTH = 1000;
14  
15 # A no-dependency port of the core of Catalyst::Utils::StructuredParameters:
16 # given a flat form/query hash (keys encode nesting: name.first, email[0],
17 # children[0].age) or already-nested body data, it whitelists and reconstructs
18 # only the rule-described shape. Validation of *values* is out of scope.
19  
20 25 ▶sub new { my ($class, %args) = @_;
Subroutine new: called CC=6 SCAR=19.9
Pod documented
21 25 ▶ my $src = $args{src} // 'body';
Condition50%1/2
$args{"src"} // "body"
l!l
250
MC/DC0%0/1
$args{"src"} // "body"
$args{"src"}
Truth table50%1/2
A
A= $args{"src"}
Aresult
11
00
22 my $flatten = exists $args{flatten_array_value}
23 ? $args{flatten_array_value}
24 25 ▶ : ($src eq 'data' ? 0 : 1);
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'flatten_array_value'} ? : 0 25 25
$src eq 'data' ? : 6 19 25
25  
26 return bless {
27 src => $src,
28 src_data => $args{src_data} // {},
29 namespace => $args{namespace},
30 flatten_array_value => $flatten,
31 max_array_depth => $args{max_array_depth} // $MAX_ARRAY_DEPTH,
32 context => $args{context},
33 25 ▶ }, $class;
Condition40%2/5
$args{"src_data"} // {}
l!l
250
$args{"max_array_depth"} // $PAGI::StructuredParameters::MAX_ARRAY_DEPTH
l!l&&r!l&&!r
0250
MC/DC0%0/1
$args{"src_data"} // {}
$args{"src_data"}
MC/DC0%0/2
$args{"max_array_depth"} // $PAGI::StructuredParameters::MAX_ARRAY_DEPTH
$args{"max_array_depth"} $PAGI::StructuredParameters::MAX_ARRAY_DEPTH
Truth table50%1/2
A
A= $args{"src_data"}
Aresult
11
00
Truth table33%1/3
A // B
A= $args{"max_array_depth"}B= $PAGI::StructuredParameters::MAX_ARRAY_DEPTH
ABresult
1X1
011
000
34 }
35  
36 # --- request adapters -------------------------------------------------------
37  
38 2 ▶sub from_body { my ($class, $req, $context) = @_;
Subroutine from_body: called CC=1 SCAR=0.0
Pod documented
39 2 require PAGI::StructuredParameters::Request;
40 2 return PAGI::StructuredParameters::Request->new(
41 request => $req, kind => 'body', context => $context,
42 );
43 }
44  
45 1 ▶sub from_query { my ($class, $req, $context) = @_;
Subroutine from_query: called CC=1 SCAR=0.0
Pod documented
46 1 require PAGI::StructuredParameters::Request;
47 1 return PAGI::StructuredParameters::Request->new(
48 request => $req, kind => 'query', context => $context,
49 );
50 }
51  
52 1 ▶sub from_data { my ($class, $req, $context) = @_;
Subroutine from_data: called CC=1 SCAR=0.0
Pod documented
53 1 require PAGI::StructuredParameters::Request;
54 1 return PAGI::StructuredParameters::Request->new(
55 request => $req, kind => 'data', context => $context,
56 );
57 }
58  
59 # --- configuration (chainable) ---------------------------------------------
60  
61 2 ▶sub namespace { my ($self, $arg) = @_;
Subroutine namespace: called CC=2 SCAR=7.1
Pod documented
62 2 ▶ $self->{namespace} = $arg if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 2 0 2
63 2 return $self;
64 }
65  
66 1 ▶sub flatten_array_value { my ($self, $arg) = @_;
Subroutine flatten_array_value: called CC=2 SCAR=7.1
Pod documented
67 1 ▶ $self->{flatten_array_value} = $arg if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 1 0 1
68 1 return $self;
69 }
70  
71 0 ▶sub max_array_depth { my ($self, $arg) = @_;
Subroutine max_array_depth: not called CC=2 SCAR=17.9
Pod documented
72 0 ▶ $self->{max_array_depth} = $arg if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 0 0
73 0 return $self;
74 }
75  
76 # --- terminal whitelisting --------------------------------------------------
77  
78 18 ▶sub permitted { my ($self, @proto) = @_;
Subroutine permitted: called CC=1 SCAR=0.0
Pod documented
79 18 my ($ns, $rules) = $self->_namespace_and_rules(@proto);
80 18 return $self->_parse(Storable::dclone($self->{src_data}), $ns, $rules, 0);
81 }
82  
83 7 ▶sub required { my ($self, @proto) = @_;
Subroutine required: called CC=3 SCAR=11.0
Pod documented
84 7 my $on_missing = pop @proto;
85 7 ▶ Carp::croak('required needs an on-missing callback')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_missing eq "CODE" 1 6 7
86 unless ref($on_missing) eq 'CODE';
87  
88 6 local $self->{_missing} = [];
89 6 my ($ns, $rules) = $self->_namespace_and_rules(@proto);
90 6 my $parsed = $self->_parse(Storable::dclone($self->{src_data}), $ns, $rules, 1);
91  
92 6 ▶ if (@{$self->{_missing}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_missing"};}) 5 1 6
93 5 die $on_missing->($self->{context}, $self->{_missing});
94 }
95 1 return $parsed;
96 }
97  
98 # Pull a leading \@namespace affix off the rule list, if present, and combine it
99 # with any namespace set via ->namespace. A leading *arrayref* is unambiguously a
100 # namespace (top-level rules are only scalars or hashrefs).
101 24 ▶sub _namespace_and_rules { my ($self, @proto) = @_;
Subroutine _namespace_and_rules: called CC=4 SCAR=13.9
102 24 ▶ my $ns = $self->{namespace} // [];
Condition100%2/2
$self->{"namespace"} // []
l!l
222
MC/DC100%1/1
$self->{"namespace"} // []
$self->{"namespace"}
Truth table100%2/2
A
A= $self->{"namespace"}
Aresult
11
00
103 24 ▶ if (@proto && ref($proto[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@proto and ref $proto[0] eq "ARRAY") 1 23 24
Condition66%2/3
@proto and ref $proto[0] eq "ARRAY"
!ll&&!rl&&r
0231
MC/DC50%1/2
@proto and ref $proto[0] eq "ARRAY"
@proto ref $proto[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @protoB= ref $proto[0] eq "ARRAY"
ABresult
0X0
100
111
104 1 my $affix = shift @proto;
105 1 $ns = [@$ns, @$affix];
106 }
107 24 return ($ns, [@proto]);
108 }
109  
110 50 ▶sub _normalize_array_value { my ($self, $value) = @_;
Subroutine _normalize_array_value: called CC=4 SCAR=13.9
111 50 ▶ return $value unless $self->{flatten_array_value};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"flatten_array_value"} 17 33 50
112 33 ▶ return ((ref($value) || '') eq 'ARRAY') ? $value->[-1] : $value;
Branch: 1 branch
BranchTrueFalseTotal
(ref $value || '') eq 'ARRAY' ? : 1 32 33
Condition100%2/2
ref $value || ''
l!l
132
MC/DC100%1/1
ref $value || ''
ref $value
Truth table100%2/2
A
A= ref $value
Aresult
11
00
113 }
114  
115 # Empty final index ('') sorts last; numeric indexes sort numerically.
116 6 ▶sub _sort_indexes { my (@indexes) = @_;
Subroutine _sort_indexes: called CC=3 SCAR=11.0
117 return sort {
118 6 ▶ $a eq '' ? 1 : $b eq '' ? -1 : $a <=> $b
Branch: 2 branches
BranchTrueFalseTotal
$a eq '' ? : 1 6 7
$b eq '' ? : 1 5 6
119 } @indexes;
120 }
121  
122 24 ▶sub _parse { my ($self, $context, $ns, $rules, $required) = @_;
Subroutine _parse: called CC=2 SCAR=6.9
123 24 ▶ return $self->{src} eq 'data'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'src'} eq 'data' ? : 6 18 24
124 ? $self->_parse_data($context, $ns, $rules, $required)
125 : $self->_parse_formlike($context, $ns, $rules, $required);
126 }
127  
128 34 ▶sub _parse_formlike { my ($self, $context, $ns, $rules, $required) = @_;
Subroutine _parse_formlike: called CC=27 SCAR=33.5
129 34 my $current = {};
130  
131 34 ▶ while (@$rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rules) 56 34 90
132 56 my $rule = shift @$rules;
133  
134 56 ▶ if ((ref($rule) || '') eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ((ref $rule || '') eq 'HASH') { } 7 49 56
elsif ((ref $rules->[0] || '') eq 'ARRAY') { } 4 45 49
elsif ($rule eq '') { } 5 40 45
Condition100%2/2
ref $rule || ''
l!l
749
MC/DC100%1/1
ref $rule || ''
ref $rule
Truth table100%2/2
A
A= ref $rule
Aresult
11
00
135 # +{ key => [...] } : an array, or an array of hashes
136 7 my ($local_ns, $sub_rules) = %$rule;
137 7 my $key = join('.', @$ns, $local_ns);
138  
139 7 my %indexes;
140 7 for my $field (keys %$context) {
141 26 my ($i, $under) = ($field =~ m/^\Q$key\E\[(\d*)\]\.?(.*)$/);
142 26 ▶ next unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 9 17 26
143 17 $indexes{$i} = $under;
144 }
145  
146 7 my $found = scalar keys %indexes;
147 PAGI::StructuredParameters::Exception::InvalidArrayLength->throw(
148 pointer => $local_ns,
149 max => $self->{max_array_depth},
150 attempted => $found,
151 7 ▶ ) if $found > $self->{max_array_depth};
Branch: 1 branch
BranchTrueFalseTotal
if $found > $self->{"max_array_depth"} 0 7 7
152  
153 7 ▶ unless (%indexes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%indexes) 1 6 7
154 # The array key is wholly absent from the input. With no indexes
155 # the index loop never runs, so report the key missing here (a
156 # no-op for permitted, where $required is false).
157 1 $self->_record_missing($key, $required);
158 1 next;
159 }
160  
161 6 for my $index (_sort_indexes(keys %indexes)) {
162 12 my $cloned = Storable::dclone($sub_rules);
163 12 ▶ $cloned = [''] unless @$cloned; # bare array case: +{ email => [] }
Branch: 1 branch
BranchTrueFalseTotal
unless @$cloned 5 7 12
164  
165 12 my $old = $self->{flatten_array_value};
166 12 ▶ $self->{flatten_array_value} = 0 if $index eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $index eq "" 2 10 12
167 12 my $value = $self->_parse_formlike(
168 $context, [@$ns, "${local_ns}[$index]"], $cloned, $required,
169 );
170  
171 12 ▶ if ($index eq '' && $old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index eq '' and $old) { } 2 10 12
Condition66%2/3
$index eq "" and $old
!ll&&!rl&&r
1002
MC/DC50%1/2
$index eq "" and $old
$index eq "" $old
Truth table66%2/3
A and B
A= $index eq ""B= $old
ABresult
0X0
100
111
172 2 $self->{flatten_array_value} = $old;
173 2 ▶ if ((ref($value) || '') eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ((ref $value || '') eq 'ARRAY') { } 1 1 2
elsif ((ref $value || '') eq 'HASH') { } 1 0 1
Condition50%1/2
ref $value || ''
l!l
20
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
174 1 push @{$current->{$local_ns}}, @$value;
175 }
176 ▶ elsif ((ref($value) || '') eq 'HASH') {
Condition50%1/2
ref $value || ''
l!l
10
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
177 1 ▶ if (ref $value->{$indexes{$index}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$value{$indexes{$index}}) { } 1 0 1
178 1 push @{$current->{$local_ns}},
179 2 map { +{ $indexes{$index} => $_ } }
180 1 @{$value->{$indexes{$index}}};
181 }
182 else {
183 0 push @{$current->{$local_ns}}, $value;
184 }
185 }
186 }
187 else {
188 # A row with only invalid (unlisted) fields is not "missing".
189 10 ▶ next if (ref($value) || '') eq 'HASH' && !%$value;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value || "") eq "HASH" and not %$value 0 10 10
Condition80%4/5
(ref $value || "") eq "HASH" and not %$value
!ll&&!rl&&r
460
ref $value || ''
l!l
64
MC/DC0%0/2
(ref $value || "") eq "HASH" and not %$value
ref $value not %$value
Truth table66%2/3
A and not(B)
A= ref $valueB= not %$value
ABresult
0X0
100
111
190 10 push @{$current->{$local_ns}}, $value;
191 }
192 }
193 }
194 ▶ elsif ((ref($rules->[0]) || '') eq 'ARRAY') {
Condition100%2/2
ref $rules->[0] || ''
l!l
544
MC/DC100%1/1
ref $rules->[0] || ''
ref $rules->[0]
Truth table100%2/2
A
A= ref $rules->[0]
Aresult
11
00
195 # scalar key followed by \@subkeys : a nested hash
196 4 my $value = $self->_parse_formlike(
197 $context, [@$ns, $rule], shift(@$rules), $required,
198 );
199 4 ▶ next unless %$value; # lenient: drop empty nested hash
Branch: 1 branch
BranchTrueFalseTotal
unless %$value 1 3 4
200 3 $current->{$rule} = $value;
201 }
202 elsif ($rule eq '') {
203 # bare leaf at the current namespace (used by array reconstruction)
204 5 my $key = join('.', @$ns);
205 5 ▶ unless (defined $context->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context->{$key}) 0 5 5
206 0 $self->_record_missing($key, $required);
207 0 next;
208 }
209 5 $current = $self->_normalize_array_value($context->{$key});
210 }
211 else {
212 # plain scalar key
213 40 my $key = join('.', @$ns, $rule);
214 40 ▶ unless (defined $context->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context->{$key}) 9 31 40
215 9 $self->_record_missing($key, $required);
216 9 next;
217 }
218 31 $current->{$rule} = $self->_normalize_array_value($context->{$key});
219 }
220 }
221  
222 34 return $current;
223 }
224  
225 14 ▶sub _parse_data { my ($self, $context, $ns, $rules, $required) = @_;
Subroutine _parse_data: called CC=28 SCAR=37.5
226 14 my $current = {};
227  
228 14 ▶ RULE: while (@$rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rules) 20 13 33
229 20 my $rule = shift @$rules;
230  
231 20 ▶ if ((ref($rule) || '') eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ((ref $rule || '') eq 'HASH') { } 4 16 20
elsif ((ref $rules->[0] || '') eq 'ARRAY') { } 1 15 16
elsif ($rule eq '') { } 5 10 15
Condition100%2/2
ref $rule || ''
l!l
416
MC/DC100%1/1
ref $rule || ''
ref $rule
Truth table100%2/2
A
A= ref $rule
Aresult
11
00
232 # +{ key => [...] } : the value at this pointer must be an array
233 4 my ($local_ns, $sub_rules) = %$rule;
234  
235 4 my $value = $context;
236 4 for my $pointer (@$ns, $local_ns) {
237 4 ▶ if ((ref($value) || '') eq 'HASH' && exists $value->{$pointer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $value || '') eq 'HASH' and exists $value->{$pointer}) { } 4 0 4
Condition40%2/5
(ref $value || "") eq "HASH" and exists $value->{$pointer}
!ll&&!rl&&r
004
ref $value || ''
l!l
40
MC/DC0%0/2
(ref $value || "") eq "HASH" and exists $value->{$pointer}
ref $value exists $value->{$pointer}
Truth table33%1/3
A and B
A= ref $valueB= exists $value->{$pointer}
ABresult
0X0
100
111
238 4 $value = $value->{$pointer};
239 }
240 else {
241 0 $self->_record_missing(join('.', @$ns, $local_ns), $required);
242 0 next RULE;
243 }
244 }
245  
246 PAGI::StructuredParameters::Exception::InvalidArrayPointer->throw(
247 4 ▶ pointer => join('.', @$ns, $local_ns),
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value || "") eq "ARRAY" 1 3 4
Condition50%1/2
ref $value || ''
l!l
40
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
248 ) unless (ref($value) || '') eq 'ARRAY';
249  
250 3 my $found = scalar @$value;
251 PAGI::StructuredParameters::Exception::InvalidArrayLength->throw(
252 pointer => $local_ns,
253 max => $self->{max_array_depth},
254 attempted => $found,
255 3 ▶ ) if $found > $self->{max_array_depth};
Branch: 1 branch
BranchTrueFalseTotal
if $found > $self->{"max_array_depth"} 0 3 3
256  
257 3 my @gathered;
258 3 for my $item (@$value) {
259 7 my $cloned = Storable::dclone($sub_rules);
260 7 ▶ $cloned = [''] unless @$cloned; # bare array case
Branch: 1 branch
BranchTrueFalseTotal
unless @$cloned 5 2 7
261 7 my $parsed = $self->_parse_data($item, [], $cloned, $required);
262 7 ▶ next if (ref($parsed) || '') eq 'HASH' && !%$parsed;
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parsed || "") eq "HASH" and not %$parsed 0 7 7
Condition80%4/5
(ref $parsed || "") eq "HASH" and not %$parsed
!ll&&!rl&&r
520
ref $parsed || ''
l!l
25
MC/DC0%0/2
(ref $parsed || "") eq "HASH" and not %$parsed
ref $parsed not %$parsed
Truth table66%2/3
A and not(B)
A= ref $parsedB= not %$parsed
ABresult
0X0
100
111
263 7 push @gathered, $parsed;
264 }
265 3 $current->{$local_ns} = \@gathered;
266 }
267 ▶ elsif ((ref($rules->[0]) || '') eq 'ARRAY') {
Condition100%2/2
ref $rules->[0] || ''
l!l
214
MC/DC100%1/1
ref $rules->[0] || ''
ref $rules->[0]
Truth table100%2/2
A
A= ref $rules->[0]
Aresult
11
00
268 # scalar key followed by \@subkeys : a nested hash
269 1 my $value = $self->_parse_data(
270 $context, [@$ns, $rule], shift(@$rules), $required,
271 );
272 1 ▶ next unless %$value;
Branch: 1 branch
BranchTrueFalseTotal
unless %$value 0 1 1
273 1 $current->{$rule} = $value;
274 }
275 elsif ($rule eq '') {
276 5 my $value = $context;
277 5 for my $pointer (@$ns) {
278 0 ▶ if ((ref($value) || '') eq 'HASH' && exists $value->{$pointer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $value || '') eq 'HASH' and exists $value->{$pointer}) { } 0 0 0
Condition0%0/5
(ref $value || "") eq "HASH" and exists $value->{$pointer}
!ll&&!rl&&r
000
ref $value || ''
l!l
00
MC/DC0%0/2
(ref $value || "") eq "HASH" and exists $value->{$pointer}
ref $value exists $value->{$pointer}
Truth table0%0/3
A and B
A= ref $valueB= exists $value->{$pointer}
ABresult
0X0
100
111
279 0 $value = $value->{$pointer};
280 }
281 else {
282 0 $self->_record_missing(join('.', @$ns, $rule), $required);
283 0 next RULE;
284 }
285 }
286 5 $current = $self->_normalize_array_value($value);
287 }
288 else {
289 10 my $value = $context;
290 10 for my $pointer (@$ns, $rule) {
291 12 ▶ if ((ref($value) || '') eq 'HASH' && exists $value->{$pointer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $value || '') eq 'HASH' and exists $value->{$pointer}) { } 11 1 12
Condition60%3/5
(ref $value || "") eq "HASH" and exists $value->{$pointer}
!ll&&!rl&&r
0111
ref $value || ''
l!l
120
MC/DC50%1/2
(ref $value || "") eq "HASH" and exists $value->{$pointer}
ref $value exists $value->{$pointer}
Truth table66%2/3
A and B
A= ref $valueB= exists $value->{$pointer}
ABresult
0X0
100
111
292 11 $value = $value->{$pointer};
293 }
294 else {
295 1 $self->_record_missing(join('.', @$ns, $rule), $required);
296 1 next RULE;
297 }
298 }
299 9 $current->{$rule} = $self->_normalize_array_value($value);
300 }
301 }
302  
303 13 return $current;
304 }
305  
306 11 ▶sub _record_missing { my ($self, $key, $required) = @_;
Subroutine _record_missing: called CC=2 SCAR=6.9
307 11 ▶ return unless $required;
Branch: 1 branch
BranchTrueFalseTotal
unless $required 4 7 11
308 7 push @{$self->{_missing}}, $key;
309 }
310  
311 5 1;
312  
313 =encoding utf8
314  
315 =head1 NAME
316  
317 PAGI::StructuredParameters - Whitelist and structure incoming request parameters
318  
319 =head1 SYNOPSIS
320  
321 use PAGI::StructuredParameters;
322  
323 my $params = PAGI::StructuredParameters->new(
324 src => 'body',
325 src_data => {
326 username => 'jnap',
327 'name.first' => 'John',
328 'name.last' => 'Napiorkowski',
329 'email[0]' => 'jnap@example.com',
330 },
331 );
332  
333 my $clean = $params->permitted(
334 'username',
335 name => ['first', 'last'],
336 +{ email => [] },
337 );
338  
339 # $clean = {
340 # username => 'jnap',
341 # name => { first => 'John', last => 'Napiorkowski' },
342 # email => ['jnap@example.com'],
343 # }
344  
345 =head1 DESCRIPTION
346  
347 A no-dependency port of the core of
348 L<Catalyst::Utils::StructuredParameters>, decoupled from Catalyst. Its job is to
349 B<whitelist and structure> incoming parameters I<before> they reach a model: a
350 prior layer to validation, not validation itself. Validation of values is out of
351 scope (use a downstream validator such as Valiant).
352  
353 Form and query parameters arrive as a flat hash whose keys encode nesting
354 (C<name.first>, C<email[0]>, C<children[0].age>). The rule grammar reconstructs
355 the named, present keys into the described nested shape and drops everything
356 else. Already-nested body data (such as decoded JSON) is simply whitelisted.
357  
358 =head1 CONSTRUCTOR
359  
360 =head2 new
361  
362 my $params = PAGI::StructuredParameters->new(
363 src => 'body', # 'body' | 'query' | 'data'
364 src_data => \%params, # the source hash to filter
365 namespace => \@fields, # optional starting namespace
366 flatten_array_value => $bool, # default: 1 for body/query, 0 for data
367 max_array_depth => 1000, # cap on array reconstruction
368 context => $obj, # optional; passed to required's callback
369 );
370  
371 C<src_data> is the materialized source hash. For request-bound, asynchronous
372 sources (a POST body), use the adapters in
373 L<PAGI::StructuredParameters::Request> instead.
374  
375 =head1 REQUEST ADAPTERS
376  
377 These class methods bind the engine to a live L<PAGI::Request>, returning a
378 L<PAGI::StructuredParameters::Request> whose C<permitted>/C<required> are
379 asynchronous (the request body is read with C<await>).
380  
381 =head2 from_body
382  
383 my $params = PAGI::StructuredParameters->from_body($req, $context);
384  
385 Source is the request's form/body parameters (array values flattened by default).
386  
387 =head2 from_query
388  
389 my $params = PAGI::StructuredParameters->from_query($req, $context);
390  
391 Source is the URI query string (array values flattened by default).
392  
393 =head2 from_data
394  
395 my $params = PAGI::StructuredParameters->from_data($req, $context);
396  
397 Source is the decoded request body data such as JSON (arrays kept as-is).
398  
399 The optional C<$context> is passed as the first argument to C<required>'s
400 on-missing callback.
401  
402 =head1 RULE SPECIFICATIONS
403  
404 =over 4
405  
406 =item C<'username', 'password'> — scalar keys.
407  
408 =item C<< name => ['first', 'last'] >> — a nested hash, reconstructed from
409 C<name.first> / C<name.last>.
410  
411 =item C<< +{ email => [] } >> — an array, reconstructed from C<email[0]>,
412 C<email[1]>, ...
413  
414 =item C<< +{ children => [['name','age']] } >> — an array of hashes,
415 reconstructed from C<children[0].name>, C<children[1].age>, ...
416  
417 =back
418  
419 A leading arrayref is treated as a starting namespace affix, e.g.
420 C<< $params->permitted(['person'], 'name', 'age') >>.
421  
422 =head1 METHODS
423  
424 =head2 namespace
425  
426 $params->namespace(['person']);
427  
428 Scope all subsequent rules under the given key path. Returns the object for
429 chaining.
430  
431 =head2 flatten_array_value
432  
433 $params->flatten_array_value(0);
434  
435 Toggle array-value flattening ("pick last"), a common workaround for HTML form
436 controls that submit repeated values. On by default for C<body>/C<query>, off
437 for C<data>. Returns the object for chaining.
438  
439 =head2 max_array_depth
440  
441 $params->max_array_depth(50);
442  
443 Cap the number of items reconstructed for any single array, guarding against
444 hostile input. Throws
445 L<PAGI::StructuredParameters::Exception::InvalidArrayLength> when exceeded.
446 Returns the object for chaining.
447  
448 =head2 permitted
449  
450 my $clean = $params->permitted(@rules);
451  
452 Lenient whitelist. Returns a clean hashref containing only the permitted, present
453 keys, reconstructed into the shape the rules describe. Absent keys are simply
454 omitted.
455  
456 =head2 required
457  
458 my $clean = $params->required(@rules, $on_missing);
459  
460 Strict whitelist. The trailing argument B<must> be a coderef (otherwise a loud
461 C<required needs an on-missing callback> error is raised at the call site).
462  
463 On success, returns the clean hashref, exactly as L</permitted> would. If any
464 required key is absent, C<$on_missing> is invoked as
465 C<< $on_missing->($context, \@missing) >> — where C<$context> is the object's
466 C<context> attribute (in L<PAGI::Nano> this is the request context C<$c>) and
467 C<\@missing> is an arrayref of every missing dotted key path — and its return
468 value is thrown. PAGI::Nano's dispatch catches that thrown, respond-able value
469 and sends it, making the failure response explicit and chosen at the call site.
470  
471 Passing the context as an argument (rather than closing over it) lets the
472 callback be a shared, named sub reused across routes.
473  
474 =head1 SEE ALSO
475  
476 L<PAGI::StructuredParameters::Request>, L<PAGI::Nano>,
477 L<Catalyst::TraitFor::Request::StructuredParameters>.
478  
479 =head1 AUTHOR
480  
481 John Napiorkowski C<< <jjnapiork@cpan.org> >>
482  
483 =head1 COPYRIGHT & LICENSE
484  
485 Copyright 2026, John Napiorkowski.
486  
487 This library is free software; you can redistribute it and/or modify it under
488 the same terms as Perl itself.
489  
490 =cut
↑ Summary StructuredParameters/Exception/MissingParameter.pm »