marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

@validates decorated function is invoked when the field is loaded with the default value

Open dharani7998 opened this issue 1 year ago • 1 comments

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({})

dharani7998 avatar Sep 12 '22 06:09 dharani7998

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.

deckar01 avatar Nov 19 '23 01:11 deckar01