What is the purpose of string validation in FastAPI?
String validation ensures that input strings meet specific rules like minimum length, maximum length, or matching a pattern. This helps keep data clean and safe.
Click to reveal answer
beginner
How do you set a minimum and maximum length for a string in FastAPI using Pydantic?
Use the Field function with min_length and max_length parameters inside your Pydantic model. For example: name: str = Field(..., min_length=3, max_length=50)
Click to reveal answer
intermediate
How can you validate a string against a regular expression in FastAPI?
Use <code>constr</code> from Pydantic with the <code>regex</code> parameter. For example: <br><code>from pydantic import constr<br>email: constr(regex=r'^[\w\.-]+@[\w\.-]+\.\w+$')</code>
Click to reveal answer
beginner
What happens if a string input does not meet the validation rules in FastAPI?
FastAPI automatically returns a clear error response with details about which validation rule failed. This helps users fix their input.
Click to reveal answer
intermediate
Write a Pydantic model field that requires a username string between 4 and 20 characters and only letters or numbers.
Use constr with min_length=4, max_length=20, and a regex for letters and numbers: username: constr(min_length=4, max_length=20, regex=r'^[a-zA-Z0-9]+$')
Click to reveal answer
Which Pydantic function is used to add min and max length constraints to a string field in FastAPI?
ABaseModel
Bconstr
Cvalidator
DField
✗ Incorrect
The Field function allows setting min_length and max_length for string fields.
How do you enforce a string to match a pattern using regex in FastAPI's Pydantic models?
AUse <code>constr(regex=...)</code>
BUse <code>BaseModel(regex=...)</code>
CUse <code>Field(regex=...)</code>
DUse <code>validator(regex=...)</code>
✗ Incorrect
The constr type allows regex pattern matching for strings.
What error response does FastAPI return if string validation fails?
A500 Internal Server Error
B422 Unprocessable Entity with details
C200 OK with warning
D404 Not Found
✗ Incorrect
FastAPI returns a 422 status with details about validation errors.
Which import is needed to use constr in FastAPI models?
Afrom pydantic import constr
Bfrom fastapi import Field
Cfrom typing import constr
Dfrom fastapi import constr
✗ Incorrect
constr is imported from Pydantic.
What does min_length=5 do in a string field validation?
ASets the string length to exactly 5
BAllows strings shorter than 5
CRequires string length to be at least 5 characters
DLimits string length to 5 characters maximum
✗ Incorrect
min_length=5 means the string must be 5 or more characters long.
Explain how to validate a string field in FastAPI to have a minimum length, maximum length, and match a regex pattern.
Think about combining Field and constr from Pydantic.
You got /4 concepts.
Describe what happens when a user sends a string that does not meet validation rules in a FastAPI app.
Focus on FastAPI's automatic error handling for validation.
You got /4 concepts.
Practice
(1/5)
1. What does the constr function in FastAPI's Pydantic models help you do?
easy
A. Validate string fields with rules like minimum length, maximum length, and regex patterns
B. Create integer fields with range validation
C. Automatically generate API documentation
D. Handle database connections
Solution
Step 1: Understand constr purpose
constr is a Pydantic type used to validate string fields with constraints like length and regex.
Step 2: Compare options
Only Validate string fields with rules like minimum length, maximum length, and regex patterns correctly describes constr usage for string validation.
Final Answer:
Validate string fields with rules like minimum length, maximum length, and regex patterns -> Option A
Quick Check:
constr = string validation [OK]
Hint: Remember constr is for string validation rules [OK]
Common Mistakes:
Confusing constr with integer validation
Thinking constr manages database or docs
Assuming constr auto-generates API docs
2. Which of the following is the correct way to define a string field in a Pydantic model with minimum length 3 and maximum length 10?
easy
A. name: str(min_length=3, max_length=10)
B. name: constr(min=3, max=10)
C. name: constr(min_length=3, max_length=10)
D. name: str(min=3, max=10)
Solution
Step 1: Recall correct syntax for constr
The correct parameters are min_length and max_length inside constr().
Step 2: Check options
Only name: constr(min_length=3, max_length=10) uses constr(min_length=3, max_length=10) correctly. Options A, B, and D use wrong parameter names or types.
Final Answer:
name: constr(min_length=3, max_length=10) -> Option C
Quick Check:
Use min_length and max_length in constr [OK]
Hint: Use min_length and max_length, not min or max [OK]
Common Mistakes:
Using min and max instead of min_length and max_length
Trying to add length constraints directly to str type
Mixing parameter names
3. Given this Pydantic model snippet:
from pydantic import BaseModel, constr
class User(BaseModel):
username: constr(min_length=4, max_length=8, regex=r'^[a-zA-Z]+$')
What happens if you try to create User(username='abc123')?
medium
A. Validation error because username is too long
B. User instance created successfully
C. Validation error because username is too short
D. Validation error because username contains digits
Solution
Step 1: Analyze regex pattern
The regex r'^[a-zA-Z]+$' allows only letters, no digits.
Step 2: Check input username
Input 'abc123' contains digits '123', violating the regex.
Final Answer:
Validation error because username contains digits -> Option D
Quick Check:
Regex restricts digits, so error occurs [OK]
Hint: Regex restricts characters; digits cause validation error [OK]
Common Mistakes:
Ignoring regex and only checking length
Assuming digits are allowed by regex
Confusing length errors with pattern errors
4. Identify the error in this FastAPI Pydantic model field:
from pydantic import BaseModel, constr
class Item(BaseModel):
code: constr(min_length=5, max=10)
medium
A. code field must be an int, not constr
B. max should be max_length to specify maximum string length
C. constr cannot be used for string validation
D. min_length should be min to specify minimum length
Solution
Step 1: Check parameter names in constr
The correct parameter for maximum length is max_length, not max.
Step 2: Identify error in code
Using max=10 causes a syntax or validation error because it's invalid.
Final Answer:
max should be max_length to specify maximum string length -> Option B
Quick Check:
Use max_length, not max, in constr [OK]
Hint: Use max_length, not max, for string max length [OK]
Common Mistakes:
Using max instead of max_length
Confusing min_length with min
Thinking constr is for int fields
5. You want to create a FastAPI model field password that must be between 8 and 16 characters and include at least one digit. Which is the correct way to define it?
hard
A. password: constr(min_length=8, max_length=16, regex=r'^(?=.*\\d).+$')
B. password: constr(min_length=8, max_length=16, regex=r'^[a-zA-Z]+$')
C. password: constr(min=8, max=16, regex=r'^(?=.*\\d).+$')
D. password: str(min_length=8, max_length=16, regex=r'^(?=.*\\d).+$')
Solution
Step 1: Check length parameters
Use min_length=8 and max_length=16 in constr for length limits.
Step 2: Verify regex for digit inclusion
The regex r'^(?=.*\\d).+$' ensures at least one digit is present.
Step 3: Validate option correctness
password: constr(min_length=8, max_length=16, regex=r'^(?=.*\\d).+$') correctly combines length and regex. password: constr(min_length=8, max_length=16, regex=r'^[a-zA-Z]+$')'s regex disallows digits. password: constr(min=8, max=16, regex=r'^(?=.*\\d).+$') uses invalid min and max. password: str(min_length=8, max_length=16, regex=r'^(?=.*\\d).+$') wrongly applies regex to str type.
Final Answer:
password: constr(min_length=8, max_length=16, regex=r'^(?=.*\\d).+$') -> Option A
Quick Check:
Use constr with min_length, max_length, and digit regex [OK]
Hint: Use constr with min_length, max_length, and digit regex [OK]
Common Mistakes:
Using min and max instead of min_length and max_length
Using regex that excludes digits
Trying to add regex to plain str type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.