Reject empty search queries with 400; add schema validation and isolated test #475
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


This PR improves the search API by preventing empty or whitespace-only queries from being processed, saving resources and providing clear feedback to clients. It enforces validation at the schema level, ensures a consistent HTTP 400 response for invalid queries, and adds robust, isolated tests.
Changes
Validation
Enforces non-empty, non-whitespace query in SearchRequest using Pydantic’s constr(min_length=1, strip_whitespace=True) and a custom validator.
Error Handling
Adds an app-level RequestValidationError handler that returns HTTP 400 with a clear message for /api/v1/search validation errors (default 422 for other endpoints is preserved).
Keeps a defensive runtime check in the router for extra safety.
Testing
Adds test_search_router_isolated.py: a fast, self-contained test that mounts only the search router, stubs auth/db dependencies, and asserts 400 for empty/whitespace queries.
Adds schema validation unit tests (test_search_schema_validation.py) and a direct runner script (run_schema_validation_direct.py) for local validation.
Developer Experience
Isolated tests do not require a running database or external services, making local development and PR review faster and more reliable.
How to Test
Isolated Router Test (no DB/external deps):
Schema Validation Test:
Direct Schema Validation Script:
Integration/CI:
Ensure CI sets required environment variables (POSTGRES_SERVER, GH_TOKEN_LIST, etc.).
If dependency installation fails due to blar-graph, consider removing or replacing it in requirements.txt.
Changelog
Reject empty/whitespace search queries with HTTP 400 and clear error message
Enforce schema-level validation for SearchRequest.query
Add app-level handler to map search validation errors to 400
Add isolated router test for empty/whitespace query rejection
Add schema validation unit tests
Summary by CodeRabbit
Release Notes
New Features
Tests