PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 All 203 releases
betterdocs / react-src / admin / faq-builder / utils / validations.js

validations.js in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.5, at react-src/admin/faq-builder/utils/validations.js

120 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 export const validateFormFields = (formFields, rules) => {
2 const errors = {};
3 for (const field in rules) {
4 if (rules.hasOwnProperty(field)) {
5 const fieldRules = rules[field];
6 let value = "";
7 if (formFields.hasOwnProperty(field)) {
8 if (typeof formFields[field] !== "object") {
9 value = formFields[field];
10 } else {
11 value = formFields[field].value;
12 }
13 }
14 if (fieldRules.hasOwnProperty("required")) {
15 let valid = true;
16 if (typeof value === "boolean") {
17 if (!value) {
18 valid = false;
19 }
20 }
21 if (!value.length) {
22 valid = false;
23 }
24 if (!valid) {
25 if (typeof fieldRules.required !== "object") {
26 errors[field] = `${ucFirst(field)} is required`;
27 } else {
28 errors[field] = fieldRules.required.message;
29 }
30 continue;
31 }
32 }
33 if (fieldRules.hasOwnProperty("email")) {
34 const pattern = new RegExp(
35 /*/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i*/
36 /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/
37 );
38 if (!pattern.test(formFields[field])) {
39 if (typeof fieldRules.email !== "object") {
40 errors[field] = `${ucFirst(field)} must be a valid email`;
41 } else {
42 errors[field] = fieldRules.email.message;
43 }
44 }
45 }
46
47 if (fieldRules.hasOwnProperty("username")) {
48 const pattern = new RegExp(/^\S+$/);
49 if (!pattern.test(formFields[field])) {
50 if (typeof fieldRules.username !== "object") {
51 errors[field] = `${ucFirst(field)} must be a valid username`;
52 } else {
53 errors[field] = fieldRules.username.message;
54 }
55 }
56 }
57
58 if (fieldRules.hasOwnProperty("minLength")) {
59 // QA-003: for rich-text fields, measure the VISIBLE text, not the raw
60 // HTML — otherwise an effectively-empty answer like `<p>&nbsp;</p>`
61 // (12 chars) passes a minLength:9 check and a blank FAQ is saved.
62 let measured = value;
63 if (fieldRules.minLength.stripHtml && typeof measured === "string") {
64 measured = measured
65 .replace(/<[^>]*>/g, "")
66 .replace(/&nbsp;|&#160;/gi, " ")
67 .replace(/\s+/g, " ")
68 .trim();
69 }
70 if ((measured ? measured.length : 0) < fieldRules.minLength.value) {
71 if (typeof fieldRules.minLength !== "object") {
72 errors[field] = `The length of ${ucFirst(field)} must be more ${
73 fieldRules.minLength.value
74 } character`;
75 } else {
76 errors[field] = fieldRules.minLength.message;
77 }
78 }
79 }
80 if (fieldRules.hasOwnProperty("maxLength")) {
81 if (formFields[field].length > fieldRules.maxLength.value) {
82 if (typeof fieldRules.maxLength !== "object") {
83 errors[field] = `The length of ${ucFirst(field)} must be under ${
84 fieldRules.maxLength.value
85 } character`;
86 } else {
87 errors[field] = fieldRules.maxLength.message;
88 }
89 }
90 }
91 if (fieldRules.hasOwnProperty("match")) {
92 const matchField =
93 typeof fieldRules.match !== "object"
94 ? fieldRules.match
95 : fieldRules.match.value;
96 const matchFieldValue = formFields[matchField];
97 if (matchFieldValue && matchFieldValue !== formFields[field]) {
98 if (typeof fieldRules.match !== "object") {
99 errors[field] = `The ${ucFirst(field)} must match with ${ucFirst(
100 matchField
101 )}`;
102 } else {
103 errors[field] = fieldRules.match.message;
104 }
105 }
106 }
107 }
108 }
109 if (Object.keys(errors).length) {
110 return {
111 valid: false,
112 errors,
113 };
114 }
115 return {
116 valid: true,
117 errors: undefined,
118 };
119 };
120