Skip to content

customerpayment: cpayAmount accepts 0 and Infinity — both operator/data-quality bugs #171

@CryptoJones

Description

@CryptoJones

Problem

app/schemas/customerpayment.schema.js declares the amount field as:

cpayAmount: z.coerce.number(),

z.coerce.number() accepts every value Number() would produce — including:

  • 0 — a $0 payment recorded against a customer ledger has no business
    meaning. Always operator error.
  • Infinity / -Infinity — zod's .number() rejects NaN by default
    but lets the infinities through. The cpayAmount column is a Sequelize
    DOUBLE which happily stores inf, and any consumer doing arithmetic
    (totals, aging buckets, CSV exports) gets contaminated thereafter.

Negative values are valid — some operators model refunds that way.

Fix

Extract the amount validator to a named cpayAmountField so the create
and update bodies share one definition, and chain:

.finite({ message: 'cpayAmount must be a finite number.' })
.refine((n) => n !== 0, { message: 'cpayAmount must not be zero.' })

Pin the new behavior in tests/api/customerpayment.test.js:

  • POST with cpayAmount: 0 → 400
  • POST with cpayAmount: -50 → not 400 (refund flow stays valid)
  • PATCH with cpayAmount: 0 → 400

Acceptance

  • Schema rejects cpayAmount: 0
  • Schema rejects Infinity / -Infinity
  • Schema still accepts negative values
  • Tests cover all three cases

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions