I am using marshmallow to validate json data that I am receiving in a flask restful api. In the post request however there is a mutually exclusive field.
For example :
{"predict": {"id": "5hgy667y4h7f"}}
or {"predict": {"text": "This is a sample sentence"}}
But NOT both id
and text
should be sent together. Moreover different methods are called based on weather id
or text
is received.
Q) How do I construct a schema in marshmallow that allows me to validate the above?
Sample code I have for either one of the fields is below -
from flask import Flask, request
from flask_restful import Resource, Api, abort
from marshmallow import Schema, fields, ValidationError
app = Flask(__name__)
api = Api(app)
class Mutex1(Schema):
text = fields.Str(required=True)
class Meta:
strict = True
class Mutex2(Schema):
id_ = fields.Str(required=True)
class Meta:
strict = True
class MySchema(Schema):
predict = fields.Nested(Mutex1)
class Meta:
strict = True
class Test(Resource):
def post(self):
input_req = request.get_json(force=True)
try:
result = MySchema().load(input_req)
except ValidationError:
return {'message': 'Validation Error'}, 500
else:
return {'message': 'Successful validation'}, 200
api.add_resource(Test, '/test')
app.run(host='0.0.0.0', port=5000, debug=True)
This code accepts only text
, and text
with id_
, however it rejects only id_
. Any idea how to make it accept id_
and reject both text
and id_
when passed together ?
Create a Mutex
schema with both text
and id_
and add a schema-level validation to fail if both are provided.
class Mutex(Schema):
@validates_schema
def validate_numbers(self, data):
if (
('text' in data and 'id_' in data) or
('text' not in data and 'id_' not in data)
):
raise ValidationError('Only one of text and _id is allowed')
text = fields.Str()
id_ = fields.Str()
class Meta:
strict = True
Side notes: