Skip to content

Commit 569a532

Browse files
feat: validate request shape against Zod schema
1 parent 2d196b2 commit 569a532

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"axios": "^1.7.9",
1818
"cors": "^2.8.5",
1919
"dotenv": "^16.4.7",
20-
"express": "^4.21.2"
20+
"express": "^4.21.2",
21+
"zod": "^3.24.1"
2122
},
2223
"devDependencies": {
2324
"@types/cors": "^2.8.17",

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import cors from 'cors'
33
import express from 'express'
44
import dotenv from 'dotenv'
55

6+
import * as schema from './schema'
7+
68
dotenv.config()
79

810
const app = express()
@@ -12,8 +14,9 @@ app.use(express.json())
1214

1315
app.post('/', async (req, res) => {
1416
console.log(`Received POST request:`, req.body)
15-
const { url, ...params } = req.body
16-
const response = await axios.post(url, params)
17+
const { url, ...evaluationFunctionRequestData } =
18+
schema.TestServerRequestData.parse(req.body)
19+
const response = await axios.post(url, evaluationFunctionRequestData)
1720
res.send(response.data)
1821
})
1922

src/schema.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { z } from 'zod'
2+
3+
export const EvaluationFunctionRequestData = z
4+
.object({
5+
answer: z.string(),
6+
response: z.string(),
7+
params: z.record(z.string(), z.any()),
8+
})
9+
.strict()
10+
export type EvaluationFunctionRequestData = z.infer<
11+
typeof EvaluationFunctionRequestData
12+
>
13+
14+
export const TestServerRequestData = EvaluationFunctionRequestData.extend({
15+
url: z.string(),
16+
}).strict()
17+
export type TestServerRequestData = z.infer<typeof TestServerRequestData>

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,3 +1222,8 @@ yocto-queue@^0.1.0:
12221222
version "0.1.0"
12231223
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
12241224
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
1225+
1226+
zod@^3.24.1:
1227+
version "3.24.1"
1228+
resolved "https://registry.yarnpkg.com/zod/-/zod-3.24.1.tgz#27445c912738c8ad1e9de1bea0359fa44d9d35ee"
1229+
integrity sha512-muH7gBL9sI1nciMZV67X5fTKKBLtwpZ5VBp1vsOQzj1MhrBZ4wlVCm3gedKZWLp0Oyel8sIGfeiz54Su+OVT+A==

0 commit comments

Comments
 (0)