Skip to content

fix(forecast): validate query field parameter and reject invalid values with 400 (closes #891) - #917

Closed
mopxam-art wants to merge 1 commit into
Heliobond:mainfrom
mopxam-art:fix/forecast-field-validation
Closed

mopxam-art wants to merge 1 commit into
Heliobond:mainfrom
mopxam-art:fix/forecast-field-validation

Conversation

@mopxam-art

Copy link
Copy Markdown

Overview

Closes #891 by validating the field query parameter across forecast routes (/:id, /seasonal/:id, and /accuracy/:id) and rejecting unknown metrics with 400 Bad Request listing valid options (power_output_kw, efficiency_pct), eliminating silent data substitution.

Problem & Motivation

Previously, parseForecastField defaulted any string other than "efficiency_pct" to "power_output_kw". A typo such as ?field=efficiency or ?field=power was silently coerced to power_output_kw, returning 200 OK with unexpected data. Sibling endpoints uniformly reject unrecognized query enums with 400 Bad Request.

Changes

  • src/routes/forecast.ts:
    • Defined VALID_FORECAST_FIELDS = ["power_output_kw", "efficiency_pct"] as const.
    • In parseForecastField, preserved default "power_output_kw" for omitted or empty field parameters.
    • Throws badRequest when an unknown metric name is supplied, explicitly listing valid options in the error message.
  • src/__tests__/forecastFieldValidation.test.ts:
    • Added 110 exhaustive unit tests covering boundary clamping, typo combinatorics, URL encodings, format variations (JSON/CSV), and state isolation.

Verification

  • All 155 unit tests pass cleanly (npx jest src/__tests__/forecast.test.ts src/__tests__/forecastFieldValidation.test.ts --forceExit).
  • TypeScript typecheck passes with 0 errors (npx tsc --noEmit).
  • ESLint passes with 0 warnings/errors (npx eslint src/routes/forecast.ts).
  • Production build compiles cleanly (npm run build).

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.

Forecast field query silently coerces unknown values instead of rejecting them

2 participants