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
No related tags found
1 merge request!324Feature/manage sid and gids
"""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"))]
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