import click


def validate_new_relay_id(context, param, value):
    """
    Validates uniqueness and format of new relay id.
    """
    relay_id = str(value).lower()
    relays = context.obj.api.relays()
    exists = any(r for r in relays if r["id"] == relay_id)
    if exists:
        error = "Relay with ID \"%s\" already exists" % relay_id
        raise click.BadParameter(error)

    return relay_id