Skip to content

Commit 6e7bf73

Browse files
committedNov 13, 2024
feat: Add input validation example
1 parent 34a67f6 commit 6e7bf73

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed
 

‎docs/concepts/plugins.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ There is a small group of official plugins maintained, which provide useful func
1515

1616
- [**Input Validator**](https://github.com/react-chatbotify-plugins/input-validator)
1717
- [**Markdown Parser**](https://github.com/react-chatbotify-plugins/markdown-parser)
18-
- [**llm-connector**](https://github.com/react-chatbotify-plugins/llm-connector) (WIP)
19-
- [**discord-live-chat**](https://github.com/react-chatbotify-plugins/discord-live-chat) (WIP)
18+
- [**LLM Connector**](https://github.com/react-chatbotify-plugins/llm-connector) (WIP)
19+
- [**Discord Live Chat**](https://github.com/react-chatbotify-plugins/discord-live-chat) (WIP)
2020

2121
If you strongly believe that there are common use cases with no suitable plugins, you may feel free to make a suggestion for one on [**discord**](https://discord.gg/J6pA4v3AMW).
2222

‎docs/examples/input_validation.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
sidebar_position: 22
3+
title: Input Validation
4+
description: input validation chatbot example
5+
keywords: [react, chat, chatbot, chatbotify]
6+
---
7+
8+
# Custom Icon
9+
10+
The following is an example for performing user input validation. It leverages on the [**Input Validator Plugin**](https://www.npmjs.com/package/@rcb-plugins/input-validator), which is maintained separately on the [**React ChatBotify Plugins**](https://github.com/orgs/React-ChatBotify-Plugins) organization. If you require support with the plugin, please reach out to support on the [**plugins discord**](https://discord.gg/J6pA4v3AMW) instead.
11+
12+
```jsx live noInline title=MyChatBot.js
13+
const MyChatBot = () => {
14+
// loads input validator plugin to be passed into chatbot
15+
const plugins = [InputValidator()];
16+
17+
// example flow for testing validation that age must be a number
18+
const flow: Flow = {
19+
start: {
20+
message: "Hey there, please enter your age!",
21+
path: "try_again",
22+
validateInput: (userInput: string) => {
23+
if (typeof userInput === "string" && !Number.isNaN(Number(userInput))) {
24+
return {success: true};
25+
}
26+
return {success: false, promptContent: "Age must be a number!", promptDuration: 3000, promptType: "error", highlightTextArea: true};
27+
}
28+
} as InputValidatorBlock,
29+
try_again : {
30+
message: "Nice, you passed the input validation!",
31+
path: "start",
32+
}
33+
}
34+
35+
const settings = {
36+
general: {
37+
embedded: true
38+
},
39+
chatHistory: {
40+
storageKey: "example_input_validation"
41+
}
42+
}
43+
44+
return (
45+
<ChatBot plugins={plugins} settings={settings} flow={flow}/>
46+
);
47+
};
48+
49+
render(<MyChatBot/>)
50+
```

‎package-lock.json

+30-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
"@docusaurus/theme-live-codeblock": "^3.2.0",
2323
"@google/generative-ai": "^0.2.1",
2424
"@mdx-js/react": "^3.0.1",
25+
"@rcb-plugins/input-validator": "^0.1.0",
2526
"clsx": "^1.2.1",
2627
"openai": "^4.47.1",
2728
"prism-react-renderer": "^2.3.1",
2829
"react": "^18.2.0",
29-
"react-chatbotify": "^2.0.0-beta.23",
30+
"react-chatbotify": "^2.0.0-beta.24",
3031
"react-dom": "^18.2.0",
3132
"react-github-btn": "^1.4.0"
3233
},

‎src/theme/ReactLiveScope/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ import OpenAI from "openai";
44
import ExecutionEnvironment from "@docusaurus/ExecutionEnvironment";
55

66
let ChatBot = null;
7+
let InputValidator = null;
78
let reactChatbotify = {};
89

910
if (ExecutionEnvironment.canUseDOM) {
11+
// imports react chatbotify core library
1012
const chatbotifyModule = require("react-chatbotify");
1113
ChatBot = chatbotifyModule.default;
1214
reactChatbotify = { ...chatbotifyModule };
1315
delete reactChatbotify.default;
16+
17+
// imports rcb plugin - input validator
18+
InputValidator = require("@rcb-plugins/input-validator");
1419
}
1520

1621
const ReactLiveScope = {
1722
React,
1823
ChatBot,
1924
...reactChatbotify,
25+
InputValidator,
2026
GoogleGenerativeAI,
2127
OpenAI,
2228
};

0 commit comments

Comments
 (0)