Skip to main content

2 posts tagged with "json"

json tag description

View All Tags

Pydantic for JSON Validation in Python

· 6 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

In our last article(python simpleeval examples), we built a dynamic rules engine using simpleeval. But there is a golden rule in software engineering: Garbage In, Garbage Out.

If your rules engine expects a checkout cart to have a cart_total (a number) and a user_role (a string), but the frontend accidentally sends {"cart_total": "free", "role": null}, your engine will crash. Before untrusted JSON data ever reaches your core logic, it needs to pass through a strict gatekeeper. In modern Python, that gatekeeper is Pydantic.

Annotate JSON schema properties in Python with msgspec

· 4 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

To annotate JSON schema properties in Python using msgspec, you use msgspec.field to provide metadata and constraints for a struct field. This allows you to define a more detailed schema beyond just the Python type hints, including documentation, default values, and validation rules.