Intermediate
Validate a product with Pydantic
Fill in the class Product as a Pydantic model with three fields: name (text, at least 1 character long), price (a number greater than 0) and in_stock (true or false, True if not given).
Add a validator so that name is saved without spaces at the start or end (" Pen " becomes "Pen").
What Check tests
- builds a product and converts the price string
- strips spaces around the name
- rejects a price of 0 or less
- wrong types are rejected and in_stock can be overridden
- rejects an empty name
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.