| 1 | package PAGI::FastAPI::TypedPath; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = qv('v1.7.3'); | |||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use PAGI::FastAPI::Depends qw(Depends); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | our @EXPORT_OK = qw(TypedPath); | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Path parameters are captured via regex ([^/]+) and land in Context as | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # plain strings, unconditionally, there is no coercion step at the | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # routing layer itself. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # What per-route 'dependencies' DOES already give us is a genuine extension | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # point, executed in order against $c before the handler runs, using this | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # exact contract (read directly from PAGI::FastAPI's own | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # dependency-execution loop): | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # my $dep_res = await $dep->{code}->($c); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # if ($c->status >= 400) { return $dep_res // {...}; } # short-circuits | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # if (defined $dep->{key} && defined $dep_res) { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # $c->stash->{$dep->{key}} = $dep_res; # otherwise stashed | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # TypedPath() builds a Depends()-compatible coderef against that exact | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # contract: on failure it sets $c->status(422) and returns a body (which | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # the dependency loop above will use verbatim, short-circuiting the | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # handler), on success it returns the COERCED value, so if you give it a | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # `key`, the typed value lands in $c->stash->{$key} for your handler to | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # read, rather than making you re-parse $c->path_param() yourself. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # TypedPath($param_name, $type) -> a coderef of async sub ($c) {...}, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # meant to be passed to Depends(), e.g.: | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # dependencies => [ Depends(TypedPath('item_id', Int), key => 'item_id') ] | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # $type must be a Type::Tiny object/constraint (the same convention core | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # uses for 'query'/'body'). | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | sub TypedPath ($param_name, $type) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TypedPath: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | ▶ | return async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | my $raw = $c->path_param($param_name); | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | if (my $err = $type->validate($raw)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | $c->status(422); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | return { detail => "Path parameter '$param_name' invalid: $err" }; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Coerce if the type supports it (e.g. Types::Standard's Int | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # doesn't numify a string automatically on ->validate, validate | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # only checks, it doesn't transform). If the type is coercible | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # (has_coercion) and provides ->coerce, use the coerced value; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # otherwise return the raw (already-validated) string as-is. This | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # means plain Types::Standard::Int will validate that "42" LOOKS | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # like an integer but still hand back the string "42", not the | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # number 42, pass a coercing type (e.g. Type::Tiny's ->plus_coercions | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # or Types::Standard::Int->coercibly) if you need a real Perl | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # integer, not just a validated string. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | ▶ | if ($type->can('has_coercion') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$type->can("has_coercion") and $type->has_coercion and $type->can("coerce")
$type->can("has_coercion") and $type->has_coercion
MC/DC33%1/3
$type->can("has_coercion") and $type->has_coercion and $type->can("coerce")
$type->can("has_coercion")
$type->has_coercion
$type->can("coerce")
Truth table50%2/4
A and B and C
A= $type->can("has_coercion")B= $type->has_coercionC= $type->can("coerce")
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | && $type->has_coercion | ||||||||||||||||||||||||||||||||||||||||||
| 69 | && $type->can('coerce')) { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | return $type->coerce($raw); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | return $raw; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | PAGI::FastAPI::TypedPath - Path Parameter Validation for PAGI::FastAPI | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | Version v1.7.3 | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | use PAGI::FastAPI::Depends qw(Depends); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | use PAGI::FastAPI::TypedPath qw(TypedPath); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | use Types::Standard qw(Int); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | $app->get('/items/{item_id}', | ||||||||||||||||||||||||||||||||||||||||||
| 94 | dependencies => [ | ||||||||||||||||||||||||||||||||||||||||||
| 95 | Depends(TypedPath('item_id', Int), key => 'item_id'), | ||||||||||||||||||||||||||||||||||||||||||
| 96 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 97 | handler => async sub ($c) { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | my $item_id = $c->stash->{item_id}; # validated | ||||||||||||||||||||||||||||||||||||||||||
| 99 | return { item_id => $item_id }; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # Invalid input (e.g. GET /items/abc) never reaches the handler -- | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # the dependency short-circuits with 422 automatically, the same way | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # core's own query/body validation does. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | Python FastAPI infers a path parameter's type from your function signature | ||||||||||||||||||||||||||||||||||||||||||
| 110 | (e.g. C<item_id: int>) and rejects non-matching requests with a C<422> | ||||||||||||||||||||||||||||||||||||||||||
| 111 | automatically. C<PAGI::FastAPI> doesn't have an equivalent at the routing | ||||||||||||||||||||||||||||||||||||||||||
| 112 | layer, path parameters always arrive as plain strings. C<TypedPath()> | ||||||||||||||||||||||||||||||||||||||||||
| 113 | reproduces the validate-and-422-automatically behaviour by building a | ||||||||||||||||||||||||||||||||||||||||||
| 114 | coderef compatible with C<Depends()>, so it plugs into the exact same | ||||||||||||||||||||||||||||||||||||||||||
| 115 | dependency short-circuit mechanism core already uses for C<query>/C<body> | ||||||||||||||||||||||||||||||||||||||||||
| 116 | validation failures (confirmed against the real dependency-execution loop | ||||||||||||||||||||||||||||||||||||||||||
| 117 | in C<PAGI::FastAPI>'s source). | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | B<Validation vs. coercion:> C<< $type->validate($raw) >> (what | ||||||||||||||||||||||||||||||||||||||||||
| 120 | L<Type::Tiny> does by default) checks that the string looks right but | ||||||||||||||||||||||||||||||||||||||||||
| 121 | doesn't transform it, a plain C<Types::Standard::Int> will accept | ||||||||||||||||||||||||||||||||||||||||||
| 122 | C<"42"> but still hand back the I<string> C<"42">, not the Perl integer | ||||||||||||||||||||||||||||||||||||||||||
| 123 | C<42>. If you need an actual coerced value, pass a type with coercion | ||||||||||||||||||||||||||||||||||||||||||
| 124 | enabled (e.g. via L<Type::Tiny>'s C<plus_coercions>), and C<TypedPath()> | ||||||||||||||||||||||||||||||||||||||||||
| 125 | will use C<< ->coerce >> automatically when C<< ->has_coercion >> is true. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =head2 C<TypedPath($param_name, $type)> | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Returns a coderef of C<< async sub ($c) {...} >> suitable for passing | ||||||||||||||||||||||||||||||||||||||||||
| 132 | directly to C<Depends()>. C<$type> must be a L<Type::Tiny> object. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | On validation failure: sets C<< $c->status(422) >> and returns | ||||||||||||||||||||||||||||||||||||||||||
| 135 | C<< { detail => "Path parameter '$param_name' invalid: $err" } >>, which | ||||||||||||||||||||||||||||||||||||||||||
| 136 | (via core's own dependency-execution loop) short-circuits before your | ||||||||||||||||||||||||||||||||||||||||||
| 137 | handler runs. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | On success: returns the (optionally coerced) value. Pass C<key =E<gt> ...> | ||||||||||||||||||||||||||||||||||||||||||
| 140 | to C<Depends()> to have it land in C<< $c->stash->{...} >>. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | This validates AFTER routing has already matched the request to a route | ||||||||||||||||||||||||||||||||||||||||||
| 145 | using the raw, untyped C<{param}> capture, it cannot be used to | ||||||||||||||||||||||||||||||||||||||||||
| 146 | distinguish between two routes based on a path segment's type (e.g. | ||||||||||||||||||||||||||||||||||||||||||
| 147 | C</items/{id}> where C<id> must be numeric vs. a separate literal route). | ||||||||||||||||||||||||||||||||||||||||||
| 148 | That level of route-matching-time type dispatch would require a change to | ||||||||||||||||||||||||||||||||||||||||||
| 149 | the router itself, not just a dependency. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | L<PAGI::FastAPI::Depends>, L<Type::Tiny>, L<Types::Standard> | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||||||||||||||||
| 167 | bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | perldoc PAGI::FastAPI::TypedPath | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | =item * BUG Report | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 194 | the terms of the Artistic License (2.0). | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | 1; # End of PAGI::FastAPI::TypedPath | |||||||||||||||||||||||||||||||||||||||||