I am new to marshmallow, and am working on validation. I have been able to use custom validators either by using @ validates or @validates_schema, but they don't seem to work at the same time. Is the following possible? Not sure what I'm missing. If one or the other is commented out, the opposite one works. Any suggestions?
Thank you!!
class TestSchema(Schema):
field_1 = fields.String(required=True)
field_2 = fields.String(required=True)
field_3 = fields.String(required=True)
@validates_schema()
def validate_field1_less_field_2(self, data, **kwargs):
errors = dict()
if int(data["field_1"]) > int(data["field_2"]):
errors["field_1"] = ["field_1 must be less than field_2"]
if errors:
raise ValidationError(errors)
@validates('field_3')
def validate_field_3(self, field_3):
if any(field_3.upper() not in ['A', 'B', 'C'] for value in field_3):
raise ValidationError(f'Incorrect characters in field_3.')
try:
schema = TestSchema()
schema.load(json.loads(json_string))
except ValidationError as err:
print({err.messages})