问题
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