Skip to content

fix(ui): submit the only available query type and reject an empty target - #537

Merged
jsenecal merged 2 commits into
mainfrom
fix/512-514-form-validation
Nov 21, 2026
Merged

jsenecal merged 2 commits into
mainfrom
fix/512-514-form-validation

Conversation

@jsenecal

Copy link
Copy Markdown
Collaborator

Problem

The query form keeps its values in three places: react-hook-form (which runs the vest validation), the useFormState store's form values, and the store's selections (which the selects display). They drift apart.

  • Form rejects the only available query type as unselected #512: when the selected locations have one query type in common, locationChange set it in form.queryType only. The select showed no selection, and react-hook-form still had '', so a submit failed with "Query Type must be specified." until the user picked the only option by hand.
  • A typed-then-cleared target passes validation #514: a target that is typed and then cleared becomes ['']. The vest rule only checked that the target list was not empty, so the form submitted and switched to the results view with an empty target.

Change

Two commits, one per issue.

#512 (hooks/use-form-state.ts, components/looking-glass-form.tsx)

  • locationChange also sets selections.queryType when it chooses the only query type, so the select shows it.
  • The form mirrors the store's form values into react-hook-form with setValue in an effect that runs whenever they change. It replaces the effect that copied them once on mount to restore the previous query.

Why a mirror effect rather than a setValue next to locationChange: the store changes form values without going through handleChange in three places, and all three drift the same way.

  1. locationChange choosing the only query type (Form rejects the only available query type as unselected #512).
  2. reset() from the header title. The title is shown with the form, and the form stays mounted, so react-hook-form kept the previous query. A submit after clicking the title passed validation with values the store no longer had.
  3. Restoring the previous query after returning from results (already handled on mount; now handled by the mirror).

A setValue at the locationChange call site would fix only the first. handleChange still calls setValue itself, so the mirror writes the same values a second time. That is harmless, and I left those calls alone to keep the diff small. The setValue calls pass no options, so the mirror does not run the vest resolver on each store change. There is no URL prefill on main.

The selection is set as { value, label }. react-select matches the selected option by value, so the option's group is not needed.

#514 (components/looking-glass-form.tsx)

  • The queryTarget vest rule now requires every target to contain a non-whitespace character: isArrayOf(enforce.isString().matches(/\S/)).isNotEmpty(). [''], a whitespace-only target and an empty value among several targets (['192.0.2.1', '']) all fail with messages.noInput for web.text.queryTarget, the message an empty target already used. vest 3.2.8 has no isNotBlank, so the rule uses matches.

Targets can be several values in the store and the API, but the UI only ever sets one. I treated a list as invalid if any value is blank, rather than dropping the blank values, so the form never sends a target the user did not see. A field-less directive's ['null'] target and select targets still pass.

Tests

The #506 tests that documented both bugs now assert the fix:

  • "does not pass an automatically chosen query type to the form validation" becomes "shows and submits an automatically chosen query type". It checks the store selection, that the select's control shows "Ping", and that a submit with a target switches to the results with the expected form values.
  • "accepts a target that was typed and then cleared" becomes "reports a target that is typed and then cleared with the configured message", with a whitespace-only case.

New tests: "does not submit the previous query after the form is reset", "reports an empty value among several targets", and a selections.queryType assertion in the store's "chooses the query type when only one directive is in common".

With origin/main's code and the #512 tests:

       × chooses the query type when only one directive is in common 9ms
     × shows and submits an automatically chosen query type 123ms
     × does not submit the previous query after the form is reset 1428ms
      Tests  3 failed | 44 passed (47)

Reverting only the locationChange selection fails the two selection tests; reverting only the mirror effect fails the submit and reset tests:

== no selection fix
       × chooses the query type when only one directive is in common 10ms
     × shows and submits an automatically chosen query type 102ms
      Tests  2 failed | 45 passed (47)
== no mirror
     × shows and submits an automatically chosen query type 1338ms
     × does not submit the previous query after the form is reset 1500ms
      Tests  2 failed | 45 passed (47)

With the old queryTarget rule and the #514 tests:

     × reports a target that is typed and then cleared with the configured message 1227ms
     × reports a target that is only whitespace with the configured message 1323ms
     × reports an empty value among several targets 1120ms
      Tests  3 failed | 22 passed (25)

With both fixes: biome format, biome lint and tsc --noEmit are clean; Test Files 25 passed (25), Tests 207 passed (207).

Changelog

Notes

Found but not changed here:

  • When the location changes and the current query type is no longer offered, the store keeps the old form.queryType and selections.queryType. Validation catches it (inside(queryTypes)), but the select still shows the old option.
  • When locationChange chooses the only query type, it does not clear an existing target, unlike a manual query type change in handleChange.
  • reset() does not clear react-hook-form's errors, so an error shown before a title reset stays visible.

Closes #512
Closes #514

When the selected locations have one query type in common,
`locationChange` set it in the store's form values but not in its
selections or in react-hook-form. The select showed nothing, and a
submit failed with "Query Type must be specified." until the user
picked the only option by hand.

`locationChange` now also sets the query type selection. The form
mirrors the store's values into react-hook-form whenever they change,
instead of copying them once on mount. This also covers the other
values the store sets on its own: the query restored after returning
from results, and a reset from the header title, which left the
previous query in react-hook-form so it could still be submitted.

The #506 test that documented the bug now asserts the fix, and a test
covers the reset.
A target that was typed and then cleared is `['']`. The form checked
only that the target list was not empty, so it submitted and switched
to the results view with an empty target.

Every target must now contain a non-whitespace character, so `['']`,
a whitespace-only target and an empty value among several targets
fail with the configured `messages.noInput` message for
`web.text.queryTarget`.

The #506 test that documented the bug now asserts the fix.
@jsenecal jsenecal added bug Confirmed bug or unexpected behavior frontend Next.js UI and the runtime UI build labels Nov 21, 2026
@jsenecal jsenecal self-assigned this Nov 21, 2026
@jsenecal
jsenecal requested a review from WilhelmZA November 21, 2026 04:45
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 85.356% (+0.001%) from 85.355% — fix/512-514-form-validation into main

@WilhelmZA WilhelmZA left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 65c22336.

I read the changes to use-form-state.ts and looking-glass-form.tsx and followed QueryType, the hidden QueryTarget input, reset() and back(). The single query type is now selected and submitted, and a target with only whitespace is rejected. The field-less ['null'] target and select targets still pass validation. CI gate passes and both commits are verified.

#545 and #567 are stacked on this branch and change the same four files.

Nits:

  • The body has a Changelog section with three entries but CHANGELOG.md is not edited. Please add them under [Unreleased], here or in the release PR.

@WilhelmZA WilhelmZA added the reviewing Issue is being reviewed by maintainers label Nov 21, 2026
@jsenecal
jsenecal merged commit b9517a3 into main Nov 21, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Confirmed bug or unexpected behavior frontend Next.js UI and the runtime UI build reviewing Issue is being reviewed by maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A typed-then-cleared target passes validation Form rejects the only available query type as unselected

3 participants