diff --git a/asyncord/color.py b/asyncord/color.py index 04ac7a6..073378f 100644 --- a/asyncord/color.py +++ b/asyncord/color.py @@ -59,30 +59,6 @@ def to_hex(self) -> str: """Return the hexadecimal code of the color.""" return hex(self.value) - @classmethod - def validate(cls, value: int | str | RGB | tuple[int, int, int] | Self) -> Self: - """Pydantic auxiliary validation method. - - Args: - value: Value to validate. - - Returns: - Validated Color. - - Raises: - ValueError: If value is not - """ - if isinstance(value, int | str | RGB): - return cls.build(value) - - if isinstance(value, tuple) and len(value) == 3 and all(isinstance(v, int) for v in value): - return cls.build(value) - - if isinstance(value, cls): - return value - - raise ValueError('Invalid value type') - @classmethod def __get_pydantic_core_schema__( cls, _source: type[BaseModel], _handler: Callable[[Any], CoreSchema], @@ -107,9 +83,12 @@ def __get_pydantic_core_schema__( ]) return core_schema.no_info_after_validator_function( - function=cls.validate, + function=cls._validate, schema=schema, - serialization=core_schema.simple_ser_schema('int'), + serialization=core_schema.plain_serializer_function_ser_schema( + function=cls._serialize, + when_used='json-unless-none', + ), ) def __repr__(self) -> str: @@ -130,6 +109,42 @@ def __int__(self) -> int: """Return the integer value of the color.""" return self.value + @classmethod + def _validate(cls, value: int | str | RGB | tuple[int, int, int] | Self) -> Self: + """Pydantic auxiliary validation method. + + Args: + value: Value to validate. + + Returns: + Validated Color. + + Raises: + ValueError: If value is not + """ + if isinstance(value, int | str | RGB): + return cls.build(value) + + if isinstance(value, tuple) and len(value) == 3 and all(isinstance(v, int) for v in value): + return cls.build(value) + + if isinstance(value, cls): + return value + + raise ValueError('Invalid value type') + + @classmethod + def _serialize(cls, value: Self) -> int: + """Pydantic auxiliary method to serialize the color. + + Args: + value: Value to serialize. + + Returns: + Serialized value. + """ + return int(value) + ColorInput = Annotated[int | str | RGB | tuple[int, int, int] | Color, Color] diff --git a/tests/test_color.py b/tests/test_color.py index 59ec6e2..147debd 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -89,8 +89,8 @@ class TestModel(BaseModel): assert isinstance(model.color, Color) assert model.color == color - assert model.model_dump(mode='json') == f'{{"color": "{int(color)}"}}' - assert model.model_dump() == {'color': int(color)} + assert model.model_dump() == {'color': color} + assert model.model_dump(mode='json') == {'color': DECIMAL_VALUE} def test_color_error_in_models():