Skip to content

Improve input parsing and validation in create-room cloud function#160

Open
BST1807 wants to merge 1 commit intoAOSSIE-Org:mainfrom
BST1807:improve-create-room-validation
Open

Improve input parsing and validation in create-room cloud function#160
BST1807 wants to merge 1 commit intoAOSSIE-Org:mainfrom
BST1807:improve-create-room-validation

Conversation

@BST1807
Copy link

@BST1807 BST1807 commented Feb 12, 2026

Summary

This PR improves the robustness and structure of the create-room cloud function.

Changes Made

  • Removed duplicate JSON.parse(req.body) to avoid redundant parsing
  • Added strict type validation for required fields (name, adminUid, tags)
  • Added safe handling and validation for the optional description field
  • Extracted validation logic into a reusable helper function in utils.js for better separation of concerns and maintainability

Impact

These changes improve input safety and prevent malformed requests from reaching business logic, while keeping the existing response format and frontend integration unchanged.

Checklist

  • [ x] My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • [x ] My code follows the project's code style and conventions
  • [ x] My changes generate no new warnings or errors
  • [ x] I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • [ x] Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.

Summary by CodeRabbit

Release Notes

Improvements

  • Enhanced input validation and data sanitization for room creation operations
  • Improved consistency of error handling when invalid room creation data is submitted
  • Streamlined room creation data validation workflow to ensure data integrity throughout the process

@coderabbitai
Copy link

coderabbitai bot commented Feb 12, 2026

📝 Walkthrough

Walkthrough

The changes extract input validation logic into a dedicated utility function. The main.js file now imports and calls validateCreateRoomInput() instead of performing inline validation, while utils.js introduces this new validation function that handles field type checking and data sanitization.

Changes

Cohort / File(s) Summary
Validation Refactoring
functions/create-room/src/main.js, functions/create-room/src/utils.js
Extracted scattered validation checks into a centralized validateCreateRoomInput() utility function. Main.js now imports and delegates to this function, while utils.js defines the new validation logic with field type enforcement (name, adminUid, tags as required; description as optional) and data sanitization via trimming.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 The validation hunt begins anew,
From scattered checks to functions true,
A fuzzy bunny hops with glee,
As messy code becomes clean and free,
Trimmed and sanitized, neat and bright!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly summarizes the main change: improving input parsing and validation in the create-room cloud function, which matches the core purpose of refactoring validation logic into a centralized function.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
functions/create-room/src/utils.js (1)

21-23: Tags are validated but not sanitized, unlike other string fields.

name, adminUid, and description are all .trim()-ed in the returned object, but tags entries are passed through as-is. This means whitespace-only or empty-string tags (e.g., ["rock", " ", ""]) will pass validation and reach business logic. Consider trimming and filtering tags for consistency.

♻️ Suggested improvement
+    const sanitizedTags = tags.map(tag => tag.trim()).filter(tag => tag.length > 0);
+    if (sanitizedTags.length === 0) {
+        throw new Error("Tags must contain at least one non-empty string");
+    }
     return {
         name: name.trim(),
         description: typeof description === "string" ? description.trim() : "",
         adminUid: adminUid.trim(),
-        tags,
+        tags: sanitizedTags,
     };

Also applies to: 27-31

functions/create-room/src/main.js (1)

21-28: throwIfMissing on Line 23 is now redundant with validateCreateRoomInput.

validateCreateRoomInput already rejects missing/undefined name, adminUid, and tags with descriptive errors (e.g., typeof undefined !== "string"). The throwIfMissing call adds an extra pass over the same fields. Keeping it isn't harmful, but removing it would simplify the flow without losing any safety.

♻️ Optional simplification
     try {
       body = JSON.parse(req.body);
-      throwIfMissing(body, ["name", "adminUid", "tags"]);
       validatedData = validateCreateRoomInput(body);
     } catch (err) {

If you prefer the "Missing required fields: …" message for absent keys, you could incorporate that into validateCreateRoomInput instead.

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant