Challenge - 5 Problems
Custom Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What output does this FastAPI Pydantic model produce when invalid data is sent?
Consider this FastAPI Pydantic model with a custom validator. What happens if the input JSON is {"age": 15}?
FastAPI
from pydantic import BaseModel, validator class User(BaseModel): age: int @validator('age') def check_age(cls, v): if v < 18: raise ValueError('Must be at least 18') return v user = User(age=15)
Attempts:
2 left
💡 Hint
Think about how Pydantic handles validation errors inside models.
✗ Incorrect
Pydantic wraps validation errors raised in custom validators into a ValidationError. So when age is less than 18, it raises a ValidationError with details about the 'age' field failing validation.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Pydantic custom validator for multiple fields?
You want to create a validator that checks two fields together in a Pydantic model. Which code snippet is correct?
FastAPI
from pydantic import BaseModel, root_validator class Data(BaseModel): start: int end: int @root_validator def check_start_end(cls, values): start, end = values.get('start'), values.get('end') if start >= end: raise ValueError('start must be less than end') return values
Attempts:
2 left
💡 Hint
Use root_validator to validate multiple fields together.
✗ Incorrect
The root_validator decorator is used to validate multiple fields at once. Option A correctly uses @root_validator and accesses values dict to check start and end.
🔧 Debug
advanced2:00remaining
Why does this custom validator not run in FastAPI?
Given this Pydantic model, why does the custom validator not execute when creating an instance?
FastAPI
from pydantic import BaseModel, validator class Item(BaseModel): name: str @validator('name') def name_must_not_be_empty(cls, value): if not value: raise ValueError('Name cannot be empty') return value item = Item(name=None)
Attempts:
2 left
💡 Hint
Check how Pydantic treats null values for required fields.
✗ Incorrect
By default, Pydantic does not allow null for required fields. Since name is str without Optional, null is invalid and raises a validation error before the validator runs. The validator only runs if the value passes type checks.
❓ state_output
advanced2:00remaining
What is the value of 'email' after validation in this FastAPI model?
This Pydantic model uses a custom validator to normalize emails. What is the value of user.email after creating User(email=' EXAMPLE@Domain.COM ')?
FastAPI
from pydantic import BaseModel, validator class User(BaseModel): email: str @validator('email') def normalize_email(cls, v): return v.strip().lower() user = User(email=' EXAMPLE@Domain.COM ') result = user.email
Attempts:
2 left
💡 Hint
Look at what the validator returns after stripping and lowering.
✗ Incorrect
The validator strips whitespace and converts the email to lowercase, so the final value is 'example@domain.com'.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI custom validators is true?
Select the correct statement about how custom validators work in FastAPI's Pydantic models.
Attempts:
2 left
💡 Hint
Think about when validators run in Pydantic's validation process.
✗ Incorrect
Custom validators run after Pydantic converts input data to Python types. They receive the converted value and must return the validated or modified value.