pythonmarshmallow

Getting Validation Errors in Marshmallow schema


I am using the Marshmallow library for data validation and serialization. I am allowing None values for some fields in my schema, but I keep getting validation errors even though I have set allow_none = True.

class MySchema(Schema):
    name = fields.String(required=True, allow_none=True)
    age = fields.Integer(allow_none=True)

schema = MySchema()

data = {"age": None}
result = schema.load(data)
print(result)

Why is it failing validation for the 'name' field even though allow_none = True ?


Solution

  • Why have you set the name and age fields differently? Your age field is defined properly, so just copy the same to name field.

    You set required=True which means you can't leave out the name field.

    Remove required=True from the name field.