-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrict-mode.ts
More file actions
36 lines (31 loc) · 1.16 KB
/
Copy pathstrict-mode.ts
File metadata and controls
36 lines (31 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Strict Mode — Fail on Drift
*
* Returns 500 when the response doesn't match the OpenAPI spec.
* Use this in CI/staging to catch contract violations early.
*
* Run: npx ts-node examples/strict-mode.ts
* Test: curl http://localhost:3000/pets/1
*/
import express from 'express';
import { contractlens } from 'contractlens';
const app = express();
app.use(contractlens({
spec: './examples/petstore.yaml',
mode: 'strict',
}));
// This will return 500 because "status" is required but missing,
// and "id" is a string instead of integer.
app.get('/pets/:petId', (_req, res) => {
res.json({ id: '1', name: 'Buddy' });
});
// This matches the spec — returns normally.
app.post('/pets', express.json(), (_req, res) => {
res.status(201).json({ id: 3, name: 'Luna', status: 'available' });
});
app.listen(3001, () => {
console.log('Strict mode example running on http://localhost:3001');
console.log('Try:');
console.log(' curl http://localhost:3001/pets/1 # 500 — drift detected');
console.log(' curl -X POST -H "Content-Type: application/json" -d \'{"name":"Luna"}\' http://localhost:3001/pets # 201 — valid');
});