Skip to content

Indicate required vs optional fields #390

Open

Description

@joshunrau

Current Implementation and Limitations

This is stored but not indicated

Associated Application Components

Client

Proposed Solution

Add some sort of star to indicate a field is required

Estimated Difficulty

Low

Priority

High

Activity

  1. added
    Enhancement
    on May 12, 2023
  2. self-assigned this
    on May 12, 2023
  3. added
    Difficulty: Low
    Priority: High
    on May 12, 2023
  4. changed the title [-]Indicate required vs optional fields[/-] [+][Enhancement]: Indicate required vs optional fields[/+] on May 12, 2023
  5. added a commit that references this issue on Sep 26, 2023
  6. added a commit that references this issue on Sep 27, 2023
  7. joshunrau commented on Mar 5, 2024

    @joshunrau
    CollaboratorAuthor

    Make as required in JSLibs please @david-roper

  8. added
    Priority: Medium
    Difficulty: High
    Area: Instruments
    Area: Web
    and removed
    Priority: High
    Difficulty: Low
    on Sep 27, 2026
  9. changed the title [-][Enhancement]: Indicate required vs optional fields[/-] [+]Indicate required vs optional fields[/+] on Sep 27, 2026
  10. added
    Upstream
    Needs Decision
    on Sep 27, 2026
  11. joshunrau commented on Oct 26, 2026

    @joshunrau
    CollaboratorAuthor

    Still open on main (d86740e), and nothing upstream supports it yet. libui's Form renders no required marker, in the installed 6.16.1 or in the latest 6.18.0. BaseField in libui-form-types has only description, disabled, kind and label, in the installed 1.4.0 and in the latest 1.5.0. Neither repo has an issue for it.

    The flag is no longer stored

    When this was filed, isRequired?: boolean was on the base field type (packages/common/src/instruments/interfaces/form/form-fields.types.ts:28 at 5752c8c). No published version of libui-form-types has ever had the key. runtime-core's FieldMixin does not add it, and c73d4fa (2024-09-06) replaced isRequired: z.boolean().optional() with disabled in packages/schemas. Whether a field is required now lives only in validationSchema.

    Four isRequired keys are left over, and nothing reads them:

    The schema alone cannot drive the marker

    To make a field required only under some condition, authors make the key .optional() and enforce it in a superRefine. Examples: Happiness reasonNotSatisfied (:169, :172), playground Multilingual-Form-With-Dynamic-Field (:78, :80) and the guidelines example (AGENTS.md:937, :939). In apps/web/src/components/StartSessionForm/StartSessionForm.tsx, subjectDateOfBirth and subjectSex are static fields (:162, :167) that are optional in the shape (:225-241) and required only for PERSONAL_INFO (:291). A marker read from the shape shows all of these as optional.

    libui cannot read the schema at all. It types it as ZodTypeLike (Form.tsx:63), which exposes only safeParseAsync. ODC can, because runtime-core types validationSchema as z3.ZodType | z4.ZodType (packages/runtime-core/src/types/instrument.base.ts:161).

    Recommendation: an explicit flag, with a default derived from the schema

    1. Upstream. Add isRequired?: boolean to BaseField. The flag only controls display; validation stays in the schema. Existing keys and instruments copied from the guidelines then work without edits.
      • libui should render the marker once, in a shared FieldGroup label. Today 16 field files each render their own <Label>.
      • Set aria-required on controls whose role supports it. slider does not, so the label also needs screen-reader text. Add the legend string to libui's en/es/fr translations.
      • Do not use the required attribute. libui's <form> (Form.tsx:192) has no noValidate, so the browser would block onSubmit and the localized zod errors would never show.
    2. ODC types. Bump both catalog: entries. runtime-core picks up the key through FieldMixin, and translateScalarField already copies it with ...field.
      • Also add the key to $$FormInstrumentBaseField (packages/schemas/src/instrument/instrument.form.ts:21-27). Its satisfies does not catch a missing optional key. Without the key, $AnyInstrument.parseAsync strips the flag from static fields wherever validate is on: the playground preview (apps/playground/src/preview/PreviewApp.tsx:67) and apps/web in dev (apps/web/src/hooks/useInstrument.ts:20). The outreach JSON Schema is also generated from it.
      • This ships in a runtime/v1 release, because runtime/v1 re-bundles runtime-core.
    3. Derived default. Add a helper to packages/instrument-utils and apply it in FormContent.
      • For each field the author left unset, set isRequired to !shape[key].safeParse(undefined).success. That covers .optional(), .default() and .partial() in both zod majors.
      • Walk both flat and grouped content. Wrap dynamic render the same way translateFormFields does (translate.ts:184-195).
      • Call safeParse only when the flag is unset. It throws on an async refinement, which libui accepts because it validates with safeParseAsync. Decide up front whether that case renders unmarked or fails loudly.
      • upload.ts already finds the object shape in four places (:448-457, :483-501, :810-820, :835-847), including the zod 3 ZodEffects unwrap. Put one shared lookup next to the helper instead of writing a fifth.
    4. Content. Set isRequired: true in the render of Happiness reasonNotSatisfied and of playground favoriteColor. Document the flag in the guidelines (that means a release of @opendatacapture/instrument-guidelines) and in docs/en/5-reference/5.0-form-instrument.md.

    Land this after #1825, which edits the same instrument and instrument-completion.spec.ts.

    Tests

    • The helper's own test in packages/instrument-utils/src/__tests__/: a required key, an .optional() key, a .default() key, a zod 3 superRefine object, a zod 4 object and a dynamic field. Also check that the author's flag overrides the derived value.
    • packages/react-core/src/components/FormContent/__tests__/FormContent.test.tsx: a required field shows the marker and an optional one does not.
    • packages/instrument-utils/src/__tests__/translate.test.ts: the flag survives translation of static and dynamic fields.
    • packages/schemas/src/instrument/__tests__/instrument.form.test.ts: the parsed output keeps the flag.
    • testing/src/specs/instrument-completion.spec.ts: in the Happiness Questionnaire, the sliders and the overall question are marked and "causes" is not. After answering "No", the reason field is marked.

    Open questions

    • Asterisk on required fields, or "(optional)" on optional ones? I lean towards the asterisk. When an override is missing, an unmarked required field misleads less than a required field labelled "(optional)".
    • Derive a default at all, or rely on the explicit flag only?
    • Should the 13 <Form> sites in apps/web get markers too? They get no derived default from FormContent. In StartSessionForm, subjectSex and subjectDateOfBirth would also have to become dynamic fields that depend on subjectIdentificationMethod.
    • Handle record-array fieldsets and number-record rows now, or only top-level keys?

    Triage note written with Claude Code. Code references point at main @ d86740e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: InstrumentsArea: WebDifficulty: HighEnhancementNeeds DecisionPriority: MediumUpstream

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions