0
0
FastAPIframework~10 mins

String validation (min, max, regex) 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 set a minimum length of 3 for the username field.

FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    username: str = Field(..., min_length=[1])
Drag options to blanks, or click blank then click option'
A3
B5
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 which allows too short usernames.
Confusing min_length with max_length.
2fill in blank
medium

Complete the code to set a maximum length of 10 for the password field.

FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    password: str = Field(..., max_length=[1])
Drag options to blanks, or click blank then click option'
A8
B5
C12
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Setting max_length too low, making passwords too short.
Mixing up max_length with min_length.
3fill in blank
hard

Fix the error in the regex pattern to only allow lowercase letters for the nickname field.

FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    nickname: str = Field(..., pattern=r"[1]")
Drag options to blanks, or click blank then click option'
A^[a-z]+$
B^[A-Z]+$
C^[a-zA-Z]+$
D^[0-9]+$
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase letters in the pattern.
Forgetting to anchor the pattern with ^ and $.
4fill in blank
hard

Fill both blanks to create a field that requires a string with minimum length 5 and matches only digits.

FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    code: str = Field(..., min_length=[1], pattern=r"[2]")
Drag options to blanks, or click blank then click option'
A5
B^[0-9]+$
C^[a-z]+$
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong regex that allows letters.
Setting min_length too low.
5fill in blank
hard

Fill all three blanks to create a field that requires an uppercase string between 2 and 6 characters.

FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    tag: str = Field(..., min_length=[1], max_length=[2], pattern=r"[3]")
Drag options to blanks, or click blank then click option'
A2
B6
C^[A-Z]+$
D^[a-z]+$
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up min_length and max_length values.
Using lowercase regex instead of uppercase.