Problem · target: how to test invalid payloads api

Negative Tests for Payments API: What to Assert

Engineer must prove the API rejects bad input, not just accepts good input.

Direct answer

Invalid payloads slip through because the schema is only used to validate requests, never to produce the negative cases, and Payments API has 11 constraints that nobody is testing.

This covers contract violations rather than memory-safety bugs, so a fuzzer is still the right tool when the defect class is a parser crash.

The engine rejected the violating row for Payments API with "CHECK constraint failed: amount_cents > 0", which is the error the API should return to the client instead of a success response.

Data as of 2026-11-18 · source: 17 Stack Overflow questions, 1 Hacker News stories, character-reference-invalid at 309,366,262 monthly downloads · negative-case messages reproduced by executing the fixture against a local SQL engine (Node.js v24.22.0, engine 3.53.4) at 2026-11-18T11:53:54.233Z

What actually goes wrong — Payments API

The failure is rarely a crash. The API accepts the payload, returns 200, and stores a row that violates a rule the schema promised to enforce. For Payments API, the reference engine rejected the bad row with "NOT NULL constraint failed: merchants.name" — that is the message the API should have surfaced to the client instead of a success response.

The four failure modes

Invalid payloads cluster into four families, and the Payments API fixture exercises all of them. Missing required fields, wrong types, out-of-range values and values outside a declared enum. Each family maps to a distinct engine error, and each family needs a distinct assertion in the test suite.

What the boundary probes look like

For Payments API, the range probe on payments.amount_cents sits one step outside the boundary. The constraint is "amount_cents" > 0 and the probe value is 0. The engine said "CHECK constraint failed: amount_cents > 0". A test that asserts a 4xx response for this value is asserting that the constraint the schema declares is actually enforced at the edge.

The assertion you actually want

Assert on the response code and on the error body, not on the absence of a crash. For the Payments API fixture, the assertion that catches a regression is a request carrying amount_cents=0, expecting a 4xx status and an error body that names the field. A test that only asserts "the request failed" passes even when the API fails for the wrong reason.

Where a fuzzing tool is the wrong answer

Unstructured fuzzing finds crashes, which is a different job from finding contract violations. For Payments API, a schema-derived negative set covers the cases a fuzzer would take hours to stumble into, and each one has a deterministic expected response. Reach for a fuzzer when the bug class is a parser crash, not when the bug class is a contract the API forgot to enforce.

Real constraint failures for the Payments API fixtureReproduced by executing the fixture against a local SQL engine (Node.js v24.22.0, engine 3.53.4) on 2026-11-18T11:53:54.233Z. The message column is verbatim engine output; the SQLSTATE column is the PostgreSQL error class for the same constraint class.
Failure classColumnSQLSTATEError nameEngine message (verbatim)
Missing required fieldmerchants.name23502not_null_violationNOT NULL constraint failed: merchants.name
Out-of-contract valuepayments.amount_cents23514check_violationCHECK constraint failed: amount_cents > 0
Dangling foreign keypayments.merchant_id23503foreign_key_violationCHECK constraint failed: length(currency) = 3
Boundary probes for every constrained column in Payments APIOne violating value per constraint, with the boundary it crosses.
TableColumnDeclared ruleProbe valueWhy it is invalid
paymentsamount_cents> 00not strictly positive
paymentscurrencyexactly 3 charactersSEtoo short
paymentsstatusone of pending, captured, refunded, failednot_a_real_membernot a declared member
paymentsstatusone of pending, captured, refunded, failedPENDINGwrong case
README.md

npx --yes @wenlu8848/schemaseed generate --input ./prob_payments.openapi.yaml --out ./seed --formats pytest --count 10 --seed 42

Download prob_payments.openapi.yaml

# Generated pytest suite

Seed: 42 — mode: nested

Run it with:

```
pytest pytest/
```

The suite is self contained: `_validator.py` implements the JSON Schema
keywords the generated data uses, so only pytest is needed.

- `test_merchants.py`: 10 valid rows, 7 negative cases (format-violation, missing-required, null-violation, type-mismatch)
- `test_payments.py`: 10 valid rows, 24 negative cases (enum-violation, format-violation, length-violation, missing-required, null-violation, range-violation, type-mismatch)

Frequently asked questions

How many invalid cases do I actually need?
One per constraint class, not one per field. The Payments API fixture declares its rules across 2 tables and 7 columns, and the table above lists the probe value for each.
Should I fuzz instead?
Only for crashes. A schema-derived negative set covers every declaration in Payments API deterministically, with an expected response for each.
What status code should the API return?
Your API decides that, consistently. The generator hands you the violating value and the constraint it breaks — payments.amount_cents on this fixture — so the assertion can name the field.
Do I need to write the assertions by hand?
No. The negative instances come from the schema. For Payments API the first probe is payments.amount_cents = 0, and the engine replied "CHECK constraint failed: amount_cents > 0".

Run it against your own schema

SchemaSeed is MIT licensed and free: unlimited local generation, no account, no telemetry and no usage limit. It reads your schema file on your own machine, so nothing is uploaded anywhere. If it saves you time, you can support the project.

Get SchemaSeed — free Support the project

Questions: support@schemaseed.drz8848.win