diff --git a/src/marshmallow_sqlalchemy/convert.py b/src/marshmallow_sqlalchemy/convert.py index 2b7c670..e5f26c0 100644 --- a/src/marshmallow_sqlalchemy/convert.py +++ b/src/marshmallow_sqlalchemy/convert.py @@ -1,5 +1,6 @@ from __future__ import annotations +import enum import functools import inspect import uuid @@ -435,8 +436,9 @@ def _add_column_kwargs(self, kwargs: dict[str, Any], column: sa.Column) -> None: kwargs["validate"].append(validate.OneOf(choices=column.type.enums)) # Add a length validator if a max length is set on the column - # Skip UUID columns - # (see https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/54) + # Skip UUID and Enum columns + # - https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/54 + # - https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/673 if hasattr(column.type, "length") and not kwargs.get("dump_only"): column_length = column.type.length if column_length is not None: @@ -444,7 +446,9 @@ def _add_column_kwargs(self, kwargs: dict[str, Any], column: sa.Column) -> None: python_type = column.type.python_type except (AttributeError, NotImplementedError): python_type = None - if not python_type or not issubclass(python_type, uuid.UUID): + if not python_type or not issubclass( + python_type, uuid.UUID | enum.Enum + ): kwargs["validate"].append(validate.Length(max=column_length)) if getattr(column.type, "asdecimal", False): diff --git a/tests/test_conversion.py b/tests/test_conversion.py index dd467cc..4f50092 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -82,6 +82,7 @@ def test_enum_with_class_converted_to_enum_field(self, models): field = fields_["level_with_enum_class"] assert type(field) is fields.Enum assert contains_validator(field, validate.OneOf) is False + assert contains_validator(field, validate.Length) is False assert field.enum is CourseLevel def test_many_to_many_relationship(self, models):