-
Notifications
You must be signed in to change notification settings - Fork 23
refactor: centralize test and test set type validation #1535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harry-rhesis
wants to merge
5
commits into
main
Choose a base branch
from
fix/validate-test-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dd712a1
refactor(backend): centralize test and test set type validation
harry-rhesis 909c3fe
chore: fix ruff linting errors
harry-rhesis 98c844f
fix(tests): add missing SESSION_SECRET_KEY to test backend env
harry-rhesis 8e870bb
fix(backend): make from_string case-sensitive and strict for TestType…
harry-rhesis f63ad2e
Merge remote-tracking branch 'origin/main' into fix/validate-test-type
harry-rhesis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 3 additions & 4 deletions
7
.../src/rhesis/backend/alembic/versions/1776e6dd47d3_add_model_type_column_to_model_table.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| from pydantic import ValidationError | ||
|
|
||
| from rhesis.backend.app.constants import TestSetType, TestType | ||
| from rhesis.backend.app.schemas.multi_turn_test_config import validate_multi_turn_config | ||
|
|
||
|
|
||
| def format_test_type(v: Optional[str]) -> Optional[str]: | ||
| """Format test type to title case and validate against allowed types.""" | ||
| if v is None: | ||
| return None | ||
|
|
||
| formatted = v.title() | ||
| allowed_types = [t.value for t in TestType] | ||
|
|
||
| if formatted not in allowed_types: | ||
| raise ValueError(f"Invalid test type '{v}'. Allowed values are: {', '.join(allowed_types)}") | ||
|
|
||
| return formatted | ||
|
|
||
|
|
||
| def format_test_set_type(v: Optional[str]) -> Optional[str]: | ||
| """Format test set type to title case and validate against allowed types.""" | ||
| if v is None: | ||
| return None | ||
|
|
||
| formatted = v.title() | ||
| allowed_types = [t.value for t in TestSetType] | ||
|
|
||
| if formatted not in allowed_types: | ||
| raise ValueError( | ||
| f"Invalid test set type '{v}'. Allowed values are: {', '.join(allowed_types)}" | ||
| ) | ||
|
|
||
| return formatted | ||
|
|
||
|
|
||
| def validate_test_config_content(v: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]: | ||
| """ | ||
| Validate test_configuration JSON based on content. | ||
|
|
||
| For multi-turn tests (when goal is present), validates against MultiTurnTestConfig schema. | ||
| """ | ||
| if v is None: | ||
| return None | ||
|
|
||
| # If 'goal' is present, this is a multi-turn test configuration | ||
| if "goal" in v: | ||
| try: | ||
| # Validate using multi-turn config schema | ||
| validated_config = validate_multi_turn_config(v) | ||
| # Return as dict for storage | ||
| return validated_config.model_dump(exclude_none=True) | ||
| except ValidationError as e: | ||
| # Re-raise with more context | ||
| error_messages = [] | ||
| for error in e.errors(): | ||
| field = " -> ".join(str(loc) for loc in error["loc"]) | ||
| error_messages.append(f"{field}: {error['msg']}") | ||
| raise ValueError(f"Invalid multi-turn test configuration: {'; '.join(error_messages)}") | ||
|
|
||
| # For other configurations, allow any valid JSON | ||
| return v | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical:
v.title()won’t normalize snake_case/underscore inputs (e.g."single_turn" → "Single_Turn"), so those will now 422 even thoughTestType.from_string()/TestSetType.from_string()explicitly support snake_case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack — if rejecting snake_case/underscore inputs is intentional for API consistency, then the
v.title()approach here is fine and my earlier comment isn’t a blocker.One thing to consider: since we now have both
TestType.from_string()(which does accept snake_case) and these validators (which intentionally don’t), it may be worth updating/removingfrom_string()(or at least its docstring) to avoid sending mixed signals about what inputs are supported.