-
Notifications
You must be signed in to change notification settings - Fork 4
[DBA-141] Add field validations on datasource adding #81
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
Merged
gasparian
merged 11 commits into
main
from
simon.karan/DBA-141-field-validations-on-datasource-adding
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
67e0a92
[DBA-141] Add field validations on datasource adding
SimonKaran13 d6e65f1
[DBA-141] Extract shared validation helper in datasource manager
SimonKaran13 f3ca6da
[DBA-141] Remove screenshot from repo
SimonKaran13 3f2bcdc
[DBA-141] Align name regex with agent, add port and hostname validation
SimonKaran13 cf3edf0
fixes to pass e2e
04afd37
[DBA-141] Address PR review: stricter validation, union support, veri…
SimonKaran13 35eb85f
[DBA-141] Validate config fields on existing datasource save
SimonKaran13 bae1a63
[DBA-141] use 'whitespace' in the error message instead of 'space'
SimonKaran13 bdaf9e5
[DBA-141] Remove bracket handling from IP validation, add single-vari…
SimonKaran13 79a0c5f
[DBA-141] Fix test assertions to match updated whitespace error message
SimonKaran13 d9dae92
[DBA-141] Add multi-variant union error, clarify empty-segment message
SimonKaran13 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
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,102 @@ | ||
| """Validation rules for datasource fields. | ||
|
|
||
| These rules are shared between the CLI workflow and the Streamlit UI so | ||
| that users get the same feedback regardless of how they create a | ||
| datasource. | ||
| """ | ||
|
|
||
| import ipaddress | ||
| import re | ||
|
|
||
| # The agent requires source names to match this pattern so they can be | ||
| # used unquoted in SQL queries. See databao-agent domain.py. | ||
| _SOURCE_NAME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_]*$") | ||
|
|
||
| # Folder segments in the datasource path are more permissive — they only | ||
| # need to be valid filesystem names. | ||
| _FOLDER_SEGMENT_RE = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$|^[a-zA-Z0-9]$") | ||
|
|
||
| MAX_DATASOURCE_NAME_LENGTH = 255 | ||
|
|
||
| MIN_PORT = 1 | ||
| MAX_PORT = 65535 | ||
|
|
||
| # Hostname: RFC 952 / RFC 1123 — labels separated by dots. | ||
| _HOSTNAME_RE = re.compile(r"^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z0-9-]{1,63})*$") | ||
|
|
||
|
|
||
| def validate_datasource_name(name: str) -> str | None: | ||
| """Return an error message if *name* is invalid, or ``None`` if it is OK. | ||
|
|
||
| Datasource names may contain forward-slash separators to organise | ||
| datasources into folders (e.g. ``resources/my_db``). Folder segments | ||
| are validated as filesystem-safe names. The final segment (the actual | ||
| source name) must match the agent's ``^[A-Za-z][A-Za-z0-9_]*$`` | ||
| pattern so it can be used unquoted in SQL queries. | ||
SimonKaran13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| if not name or not name.strip(): | ||
| return "Datasource name must not be empty." | ||
|
|
||
| if len(name) > MAX_DATASOURCE_NAME_LENGTH: | ||
| return f"Datasource name must be at most {MAX_DATASOURCE_NAME_LENGTH} characters." | ||
|
|
||
| if re.search(r"\s", name): | ||
| return "Datasource name must not contain whitespace." | ||
|
|
||
| segments = name.split("/") | ||
| for segment in segments: | ||
| if not segment: | ||
| return ( | ||
| "Datasource name must not contain empty path segments (for example, leading, trailing, or repeated slashes)." | ||
| ) | ||
|
|
||
| # Validate folder segments (all but the last). | ||
| for segment in segments[:-1]: | ||
| if not _FOLDER_SEGMENT_RE.match(segment): | ||
| return ( | ||
| f"Folder segment '{segment}' may only contain letters, digits, " | ||
| "hyphens, underscores, and dots, and must start and end with " | ||
| "a letter or digit." | ||
| ) | ||
|
|
||
| # Validate the source name (last segment) against the agent's pattern. | ||
| source_name = segments[-1] | ||
| if not _SOURCE_NAME_RE.match(source_name): | ||
| return "Datasource name must start with a letter and contain only letters, digits, and underscores." | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def validate_port(value: str) -> str | None: | ||
| """Return an error message if *value* is not a valid port number.""" | ||
| try: | ||
| port = int(value) | ||
| except ValueError: | ||
| return "Port must be a number." | ||
| if port < MIN_PORT or port > MAX_PORT: | ||
| return f"Port must be between {MIN_PORT} and {MAX_PORT}." | ||
| return None | ||
|
|
||
|
|
||
| def validate_hostname(value: str) -> str | None: | ||
| """Return an error message if *value* is not a valid hostname or IP.""" | ||
| value = value.strip() | ||
| if not value: | ||
| return "Hostname must not be empty." | ||
| # Allow localhost and IP addresses as-is. | ||
| if value == "localhost" or _is_ip_address(value): | ||
| return None | ||
| if len(value) > 253: | ||
| return "Hostname must not exceed 253 characters." | ||
| if not _HOSTNAME_RE.match(value): | ||
| return "Hostname contains invalid characters." | ||
| return None | ||
SimonKaran13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _is_ip_address(value: str) -> bool: | ||
| """Return True if *value* is a valid IPv4 or IPv6 address.""" | ||
| try: | ||
| ipaddress.ip_address(value) | ||
| return True | ||
| except ValueError: | ||
| return False | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.