Skip to content

feat(forms): conditional required fields with requiredWhen - #707

Merged
jimhoyd merged 1 commit into
mainfrom
feat/forms-required-when
Sep 25, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
feat/forms-required-when

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Fixes #528

Implements the owner's decision on #528: requiredWhen: {field, in: [values]} on a dependent field.

Behavior

  • Submission: when the sibling was submitted once with a value in in, an empty dependent field gets the same 422 is required error as a required field (a conditional checkbox must be checked). Otherwise it is optional. Its other rules (length, pattern, enum, type, bounds) apply whenever it has a value.
  • Startup (validateFlow) refuses: undeclared sibling; self-reference; a sibling without a fixed value set (only a select, using its options[].value narrowed by enum if present, or an input with enum qualify; checkboxes and free text are refused); an in value the sibling does not allow; a sibling that has its own requiredWhen (one level only); and requiredWhen together with required. The config schema refuses an empty or duplicated in, more than 128 values (same as the enum/options limit), and unknown keys.
  • required rule: fields are required by default, so requiredWhen takes the place of required. required: true contradicts it, and required: false would only repeat it, so both are refused. That leaves one way to write it.
  • Rendering: server-only. The input is not marked required in HTML, and there is no JS or show/hide. No automatic hint is added: the author's existing description is the place to say when the field becomes required, and the README says so.
  • Out of scope (documented in README and llms.txt): AND/OR, not-equal, comparisons, cross-field ordering, conditions on free-text fields, show/hide. These stay in onSubmit. Relative date bounds are split out to [Request]: relative date bounds for forms date and datetime-local fields #705.

Changes

  • packages/forms/src/forms.ts: schema, validateRequiredWhen, isRequired, rendering
  • packages/forms/urlcode.json regenerated (npm run build:addons)
  • Tests: condition met/unmet, select and enum siblings, conditional checkbox, interplay with length/number bounds, unknown sibling value, HTML required absent, and all startup and schema refusals
  • README validation section, CHANGELOG (Unreleased), package llms.txt. No docs/ page lists forms validation rules. llms-full.txt check is unchanged.

Evidence

  • npm run typecheck: pass
  • forms tests: 23/23 pass
  • npm run verify: exit 0 (core 808/808, workspaces all pass)
  • npm run test:package: exit 0 ("Packed installation and starter/cookbook checks passed")

CI passing is not a security review.

🤖 Generated with Claude Code

A field can declare requiredWhen: {field, in} to be required only when a
sibling select or enum field was submitted with one of the listed values,
and optional otherwise. Activation refuses an undeclared, self-referencing,
free-text or itself-conditional sibling, an in value the sibling does not
allow, and requiredWhen together with required. Enforcement is server-only;
the input is not marked required in HTML.

Relative date bounds are split out to #705.

Fixes #528

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd merged commit fb76cfc into main Sep 25, 2026
16 checks passed
@jimhoyd
jimhoyd deleted the feat/forms-required-when branch September 25, 2026 01:07
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.

[Gap]: add bounded conditional and date-range validation to forms

1 participant