--- title: Validation Pattern category: patterns --- # Validation Pattern ## Rules - Validate inputs early and raise ValueError or domain-specific exceptions (ProviderError) for invalid contract inputs. - Tests should assert that invalid inputs raise the expected exceptions. - Use explicit checks for types and shapes on public APIs (e.g., ensure text is str before embedding). ## Examples ### ai_provider.py - Type validation ```python if not isinstance(text, str): raise ProviderError("text must be a string") ``` ### pipeline/ai_provider_wrapper.py - Defensive empty handling ```python if not texts: return [] if motion_ids is None: motion_ids = [None for _ in texts] ``` ## Anti-Patterns ### Bad: Invalid values into computation **Problem**: Allowing invalid values to propagate into heavy computation (e.g., non-string into embedding pipeline). **Remediation**: Fail fast with a typed exception and add unit tests to cover validations.