Skip to content

fix(profile): pick employment fields from an unrefined schema - #912

Merged
thinkclay merged 1 commit into
mainfrom
fix/zod-pick-refined-schema
Sep 25, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/zod-pick-refined-schema

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Closes #910.

zod 4.6 refuses .pick() on an object that carries a refinement:

Error: .pick() cannot be used on object schemas containing refinements

zod 4.1 allowed it and quietly dropped the refinement, which is the only reason the five employment cards worked. Each picks a single field out of profileEmploymentInputSchema inside its save handler, and that schema ends in .superRefine(...). On 4.6 the .pick() throws before safeParse is reached, inside an event handler — so ticking a licence class, a military status, an availability window, a work location, or typing an hourly rate would save nothing and say nothing.

That is what made #870 red. Latent on main today, which is still on zod 4.1.13.

Changes

profileEmploymentBaseSchema is the z.object({…}).partial(), exported on its own. profileEmploymentInputSchema is now that base plus the cross-field travel rule — routers and whole-payload validation are unchanged. The five cards pick from the base.

They also each had this:

if (!result.success) return

A validation failure discards something the user just chose, with no toast, no log, nothing. That is what turned a library guard into invisible data loss: the throw and the silent-discard path lead to the same place, and neither is observable. Each now logs the zod issues before returning. I'd argue that half matters more than the schema split — the split fixes this bug, the logging is why the next one won't be silent.

employment-schema-pick.test.ts asserts every field the cards pick is pickable without throwing, that a picked field still enforces its own rules (the 3-location max), and that the refined schema still rejects open_to_travel with no distance. It fails the moment a refinement is attached to the schema the cards import — which is the only way to catch this before a browser does.

Verification

Ran both suites against zod 4.6.5 by temporarily moving the catalog pin, then reverted it — the bump belongs to #870, not here:

result
zod 4.1.13, before 40 passed, 0 failed — the pin hid the bug
zod 4.6.5, before 31 passed, 9 failed
zod 4.6.5, after 49 passed, 0 failed

Plus on 4.1.13: scf-core full suite 1267 passed / 0 failed, scf-core and scaffald-app typecheck and lint clean, pnpm prepush green.

One thing I found and did not fix

packages/supabase/functions/_shared/schemas/consolidated.ts is a near-duplicate of packages/scf-trpc/src/schemas/consolidated.ts — they differ by an import path and one line where ProfileEmploymentOutput is inferred from the input schema in the scf-trpc copy. Nothing imports the _shared one: the cards resolve through @scf/supabase/client-types → @scf/trpc/schemas → scf-trpc. I changed both in lockstep so they do not drift further, but a dead 700-line duplicate of the schema that everything actually depends on is worth deleting in its own PR.

🤖 Generated with Claude Code

zod 4.6 refuses `.pick()` on an object that carries a refinement:

    Error: .pick() cannot be used on object schemas containing refinements

zod 4.1 allowed it and quietly dropped the refinement, which is the only
reason the five employment cards worked. Each one picks a single field out
of `profileEmploymentInputSchema` inside its save handler, and that schema
ends in `.superRefine(...)`. On 4.6 the `.pick()` throws before `safeParse`
is reached, inside an event handler — so ticking a licence class, a
military status, an availability window, a work location or typing an
hourly rate would save nothing, and say nothing.

That is what made #870 (the backend Dependabot group, zod 4.1.13 -> 4.6.5)
red: 9 failures in profile-employment-left.test.tsx, every one an
array-valued field. Latent on main today, since main is still on 4.1.13.

## Changes

`profileEmploymentBaseSchema` is the `z.object({...}).partial()`, exported
on its own. `profileEmploymentInputSchema` is now that base plus the
cross-field travel rule. Routers and whole-payload validation keep using
the refined one and are unchanged; the cards pick from the base.

The five cards each swapped to the base schema. They also each had:

    if (!result.success) return

which discards something the user just chose without a toast, a log or a
console warning. That is what turned a library guard into invisible data
loss — the throw and the silent-discard path lead to the same place. Each
now logs a warning with the zod issues before returning.

`employment-schema-pick.test.ts` asserts that every field the cards pick is
pickable without throwing, that a picked field still enforces its own rules
(3-location max), and that the refined schema still rejects `open_to_travel`
with no distance. It fails the moment a refinement is attached to the schema
the cards import.

## Verification

Ran both suites against zod 4.6.5 by temporarily moving the catalog pin,
then reverted it — the bump itself belongs to #870, not here:

    zod 4.1.13, before  ->  40 passed, 0 failed  (the pin hid the bug)
    zod 4.6.5,  before  ->  31 passed, 9 failed
    zod 4.6.5,  after   ->  49 passed, 0 failed

scf-core full suite 1267 passed / 0 failed on 4.1.13; scf-core and
scaffald-app typecheck and lint clean.

Note: `packages/supabase/functions/_shared/schemas/consolidated.ts` is a
near-duplicate of the scf-trpc copy that nothing imports — the cards resolve
through `@scf/supabase/client-types` to `@scf/trpc/schemas`. Changed in
lockstep so the two do not drift further apart; the duplication itself is
left alone.

Co-Authored-By: Claude Opus 5 <[email protected]>
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.

fix(profile): zod 4.6 throws on .pick() over a refined schema — five employment cards stop saving, silently

1 participant