Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Zod Schema Validation and Error Handling #8

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/recall-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"express": "^4.19.2",
"helmet": "^7.1.0",
"pg": "^8.12.0",
"reflect-metadata": "^0.2.2"
"reflect-metadata": "^0.2.2",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/compression": "^1.7.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/recall-api/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import compression from 'compression';
import helmet from 'helmet';
import cors from 'cors';
import logger, { loggerMiddleware } from '@recall-server/common/logger';
import userRoutes from './routes/user.route';
import { dbClient } from "./database";

export default async ({ app }: { app: Application }) => {
Expand Down Expand Up @@ -82,7 +83,7 @@ export default async ({ app }: { app: Application }) => {
* Add more versions of the api below
*/

// app.use('/api/v1');
app.use('/api/v1/users', userRoutes);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The routes are auto-registered if you use the decorators in the routing-controllers package. You can read more here


// Redirect errors to specific pages
/**
Expand Down
14 changes: 14 additions & 0 deletions packages/recall-api/src/routes/user.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Request, Response, NextFunction, Router } from 'express';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the middlewares to a commons package. Only retain the module-specific middleware in the recall api

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use the folder structure in the packages/recall-api/module/example to create modules.

import { userValidation } from '../../../validation';
import { validate } from '../../../validation/validate'
import { zodErrorHandler } from '../../../middleware/zodErrorValidation';


const router = Router();

router.post('/register', validate(userValidation), zodErrorHandler, (req: Request, res: Response, next: NextFunction) => {
// Registration logic
res.status(201).json({ message: 'User registered successfully' });
});

export default router;