« StructuredParameters/Exception/MissingParameter.pm
↑ Summary
StructuredParameters/Exception/InvalidArrayLength.pm »
| 1 | package PAGI::StructuredParameters::Request; | ||||||||||||||
| 2 | 1 | $PAGI::StructuredParameters::Request::VERSION = '0.001001'; | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use PAGI::StructuredParameters; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | # A request-bound strong-parameters object. Because reading a PAGI request body | ||||||||||||||
| 9 | # is asynchronous, the terminal permitted/required are async: they materialize | ||||||||||||||
| 10 | # the source hash from the request, build a (synchronous) PAGI::StructuredParameters | ||||||||||||||
| 11 | # engine, and delegate. The configuration methods are synchronous and chainable, | ||||||||||||||
| 12 | # so the idiom reads: await $obj->namespace(['person'])->permitted(@rules) | ||||||||||||||
| 13 | |||||||||||||||
| 14 | 4 | ▶ | sub new { my ($class, %args) = @_; | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 15 | return bless { | ||||||||||||||
| 16 | request => $args{request}, | ||||||||||||||
| 17 | kind => $args{kind}, # 'body' | 'query' | 'data' | ||||||||||||||
| 18 | context => $args{context}, | ||||||||||||||
| 19 | 4 | namespace => undef, | |||||||||||||
| 20 | flatten => undef, # override of the kind default | ||||||||||||||
| 21 | max_array_depth => undef, | ||||||||||||||
| 22 | }, $class; | ||||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 0 | ▶ | sub namespace { my ($self, $arg) = @_; | ||||||||||||
|
Subroutine
namespace: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 26 | 0 | ▶ | $self->{namespace} = $arg if defined $arg; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | 0 | return $self; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 0 | ▶ | sub flatten_array_value { my ($self, $arg) = @_; | ||||||||||||
|
Subroutine
flatten_array_value: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 31 | 0 | ▶ | $self->{flatten} = $arg if defined $arg; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 0 | return $self; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | ▶ | sub max_array_depth { my ($self, $arg) = @_; | ||||||||||||
|
Subroutine
max_array_depth: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 36 | 0 | ▶ | $self->{max_array_depth} = $arg if defined $arg; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 0 | return $self; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 3 | ▶ | async sub permitted { my ($self, @rules) = @_; | ||||||||||||
|
Subroutine
permitted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 41 | 3 | my $engine = await $self->_engine; | |||||||||||||
| 42 | 3 | return $engine->permitted(@rules); | |||||||||||||
| 43 | 1 | } | |||||||||||||
| 44 | |||||||||||||||
| 45 | 1 | ▶ | async sub required { my ($self, @rules) = @_; | ||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 46 | 1 | my $engine = await $self->_engine; | |||||||||||||
| 47 | 1 | return $engine->required(@rules); | |||||||||||||
| 48 | 1 | } | |||||||||||||
| 49 | |||||||||||||||
| 50 | 4 | ▶ | async sub _engine { my ($self) = @_; | ||||||||||||
|
Subroutine
_engine: called CC=6 SCAR=18.1
| |||||||||||||||
| 51 | 4 | my $req = $self->{request}; | |||||||||||||
| 52 | 4 | my $kind = $self->{kind}; | |||||||||||||
| 53 | |||||||||||||||
| 54 | 4 | ▶ | my $src_data = | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 55 | $kind eq 'data' ? (await $req->json) | ||||||||||||||
| 56 | : $kind eq 'query' ? $req->query_params->mixed | ||||||||||||||
| 57 | : (await $req->form_params)->mixed; | ||||||||||||||
| 58 | |||||||||||||||
| 59 | my %args = ( | ||||||||||||||
| 60 | src => $kind, | ||||||||||||||
| 61 | src_data => $src_data, | ||||||||||||||
| 62 | context => $self->{context}, | ||||||||||||||
| 63 | 4 | ); | |||||||||||||
| 64 | 4 | ▶ | $args{namespace} = $self->{namespace} if defined $self->{namespace}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 4 | ▶ | $args{flatten_array_value} = $self->{flatten} if defined $self->{flatten}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 4 | ▶ | $args{max_array_depth} = $self->{max_array_depth} if defined $self->{max_array_depth}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | |||||||||||||||
| 68 | 4 | return PAGI::StructuredParameters->new(%args); | |||||||||||||
| 69 | 1 | } | |||||||||||||
| 70 | |||||||||||||||
| 71 | 1 | 1; | |||||||||||||
| 72 | |||||||||||||||
| 73 | =encoding utf8 | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =head1 NAME | ||||||||||||||
| 76 | |||||||||||||||
| 77 | PAGI::StructuredParameters::Request - Request-bound, asynchronous strong parameters | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =head1 SYNOPSIS | ||||||||||||||
| 80 | |||||||||||||||
| 81 | use PAGI::StructuredParameters; | ||||||||||||||
| 82 | |||||||||||||||
| 83 | my $clean = await PAGI::StructuredParameters->from_body($req) | ||||||||||||||
| 84 | ->permitted('username', name => ['first', 'last'], +{ email => [] }); | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =head1 DESCRIPTION | ||||||||||||||
| 87 | |||||||||||||||
| 88 | Binds the L<PAGI::StructuredParameters> engine to a live L<PAGI::Request>. | ||||||||||||||
| 89 | Because reading a PAGI request body is asynchronous, the terminal L</permitted> | ||||||||||||||
| 90 | and L</required> methods are asynchronous: each materializes the source hash from | ||||||||||||||
| 91 | the request (awaiting the body for C<body>/C<data>; the query string is read | ||||||||||||||
| 92 | synchronously), constructs a synchronous engine, and delegates. | ||||||||||||||
| 93 | |||||||||||||||
| 94 | You normally obtain one of these from L<PAGI::StructuredParameters/from_body>, | ||||||||||||||
| 95 | L<PAGI::StructuredParameters/from_query>, L<PAGI::StructuredParameters/from_data>, | ||||||||||||||
| 96 | or — in L<PAGI::Nano> — from C<< $c->params >>. | ||||||||||||||
| 97 | |||||||||||||||
| 98 | =head1 CONFIGURATION METHODS | ||||||||||||||
| 99 | |||||||||||||||
| 100 | These are synchronous and return the object for chaining before the terminal | ||||||||||||||
| 101 | call. | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =head2 namespace | ||||||||||||||
| 104 | |||||||||||||||
| 105 | $obj->namespace(['person']); | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head2 flatten_array_value | ||||||||||||||
| 108 | |||||||||||||||
| 109 | $obj->flatten_array_value(0); | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head2 max_array_depth | ||||||||||||||
| 112 | |||||||||||||||
| 113 | $obj->max_array_depth(50); | ||||||||||||||
| 114 | |||||||||||||||
| 115 | See L<PAGI::StructuredParameters> for the meaning of each. | ||||||||||||||
| 116 | |||||||||||||||
| 117 | =head1 TERMINAL METHODS | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =head2 permitted | ||||||||||||||
| 120 | |||||||||||||||
| 121 | my $clean = await $obj->permitted(@rules); | ||||||||||||||
| 122 | |||||||||||||||
| 123 | Asynchronously materializes the request source and applies | ||||||||||||||
| 124 | L<PAGI::StructuredParameters/permitted>. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =head2 required | ||||||||||||||
| 127 | |||||||||||||||
| 128 | my $clean = await $obj->required(@rules, $on_missing); | ||||||||||||||
| 129 | |||||||||||||||
| 130 | Asynchronously materializes the request source and applies | ||||||||||||||
| 131 | L<PAGI::StructuredParameters/required>. A missing required key throws the | ||||||||||||||
| 132 | callback's return value through the C<await>. | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =head1 AUTHOR | ||||||||||||||
| 135 | |||||||||||||||
| 136 | John Napiorkowski C<< <jjnapiork@cpan.org> >> | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 139 | |||||||||||||||
| 140 | Copyright 2026, John Napiorkowski. This library is free software; you can | ||||||||||||||
| 141 | redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =cut | ||||||||||||||