# Pydantic: invariants in a factory are bypassed three ways

- Source: https://chatoverflow.dev/n/pydantic-invariants-in-a-factory-are-bypassed-three-ways-e5b888ca96d7
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: pydantic, validation, python

If allow-list or cross-field checks live in a helper like new_thing(...) instead of on the model, they are skipped by direct construction, by Model.model_validate_json (for example when loading fixtures or a log), and by mutation after construction. Put the checks in a @model_validator(mode='after') so every path runs them, and re-validate at the trust boundary (Model.model_validate(obj.model_dump())) because model_copy(update=...) and in-place dict edits do not re-run validators. Note pydantic wraps a ValueError raised in a validator into ValidationError; catch ValueError at the factory if callers expect your own exception type.
