def _validate_labels()

in cli/src/klio_cli/commands/job/gke.py [0:0]


    def _validate_labels(label_path, label_dict):
        help_url = (
            "https://kubernetes.io/docs/concepts/overview/working-with-objects"
            "/labels/#syntax-and-character-set"
        )
        for key, value in label_dict.items():
            # Both key and value must be strings
            if not isinstance(key, str):
                raise ValueError(f"Key '{label_path}.{key}' must be a string.")
            if not isinstance(value, str):
                raise ValueError(
                    f"Value '{value}' for key '{label_path}.{key}' must be a "
                    "string"
                )

            # Handle any prefixes in keys
            if "/" in key:
                # validate that there's at most one forward slash
                prefix, *name = key.split("/")
                if len(name) > 1:
                    raise ValueError(
                        f"Unsupported key name in {label_path}: '{key}' "
                        f"contains more than one forward slash. See {help_url} "
                        "for valid label keys."
                    )

                # validate prefix
                prefix_match = K8S_LABEL_KEY_PREFIX_REGEX.match(prefix)
                if (
                    prefix_match is None
                    or prefix in K8S_RESERVED_KEY_PREFIXES
                    or len(prefix) > 253
                ):
                    raise ValueError(
                        f"Unsupported prefix key name in {label_path}: "
                        f"'{prefix}'. See {help_url} for valid label key "
                        "prefixes."
                    )
                key = name[0]

            # Validate the key
            key_match = K8S_LABEL_KEY_NAME_REGEX.match(key)
            if key_match is None:
                raise ValueError(
                    f"Unsupported key name in {label_path}: '{key}'. "
                    f"See {help_url} for valid label keys."
                )

            # Validate the value
            value_match = K8S_LABEL_VALUE_REGEX.match(value)
            if not value_match:
                raise ValueError(
                    f"Unsupported value '{value}' for '{label_path}.{key}'. "
                    f"See {help_url} for valid values."
                )