Flask-restplus模型中不允许任何其他字段 [英] Do not allow any extra fields in Flask-restplus models
本文介绍了Flask-restplus模型中不允许任何其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Flask Rest-plus模型来验证POST负载,但是如果存在任何额外/未知字段,我希望模型出错.
I am using Flask Rest-plus models to validate a POST payload, however the I want the model to error out if any extra/unknown fields are present.
正在使用的模型:
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')
})
我想对包含以下内容的有效载荷进行错误处理
I want to error out payload which contains:
{
"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
函数来重用模型定义,并跳过多余的字段.
One can use the marshal
function in flask_restplus
, to reuse the model definition, and skip extra fields.
from flask_restplus import marshal
marshal(api.payload, schema, skip_none=True)
这篇关于Flask-restplus模型中不允许任何其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文