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 Validation block for validation errors #11152

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

Harsh-D-2004
Copy link

@Harsh-D-2004 Harsh-D-2004 commented Mar 8, 2025

Proposed Changes

@ohcnetwork/care-fe-code-reviewers

Merge Checklist

  • Add specs that demonstrate bug / test a new feature.
  • Update product documentation.
  • Ensure that UI text is kept in I18n files.
  • Prep screenshot or demo video for changelog entry, and attach it to issue.
  • Request for Peer Reviews
  • Completion of QA in Mobile Devices
  • Completion of QA in Desktop Devices

Summary by CodeRabbit

  • New Features
    • Enhanced error notifications now provide more detailed, consolidated feedback for validation issues, ensuring users receive clearer guidance when encountering upload errors.

@Harsh-D-2004 Harsh-D-2004 requested a review from a team as a code owner March 8, 2025 20:17
Copy link
Contributor

coderabbitai bot commented Mar 8, 2025

Walkthrough

This change introduces an additional conditional block in the notifyError function within src/Utils/Notifications.ts. The new logic specifically processes errors tagged as "validation_error" by checking if the msg property exists and is an object. It then concatenates multiple error messages (if provided as an array) and displays them via a toast notification, or defaults to a preset message if no detail is found. The existing error handling remains intact.

Changes

File Change Summary
src/Utils/Notifications.ts Added a new conditional block in notifyError to handle validation_error: checks for an object-type msg, concatenates messages (including array values), and triggers a toast notification with either the compiled message or a default error message.

Sequence Diagram(s)

sequenceDiagram
  participant U as User
  participant N as notifyError()
  participant T as Toast Notification

  U->>N: Call notifyError(error)
  alt error type is "validation_error"
      N->>N: Check if error.msg exists and is object
      N->>N: Iterate over error.msg values and concatenate messages
      alt concatenated messages exist
         N->>T: Trigger toast notification with concatenated message
      else
         N->>T: Trigger toast with default error message
      end
  else Other error types
      N->>N: Process error using the existing error handling logic
      N->>T: Trigger toast notification with generic message
  end
Loading

Suggested labels

needs testing, needs review

Suggested reviewers

  • rithviknishad
  • Jacobjeevan

Poem

I'm a rabbit with a knack for code,
Hopping through errors on the debug road.
Validation errors now get a rhythmic beat,
Condensed and clear, making fixes neat.
With a twitch of my ear and a joyful hop,
I celebrate our clean code atop! 🐇💻

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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.
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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

netlify bot commented Mar 8, 2025

Deploy Preview for care-ohc ready!

Name Link
🔨 Latest commit 018abff
🔍 Latest deploy log https://app.netlify.com/sites/care-ohc/deploys/67ccaf5bb3a03e0008b334f5
😎 Deploy Preview https://deploy-preview-11152.preview.ohc.network
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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: 1

🧹 Nitpick comments (3)
src/Utils/Notifications.ts (3)

25-44: New validation error block looks good, but has minor formatting issues.

The added validation block effectively handles errors of type "validation_error" by processing error messages from the error.msg object. However, there are a few improvements that could be made:

  1. When joining array elements with commas in line 32, consider adding a space after each comma for better readability.
  2. When appending non-array messages in line 34, there's no separator between different error messages, which could make the final message difficult to read.
  3. The default error message has an unnecessary space before the comma and is specifically about uploading images, which might not apply to all validation errors.
  if (
    error?.type === "validation_error" &&
    error?.msg &&
    typeof error.msg === "object"
  ) {
    Object.values(error.msg).forEach((messages) => {
      if (Array.isArray(messages)) {
-        errorMsg += messages.join(", ");
+        errorMsg += messages.join(", ") + ". ";
      } else {
-        errorMsg += messages;
+        errorMsg += messages + ". ";
      }
    });

    if (errorMsg.length > 0) {
      toast.error(errorMsg);
    } else {
-      toast.error("Something went wrong , please upload different image");
+      toast.error("Something went wrong, please try again");
    }
    return;
  }

25-30: Consider adding type checking for the error messages.

While handling validation errors, it's important to ensure that the messages being processed are of the expected type. Currently, there's no type checking for the values returned by Object.values(error.msg).

Adding type guards would make the code more robust against unexpected data structures:

  if (
    error?.type === "validation_error" &&
    error?.msg &&
    typeof error.msg === "object"
  ) {
-    Object.values(error.msg).forEach((messages) => {
+    Object.values(error.msg).forEach((messages: any) => {
      if (Array.isArray(messages)) {

38-42: Generic error message may be more appropriate.

The default error message is specifically about uploading a different image, but validation errors could be related to other fields or actions. A more generic error message would be more versatile.

    if (errorMsg.length > 0) {
      toast.error(errorMsg);
    } else {
-      toast.error("Something went wrong , please upload different image");
+      toast.error("Validation failed. Please check your input and try again.");
    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e4570f7 and f80efeb.

📒 Files selected for processing (1)
  • src/Utils/Notifications.ts (1 hunks)
🔇 Additional comments (1)
src/Utils/Notifications.ts (1)

25-44:

❓ Verification inconclusive

Verify integration with validation error sources.

Since this code is specifically handling validation errors for image uploads (based on the default error message), it's important to verify that the error structure matches what's expected from the API or validation library.


🏁 Script executed:

#!/bin/bash
# Look for places where validation errors might be created
rg "validation_error" --type ts
# Look for API calls related to image uploads that might return this error structure
rg "upload.*image" --type ts

Length of output: 951


Action Required: Verify image upload validation error integration.
Please ensure that the error structure returned by the image upload API matches the expected format in this function. In particular:

  • Confirm that for image uploads, the API returns a validation error where error?.msg is an object (with values as arrays or strings) so that the error messages are concatenated correctly.
  • Double-check that the fallback error message ("Something went wrong , please upload different image") is appropriate given the API response, and consider aligning it with similar messages used elsewhere (e.g., in AvatarEditModal.tsx).
  • Verify that the integration of validation error handling in this file is consistent with how other validation errors (such as the phone number errors in validators.ts and PatientLogin.tsx) are managed across the codebase.

Comment on lines +25 to +44
if (
error?.type === "validation_error" &&
error?.msg &&
typeof error.msg === "object"
) {
Object.values(error.msg).forEach((messages) => {
if (Array.isArray(messages)) {
errorMsg += messages.join(", ");
} else {
errorMsg += messages;
}
});

if (errorMsg.length > 0) {
toast.error(errorMsg);
} else {
toast.error("Something went wrong , please upload different image");
}
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

Test coverage for the new validation block.

The new validation error handling block handles a specific error format. Make sure to add tests for this scenario to ensure it works as expected for various validation error structures.

Please verify that there are unit tests covering this new validation error handling logic, especially with:

  1. Various message structures (arrays, strings)
  2. Empty msg object
  3. Different validation error types

🏁 Script executed:

#!/bin/bash
# Search for test files that might test the notifyError function
fd -t f "test|spec" --exec grep -l "notifyError" {} \;

Length of output: 57


Attention: Missing Unit Tests for New Validation Logic

It appears that our automated search didn’t locate any test files referencing the validation error handling (e.g. via “notifyError” or “validation_error”) in our test or spec directories. Please manually verify if tests exist under alternate naming conventions. If not, add unit tests covering the following cases:

  • Array messages: Verify that an error message constructed from an array (joined via commas) is correctly handled.
  • String messages: Confirm that single-message (non-array) errors are processed properly.
  • Empty msg object: Ensure that an empty or missing message object falls back to the default error.
  • Different error types: Test that only errors with error?.type === "validation_error" trigger this logic, while others fall back accordingly.

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.

Bug : Large image upload error in edit avatar
1 participant