def validate_facet_exclude_terms_in_filter(cls, values):
        """Validate that excluded facet fields appear in filter string.

        This validator ensures that:
        1. Exclude terms can only be used when a filter string is present
        2. All exclude terms must appear in the filter string
        3. The filter string has valid parentheses structure

        Args:
            values: Dictionary containing the model's values

        Returns:
            The validated values dictionary

        Raises:
            ValueError: If validation fails for any of the above conditions
        """

        # TODO: rewrite validation logic to use a more robust parser
        facets = values.get('facets')
        filter_str = values.get('filter')

        if not facets or not facets.fields:
            return values

        # Check if exclude terms are used without a filter
        if not filter_str:
            if any(field.exclude_terms for field in facets.fields.values()):
                raise ValueError("Exclude terms can only be used when a filter string is provided.")
            return values

        # Extract clean terms from filter string
        def extract_clean_terms(filter_string: str) -> List[str]:
            # Remove NOT operators as they don't affect term matching
            filter_string = filter_string.replace("NOT", "")

            # Split by AND/OR operators
            raw_terms = re.split(r'\s*(?:AND|OR)\s*', filter_string)

            # Clean each term
            clean_terms = []
            for term in raw_terms:
                # Remove excessive outer parentheses while preserving inner ones
                term = term.strip()

                while term.startswith('('):
                    term = term[1:]

                total_left_parens = term.count('(')
                total_right_parens = term.count(')')
                parens_difference = total_left_parens - total_right_parens
                if parens_difference != 0:
                    if parens_difference > 0:
                        term = term[parens_difference:]
                    else:
                        term = term[:parens_difference]

                if term:  # Only add non-empty terms
                    clean_terms.append(term)

            return clean_terms

        filter_terms = extract_clean_terms(filter_str)

        # Validate each facet field's exclude terms
        for field_name, field_params in facets.fields.items():
            if not field_params.exclude_terms:
                continue

            # Check if all exclude terms appear in filter
            missing_terms = [
                term for term in field_params.exclude_terms
                if not any(term in filter_term for filter_term in filter_terms)
            ]

            if missing_terms:
                raise ValueError(
                    f"Facet field '{field_name}' has exclude terms {missing_terms} "
                    f"that do not appear in the filter string. All exclude terms must "
                    f"be present in the filter for proper filtering."
                )

        return values
Validate that excluded facet fields appear in filter string.

        This validator ensures that:
        1. Exclude terms can only be used when a filter string is present
        2. All exclude terms must appear in the filter string
        3. The filter string has valid parentheses structure

        Args:
            values: Dictionary containing the model's values

        Returns:
            The validated values dictionary

        Raises:
            ValueError: If validation fails for any of the above conditions
        
src/marqo/tensor_search/models/api_models.py