-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix/issue 138 request validation (#146)
Co-authored-by: Dileepa Mabulage <[email protected]>
- Loading branch information
1 parent
7a7aae3
commit 135dce9
Showing
19 changed files
with
297 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { type NextFunction, type Request, type Response } from 'express' | ||
import { ZodError, type ZodSchema } from 'zod' | ||
|
||
export const requestBodyValidator = <T extends ZodSchema>(schema: T) => { | ||
return (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
schema.parse(req.body) | ||
next() | ||
} catch (err) { | ||
console.log(err) | ||
if (err instanceof ZodError) { | ||
const errorMessages = err.errors.map((issue) => ({ | ||
message: `${issue.path.join('.')} is ${issue.message}` | ||
})) | ||
return res | ||
.status(400) | ||
.json({ error: 'Invalid data', details: errorMessages }) | ||
} | ||
return res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
} | ||
|
||
export const requestQueryValidator = <T extends ZodSchema>(schema: T) => { | ||
return (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
schema.parse(req.query) | ||
next() | ||
} catch (err) { | ||
if (err instanceof ZodError) { | ||
const errorMessages = err.errors.map((issue) => ({ | ||
message: `${issue.path.join('.')} is ${issue.message}` | ||
})) | ||
return res | ||
.status(400) | ||
.json({ error: 'Invalid data', details: errorMessages }) | ||
} | ||
return res.status(500).json({ error: 'Internal server error' }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
import express from 'express' | ||
import { requireAuth } from '../../../controllers/auth.controller' | ||
import { | ||
addCategory, | ||
updateCategory | ||
} from '../../../controllers/admin/category.controller' | ||
import { requireAuth } from '../../../controllers/auth.controller' | ||
import { requestBodyValidator } from '../../../middlewares/requestValidator' | ||
import { | ||
addCategorySchema, | ||
updateCategorySchema | ||
} from '../../../schemas/admin/admin.category-routes.schema' | ||
|
||
const categoryRouter = express.Router() | ||
|
||
categoryRouter.post('/', requireAuth, addCategory) | ||
categoryRouter.put('/:categoryId', requireAuth, updateCategory) | ||
categoryRouter.post( | ||
'/', | ||
[requireAuth, requestBodyValidator(addCategorySchema)], | ||
addCategory | ||
) | ||
categoryRouter.put( | ||
'/:categoryId', | ||
[requireAuth, requestBodyValidator(updateCategorySchema)], | ||
updateCategory | ||
) | ||
|
||
export default categoryRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import express from 'express' | ||
import { requireAuth } from '../../../controllers/auth.controller' | ||
import { | ||
getMentees, | ||
updateMenteeStatus, | ||
getAllMenteeEmails, | ||
getMenteeDetails | ||
getMenteeDetails, | ||
getMentees, | ||
updateMenteeStatus | ||
} from '../../../controllers/admin/mentee.controller' | ||
import { requireAuth } from '../../../controllers/auth.controller' | ||
import { | ||
requestBodyValidator, | ||
requestQueryValidator | ||
} from '../../../middlewares/requestValidator' | ||
import { | ||
getAllMenteeEmailsSchema, | ||
getMenteesSchema, | ||
updateMenteeStatusSchema | ||
} from '../../../schemas/admin/admin.mentee-routes.schema' | ||
|
||
const menteeRouter = express.Router() | ||
|
||
menteeRouter.get('/emails/', requireAuth, getAllMenteeEmails) | ||
menteeRouter.get('/applications', requireAuth, getMentees) | ||
menteeRouter.get( | ||
'/emails/', | ||
[requireAuth, requestQueryValidator(getAllMenteeEmailsSchema)], | ||
getAllMenteeEmails | ||
) | ||
menteeRouter.get( | ||
'/applications', | ||
[requireAuth, requestQueryValidator(getMenteesSchema)], | ||
getMentees | ||
) | ||
menteeRouter.get('/:menteeId', requireAuth, getMenteeDetails) | ||
menteeRouter.put('/:menteeId/state', requireAuth, updateMenteeStatus) | ||
menteeRouter.put( | ||
'/:menteeId/state', | ||
[requireAuth, requestBodyValidator(updateMenteeStatusSchema)], | ||
updateMenteeStatus | ||
) | ||
|
||
export default menteeRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import express from 'express' | ||
import { sendEmailController } from '../../controllers/admin/email.controller' | ||
import { requireAuth } from '../../controllers/auth.controller' | ||
import { requestBodyValidator } from '../../middlewares/requestValidator' | ||
import { sendEmailSchema } from '../../schemas/email-routes.schema' | ||
|
||
const emailRouter = express.Router() | ||
|
||
emailRouter.post('/send', requireAuth, sendEmailController) | ||
emailRouter.post( | ||
'/send', | ||
[requireAuth, requestBodyValidator(sendEmailSchema)], | ||
sendEmailController | ||
) | ||
|
||
export default emailRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
import express from 'express' | ||
import { requireAuth } from '../../controllers/auth.controller' | ||
import { | ||
getMenteeDetails, | ||
menteeApplicationHandler, | ||
updateMenteeStatus, | ||
getMenteeDetails | ||
updateMenteeStatus | ||
} from '../../controllers/mentee.controller' | ||
import { requestBodyValidator } from '../../middlewares/requestValidator' | ||
import { | ||
menteeApplicationSchema, | ||
updateMenteeStatusSchema | ||
} from '../../schemas/mentee-routes.schemas' | ||
|
||
const menteeRouter = express.Router() | ||
|
||
menteeRouter.post('/', requireAuth, menteeApplicationHandler) | ||
menteeRouter.post( | ||
'/', | ||
[requireAuth, requestBodyValidator(menteeApplicationSchema)], | ||
menteeApplicationHandler | ||
) | ||
menteeRouter.get('/:menteeId', getMenteeDetails) | ||
menteeRouter.put('/:menteeId/status/', requireAuth, updateMenteeStatus) | ||
menteeRouter.put( | ||
'/:menteeId/status/', | ||
[requireAuth, requestBodyValidator(updateMenteeStatusSchema)], | ||
updateMenteeStatus | ||
) | ||
|
||
export default menteeRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import express from 'express' | ||
import { requireAuth } from '../../controllers/auth.controller' | ||
import { | ||
deleteProfileHandler, | ||
getApplicationsHandler, | ||
getProfileHandler, | ||
updateProfileHandler | ||
} from '../../controllers/profile.controller' | ||
import { requireAuth } from '../../controllers/auth.controller' | ||
import { | ||
requestBodyValidator, | ||
requestQueryValidator | ||
} from '../../middlewares/requestValidator' | ||
import { | ||
getApplicationsSchema, | ||
updateProfileSchema | ||
} from '../../schemas/profile-routes.schema' | ||
|
||
const profileRouter = express.Router() | ||
|
||
profileRouter.get('/profile', requireAuth, getProfileHandler) | ||
profileRouter.put('/profile', requireAuth, updateProfileHandler) | ||
profileRouter.put( | ||
'/profile', | ||
[requireAuth, requestBodyValidator(updateProfileSchema)], | ||
updateProfileHandler | ||
) | ||
profileRouter.delete('/profile', requireAuth, deleteProfileHandler) | ||
profileRouter.get('/applications', requireAuth, getApplicationsHandler) | ||
profileRouter.get( | ||
'/applications', | ||
[requireAuth, requestQueryValidator(getApplicationsSchema)], | ||
getApplicationsHandler | ||
) | ||
|
||
export default profileRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from 'zod' | ||
|
||
export const addCategorySchema = z.object({ | ||
categoryName: z.string() | ||
}) | ||
|
||
export const updateCategorySchema = z.object({ | ||
categoryName: z.string() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod' | ||
import { MenteeApplicationStatus } from '../../enums' | ||
|
||
export const getAllMenteeEmailsSchema = z.object({ | ||
status: z.nativeEnum(MenteeApplicationStatus) | ||
}) | ||
|
||
export const getMenteesSchema = z.object({ | ||
state: z.nativeEnum(MenteeApplicationStatus) | ||
}) | ||
|
||
export const updateMenteeStatusSchema = z.object({ | ||
state: z.nativeEnum(MenteeApplicationStatus) | ||
}) |
Oops, something went wrong.