Skip to content

Commit 3670983

Browse files
committed
Add schema validation example
1 parent 88ac76c commit 3670983

File tree

7 files changed

+403
-0
lines changed

7 files changed

+403
-0
lines changed

api-schema-validation/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# Playwright
3+
node_modules/
4+
/test-results/
5+
/playwright-report/
6+
/blob-report/
7+
/playwright/.cache/

api-schema-validation/.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "semi": true, "printWidth": 60 }

api-schema-validation/package-lock.json

Lines changed: 182 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api-schema-validation/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "api-schema-validation",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"start": "node server.js"
7+
},
8+
"keywords": [],
9+
"author": "",
10+
"license": "ISC",
11+
"description": "",
12+
"devDependencies": {
13+
"@faker-js/faker": "^9.4.0",
14+
"@playwright/test": "^1.50.1",
15+
"@types/node": "^22.13.0",
16+
"zod": "^3.24.1"
17+
},
18+
"dependencies": {
19+
"ajv": "^8.17.1"
20+
}
21+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { defineConfig } from "@playwright/test";
2+
3+
/**
4+
* Read environment variables from file.
5+
* https://github.com/motdotla/dotenv
6+
*/
7+
// import dotenv from 'dotenv';
8+
// import path from 'path';
9+
// dotenv.config({ path: path.resolve(__dirname, '.env') });
10+
11+
/**
12+
* See https://playwright.dev/docs/test-configuration.
13+
*/
14+
export default defineConfig({
15+
testDir: "./tests",
16+
/* Run tests in files in parallel */
17+
fullyParallel: true,
18+
/* Fail the build on CI if you accidentally left test.only in the source code. */
19+
forbidOnly: !!process.env.CI,
20+
/* Retry on CI only */
21+
retries: process.env.CI ? 2 : 0,
22+
/* Opt out of parallel tests on CI. */
23+
workers: process.env.CI ? 1 : undefined,
24+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
25+
reporter: "html",
26+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
27+
use: {
28+
/* Base URL to use in actions like `await page.goto('/')`. */
29+
baseURL: "http://127.0.0.1:8000",
30+
31+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
32+
trace: "on-first-retry",
33+
},
34+
35+
/* Configure projects for major browsers */
36+
projects: [
37+
{
38+
name: "API tests",
39+
},
40+
],
41+
42+
/* Run your local dev server before starting the tests */
43+
webServer: {
44+
command: "npm run start",
45+
url: "http://127.0.0.1:8000",
46+
reuseExistingServer: !process.env.CI,
47+
},
48+
});

api-schema-validation/server.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const { faker } = require("@faker-js/faker");
2+
const http = require("node:http");
3+
4+
const server = http.createServer({}, (req, res) => {
5+
const url = new URL(
6+
req.url || "/",
7+
"http://localhost:8080"
8+
);
9+
const id = url.searchParams.get("id");
10+
11+
if (url.pathname === "/complex") {
12+
res.writeHead(200, {
13+
"Content-Type": "application/json",
14+
});
15+
return res.end(
16+
JSON.stringify({
17+
id: faker.string.uuid(),
18+
name: faker.person.fullName(),
19+
relatedPeople:
20+
Math.random() > 0.5
21+
? Array.from({ length: 5 }, () => ({
22+
id: faker.string.uuid(),
23+
name: faker.person.fullName(),
24+
}))
25+
: null,
26+
})
27+
);
28+
}
29+
30+
if (req.method === "GET") {
31+
res.writeHead(200, {
32+
"Content-Type": "application/json",
33+
});
34+
return res.end(
35+
JSON.stringify({
36+
id,
37+
name: "Check Lee",
38+
})
39+
);
40+
}
41+
42+
if (req.method === "POST") {
43+
let body = "";
44+
45+
req.on("data", (chunk) => {
46+
body += chunk.toString();
47+
});
48+
49+
req.on("end", () => {
50+
let parsedBody;
51+
try {
52+
parsedBody = JSON.parse(body);
53+
console.log(parsedBody);
54+
} catch (e) {
55+
res.writeHead(400, {
56+
"Content-Type": "application/json",
57+
});
58+
return res.end(
59+
JSON.stringify({ error: "Invalid JSON" })
60+
);
61+
}
62+
63+
res.writeHead(201, {
64+
"Content-Type": "application/json",
65+
});
66+
return res.end(
67+
JSON.stringify({
68+
id: faker.string.uuid(),
69+
name: parsedBody.name,
70+
})
71+
);
72+
});
73+
}
74+
});
75+
76+
server.listen(8000);

0 commit comments

Comments
 (0)