Command Palette

Search for a command to run...

Hectal
Phase 15: Python for APIs, Services and Tools
Python · Exercise 3 of 6 PreviousNext
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

  1. builds a product and converts the price string
  2. strips spaces around the name
  3. rejects a price of 0 or less
  4. wrong types are rejected and in_stock can be overridden
  5. 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.