import re


def hostname(value):
    """Validate a hostname."""
    host = str(value).lower()
    if len(host) > 253:
        raise ValueError
    if host[-1] == ".":
        host = host[:-1]
    allowed = re.compile(r"(?!-)[a-z\d-]{1,63}(?<!-)$")
    if not all(allowed.match(elem) for elem in host.split(".")):
        raise ValueError
    return host