diff --git a/gso/utils/types/geant_ids.py b/gso/utils/types/geant_ids.py index 194f57b0b2a1fade3c7eeec8d2831a4e700f44c1..ec728bc00e1953d0533a320ce67a374cccb533c4 100644 --- a/gso/utils/types/geant_ids.py +++ b/gso/utils/types/geant_ids.py @@ -1,20 +1,20 @@ """Type definitions for the GA and GS IDs.""" from functools import partial -from typing import Annotated +from typing import Annotated, Literal from pydantic import AfterValidator from gso.services.subscriptions import is_resource_type_value_unique -def validate_id(value: str, prefix: str, field_name: str) -> str: +def validate_id(value: str, prefix: Literal["GA", "GS"], field_name: str) -> str: """Validate that the ID is unique, has the correct prefix, and is within valid constraints. Args: - value (str): The ID value to validate. - prefix (str): The required prefix for the ID (e.g., "GA-" or "GS-"). - field_name (str): The database field name to check for uniqueness (e.g., "ga_id" or "gs_id"). + value: The ID value to validate. + prefix: The required prefix for the ID. + field_name: The database field name to check for uniqueness. Raises: ValueError: If the ID is not valid. @@ -29,21 +29,21 @@ def validate_id(value: str, prefix: str, field_name: str) -> str: raise ValueError(err) try: - numeric_part = int(value[len(prefix) :]) + numeric_part = int(value.split("-")[-1]) except ValueError: err = f"{field_name} must have a numeric part after the prefix '{prefix}'." raise ValueError(err) from ValueError if min_range <= numeric_part <= max_range: - err = f"{field_name} must not have a numeric part between 50000 and 99999." + err = f"{field_name} must not have a numeric part between 50000 and 99999, received {numeric_part}" raise ValueError(err) if not is_resource_type_value_unique(field_name, value): - err = f"{field_name} must be unique." + err = f"{field_name} must be unique, {value} is already in use." raise ValueError(err) return value -IMPORTED_GA_ID = Annotated[str, AfterValidator(partial(validate_id, prefix="GA-", field_name="ga_id"))] -IMPORTED_GS_ID = Annotated[str, AfterValidator(partial(validate_id, prefix="GS-", field_name="gs_id"))] +IMPORTED_GA_ID = Annotated[str, AfterValidator(partial(validate_id, prefix="GA", field_name="ga_id"))] +IMPORTED_GS_ID = Annotated[str, AfterValidator(partial(validate_id, prefix="GS", field_name="gs_id"))]