Skip to content

Geant ids

Type definitions for the GA and GS IDs.

validate_id(value, prefix, field_name)

Validate that the ID is unique, has the correct prefix, and is within valid constraints.

Parameters:

Name Type Description Default
value str

The ID value to validate.

required
prefix Literal['GA', 'GS']

The required prefix for the ID.

required
field_name str

The database field name to check for uniqueness.

required

Raises:

Type Description
ValueError

If the ID is not valid.

Returns:

Name Type Description
str str

The validated ID.

Source code in gso/utils/types/geant_ids.py
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: 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.

    Returns:
        str: The validated ID.
    """
    min_range = 50000
    max_range = 99999
    if not value.startswith(prefix):
        err = f"{field_name} must start with the prefix '{prefix}'."
        raise ValueError(err)

    try:
        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 {min_range} and {max_range}, received {numeric_part}"
        raise ValueError(err)

    if not is_resource_type_value_unique(field_name, value):
        err = f"{field_name} must be unique, {value} is already in use."
        raise ValueError(err)

    return value