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

Validation #2

Merged
merged 7 commits into from
Nov 25, 2024
Merged

Validation #2

merged 7 commits into from
Nov 25, 2024

Conversation

tasbi03
Copy link
Contributor

@tasbi03 tasbi03 commented Nov 22, 2024

Description

This PR introduces comprehensive file upload handling to the input-validator plugin, including validation for uploaded files and improved control over input types. It separates the validation logic for text and file inputs, adds real-time feedback for validation errors, and ensures backward compatibility with existing text-only behavior. Additionally, it provides a new chatDisabled property to control whether text input is allowed during specific flow blocks.

Closes #1


What Change Does This PR Introduce?

Please select the relevant option(s):

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update (changes to docs/code comments)

Updated Implementation

File Upload Handling:

  • Added validateFileInput to handle file-specific validation, ensuring:
    • Files are of allowed types (JPEG, PNG).
    • Files are not empty.
    • Files meet the size restriction (max 5MB).
  • Added a file function to process file uploads asynchronously and validate uploaded files.

Input Type Control:

  • Introduced the chatDisabled property to control whether text input is allowed during a particular flow block.
    • If chatDisabled: true, only file uploads are permitted.
    • Default behavior remains unchanged (both text and file inputs are allowed).

Enhanced Validation Feedback:

  • Implemented toast notifications for both text and file validation failures, providing real-time feedback to users.
  • Clear prompts and error messages guide users on acceptable inputs.

Separation of Validation Logic:

  • Refactored validation logic to ensure:
    • validateTextInput applies exclusively to text inputs.
    • validateFileInput applies exclusively to file uploads.
  • Improved maintainability by separating responsibilities.

Backward Compatibility:

  • Maintains compatibility with existing behavior for text-only flows unless explicitly configured to allow or restrict inputs.

Restored Original Styles:

  • Preserved and restored original styles after displaying toast notifications, ensuring visual consistency.

Checklist

  • The commit message follows our adopted guidelines.
  • Testing has been done for the change(s).
  • [] Relevant comments/docs have been added/updated (for bug fixes/features).

Testing

Verified the following scenarios:

  • File Upload Validation: Ensured only valid files (JPEG/PNG, max 5MB, non-empty) are accepted.
  • Text Input Validation: Validates text inputs correctly when chatDisabled: false.
  • chatDisabled: true: Confirms that text input is disabled, allowing only file uploads.
  • Default Behavior: Verified that both text and file inputs are accepted when chatDisabled is not set.
  • Error Feedback: Ensured appropriate toast notifications and prompts are displayed for invalid inputs.
  • Style Restoration: Verified original styles are restored after dismissing prompts.

Additional Notes

This PR enhances the plugin significantly by adding robust support for file uploads, improved feedback mechanisms, and flexible input control. It maintains backward compatibility while aligning with the plugin's design philosophy.

Let me know if any further adjustments or additional documentation are required!

Copy link
Member

@tjtanjin tjtanjin left a comment

Choose a reason for hiding this comment

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

Hey @tasbi03, thank you for the PR 😊 A couple of changes required, let me know if you need further clarifications!

src/App.tsx Outdated
Comment on lines 35 to 51
console.log("validateTextInput called with userInput:", userInput);

if (userInput && userInput.trim().length > 0) {
// Optionally, validate if the input is a valid URL
// For simplicity, we'll accept any non-empty text
return { success: true };
}

return {
success: false,
promptContent:
"Please provide a valid URL or upload a file.",
promptDuration: 3000,
promptType: "error",
};
},
Copy link
Member

Choose a reason for hiding this comment

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

No need for validateTextInput here, since chatDisabled is already set to true.

src/App.tsx Outdated
Comment on lines 58 to 68
const validationResult = validateFile(files[0]);
if (!validationResult.success) {
console.error(validationResult.promptContent);
// Return early to prevent success
return { success: false };
}
console.log("File uploaded successfully:", files[0]);
} else {
console.error("No file provided.");
}
Copy link
Member

Choose a reason for hiding this comment

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

The validation for files were already performed in validateFileInput, so we don't have to repeat any checks here. Sufficient to just log the files received.

Comment on lines +73 to +74
"Thank you! Your input has been received. You passed the validation!",
Copy link
Member

Choose a reason for hiding this comment

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

This can go in a single line.

@@ -17,7 +18,7 @@ import { getValidator } from "../utils/getValidator";
/**
* Plugin hook that handles all the core logic.
*
* @param pluginConfig configurations for the plugin
* @param pluginConfig Configurations for the plugin.
Copy link
Member

Choose a reason for hiding this comment

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

Unnecessary comment changes, there're some other ones in the rest of the code, can revert these changes :3

validateInput: (userInput?: string) => ValidationResult;

export type InputValidatorBlock = Omit<Block, "file"> & {
file?: (params: { files?: FileList }) => void | Promise<void>;
Copy link
Member

Choose a reason for hiding this comment

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

The file attribute is already present in the original Block type, not needed here.

export type InputValidatorBlock = Omit<Block, "file"> & {
file?: (params: { files?: FileList }) => void | Promise<void>;
validateTextInput?: (userInput?: string) => ValidationResult;
validateFileInput?: (file?: File) => ValidationResult;
Copy link
Member

Choose a reason for hiding this comment

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

It's actually possible that users may submit multiple files, so we have to cater to that possibility.

}

if (!event.detail.currPath) {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm any reason this was removed in favour of splitting into 2 separate checks?

Comment on lines 100 to 102
console.error("No file uploaded.");
event.preventDefault();
return;
Copy link
Member

Choose a reason for hiding this comment

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

Missing indentations, same for the code below.

Copy link
Member

@tjtanjin tjtanjin left a comment

Choose a reason for hiding this comment

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

LGTM! There'll likely be some minor touch ups required, along with the adding of test cases but this can be tackled separately. Thanks for the PR again!

@tjtanjin tjtanjin merged commit 9a2b04d into React-ChatBotify-Plugins:main Nov 25, 2024
4 checks passed
@tasbi03 tasbi03 deleted the validation branch November 26, 2024 00:44
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.

[Task] Add validation support for file uploads
2 participants