On save validation
Pydantic has a very useful config to validate values on assignment -
validate_assignment = True.
But, unfortunately, this is an expensive operation and doesn't fit some use cases.
You can validate all the values before saving the document (
with beanie config
This feature must be turned on in the
Settings inner class explicitly:
class Sample(Document): num: int name: str class Settings: validate_on_save = True
If any field has a wrong value,
it will raise an error on write operations (
sample = Sample.find_one(Sample.name == "Test") sample.num = "wrong value type" # Next call will raise an error await sample.replace()