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

Refactor Popup Validations #548

Closed

Conversation

Anurag9977
Copy link

Refactored Popup Validations in Backend

Updated the exisiting popup validations in popup.controller.js file using express validator and placed them in popup.helper.js file.

This PR addresses the issue #387

Below is a summary of all the changes:

  • Added

    1. addOrUpdatePopupValidation, deleteOrGetPopupByIdValidation, getPopupByUrlValidation added in popup.helper.js file.
    2. above mentioned validations also added as middlewares in the below routes in the popup.routes.js file.
      add_popup, delete_popup/:id, edit_popup/:id, get_popup/:id, get_popup_by_url
  • Removed

    1. Existing validation code removed from popup.controller.js file.

Existing functions in popup.helper.js file such as validatePopupSize, validatePopupSizeWrapper, validateRelativeUrl, validateUrl have been left as is since they might be useful in another implementations.

Copy link
Contributor

coderabbitai bot commented Feb 19, 2025

Walkthrough

This pull request updates documentation and environment settings while refactoring popup validation. The README now includes a demo link, updated installation instructions, and revised project names. The backend’s environment file replaces old database credentials with new ones. Additionally, the popup controller has been simplified by removing in-method validations, with corresponding validation middleware now added in the routes and new validation logic introduced in the popup helper.

Changes

File(s) Changes Summary
README.md Added a demo link and forum link, updated installation instructions (Git and optional steps), revised API script version (agent-1.0.2) and project names.
backend/.env Replaced old database credentials (DB_USERNAME, DB_PASSWORD, DB_NAME, DB_HOST) with new values; other configurations remain unchanged.
backend/src/controllers/popup.controller.js,
backend/src/routes/popup.routes.js,
backend/src/utils/popup.helper.js
Removed in-method popup validations and error handling in the controller; added validation middleware in popup routes; introduced new validation arrays in popup helper.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Router
    participant Validation
    participant Controller
    participant Database

    Client->>Router: Sends popup request
    Router->>Validation: Execute validation middleware
    Validation-->>Router: Return validation result
    Router->>Controller: Forward validated request
    Controller->>Database: Process popup operation
Loading

Possibly related PRs

Suggested reviewers

  • swoopertr
  • erenfn
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (4)
backend/src/utils/popup.helper.js (2)

24-35: Enhance URL validation logic

The current URL validation could be improved to handle edge cases and provide better error messages.

Consider this improved implementation:

-    .custom((value, {req}) => {
-      const needsUrl = ['open url', 'open url in a new tab'].includes(req.body.closeButtonAction);
-      return !needsUrl || (needsUrl && value)
-    })
+    .custom((value, {req}) => {
+      const needsUrl = ['open url', 'open url in a new tab'].includes(req.body.closeButtonAction);
+      if (needsUrl && !value?.trim()) {
+        throw new Error('URL is required when close button action is set to open URL');
+      }
+      return true;
+    })
-    .withMessage('URL is required when close button action is set to open URL')
-    .bail()
-    .custom((value) => {
-      if (!value) return true;
-      const url = new URL(value)
-      return ['http:', 'https:'].includes(url.protocol) || value.startsWith('/');
-    })
+    .bail()
+    .custom((value) => {
+      if (!value?.trim()) return true;
+      try {
+        const url = new URL(value.startsWith('/') ? `http://dummy.com${value}` : value);
+        return ['http:', 'https:'].includes(url.protocol) || value.startsWith('/');
+      } catch (error) {
+        throw new Error('Invalid URL format');
+      }
+    })

37-46: Improve error handling in actionUrl validation

The current implementation swallows the specific error message from the URL constructor.

Consider this enhanced version:

-    .custom((value) => {
-      if(!value) return true;
-      try {
-        const url = new URL(value);
-        return ['http:', 'https:'].includes(url.protocol)
-      }catch {
-        return false
-      }
-    })
+    .custom((value) => {
+      if (!value?.trim()) return true;
+      try {
+        const url = new URL(value);
+        if (!['http:', 'https:'].includes(url.protocol)) {
+          throw new Error('URL must use HTTP or HTTPS protocol');
+        }
+        return true;
+      } catch (error) {
+        throw new Error(error.message || 'Invalid URL format');
+      }
+    })
README.md (2)

275-275: Fix grammar in DataHall description

There's a grammatical error in the article usage.

-[DataHall](https://github.com/bluewave-labs/datahall), an secure file sharing application, aka dataroom.
+[DataHall](https://github.com/bluewave-labs/datahall), a secure file sharing application, aka dataroom.
🧰 Tools
🪛 LanguageTool

[misspelling] ~275-~275: Use “a” instead of ‘an’ if the following word doesn’t start with a vowel sound, e.g. ‘a sentence’, ‘a university’.
Context: ...s://github.com/bluewave-labs/datahall), an secure file sharing application, aka da...

(EN_A_VS_AN)


242-252: Add language identifier to code block

The code block is missing a language identifier.

-```
+```javascript
window.bwApiBaseUrl = 'https://guidefox-demo.bluewavelabs.ca/api/';
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

242-242: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 18c9d81 and 4d184c1.

📒 Files selected for processing (5)
  • README.md (5 hunks)
  • backend/.env (1 hunks)
  • backend/src/controllers/popup.controller.js (0 hunks)
  • backend/src/routes/popup.routes.js (1 hunks)
  • backend/src/utils/popup.helper.js (2 hunks)
💤 Files with no reviewable changes (1)
  • backend/src/controllers/popup.controller.js
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
README.md

242-242: Fenced code blocks should have a language specified
null

(MD040, fenced-code-language)

🪛 LanguageTool
README.md

[misspelling] ~275-~275: Use “a” instead of ‘an’ if the following word doesn’t start with a vowel sound, e.g. ‘a sentence’, ‘a university’.
Context: ...s://github.com/bluewave-labs/datahall), an secure file sharing application, aka da...

(EN_A_VS_AN)

@Anurag9977 Anurag9977 closed this Feb 19, 2025
@Anurag9977 Anurag9977 deleted the refactor-popup-validations branch February 19, 2025 20:31
@Anurag9977
Copy link
Author

This PR has been closed due to issues with the forked repo branch. A new PR addressing the same issue has been raised and is under review.

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.

4 participants