Skip to content

Generated form validates edits against InsertSchema: saving any row with a NULL optional column is blocked outright #227

Closed

Description

@dmealing

Problem / use case

formFile (@metaobjectsdev/codegen-ts-react) resolves every submit against <Entity>InsertSchema, including when the form is seeded with defaultValues for editing. The generated component takes a defaultValues?: Partial<<Entity>Row> prop, so editing is a first-class supported mode — but the validation schema never switches to match it.

The two schemas are not interchangeable, and the difference is exactly the one that matters here:

// InsertSchema — optionals accept absent, REJECT null
resort: z.string().max(160).optional(),
// UpdateSchema — optionals accept absent AND null
resort: z.string().max(160).optional().nullable(),

Opening a row for edit seeds defaultValues from that row, where every unset optional column is null. Those nulls are then validated against InsertSchema:

z.string().max(160).optional()
  undefined -> ok
  ""        -> ok
  null      -> FAILS   <-- what every unset optional column holds

Result: submit is blocked outright for any row holding a NULL optional column. The form renders "Invalid input" against fields the user never touched, handleSubmit never invokes the submit callback, and no request is sent — the save simply does nothing. Since a row typically has at least one unset optional, this affects most edits in practice.

Severity is worth stating plainly: this is not a cosmetic validation-message issue. Any adopter driving the generated form in edit mode against an entity with nullable columns has a non-functional edit path, and the failure presents as errors on unrelated fields rather than as anything pointing at the real cause.

Why it can hide for a long time: an e2e/golden-journey that only ever CREATEs never exercises it, and "the edit form opens and is correctly prefilled" is true right up until you press save. That is exactly how it survived undetected in a downstream project until an unrelated test happened to drive an edit.

Evidence in the published artifact (0.19.1): dist/templates/form-file.js references InsertSchema 3 times and UpdateSchema 0 times, while referencing defaultValues 5 times.

Proposed solution

Make the resolver mode-aware, using the same defaultValues presence that already distinguishes create from edit everywhere else in the template:

const form = useEntityForm(
  <Entity>,
  props.defaultValues !== undefined ? <Entity>UpdateSchema : <Entity>InsertSchema,
  props.defaultValues !== undefined ? { defaultValues: props.defaultValues } : {},
);

This is a one-line change plus adding <Entity>UpdateSchema to the emitted import list. It is semantically the right pairing regardless of the bug — an edit submits a PATCH, so it should validate against the PATCH schema. UpdateSchema still enforces per-field constraints (min/max/enum) on whatever keys are present; it just doesn't demand required keys the PATCH isn't sending, which is correct for FR-035 present-key semantics.

For a TPH subtype form the same .omit({ <discriminator>: true }) treatment applies to the update schema as it currently does to the insert schema.

Verified downstream: applying exactly this makes a previously-unsaveable edit save correctly, with no change to create-path behaviour, across 12 generated entity forms.

Relationship to #223

Related but distinct, and this one is upstream of it. #223 concerns what the submit payload should contain for blank optionals (the ""-vs-omit-vs-null tristate). This issue is that submit never happens at all, so the payload question is unreachable. Fixing #223 alone would not fix this; fixing this alone leaves #223's edit-clear wart intact. They do share the same defaultValues signal, so whoever picks one up may want both — the tristate rule that pairs with the fix above is:

  • blank on CREATE → omit the key (column defaults/NULLs)
  • was set, cleared on EDIT → send explicit null (present-null clears)
  • already blank on EDIT → omit (no pointless null write)

Cross-language impact

None. Form generation is TypeScript-only (codegen-ts-react) — no other port generates forms, so there is no metamodel vocabulary, wire-format, or cross-port conformance implication. This is a generated-output change, so adopters regenerate to pick it up; three-way merge preserves hand-edits.

Alternatives considered

  • Normalize null → "" in defaultValues before seeding the form. Rejected: it makes the form unable to distinguish "was null" from "was empty", which is precisely the state Form generator: tristate-aware blank-optional field submit handling #223's tristate needs, and it would feed "" to non-string controls (a view.image control expects string | null).
  • Loosen InsertSchema optionals to .nullable(). Rejected: it would weaken create-path validation for every consumer of that schema, including the server route, to paper over a client-side mode selection bug.

Activity

  1. added
    bug
    on Jul 20, 2026
  2. dmealing commented on Jul 20, 2026

    @dmealing
    MemberAuthor

    Fixed on main in 60e68808. Ships in the next release.

    The report was accurate on every point — verified the artifact evidence directly against the template source: form-file.ts referenced InsertSchema 3×, UpdateSchema 0×, defaultValues 3×.

    Implemented your proposed fix as written: the resolver now switches on the same defaultValues presence that already distinguishes create from edit everywhere else in the template, and <Entity>UpdateSchema joins the emitted import list. TPH forms get the .omit({ <discriminator>: true }) treatment on both schemas, as you noted.

    Your reasoning that this is the right pairing independent of the bug is what makes it a one-liner rather than a workaround — an edit submits a PATCH, so it validates against the PATCH schema. UpdateSchema still enforces min/max/enum on present keys; it just stops demanding required keys the PATCH isn't sending, which is correct under FR-035 present-key semantics. Both alternatives you listed are recorded as rejected in the commit message, for the reasons you gave.

    Verification

    A regression test asserts the emitted resolver is mode-aware AND guards against the unconditional-InsertSchema shape returning:

    expect(out).toContain("props.defaultValues !== undefined ? ReportUpdateSchema : ReportInsertSchema");
    expect(out).not.toMatch(/useEntityForm\(\s*Report,\s*ReportInsertSchema\s*,/);

    22 golden snapshots regenerated and inspected — the diff is exactly two lines per file (the import and the resolver), nothing else. codegen-ts-react 26 · codegen-ts 1001 (goldens included) · cli 400 — all green; workspace build/typecheck clean.

    An aside your report indirectly caused

    Regenerating the new examples/advanced-modeling spine surfaced a caveat in its own drift gate: it validates against the built dist/, so a source change that has not been rebuilt reads as a false "in sync". It reported clean, and only failed — correctly — after a rebuild. Worth knowing for anyone relying on that gate locally.

    On #223

    Agreed it is distinct and downstream of this. This makes submit happen; #223 governs what the payload then contains for blank optionals. Your tristate rule (omit on create · explicit null when clearing a previously-set field on edit · omit when already blank) is recorded there and pairs cleanly with the defaultValues signal this fix now keys on.

  3. added a commit that references this issue on Jul 20, 2026
  4. dmealing commented on Jul 21, 2026

    @dmealing
    MemberAuthor

    Correction to the "Cross-language impact" section.

    That section claimed "no other port generates forms." That is wrong — codegen-ts-angular also has a form generator (src/templates/form-file.ts). I overlooked it when scoping the issue.

    The fix's scope is unaffected, but the stated reason was. The accurate version:

    • The fix is still correctly confined to codegen-ts-react, because this is a Zod-schema-selection defect: the react form picks between InsertSchema and UpdateSchema, and picked wrong for edit mode.
    • codegen-ts-angular cannot have this defect, because it does not select a Zod schema at all. It emits Angular reactive forms (FormBuilder + per-field Validators — Validators.required, Validators.maxLength, etc.), so there is no Insert-vs-Update decision to get wrong.

    So: "no cross-port conformance implication" stands; "no other port generates forms" does not. To be precise about what I verified — I confirmed only that Angular uses Validators rather than the Insert/Update schema pairing, so it is immune to this specific schema-selection bug. I did not audit whether Angular's reactive-forms edit path has its own, separate nullable-field handling quirk; that would be a distinct investigation, not covered here.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions