Complete the code to set a minimum length of 3 for the username field.
from pydantic import BaseModel, Field class User(BaseModel): username: str = Field(..., min_length=[1])
The min_length parameter sets the minimum number of characters allowed for the string. Here, 3 means the username must have at least 3 characters.
Complete the code to set a maximum length of 10 for the password field.
from pydantic import BaseModel, Field class User(BaseModel): password: str = Field(..., max_length=[1])
The max_length parameter limits the string length to a maximum number of characters. Here, 10 means the password cannot be longer than 10 characters.
Fix the error in the regex pattern to only allow lowercase letters for the nickname field.
from pydantic import BaseModel, Field class User(BaseModel): nickname: str = Field(..., pattern=r"[1]")
The regex ^[a-z]+$ means the string must contain only lowercase letters from start to end. Other options allow uppercase letters or digits, which are not correct here.
Fill both blanks to create a field that requires a string with minimum length 5 and matches only digits.
from pydantic import BaseModel, Field class User(BaseModel): code: str = Field(..., min_length=[1], pattern=r"[2]")
The min_length=5 ensures the string is at least 5 characters long. The regex ^[0-9]+$ restricts the string to digits only.
Fill all three blanks to create a field that requires an uppercase string between 2 and 6 characters.
from pydantic import BaseModel, Field class User(BaseModel): tag: str = Field(..., min_length=[1], max_length=[2], pattern=r"[3]")
The field requires a string with at least 2 and at most 6 characters. The regex ^[A-Z]+$ ensures only uppercase letters are allowed.