You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
965 B
37 lines
965 B
---
|
|
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.
|
|
|