Tutorial · target: how to create a mock api from openapi spec
How to Generate Mock API build fixture Seed Data with One Command
User wants a runnable mock API from a spec file.
Direct answer
Generating a seed and a negative fixture set for Mock API build fixture takes one generate command and produces a set of committed files, with no hand-written INSERT statement in between.
The steps assume Node.js 22.6 or newer and the schema document that ships with this page; a schema with remote $ref URLs needs the files downloaded first.
One generate command writes 2 tables in dependency order into ./seed/sql/seed.sql, and the negative cases go to ./seed/sql/negatives.sql beside it; the same run also writes the pytest suite in ./seed/pytest, where each negative case is asserted against the schema keyword it breaks.
Data as of 2026-11-18 · source: 2 Stack Overflow questions, 2 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
Prerequisites — Mock API
You need a schema document for Mock API build fixture and a Node.js runtime. The Mock API build fixture fixture used here has 2 tables, and the commands below assume the file is at openapi/file that ships with this page. Nothing else is required: no database connection, no API key, no network access at generation time.
The steps
One command does the work. The generate command reads the schema, writes the seed and the negative cases in a single pass, and prints what it produced: the tables it wrote, the rows per table and the number of negative cases. For Mock API build fixture that is 2 tables in dependency order, with 8 constraints carried into the DDL. Two read-only commands sit next to it for checking instead of writing: validate reports the $ref chains that cannot be resolved and exits non-zero when one is broken, and inspect prints the tables, columns, primary keys and foreign keys the tool derived.
What you end up with
A set of committed artefacts: the PostgreSQL seed script, the negative SQL cases and the pytest suite. The seed for Mock API build fixture contains 2 table definitions and their rows in dependency order. The negative file holds the instances the API must reject, one per constraint class that the fixture declares, and the pytest suite carries both the valid rows and the negative ones. All of them are text, all of them diff in review, and all of them are regenerated by re-running the same command.
Wiring it into CI
The CI step is a diff, not a test. Regenerate the seed and fail the build if the committed file differs from the regenerated one. That single check catches the case where Mock API build fixture changed shape and the fixture did not, which is the failure that a hand-maintained seed produces silently for months.
Troubleshooting
If the generator warns about a schema it skipped, the warning names the construct. For Mock API build fixture, the constructs worth knowing about are the range declaration on pets.status and the reference from pets to categories. A skipped construct is reported, never silently dropped, which is the behaviour to insist on from any tool in this position.
| Failure class | Column | SQLSTATE | Error name | Engine message (verbatim) |
|---|---|---|---|---|
| Missing required field | categories.name | 23502 | not_null_violation | NOT NULL constraint failed: categories.name |
| Out-of-contract value | pets.status | 23514 | check_violation | CHECK constraint failed: pets_status_check |
| Dangling foreign key | pets.category_id | 23503 | foreign_key_violation | FOREIGN KEY constraint failed |
| Table | Referenced by | Written |
|---|---|---|
| categories | pets | after pets |
| pets | — | first, no dependencies |
npx --yes @wenlu8848/schemaseed generate --input ./tut_pets.openapi.yaml --out ./seed --formats sql --count 10 --seed 42
Download tut_pets.openapi.yaml
-- Generated by schemaseed.
-- seed: 42
-- rows per table: 10
-- Foreign key values are taken from the rows of the referenced table.
BEGIN;
CREATE TABLE IF NOT EXISTS "categories" (
"id" INTEGER,
"name" TEXT NOT NULL,
CONSTRAINT "categories_pkey" PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "pets" (
"id" INTEGER,
"category_id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"status" TEXT NOT NULL,
CONSTRAINT "pets_pkey" PRIMARY KEY ("id"),
CONSTRAINT "pets_status_check" CHECK ("status" IN ('available', 'pending', 'sold'))
);
DO $schema_seed$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'pets_category_id_fkey') THEN
ALTER TABLE "pets" ADD CONSTRAINT "pets_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "categories" ("id");
END IF;
END
$schema_seed$;
INSERT INTO "categories" ("id", "name")
VALUES (744, 'Iris Grant')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (286, 'lumen-willow-938')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (698, 'alpha-indigo-881')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (981, 'pia.jansen@example.org')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (333, 'Auckland, Maple Street 195')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (153, 'willow harbor')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (324, 'Bao Engel')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (652, 'zephyr-zephyr-795')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (797, 'umbra juniper')
ON CONFLICT DO NOTHING;
INSERT INTO "categories" ("id", "name")
VALUES (577, 'river tundra')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (935, 577, 'tara.duarte@example.net', 'sold')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (270, 797, 'grove-kelp-927', 'available')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (115, 744, 'Osaka, Riverside Lane 10', 'pending')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (820, 286, 'Milo Grant', 'sold')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (879, 981, 'Eli Kaur', 'pending')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (149, 744, 'rosa.fontaine@test.dev', 'available')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (198, 652, 'summit alpha', 'sold')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (640, 333, 'Osaka, Riverside Lane 151', 'pending')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (413, 333, 'zephyr grove', 'pending')
ON CONFLICT DO NOTHING;
INSERT INTO "pets" ("id", "category_id", "name", "status")
VALUES (719, 577, 'grove vertex', 'available')
ON CONFLICT DO NOTHING;
COMMIT;
Frequently asked questions
- How long does the whole workflow take?
- One generate command and a moment on the Mock API build fixture of 2 tables; the slow part is deciding the row order, which is exactly what the generator removes.
- What do I commit to the repository?
- The generated files, all as text: the PostgreSQL seed script, the negative SQL cases and the pytest suite. Re-running with the same seed reproduces them byte for byte on the Mock API build fixture schema.
- Does the CI step need a database?
- The drift check does not: it regenerates and fails if the committed file differs. Add a database service only if you also load the 2 tables.
- What happens if my schema changes?
- The regenerated file differs, CI fails with the diff attached, and every declaration in the schema is re-read, so a new one appears in the negative set immediately.
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