Skip to content

Commit d42d6e9

Browse files
committed
made some changes
1 parent 00f183b commit d42d6e9

File tree

3 files changed

+80
-61
lines changed

3 files changed

+80
-61
lines changed

src/App.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import React from "react";
21
import ChatBot, { Flow } from "react-chatbotify";
2+
33
import RcbPlugin from "./factory/RcbPluginFactory";
44
import { InputValidatorBlock } from "./types/InputValidatorBlock";
55
import { validateFile } from "./utils/validateFile";
66

77
const App = () => {
8+
// initialize example plugin
89
const plugins = [RcbPlugin()];
910

11+
// example flow for testing
1012
const flow: Flow = {
1113
start: {
1214
message: "Hey there! Please enter your age.",
@@ -31,9 +33,9 @@ const App = () => {
3133
validateInput: (userInput?: string) => {
3234
console.log("validateInput called with userInput:", userInput);
3335

34-
// Allow empty input or file names with allowed extensions
36+
3537
if (
36-
!userInput ||
38+
userInput &&
3739
/\.(jpg|jpeg|png)$/i.test(userInput.trim())
3840
) {
3941
return { success: true };
@@ -42,44 +44,46 @@ const App = () => {
4244
// Disallow other text inputs
4345
return {
4446
success: false,
45-
promptContent: "Please upload a file.",
47+
promptContent: "Please upload a valid file (JPEG or PNG). Empty inputs are not allowed.",
4648
promptDuration: 3000,
4749
promptType: "error",
4850
};
4951
},
50-
validateFile: (file?: File) => {
52+
validateFileInput: (file?: File) => {
5153
return validateFile(file); // Validate file input
5254
},
5355
file: async ({ files }) => {
5456
console.log("Files received:", files);
55-
57+
5658
if (files && files[0]) {
5759
const validationResult = validateFile(files[0]);
5860
if (!validationResult.success) {
5961
console.error(validationResult.promptContent);
60-
return;
62+
// Return early to prevent success
63+
return { success: false };
6164
}
6265
console.log("File uploaded successfully:", files[0]);
6366
} else {
6467
console.error("No file provided.");
6568
}
6669
},
70+
6771
} as InputValidatorBlock,
6872

6973
file_upload_validation: {
7074
message:
71-
"Thank you! Your profile picture has been uploaded successfully.",
72-
path: "end",
75+
"Thank you! Your picture has been uploaded successfully. You passed the file upload validation!",
76+
path: "start",
7377
},
78+
}
7479

75-
end: {
76-
message: "This is the end of the flow. Thank you!",
77-
},
78-
};
79-
80-
81-
82-
return <ChatBot id="chatbot-id" plugins={plugins} flow={flow} />;
83-
};
80+
return (
81+
<ChatBot
82+
id="chatbot-id"
83+
plugins={plugins}
84+
flow={flow}
85+
></ChatBot>
86+
);
87+
}
8488

8589
export default App;

src/types/InputValidatorBlock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import { Block } from "react-chatbotify";
44
import { ValidationResult } from "./ValidationResult";
55

6+
/**
7+
* Extends the Block from React ChatBotify to support inputValidator attribute.
8+
*/
9+
610
export type InputValidatorBlock = Omit<Block, "file"> & {
711
file?: (params: { files?: FileList }) => void | Promise<void>; // Updated
812
validateInput?: (userInput?: string) => ValidationResult;

src/utils/validateFile.ts

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,57 @@ import { ValidationResult } from "../types/ValidationResult";
55
* Ensures the file is of allowed type and size, and rejects non-file inputs.
66
*/
77
export const validateFile = (file?: File): ValidationResult => {
8-
const allowedTypes = ["image/jpeg", "image/png"];
9-
const maxSizeInBytes = 5 * 1024 * 1024; // 5MB
10-
11-
// Check if no file is provided
12-
if (!file) {
13-
return {
14-
success: false,
15-
promptContent: "No file uploaded.",
16-
promptDuration: 3000,
17-
promptType: "error",
18-
};
19-
}
20-
21-
// Check if the input is not a File object (e.g., text input or invalid type)
22-
if (!(file instanceof File)) {
23-
return {
24-
success: false,
25-
promptContent: "Invalid input. Please upload a valid file.",
26-
promptDuration: 3000,
27-
promptType: "error",
28-
};
29-
}
30-
31-
// Validate file type
32-
if (!allowedTypes.includes(file.type)) {
33-
return {
34-
success: false,
35-
promptContent: "Only JPEG or PNG files are allowed.",
36-
promptType: "error",
37-
};
38-
}
39-
40-
// Validate file size
41-
if (file.size > maxSizeInBytes) {
42-
return {
43-
success: false,
44-
promptContent: "File size must be less than 5MB.",
45-
promptType: "error",
46-
};
47-
}
48-
49-
return { success: true };
50-
};
8+
const allowedTypes = ["image/jpeg", "image/png"];
9+
const maxSizeInBytes = 5 * 1024 * 1024; // 5MB
10+
11+
// Check if no file is provided
12+
if (!file) {
13+
return {
14+
success: false,
15+
promptContent: "No file uploaded.",
16+
promptDuration: 3000,
17+
promptType: "error",
18+
};
19+
}
20+
21+
// Check if the input is not a File object (e.g., text input or invalid type)
22+
if (!(file instanceof File)) {
23+
return {
24+
success: false,
25+
promptContent: "Invalid input. Please upload a valid file.",
26+
promptDuration: 3000,
27+
promptType: "error",
28+
};
29+
}
30+
31+
// Check if the file is empty
32+
if (file.size === 0) {
33+
return {
34+
success: false,
35+
promptContent: "The uploaded file is empty. Please upload a valid file.",
36+
promptDuration: 3000,
37+
promptType: "error",
38+
};
39+
}
40+
41+
// Validate file type
42+
if (!allowedTypes.includes(file.type)) {
43+
return {
44+
success: false,
45+
promptContent: "Only JPEG or PNG files are allowed.",
46+
promptType: "error",
47+
};
48+
}
49+
50+
// Validate file size
51+
if (file.size > maxSizeInBytes) {
52+
return {
53+
success: false,
54+
promptContent: "File size must be less than 5MB.",
55+
promptType: "error",
56+
};
57+
}
58+
59+
return { success: true };
60+
};
61+

0 commit comments

Comments
 (0)