lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 150 forks source link

[BUG] decorator @validates_schema not working when schema loads #520

Open hel-o opened 4 months ago

hel-o commented 4 months ago

Description

The @validates_schema decorator does not work when the Schema is loaded.

Code snippet that reproduces the issue

@dataclass_json
@dataclass
class Person:
    name: str = field(
        metadata=config(
            mm_field=fields.String(required=True, validate=validate.Length(min=1, max=15))
        )
    )

    surname: str = field(
        metadata=config(
            mm_field=fields.String(
                validate=validate.Length(min=3, max=15)
            )
        )
    )

    @validates_schema()
    def validate_user_auth(self, data: dict, **kwargs) -> None:
        print('not working...')

    def save(self):
        print('db value:', self.name)

if __name__ == '__main__':
    try:
        value = Person.schema().loads('{"name": "1", "surname": "abd"}')
    except ValidationError as e:
        print(e.messages)
    else:
        print(value.name, ":", value.surname)

Describe the results you expected

the function: validate_user_auth should be called.

Python version you are using

3.12

Environment description

marshmallow==3.20.2 dataclasses-json==0.6.4