Do not allow any extra fields in Flask-restplus models
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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>"
}
python flask-restful
add a comment |
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>"
}
python flask-restful
Maybe you can somehow extract keys from restplus model, let's name itx
, then extract keys from post payloady = request.get_json().keys()
. Convert both variables to sets and calculate a difference:set(y) - set(x)
.
– needtobe
Nov 23 '18 at 14:38
I would strongly recommend swapping to marshmallow, as docs says:The whole request parser part of Flask-RESTPlus is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.
– needtobe
Nov 23 '18 at 14:45
How do I extract all keys from the restplus model? Just to add some fields can also be "optional" in model, but they may or may not be present in the payload.
– Sawan Choudhary
Nov 26 '18 at 20:02
add a comment |
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>"
}
python flask-restful
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>"
}
python flask-restful
python flask-restful
edited Nov 23 '18 at 13:17
Cleb
11.4k125683
11.4k125683
asked Nov 23 '18 at 13:10
Sawan ChoudharySawan Choudhary
312
312
Maybe you can somehow extract keys from restplus model, let's name itx
, then extract keys from post payloady = request.get_json().keys()
. Convert both variables to sets and calculate a difference:set(y) - set(x)
.
– needtobe
Nov 23 '18 at 14:38
I would strongly recommend swapping to marshmallow, as docs says:The whole request parser part of Flask-RESTPlus is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.
– needtobe
Nov 23 '18 at 14:45
How do I extract all keys from the restplus model? Just to add some fields can also be "optional" in model, but they may or may not be present in the payload.
– Sawan Choudhary
Nov 26 '18 at 20:02
add a comment |
Maybe you can somehow extract keys from restplus model, let's name itx
, then extract keys from post payloady = request.get_json().keys()
. Convert both variables to sets and calculate a difference:set(y) - set(x)
.
– needtobe
Nov 23 '18 at 14:38
I would strongly recommend swapping to marshmallow, as docs says:The whole request parser part of Flask-RESTPlus is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.
– needtobe
Nov 23 '18 at 14:45
How do I extract all keys from the restplus model? Just to add some fields can also be "optional" in model, but they may or may not be present in the payload.
– Sawan Choudhary
Nov 26 '18 at 20:02
Maybe you can somehow extract keys from restplus model, let's name it
x
, then extract keys from post payload y = request.get_json().keys()
. Convert both variables to sets and calculate a difference: set(y) - set(x)
.– needtobe
Nov 23 '18 at 14:38
Maybe you can somehow extract keys from restplus model, let's name it
x
, then extract keys from post payload y = request.get_json().keys()
. Convert both variables to sets and calculate a difference: set(y) - set(x)
.– needtobe
Nov 23 '18 at 14:38
I would strongly recommend swapping to marshmallow, as docs says:
The whole request parser part of Flask-RESTPlus is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.
– needtobe
Nov 23 '18 at 14:45
I would strongly recommend swapping to marshmallow, as docs says:
The whole request parser part of Flask-RESTPlus is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.
– needtobe
Nov 23 '18 at 14:45
How do I extract all keys from the restplus model? Just to add some fields can also be "optional" in model, but they may or may not be present in the payload.
– Sawan Choudhary
Nov 26 '18 at 20:02
How do I extract all keys from the restplus model? Just to add some fields can also be "optional" in model, but they may or may not be present in the payload.
– Sawan Choudhary
Nov 26 '18 at 20:02
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447366%2fdo-not-allow-any-extra-fields-in-flask-restplus-models%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447366%2fdo-not-allow-any-extra-fields-in-flask-restplus-models%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe you can somehow extract keys from restplus model, let's name it
x
, then extract keys from post payloady = request.get_json().keys()
. Convert both variables to sets and calculate a difference:set(y) - set(x)
.– needtobe
Nov 23 '18 at 14:38
I would strongly recommend swapping to marshmallow, as docs says:
The whole request parser part of Flask-RESTPlus is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.
– needtobe
Nov 23 '18 at 14:45
How do I extract all keys from the restplus model? Just to add some fields can also be "optional" in model, but they may or may not be present in the payload.
– Sawan Choudhary
Nov 26 '18 at 20:02