Skip to content
Snippets Groups Projects
Commit 73d83091 authored by Karel van Klink's avatar Karel van Klink :smiley_cat: Committed by Mohammad Torkashvand
Browse files

Update typing of ga and gs id validation method

parent 7fdced2d
No related branches found
Tags 2.12
1 merge request!324Feature/manage sid and gids
"""Type definitions for the GA and GS IDs.""" """Type definitions for the GA and GS IDs."""
from functools import partial from functools import partial
from typing import Annotated from typing import Annotated, Literal
from pydantic import AfterValidator from pydantic import AfterValidator
from gso.services.subscriptions import is_resource_type_value_unique 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. """Validate that the ID is unique, has the correct prefix, and is within valid constraints.
Args: Args:
value (str): The ID value to validate. value: The ID value to validate.
prefix (str): The required prefix for the ID (e.g., "GA-" or "GS-"). prefix: The required prefix for the ID.
field_name (str): The database field name to check for uniqueness (e.g., "ga_id" or "gs_id"). field_name: The database field name to check for uniqueness.
Raises: Raises:
ValueError: If the ID is not valid. ValueError: If the ID is not valid.
...@@ -29,21 +29,21 @@ def validate_id(value: str, prefix: str, field_name: str) -> str: ...@@ -29,21 +29,21 @@ def validate_id(value: str, prefix: str, field_name: str) -> str:
raise ValueError(err) raise ValueError(err)
try: try:
numeric_part = int(value[len(prefix) :]) numeric_part = int(value.split("-")[-1])
except ValueError: except ValueError:
err = f"{field_name} must have a numeric part after the prefix '{prefix}'." err = f"{field_name} must have a numeric part after the prefix '{prefix}'."
raise ValueError(err) from ValueError raise ValueError(err) from ValueError
if min_range <= numeric_part <= max_range: 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) raise ValueError(err)
if not is_resource_type_value_unique(field_name, value): 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) raise ValueError(err)
return value return value
IMPORTED_GA_ID = Annotated[str, AfterValidator(partial(validate_id, prefix="GA-", field_name="ga_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"))] IMPORTED_GS_ID = Annotated[str, AfterValidator(partial(validate_id, prefix="GS", field_name="gs_id"))]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment