Problem · target: property based testing for openapi

Negative Tests for Double-entry ledger API: What to Assert

User wants random-but-reproducible data generation driven by the contract.

Direct answer

Invalid payloads slip through because the schema is only used to validate requests, never to produce the negative cases, and Double-entry ledger 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 Double-entry ledger API with "CHECK constraint failed: length(currency) = 3", which is the error the API should return to the client instead of a success response.

Data as of 2026-11-18 · source: 7 Stack Overflow questions, 12 Hacker News stories, @hey-api/spec-types at 20,088,983 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 — Double-entry ledger

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 Double-entry ledger API, the reference engine rejected the bad row with "NOT NULL constraint failed: accounts.code" — 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 Double-entry ledger 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 Double-entry ledger API, the range probe on entries.currency sits one step outside the boundary. The constraint is char_length("currency") = 3 and the probe value is US. The engine said "CHECK constraint failed: length(currency) = 3". 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 Double-entry ledger API fixture, the assertion that catches a regression is a request carrying currency=US, 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 Double-entry ledger 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 Double-entry ledger 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 fieldaccounts.code23502not_null_violationNOT NULL constraint failed: accounts.code
Out-of-contract valueentries.currency23514check_violationCHECK constraint failed: length(currency) = 3
Dangling foreign keyentries.account_id23503foreign_key_violationCHECK constraint failed: length(currency) = 3
Boundary probes for every constrained column in Double-entry ledger APIOne violating value per constraint, with the boundary it crosses.
TableColumnDeclared ruleProbe valueWhy it is invalid
accountskindone of asset, liability, equity, revenue, expensenot_a_real_membernot a declared member
accountskindone of asset, liability, equity, revenue, expenseASSETwrong case
entriescurrencyexactly 3 charactersSEtoo short
README.md

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

Download prob_ledger.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_accounts.py`: 10 valid rows, 11 negative cases (enum-violation, format-violation, missing-required, null-violation, type-mismatch)
- `test_entries.py`: 10 valid rows, 24 negative cases (enum-violation, format-violation, length-violation, missing-required, null-violation, type-mismatch)

Frequently asked questions

How many invalid cases do I actually need?
One per constraint class, not one per field. The Double-entry ledger API fixture declares its rules across 2 tables and 9 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 Double-entry ledger 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 — entries.currency 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 Double-entry ledger API the first probe is entries.currency = US, and the engine replied "CHECK constraint failed: length(currency) = 3".

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