jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.01k stars 59 forks source link

Add an option to disable constraint checks #688

Open corentin-regent opened 1 month ago

corentin-regent commented 1 month ago

Description

Hi @jcrist!

I am currently using msgspec for deserializing data queried from external REST APIs. Constraints are great for this purpose, because it provides some documentation within the code about the format of the data that we retrieve, and it ensures that we really receive what we expect.

However, once the end-to-end tests for my API client pass, well we are pretty sure that any other request that we may perform in a near future will also return some valid data as expected. Or at least it would be the case for APIs that are stable enough.

This means that all the tests that may be performed at runtime once in production will be nearly redundant, however they could introduce some overhead depending on the size of the data and on the performance that is required.

Therefore I am suggesting to add an enforce_constraints flag (or whatever name fits best) for the Decoders and the decode functions, which could be set to False for disabling these checks.