Skip to content

feat(validation): add request validation for server and channel mutat…#82

Open
varun29sharma wants to merge 7 commits into
0rigin-c0de:mainfrom
varun29sharma:feat/request-validation-issue-18
Open

feat(validation): add request validation for server and channel mutat…#82
varun29sharma wants to merge 7 commits into
0rigin-c0de:mainfrom
varun29sharma:feat/request-validation-issue-18

Conversation

@varun29sharma
Copy link
Copy Markdown
Contributor

Summary

Closes #18

Adds input validation to all six mutation routes in server/routes/servers.js
using express-validator. Previously, missing or malformed fields caused
either a BSONError crash (500) or a silent no-op where bad data was saved
to MongoDB without any error returned to the client.

Changes

New file: server/middleware/validate.js

A reusable middleware that accepts an array of express-validator rules,
runs them, and returns a consistent 400 response with field-level error
messages if any rule fails. The route handler is never called when validation
fails. ;)

Modified: server/routes/servers.js

Route Problem fixed
POST /create_server server_details and server_details.server_name now required — previously undefined.role threw a TypeError
POST /add_new_channel Both server_id and category_id validated as MongoId before new ObjectId() — previously any bad string caused a BSONError 500
POST /add_new_channel channel_name and channel_type now required and validated
POST /add_new_category server_id validated as MongoId; category_name required
POST /delete_server server_id required — previously missing field caused a silent no-op
POST /leave_server server_id required — same silent failure fixed
POST /server_info server_id validated before new ObjectId() call — bad ID no longer crashes after auth

modifiedCount === 0 responses on /add_new_channel and /add_new_category
now return 404 instead of 500, since the correct interpretation is
"server or category not found."

Test evidence

All tests run with curl against a local instance (port 2000):

  • POST /create_server with empty body → 400
  • POST /create_server with server_name: ""400
  • POST /add_new_channel with server_id: "notanid"400
  • POST /add_new_channel with channel_type: "video"400
  • POST /add_new_category with valid MongoId but no matching server → 404
  • POST /delete_server with empty body → 400
  • POST /server_info with bad ID → 400 (was 500)

Compatibility

All existing happy-path response shapes are unchanged. No frontend code
was modified. The { status: ... } pattern used throughout the file
is preserved in the new error responses.

…ion APIs

- Add server/middleware/validate.js with reusable express-validator wrapper
- Validate server_details, server_name on /create_server
- Validate server_id, category_id (isMongoId) on /add_new_channel and /add_new_category
- Validate channel_name, channel_type on /add_new_channel
- Validate server_id on /delete_server and /leave_server
- Guard /server_info against bad ObjectId before DB call
- Return 404 when modifiedCount is 0 (server/category not found)

Closes 0rigin-c0de#18
@netlify
Copy link
Copy Markdown

netlify Bot commented May 16, 2026

👷 Deploy request for piperchat01 pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 7d836f7

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 17, 2026

@varun29sharma is attempting to deploy a commit to the Sunil Kumar's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
piperchat Ready Ready Preview, Comment May 17, 2026 11:20am

Comment thread server/src/routes/servers.js
@varun29sharma
Copy link
Copy Markdown
Contributor Author

Hi @0rigin-c0de .... can u please merge this PR and we can finally close issue 18😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add request validation for server and channel mutation APIs

2 participants