diff --git a/charmcraft/models/project.py b/charmcraft/models/project.py index 171764166..368cd7194 100644 --- a/charmcraft/models/project.py +++ b/charmcraft/models/project.py @@ -91,6 +91,7 @@ class CharmLib(models.CraftBaseModel): version: str = pydantic.Field( title="Version filter for the charm. Either an API version or a specific [api].[patch].", pattern=r"[0-9]+(\.[0-9]+)?", + coerce_numbers_to_str=False, ) @pydantic.field_validator("lib", mode="before") @@ -129,7 +130,7 @@ def _validate_api_version(cls, value: str) -> str: @pydantic.field_validator("version", mode="before") def _validate_patch_version(cls, value: str) -> str: """Validate the optional patch version, providing a useful error message.""" - api, separator, patch = value.partition(".") + api, separator, patch = str(value).partition(".") if not separator: return value try: diff --git a/tests/unit/models/test_project.py b/tests/unit/models/test_project.py index 045111cf0..fe6781a11 100644 --- a/tests/unit/models/test_project.py +++ b/tests/unit/models/test_project.py @@ -359,6 +359,15 @@ def test_invalid_patch_version(version: str): project.CharmLib(lib="charm_name.lib_name", version=version) +@pytest.mark.parametrize("version", [1, 1.1]) +def test_bad_version_type(version: str): + with pytest.raises( + pydantic.ValidationError, + match="1 validation error for CharmLib\nversion\n Input should be a valid string", + ): + project.CharmLib(lib="charm_name.lib_name", version=version) + + @pytest.mark.parametrize( ("run_on", "expected"), [