marshmallow
marshmallow copied to clipboard
@validates decorated function is invoked when the field is loaded with the default value
Here id field has a load_default value, it doesn't invoke the fn_validator when the value is not present in the dict but it invokes the decorator function
def fn_validator(value: int):
print(f'In fn_validator: {value}')
class Test(Schema):
id = fields.Int(load_default=None, validate=fn_validator)
@validates('id')
def dec_validator(self, value, **kwargs):
print(f'In dec_validator: {value}')
sch = Test()
sch.load({'id': 123})
print('=========================')
sch.load({})
The schema would need to either keep track of which values are defaulted, or pass the extra validator methods to the field during initialization. I would lean toward the latter, but honestly I am not very motivated to add code to normalize what seems to be an inert side effect.