Skip to content

fix: store event data as published and validate it in the form the store keeps - #97

Merged
dillonstreator merged 2 commits into
mainfrom
fix/event-data-round-trip
Nov 28, 2026
Merged

dillonstreator merged 2 commits into
mainfrom
fix/event-data-round-trip

Conversation

@dillonstreator

Copy link
Copy Markdown
Member

What was wrong

publish stored the schema's output, and the processor parsed that stored value again as input before every delivery. Three things followed for anyone using a real Zod/Valibot schema:

  • z.date() events were dead-lettered on their first claim on Postgres, Redis and DynamoDB. Those stores keep data as JSON, so the Date came back as an ISO string and failed z.date(). The handler never ran. The memory store kept Dates (structuredClone), so unit tests passed and the problem only showed up in production.
  • Transforms ran twice. z.string().transform((s) => s + "!") delivered "bob!!".
  • Transforms whose output isn't valid input never delivered. z.string().transform(Number) stored a number and failed z.string() at delivery, on every store including memory.

On top of that, priority, perKey.key and defer functions got the raw stored JSON, typed as the schema output (audit TXOB-CONS-002; verified High).

What changed

The contract is now: validate at publish in the form the store will hand back, store the published input, and parse once per delivery.

  • publish stores the data it was given (the schema's input) and returns the parsed output as before. The processor parses the stored input once before fan-out functions and once per delivery, so transforms apply once and handlers and priority/perKey/defer functions all get the output type their signatures already promised.
  • New optional TxOBStore.roundTripData(data) returns data as the store will read it back. publish validates that form. Postgres, Redis, DynamoDB and memory use a JSON round-trip. MongoDB uses a BSON round-trip with the collection's bsonOptions, so Dates survive and undefined becomes null (as the driver stores it). Data the store can't keep (a Date under z.date() in a JSON store) now throws TxOBValidationError at publish, inside the caller's transaction, with a message that names the cause and suggests z.coerce.date(). Before, it was dead-lettered later.
  • The memory store keeps event data as JSON, like the Postgres, Redis and DynamoDB stores, so this class of bug fails in unit tests.
  • If the stored data doesn't pass the schema at fan-out, the fan-out functions aren't called. The deliveries get default settings and are dead-lettered when claimed, as stored-data validation failures already were (with onDeliveryDead).
  • README (how data is stored, z.coerce.date(), deterministic defaults and transforms, custom stores), MIGRATING (0.6 → 0.7), changeset.

Compatibility

  • Rows written by 0.6.0 still deliver. They hold the output, and the processor parses them exactly as 0.6.0 did. A contract test inserts a 0.6.0-shaped row on every backend and checks that it delivers. (A 0.6.0 row with a non-idempotent transform still gets that transform applied a second time, as on 0.6.0. A row can't tell us which shape it holds without a format marker. I didn't add one: it would be a schema change.)
  • Mixed versions during a rollout are safe. A 0.6.0 processor reading new input-shaped rows parses them once, which is what it should have done all along.
  • Stored data shape: new txob_events.data rows hold the published input, not the output. This matters only to people who query that column directly.
  • Behavior change: publishes that used to succeed and then be dead-lettered (e.g. z.date() on Postgres) now throw. Defaults and transforms run at delivery: a .default(() => randomUUID()) now gives each delivery a different value. Both are documented in README and MIGRATING.
  • Public types: TxOBStore gains an optional roundTripData. createOutbox accepts it through Pick<TxOBStore, "insertEvent" | "roundTripData">. TxOBValidationError gets an optional third constructor argument. All additive.
  • Semver: minor. The CHANGELOG has shipped behavior changes as pre-1.0 minors (0.5, 0.6), and this changes what publish accepts and what it stores.

How it was tested

Real backends in temporary containers: Postgres 17 (postgres:17-alpine), MongoDB 9 single-node replica set, Redis 8 (redis:8-alpine), DynamoDB Local.

  • pnpm install --frozen-lockfile, pnpm test:prepare, pnpm typecheck, pnpm build (ESM + CJS): pass. The repo has no lint script.
  • Full suite: 602/602 passed (baseline on main: 557/557).
  • New contract tests run on every store: memory, Postgres, 8 Postgres ORM adapter setups (Drizzle on node-postgres and on postgres.js, Kysely, Prisma, postgres.js, Sequelize, Knex, TypeORM), MongoDB, Redis (ioredis, node-redis, iovalkey) and DynamoDB. The test schemas mimic z.date(), z.coerce.date(), a string transform and a type-changing transform:
    • handlers and priority/perKey/defer functions get the output, with each transform applied once;
    • z.date() data is rejected at publish on JSON stores and delivered as a Date on MongoDB; nothing is dead-lettered;
    • an event stored the 0.6.0 way delivers.
  • Before/after: with src/ reverted, 12 of the new contract runs fail (Postgres, DynamoDB, MongoDB, memory and all three Redis clients). With the fix, all pass.
  • Audit PoC poc-dates.mjs with real zod 4.6.7: z.date() now throws at publish on the memory store as well as Postgres. A z.coerce.date() variant delivers a Date on Postgres, and the transform runs once ("bob!").

For the reviewer to decide

  • Contract choice. I store the input and parse at delivery. The alternative was to keep storing the output and skip re-parsing at delivery. That fixes double transforms but gives handlers ISO strings typed as Date, and it breaks z.coerce.date(), which the code comment shows was the intended way to get dates. The cost of my choice: defaults and transforms must be deterministic. It's documented, but say if you'd rather go the other way.
  • Fail fast vs. dead-letter. Rejecting at publish fails the caller's business transaction instead of dead-lettering later. I think that's right for "validated on publish", but it is a visible change.
  • This PR and the orphan/key/defer PR both edit the fan-out loop in src/processor.ts, and both add a "Migrating from 0.6 to 0.7" section. The conflicts are textual. Whichever merges second needs a rebase.

Not in this PR

Orphaned deliveries, perKey keys, the fan-out cap and defer opt-out are in a separate PR. Release, packaging, CI and docs hygiene are in another, and the fan-out/rolling-deploy proposal in a third. The PR descriptions list what was skipped.

publish stored the schema's output and the processor parsed it again as
input. Dates under z.date() came back from JSON as strings and were
dead-lettered on their first claim on Postgres, Redis and DynamoDB,
transforms ran twice, and transforms whose output isn't valid input
never delivered. The memory store kept Dates, so tests passed.

Store the published input instead, validate it at publish in the form
the store hands it back (new optional TxOBStore.roundTripData), give
priority, key and defer functions the parsed output like handlers, and
keep memory store data as JSON. Events stored by 0.6.0 parse as before.
@codecov

codecov Bot commented Nov 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.13084% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.13%. Comparing base (f19e00f) to head (767b90d).

Files with missing lines Patch % Lines
test/contract.ts 98.36% 1 Missing ⚠️
test/helpers.ts 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #97      +/-   ##
==========================================
+ Coverage   96.09%   96.13%   +0.04%     
==========================================
  Files          33       34       +1     
  Lines        2253     2355     +102     
  Branches      455      478      +23     
==========================================
+ Hits         2165     2264      +99     
- Misses         71       74       +3     
  Partials       17       17              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dillonstreator
dillonstreator merged commit b9a22f4 into main Nov 28, 2026
4 checks passed
@dillonstreator
dillonstreator deleted the fix/event-data-round-trip branch November 28, 2026 19:34
@github-actions github-actions Bot mentioned this pull request Nov 28, 2026
dillonstreator added a commit that referenced this pull request Nov 28, 2026
Resolve the fan-out loop against the schema round-trip change from #97:
computeDelivery now receives the parsed event, and events whose stored
data fails the schema still get default deliveries. Both 0.6 -> 0.7
migration notes are kept.
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