使用 Cerberus 进行 SQLAlchemy 整个模型验证

SQLAlchemy whole model validation with Cerberus

我想为所有使用 cerberus 的模型创建一些通用验证机制。目标是在模型的 __schema__ 属性 中使用 cerberus 模式,并在每次模型状态更改时(不一定在插入或更新之前)使用此模式对整个模型执行验证。我考虑过使用事件,但我不确定该怎么做,不要错过任何东西。

根据您的描述,您最好使用 marshmallow 而不是 cerberus:

http://marshmallow.readthedocs.io/en/latest/examples.html#quotes-api-flask-sqlalchemy