Skip to content
Sharp

FastAPI · interview question

Which option is the safer default when shipping code around validation?

Multiple choiceHardchapter-13-curatedpremiumfastapivalidation
python
class CreateOrderRequest(BaseModel):
    sku: str
    quantity: int

    @field_validator("quantity")
    @classmethod
    def quantity_must_be_positive(cls, value: int) -> int:
        if value <= 0:
            raise ValueError("quantity must be positive")
        return value
Choose one answer
Answer and explanation

Correct answer

D. Choose the simpler shape that keeps the rule obvious, validated, and easy to observe in logs or tests.

Why

The safest default is usually the one that lowers ambiguity and makes errors easier to diagnose.

Test yourself on FastAPI

A free test with instant, explained results. No account needed.

Start free test
All FastAPI questions →