[英]Do not allow any extra fields in Flask-restplus models
我正在使用 Flask Rest-plus 模型來驗證 POST 有效負載,但是如果存在任何額外/未知字段,我希望模型出錯。
正在使用的型號:
interface_config = api.model('Network Interface Validation', {
'gateway': fields.String(required=True, description='Gateway IP'),
'subnet': fields.String(required=True, description='Subnet IP'),
'netmask': fields.String(required=True, description='Netmask'),
'vlan_id': fields.Integer(required=True, description='VLAN ID'),
'type': fields.String(required=True, description='IP Version')
})
我想錯誤出有效載荷,其中包含:
{
"gateway": "172.22.191.129",
"subnet": "172.22.191.128",
"netmask": "255.255.255.128",
"vlan_id": 887,
"type": "static",
"extra_key_name": "<some_str>"
}
可以使用flask_restplus
的marshal
函數來重用模型定義,並跳過額外的字段。
from flask_restplus import marshal
marshal(api.payload, schema, skip_none=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.