Skip to content

fix(persistence): run validate on migrated state - #11

Merged
bcheung merged 1 commit into
mainfrom
fix/validate-migrated-state
Nov 6, 2026
Merged

bcheung merged 1 commit into
mainfrom
fix/validate-migrated-state

Conversation

@bcheung

@bcheung bcheung commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

createPersister({ validate }) is documented as "return false to reject the persisted state", but it only ran on state saved by the current version. When a version bump triggered migrate, the migrated state was returned straight away and validate was skipped. A migration that produces bad data (a renamed field it missed, an invalid step) reached the flow even though the app had a check that would reject it.

Now every restore goes through validate, migrated or not:

const persister = createPersister({
  store,
  validate: (state) => typeof state.context.email === "string",
});

// v1 saved { mail: "a@b.co" }; the v2 migration forgets to rename it
<Flow flow={signupFlow} persister={persister} />
// before: restores { mail } into v2 and the email step breaks
// after:  rejected, the flow starts fresh, onDiscard gets "validation_rejected"

Behavior change: migrated state that validate rejects is now discarded instead of restored. Released as a minor bump.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📝 Documentation
  • ♻️ Refactoring
  • ✅ Tests

Checklist

  • Tests pass (bun run test)
  • Types are valid (bun run typecheck)
  • Code is formatted (bun run check)
  • Added/updated tests
  • Updated docs (if needed)
  • Added changeset (if needed - run bun changeset)

Testing

  • bun run test: 390 tests pass. Adds a case where validate rejects migrated state (returns null, reports validation_rejected with fromVersion/toVersion, skips onRestore), and asserts validate now runs on successful migrations.
  • Removes the "validate does not run on migrated state" notes added in feat: report discarded flow restores #10 and documents that validate runs on every restore.

@bcheung
bcheung changed the base branch from feat/restore-discard-callback to main November 6, 2026 03:55
@bcheung
bcheung force-pushed the fix/validate-migrated-state branch from 7fc4d65 to 9825a54 Compare November 6, 2026 03:56
@bcheung
bcheung marked this pull request as ready for review November 6, 2026 03:56
@bcheung
bcheung merged commit cb4290d into main Nov 6, 2026
1 check passed
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.

1 participant