0
0
FastAPIframework~10 mins

Field validation rules in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct class for field validation in FastAPI.

FastAPI
from pydantic import [1]
Drag options to blanks, or click blank then click option'
ASchema
BBaseModel
CValidator
DField
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BaseModel instead of Field
Using Validator which is not a class in pydantic
Using Schema which is deprecated
2fill in blank
medium

Complete the code to set a minimum length validation for a string field.

FastAPI
name: str = Field(..., min_length=[1])
Drag options to blanks, or click blank then click option'
A0
B1
C10
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero which allows empty strings
Using too large minimum length causing validation errors
3fill in blank
hard

Fix the error in the code by choosing the correct parameter to enforce a maximum value for an integer field.

FastAPI
age: int = Field(..., [1]=120)
Drag options to blanks, or click blank then click option'
Amax
Bmax_value
Cle
Dmax_length
Attempts:
3 left
💡 Hint
Common Mistakes
Using max_length which is for strings
Using max_value which is not a valid parameter
Using max which is not recognized
4fill in blank
hard

Fill both blanks to add a regex pattern and a default value to a string field.

FastAPI
email: str = Field(default=[1], regex=[2])
Drag options to blanks, or click blank then click option'
A"user@example.com"
B"^\S+@\S+\.\S+$"
C"[a-z]+"
D"default@example.com"
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid regex patterns
Setting default to an invalid email format
5fill in blank
hard

Fill all three blanks to create a field with a title, description, and example value.

FastAPI
username: str = Field([1], title=[2], example=[3])
Drag options to blanks, or click blank then click option'
A"guest"
B"User Name"
C"guest123"
D"The name of the user"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up title and description
Using example as a description