Skip to content
Snippets Groups Projects
validators.py 1.21 KiB
from pydantic import BaseModel, validator, ModelField

from gso.utils.helpers import (
    validate_country_code,
    validate_ipv4_or_ipv6,
    validate_site_fields_is_unique,
    validate_site_name,
)

def common_ts_address_validator(cls, site_ts_address: str) -> str:
    """Validate that a terminal server address is valid."""
    validate_ipv4_or_ipv6(site_ts_address)
    return site_ts_address

def common_country_code_validator(cls, country_code: str) -> str:
    """Validate that the country code exists."""
    validate_country_code(country_code)
    return country_code

def common_unique_fields_validator(cls, value: str, field: ModelField) -> str | int:
    """Validate that the internal and :term:`BGP` community IDs are unique."""
    return validate_site_fields_is_unique(field.name, value)

def common_site_name_validator(cls, site_name: str) -> str:
    """Validate the site name.

    The site name must consist of three uppercase letters, followed by an optional single digit.
    """
    validate_site_name(site_name)
    return site_name

def validator_decorator(func):
    def wrapper(cls, *args, **kwargs):
        return validator(func.__name__, pre=True, allow_reuse=True)(*args, **kwargs)

    return wrapper