Do not allow any extra fields in Flask-restplus models

ε祈祈猫儿з 提交于 2019-12-11 00:17:33

问题


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.

Model which am using:

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>"
}

回答1:


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)


来源:https://stackoverflow.com/questions/53447366/do-not-allow-any-extra-fields-in-flask-restplus-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!