Skip to content

Validate monitoring_location_id format in waterdata functions#229

Draft
thodson-usgs wants to merge 1 commit intoDOI-USGS:mainfrom
thodson-usgs:validate-monitoring-location-id
Draft

Validate monitoring_location_id format in waterdata functions#229
thodson-usgs wants to merge 1 commit intoDOI-USGS:mainfrom
thodson-usgs:validate-monitoring-location-id

Conversation

@thodson-usgs
Copy link
Copy Markdown
Collaborator

Summary

Closes #188.

Passing an integer or a bare string (without the required AGENCY-ID prefix) to a waterdata function silently wasted an API call and returned confusing empty results. Now all 10 public functions that accept monitoring_location_id raise immediately before any network call:

# Before: silent empty result
get_daily(monitoring_location_id=5129115, parameter_code="00060")
get_daily(monitoring_location_id="dog", parameter_code="00060")

# After:
# TypeError: monitoring_location_id must be a string or list of strings, not int.
#            Expected format: 'AGENCY-ID', e.g., 'USGS-5129115'.
# ValueError: Invalid monitoring_location_id: 'dog'.
#             Expected 'AGENCY-ID' format, e.g., 'USGS-01646500'.

Functions covered: get_daily, get_continuous, get_monitoring_locations, get_time_series_metadata, get_latest_continuous, get_latest_daily, get_field_measurements, get_stats_por, get_stats_date_range, get_channel

Validation rules:

  • Must be a str or list[str] (not int, float, etc.) → TypeError
  • Each string must contain a - separator (AGENCY-ID format) → ValueError
  • None is allowed (optional parameter)

Test plan

  • 9 unit tests covering: valid inputs, None, bare integer, integer in list, missing agency prefix, bare site number, and end-to-end get_daily checks
  • Full test suite passes (128/128)

🤖 Generated with Claude Code

Passing an integer (e.g. 5129115) or a bare string without an agency
prefix (e.g. "dog") to any waterdata function silently wasted an API
call and returned empty data. Now all ten public functions that accept
monitoring_location_id raise before touching the network:

- TypeError if the value is not a string or list of strings
- ValueError if any string doesn't match the 'AGENCY-ID' format
  (e.g. 'USGS-01646500')

Closes DOI-USGS#188.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Format checking for waterdata api inputs

1 participant