1. Learn
  2. /
  3. Courses
  4. /
  5. Deploying AI into Production with FastAPI

Connected

Exercise

Field validation

You are building on the user comment moderation service. Your goal is to create a Pydantic User model that ensures data integrity across all users.

Implement validations for the username (min 5, max 50 characters) field.

Use Pydantic's Field class to add these constraints, and test your model with both valid and invalid product data to ensure it correctly handles various scenarios.

Instructions

100 XP
  • Import the base model and field classes from Pydantic.
  • Inherit Pydantic's base model in the User model.
  • Add field validations on username attribute in the User class to have at least 5 characters and no more than 20 characters.