Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

FastAPI Path Parameter Numeric Validation

FastAPI Path Parameter Numeric Validation

Just as query parameters use Query to add validation, you can use Path to declare numeric validation and metadata for path parameters.


Import Path

From the fastapi Import Path, from typing Import Annotated:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Path

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_items

(

   

Add metadata and validation to path parameters

    item_id: Annotated

[

int

,

Path

(

title

=

"product_id"

,

description

=

"The ID of the item to retrieve"

,

ge

=

1

)

]

,

)

:

   

return

{

"item_id"

: item_id

}

Path parameters are always required because they must be part of the URL path. Even if you declare a default value for them, they are still treated as required parameters.


Numeric validation

Path and Query both support the following numeric validation parameters:

Parameters Meaning English source
gt Greater than greater than
ge Greater than or equal to greater than or equal
lt Less than less than
le Less than or equal to less than or equal

Greater than or equal (ge)

Limitation item_id Must be >= 1:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Path

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_items

(

    item_id: Annotated

[

int

,

Path

(

ge

=

1

)

]

,

 

item_id must be >= 1

)

:

   

return

{

"item_id"

: item_id

}

Combining numeric validations

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Path

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_items

(

   

item_id must be > 0 and <= 1000

    item_id: Annotated

[

int

,

Path

(

gt

=

0

,

le

=

1000

)

]

,

   

Query parameters can also use numeric validation

    size: Annotated

[

float

,

Query

(

gt

=

0

,

lt

=

10.5

)

]

=

5.0

,

)

:

   

return

{

"item_id"

: item_id

,

"size"

: size

}

Float validation

Numeric validation applies to floating-point numbers as well.gt(greater than) and ge(greater than or equal) is an important distinction in floating-point scenarios:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Path

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_items

(

    item_id: Annotated

[

int

,

Path

(

ge

=

1

,

le

=

1000

)

]

,

   

size must be > 0 and < 10.5

   

0.5 is a valid value, but 0.0 and 0 are not (because gt=0 is strictly greater than 0)

    size: Annotated

[

float

,

Query

(

gt

=

0

,

lt

=

10.5

)

]

,

)

:

   

return

{

"item_id"

: item_id

,

"size"

: size

}


Mixing path parameters and query parameters

When you use both Path and Query together, using Annotated can avoid parameter order issues:

Example

from

typing

import

Annotated

from

fastapi

import

FastAPI

,

Path

,

Query

app

=

FastAPI

(

)

@

app.

get

(

"/items/{item_id}"

)

async

def

read_items

(

   

After using Annotated, parameter order no longer matters

    item_id: Annotated

[

int

,

Path

(

title

=

"product_id"

,

ge

=

1

,

le

=

1000

)

]

,

    q: Annotated

[

str

|

None

,

Query

(

max_length

=

50

)

]

=

None

,

)

:

    results

=

{

"item_id"

: item_id

}

   

if

q:

        results.

update

(

{

"q"

: q

}

)

   

return

results


Summary

Key points of path parameter numeric validation: