0
0
FastAPIframework~20 mins

Numeric validation (gt, lt, ge, le) in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Numeric Validation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a FastAPI endpoint receives a value violating gt constraint?

Consider this FastAPI endpoint using Pydantic for validation:

from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class Item(BaseModel):
    quantity: int = Field(..., gt=0)

@app.post("/items/")
async def create_item(item: Item):
    return {"quantity": item.quantity}

What will be the response if the client sends {"quantity": 0}?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class Item(BaseModel):
    quantity: int = Field(..., gt=0)

@app.post("/items/")
async def create_item(item: Item):
    return {"quantity": item.quantity}
AThe server accepts the value and returns {"quantity": 0}.
BThe server crashes with a TypeError due to invalid input.
CThe server responds with a 422 Unprocessable Entity error explaining quantity must be greater than 0.
DThe server responds with a 400 Bad Request without details.
Attempts:
2 left
💡 Hint

Think about how Pydantic validates fields with gt constraints.

📝 Syntax
intermediate
1:30remaining
Which Field declaration correctly enforces value less than or equal to 10?

Choose the correct Pydantic Field declaration to ensure a numeric value is at most 10.

Avalue: int = Field(..., lt=10)
Bvalue: int = Field(..., le=10)
Cvalue: int = Field(..., ge=10)
Dvalue: int = Field(..., gt=10)
Attempts:
2 left
💡 Hint

Remember: le means less than or equal, lt means less than.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint accept a score of null despite ge=0?

Examine this code snippet:

from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class Data(BaseModel):
    score: float = Field(ge=0)

@app.post("/data/")
async def post_data(data: Data):
    return {"score": data.score}

Why does sending {"score": null} not raise a validation error?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class Data(BaseModel):
    score: float = Field(ge=0)

@app.post("/data/")
async def post_data(data: Data):
    return {"score": data.score}
ABecause the type float ignores Field constraints.
BBecause the default value is missing, so the field is optional and validation is skipped.
CBecause FastAPI does not validate numeric constraints automatically.
DBecause the Field declaration lacks the required ellipsis (...) making the field optional and validation not enforced.
Attempts:
2 left
💡 Hint

Check how required fields are declared in Pydantic.

state_output
advanced
1:30remaining
What is the value of 'price' after validation with ge and le constraints?

Given this Pydantic model:

class Product(BaseModel):
    price: float = Field(..., ge=1.0, le=100.0)

And this input JSON:

{"price": 100.0}

What will be the value of price in the validated model instance?

FastAPI
from pydantic import BaseModel, Field

class Product(BaseModel):
    price: float = Field(..., ge=1.0, le=100.0)

product = Product(price=100.0)
value = product.price
A100.0
B99.999999
CRaises a validation error because 100.0 is not less than 100.0
DNone
Attempts:
2 left
💡 Hint

Check the meaning of le constraint.

🧠 Conceptual
expert
2:30remaining
Which numeric validation combination allows values from 5 (inclusive) up to but not including 10?

Using Pydantic's Field constraints, which combination correctly restricts a number to be at least 5 and less than 10?

AField(..., ge=5, lt=10)
BField(..., gt=5, le=10)
CField(..., ge=5, le=10)
DField(..., gt=5, lt=10)
Attempts:
2 left
💡 Hint

Remember: ge means greater or equal, lt means less than.