Pydantic: Catch Bad Data Before It Reaches Your Database
A library lets members borrow books through a web form. One day, a member types -5 into the loan period field, another mistypes her email, and a third pastes an ISBN with a letter in it. The form saves all three loans without complaint. The reminder email bounces, the -5 loan shows as overdue, and nobody can find the book with the broken ISBN. The database stored what it was given. The problem is that nobody checked what was given first. That is the gap Pydantic fills. You describe the shape of the data once, as a Python class, and Pydantic checks every record against it before your code trusts it. What I would check before using it My judgement is that Pydantic is worth adding the moment your code receives data it did not create: a form submission, a JSON body from an API, a row read from a spreadsheet. Inside your own code, a model is extra ceremony. At a boundary, where the data comes from a stranger, it is the cheapest insurance you can buy. The first check I would make is whic...
