Skip to content

feat(invoice): reject inverted invDate/invDueDate at the schema layer#146

Merged
CryptoJones merged 1 commit into
masterfrom
feat/invoice-due-date-after-issue-date
May 19, 2026
Merged

feat(invoice): reject inverted invDate/invDueDate at the schema layer#146
CryptoJones merged 1 commit into
masterfrom
feat/invoice-due-date-after-issue-date

Conversation

@CryptoJones
Copy link
Copy Markdown
Owner

Closes #145.

Summary

createInvoiceBody and updateInvoiceBody accepted any combination of invDate and invDueDate strings without a cross-field check. An invoice could be persisted with the due date BEFORE the issue date — bookkeeping nonsense the controller had no recourse to reject. Same shape as #129/#130 for timeentry.

Add a zod .refine() cross-field check that fires on both single and PATCH paths. Equality stays allowed (Due on Receipt is a real term). The bulk-create path inherits the refinement automatically via z.array(createInvoiceBody) — an attacker can't bypass it by wrapping the bad entry in a bulk envelope.

String comparison is safe because isoDate is the strict ^\d{4}-\d{2}-\d{2}$ regex — lexicographic order matches chronological order for any valid input.

Single-bound PATCH (only invDueDate or only invDate) is intentionally not rejected — the schema can't see the row's other half. Controller-layer enforcement is a follow-up.

Test plan

  • npm run lint — clean
  • npm test — 520 passed (was 515 + 5 new), 15 skipped
  • Five new tests cover: inverted CREATE → 400, equality CREATE → schema-pass, inverted both-bound PATCH → 400, single-bound PATCH → schema-pass, inverted bulk-entry → 400 (with invoices.0.invDueDate issue path)

Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/

`createInvoiceBody` and `updateInvoiceBody` accepted any
combination of `invDate` and `invDueDate` strings. There was no
check that the due date was on or after the issue date, so an
operator could persist an invoice that was "due" before it was
issued — bookkeeping nonsense the controller had no recourse to
reject.

Same shape as the timeentry `teEndedAt >= teStartedAt` refinement
from #130: add a zod `.refine()` that fires on both the single-
and bulk-create paths, and on PATCH when both bounds appear in
the same body. Equality stays allowed — `Due on Receipt` is a
real billing term.

String comparison is safe here because `isoDate` is the strict
`^\d{4}-\d{2}-\d{2}$` regex above; lexicographic order on that
shape matches chronological order for every valid input. No need
to parse to Date objects (timeentry uses `new Date()` because
`isoDatetime` has timezone offsets in play; isoDate does not).

The bulk-create path inherits the refinement automatically because
`bulkInvoiceBody` wraps `createInvoiceBody` in `z.array(...)` and
zod runs each element's refinements during array validation —
so an attacker can't bypass the check by wrapping the bad entry
in a bulk envelope.

Single-bound PATCH (only invDueDate or only invDate) is
intentionally NOT rejected — the schema doesn't see the existing
row's other half. That's a controller-layer follow-up.

Five new tests cover: inverted single CREATE → 400, equality
CREATE → schema-pass, inverted both-bound PATCH → 400, single-
bound PATCH → schema-pass, inverted bulk entry → 400 (with the
`invoices.0.invDueDate` issue path).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@CryptoJones CryptoJones merged commit 258b21c into master May 19, 2026
3 checks passed
@CryptoJones CryptoJones deleted the feat/invoice-due-date-after-issue-date branch May 19, 2026 06:51
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.

invoice: invDueDate before invDate is silently accepted (no cross-field validation)

1 participant