On this page
The short answerValidate the spec firstCheck examples against their schemasTest the examples against a running serverTest the spec with a mock serverKeep credentials out of the runCatch drift between spec and codeRun it on every changeChecklist for an OpenAPI example testWhere DocsAlive fitsWhat this does not replaceThe sources we checkedFAQAn OpenAPI spec is often the source of truth for an API reference, but the examples inside it are copied into guides, READMEs and SDK docs too. When a field is renamed in the schema, the spec can still be valid while every example that uses the old field is wrong. This guide covers how to check the examples, not just the schema.
The short answer
Test an OpenAPI document in layers, because each layer catches a different failure. Lint the spec so structural and style problems fail early. Validate every example and examples value against the schema it sits beside, because a spec can parse cleanly while its samples describe fields that no longer exist. Then run the documented requests against a real server, or against a mock built from the spec, and compare what comes back with what the docs promise. Finally, test the copies: examples travel from the spec into Markdown guides and README quickstarts, and those copies drift on their own schedule.
DocsAlive does not parse, lint or validate an OpenAPI document. It reads the Markdown pages around the spec — the guides, tutorials and READMEs where the same examples appear as code blocks — and runs those blocks in a fresh container. Keep the spec tooling described here for the spec itself, and use an execution check for the pages readers actually copy from. The API reference testing guide covers the rendered reference page, and the curl examples guide covers the command-line form of the same requests.
Validate the spec first
Run a linter or validator on the spec before testing any example. A spec that does not parse cannot give useful example results, and a spec with structural errors makes every downstream check harder to read. Two widely used linters are Spectral and Redocly CLI. Spectral needs a ruleset file before it will lint:
npm install -g @stoplight/spectral-cli
echo 'extends: ["spectral:oas"]' > .spectral.yaml
spectral lint openapi.yaml
The spectral:oas ruleset applies Spectral's OpenAPI rules. Extend it with your own rules in the same file when your style guide has requirements the defaults do not cover, and raise the failure threshold in CI so warnings do not scroll past unnoticed:
spectral lint openapi.yaml --ruleset .spectral.yaml --fail-severity warn
redocly lint openapi.yaml
Linting answers a narrow question: is this document well-formed and consistent with the rules we chose? It does not prove the examples are right. A renamed field passes lint as long as the schema and the example are edited in the same commit — and the failure mode this guide is about is the commit that edits only one of them. That is why the next check exists.
Check examples against their schemas
Every example in the document should be a valid instance of the schema it illustrates. Check request examples against the request schema, response examples against the response schema for that status code, and parameter examples against the parameter schema. Spectral's built-in OpenAPI ruleset includes a rule for exactly this — oas3-valid-media-example for OpenAPI 3 media types — so turn it up to an error in your ruleset:
extends: ["spectral:oas"]
rules:
oas3-valid-media-example: error
The failure this catches is mundane and common. The schema renames a field, and the example keeps the old name:
components:
schemas:
Widget:
type: object
properties:
id:
type: string
displayName: # renamed from "name"
type: string
examples:
- id: "w_123"
name: "Gear" # stale: no longer a property of Widget
Depending on the tool, an unknown property in an example is an error, a warning, or silently ignored — additionalProperties decides much of that behaviour — so do not rely on defaults. Make the example-validity rule an error and treat its output as a docs bug list, with the file and line of each example recorded.
Version differences matter here. OpenAPI 3.1 aligns the schema language with JSON Schema 2020-12, so a 3.0 idiom such as nullable: true becomes a type array, type: ["string", "null"]. An example that was valid under a 3.0 reading can be invalid under 3.1, and a validator set to the wrong version will either miss it or flag correct examples. Pin the validator to the version the document declares, and re-run the example checks as part of any 3.0-to-3.1 migration rather than after it.
Test the examples against a running server
- Start the API in a clean container, from the same commit the docs describe.
- Pull each documented request example from the spec or the guide.
- Send it to the server and compare the status code and the top-level response fields with the documented response example.
- Record any mismatch with the file and line of the example.
For a documented request, the test is a real call. This documented example:
curl -s https://api.example.com/v1/widgets/w_123 \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json"
should return the status code the operation documents, with a body whose shape matches the documented response example: the same top-level fields, the same types, and the same nested objects where the docs name them. Do not compare full response bodies unless the API is deterministic. Timestamps, IDs and paging tokens change between runs. Compare shape and the fields the docs promise, and write down which fields you excluded and why, so a reviewer can tell a deliberate exclusion from an oversight.
Test the error examples too. Most specs document at least one error response, and error shapes drift quietly because fewer readers exercise them in public. Send the request that is supposed to produce the documented 404 or 422 and compare the error body's fields with the example. A changed error field breaks client error handling exactly the way a changed success field breaks the happy path.
Test the spec with a mock server
A mock server built from the spec tests the document from the reader's side. Prism, Stoplight's mock server, serves the paths in the document and answers with the examples the spec contains:
npm install -g @stoplight/prism-cli
prism mock openapi.yaml
# serves the documented paths on http://127.0.0.1:4010
Send the documented requests to the mock. If the mock cannot produce the documented response, or rejects a documented request as invalid, the document disagrees with itself — no running API is needed to find that class of bug. Prism also runs as a proxy in front of a real staging API, validating live traffic against the spec:
prism proxy openapi.yaml https://staging.api.example.com --errors
Property-based testing goes one step further. Schemathesis generates test cases from the schemas — boundary values, missing required fields, wrong types — and checks that the server's responses conform to the document. It finds behaviour the hand-written examples never covered. Keep its role straight, though: it tests the API against the spec. It does not test the prose around the spec, and it does not know which examples your guides copied.
Know what each layer proves. The linter proves the document is well-formed. Example validation proves the samples match the schemas. The mock proves the document is self-consistent. The live run proves the document matches the server you ran. None of them proves that the quickstart page a reader lands on still shows the current field names — that copy lives outside the spec, which is the drift problem covered next.
Keep credentials out of the run
Examples often show an Authorization header with a real-looking token. Replace these with a placeholder in the test run, and use a sandbox key only where the test needs a live call. Mark examples that need a paid or production key so a green run does not hide them.
Keep the placeholder obviously a placeholder. A realistic-looking fake token in a published example gets copied into issue reports and, occasionally, into someone else's code as if it were a format example. Use a value a reader cannot mistake for a real credential, keep real sandbox keys in the CI secret store rather than in the repository, and make sure test logs redact the header — a passing run that prints a live key to a public log has created a worse problem than the stale example it was checking.
Catch drift between spec and code
Two checks catch most drift:
- Spec against code: generate a spec from the running server and diff it with the committed spec. Any field present in one and not the other is drift.
- Examples against spec: run the validator on examples after every spec change, not only on release.
Add a third check at release time: diff the spec itself between versions and read the breaking changes. oasdiff's breaking command reports the changes that break existing clients between two versions of a document:
oasdiff breaking openapi-v1.yaml openapi-v2.yaml
Every breaking change it reports needs a docs action: an example updated, a migration note written, or a deliberate decision recorded. The documentation drift guide covers the wider pattern, including how a breaking-change list becomes docs tickets.
The copies need their own check. When an example is pasted from the spec into a getting-started guide, the spec's checks stop covering it. The guide now has its own version of the truth, edited on a different schedule. Inventory the places each important example appears — spec, reference page, quickstart, README — and make sure at least one check runs each copy. In practice the Markdown copies are the ones that go stale, because nothing in the spec pipeline ever opens those files.
Run it on every change
- Pull requests that touch the spec or docs: lint the spec and run the examples against a test server.
- Releases: run the full example set against the release build.
- Nightly: run all examples, so upstream changes show up even when nobody edits the docs.
A minimal CI outline, with each layer as its own step so a failure names the layer that caught it:
# 1. Lint the document
spectral lint openapi.yaml --ruleset .spectral.yaml --fail-severity warn
# 2. Serve the spec's own examples and probe them
prism mock openapi.yaml &
curl -s http://127.0.0.1:4010/v1/widgets/w_123
# 3. Run the documented requests against the release build
# (your contract-test runner, pointed at the staging URL)
# 4. List breaking changes since the last release
oasdiff breaking openapi-prev.yaml openapi.yaml
Keep the results separate. A lint failure, an invalid example and a live-response mismatch have different owners and different fixes, and a single rolled-up green or red hides which one you have. Record the spec revision and the server build with each result so a failure can be reproduced against the same pair later.
Checklist for an OpenAPI example test
| Check | Evidence to retain |
|---|---|
| Spec parses and lints clean | Linter name, ruleset and revision of the document |
| Every example validates against its schema | Rule output with the file and line of each failing example |
| Documented requests run against a real build | Status codes and compared fields, with exclusions named |
| Error examples exercised | The request used and the error body returned |
| Mock serves the documented responses | Mock log or probe output for the main paths |
| Breaking changes reviewed | oasdiff output and the docs action taken for each change |
| Copies outside the spec inventoried | List of guides and READMEs carrying each key example |
| Credentials kept out | Placeholder values in examples; sandbox keys held in CI secrets |
Where DocsAlive fits
DocsAlive covers the layer the spec tools cannot see: the examples as they appear in your Markdown documentation. It runs the code samples in your docs in an isolated container on each release and nightly, separates likely docs bugs from expected failures, and can open a fix PR through its GitHub App for a human to approve. A stale field name in a quickstart's JSON block, or a curl example that now returns a different shape, is the kind of finding it reports with the page and line.
For a starting point, the free health check lets you send a public repository you own or maintain; the team runs the engine on up to 40 of its docs pages and emails the real report within 2 business days; the checkout is deleted after the run. Pricing is Open source $0/month, Team $299/month, Growth $699/month, and Scale from $1,499/month.
What this does not replace
DocsAlive does not parse or lint your OpenAPI document, validate its examples against their schemas, or stand in for a contract test against your API. Keep Spectral or Redocly for the document, a mock or property-based tool for the contract, and your own review for whether the prose explains the API well. A passing example also does not prove the surrounding text is right: descriptions, limits and behaviour notes drift in ways no execution check can see, and they still need a reader.
The sources we checked
| Source | What it covers |
|---|---|
| OpenAPI Specification | Where examples live in a document and what they must agree with |
| Spectral documentation and OpenAPI ruleset reference | Lint commands, rulesets, and the oas3-valid-media-example rule |
| Redocly CLI documentation | The redocly lint command |
| Prism documentation | Mock and proxy modes, request validation, the default local port |
| Schemathesis documentation | Schema-based test generation and response conformance checks |
| oasdiff documentation | The breaking-changes command used for release diffs |
FAQ
Is a valid spec enough?
No. Validity means the document follows the OpenAPI format. Its examples can still name fields the schema dropped, and its operations can still disagree with the running server. Example validation and a live run answer those separate questions.
Should examples be compared byte for byte with live responses?
Usually not. Timestamps, generated IDs and paging tokens change between runs. Compare status codes, field names, types and the fields the docs promise, and record any field you deliberately exclude.
What does a mock server prove?
That the document is self-consistent: the mock serves the spec's own examples and validates requests against the spec, so a document that disagrees with itself fails without any API running. It says nothing about whether the real API matches the document.
Does DocsAlive lint the spec file?
No. It runs the code samples in your Markdown docs — the guides and READMEs where examples are copied — and reports the ones that break. Keep your spec linter and contract tests for the document itself.
How often should example checks run?
On pull requests that touch the spec or the docs, against the release build before a release, and nightly, so dependency and upstream changes surface even in weeks when nobody edits the docs.




