learning-it by example

#139 Python for data 6.8 min

Validate Data with Pydantic: Catch Bad Rows

Six made-up CSV rows through Pydantic: 3 valid, 3 rejected, with 6 named errors in one pass.

1. Six made-up CSV order rows were checked with a Pydantic model. How many rows passed, how many were rejected, and how many errors were reported in one pass?
2. By default, what does Pydantic do with the raw CSV string '3' when the model field is an int?
3. What does extra='forbid' in the model config do when a row has a column the model does not list, such as coupon?
4. Why is strict mode the wrong switch for validating a CSV file?
5. In the Pydantic model, a field_validator on the ordered date rejects any year other than 2026. What is the value it receives, by default?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Fix one rejected row

    Copy the code folder first. In rows.py change the email chen@example to chen@example.com. Before running, predict how many rows will be valid and how many errors python run.py bad will print for that row. Then run python run.py good and python run.py bad to check. This needs Pydantic and its helper packages as in the video's requirements.

    A hint

    Look at the pattern in models.py: the part after the at sign needs a dot.

  2. Change the allowed countries

    In models.py add "DE" to the Literal list so it reads Literal["GB", "IE", "FR", "DE"]. Predict how many errors the row for dina@example.com now has and which one disappears. Then run python run.py bad and compare.

    A hint

    Dina's row had three errors before; the country error is the one that goes away.

  3. Change the business rule

    In models.py change the made-up year rule from 2026 to 2025 (both the number in the if and the message). Predict how many rows are valid and which rows now fail with the year error. Run only python run.py bad; python run.py good will stop with an IndexError because no row is valid. Put the rule back to 2026 afterwards.

    A hint

    Only one row in the data has a 2025 date, and it also has another problem.

  4. Turn on strict for one row

    In run.py strict mode, change rows[0] in the first strict call to a row from rows with a different order, for example rows[1]. Predict whether the error count in 'strict, CSV strings' stays at 4, and why. Run python run.py strict to check.

    A hint

    Count which fields are not already a string in the row you chose.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

Six made-up rows and a rough email pattern; it shows how errors are reported, not how Pydantic handles a real file, JSON input or speed.

Up next in Python for data

More in Python for data

All Python for data videos

Related videos