Skip to content

Commit 99b40c4

Browse files
committed
Update zod to validate more streamlined
1 parent 63c48fc commit 99b40c4

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

packages/backend/src/server/tasks.ts

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { randomUUID } from 'crypto';
2-
import { ErrorRequestHandler, json, Router } from 'express';
2+
import {
3+
ErrorRequestHandler,
4+
json,
5+
Router,
6+
type RequestHandler,
7+
} from 'express';
38
import { Task } from 'interface';
49
import pg from 'pg';
510
import { z } from 'zod';
@@ -46,6 +51,16 @@ const unknownErrorHandler: ErrorRequestHandler = (err, req, res, next) => {
4651
console.error(err);
4752
next();
4853
};
54+
55+
const validate =
56+
<T extends z.ZodType<unknown>>(
57+
schema: T,
58+
): RequestHandler<Record<string, string>, unknown, z.infer<T>> =>
59+
async (req, _, next) => {
60+
const validated = await schema.parseAsync(req.body);
61+
req.body = validated;
62+
return next();
63+
};
4964
export function taskRouter(client: pg.Pool, router = Router()) {
5065
router.use(json());
5166

@@ -85,8 +100,8 @@ export function taskRouter(client: pg.Pool, router = Router()) {
85100
res.status(200).json({ rowsDeleted: response.rowCount });
86101
});
87102

88-
router.post('/:id', async (req, res) => {
89-
const task = await Task.parseAsync(req.body);
103+
router.post('/:id', validate(Task), async (req, res) => {
104+
const task = req.body;
90105
const response = await client.query(
91106
'UPDATE tasks SET name = $1::text, etag = etag + 1 WHERE id = $2::integer AND etag = $3::integer RETURNING etag',
92107
[task.name, req.params.id, task.etag],
@@ -105,16 +120,20 @@ export function taskRouter(client: pg.Pool, router = Router()) {
105120
res.status(200).json(returnedTask);
106121
});
107122

108-
router.post('/', async (req, res) => {
109-
const task = await Task.omit({ etag: true, id: true }).parseAsync(req.body);
110-
const result = await client.query(
111-
'INSERT INTO tasks (name) VALUES ($1::text) RETURNING *',
112-
[task.name],
113-
);
114-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
115-
const response = { ...req.body, ...result.rows[0] };
116-
res.status(201).send(await Task.parseAsync(response));
117-
});
123+
router.post(
124+
'/',
125+
validate(Task.omit({ etag: true, id: true })),
126+
async (req, res) => {
127+
const task = req.body;
128+
const result = await client.query(
129+
'INSERT INTO tasks (name) VALUES ($1::text) RETURNING *',
130+
[task.name],
131+
);
132+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
133+
const response = { ...req.body, ...result.rows[0] };
134+
res.status(201).send(await Task.parseAsync(response));
135+
},
136+
);
118137

119138
router.get('/debug/populate', async (req, res) => {
120139
const n = req.query.n;

0 commit comments

Comments
 (0)