I am creating an API using marshmallow for the data validation.
the data is given to the schema in JSON:
data = request.get_json()
schema = ItemSchema()
evaluated = schema.load(data)
if evaluated.errors:
return {'message': evaluated.errors}, 400
The schema has field validation methods which are decorated with the @validates
decorator:
@validates('name')
def validate_name(self, name):
existing_item = ItemModel.name_exists(name) #returns an object of type Item if the name exists. Names are unique
if existing_item and existing_item._id != data['_id']:
raise ValidationError('Item already exists.')
As in this example i would like to access the data dictionary which is passed via the load function. How can i access the data object inside the validation method of the schema?
Thanks for your help!