-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcomponentNameValidation.js
More file actions
63 lines (59 loc) · 1.73 KB
/
Copy pathcomponentNameValidation.js
File metadata and controls
63 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { setNameError } from "../redux/EditorSlice";
import { allowedExtensionsString } from "./allowedExtensionsString";
const allowedExtensions = {
python: ["py", "csv", "txt"],
html: ["html", "css", "js"],
};
const reservedFileNames = ["INSTRUCTIONS.md"];
const isValidFileName = (fileName, projectType, componentNames) => {
const extension = fileName.split(".").slice(1).join(".");
if (
!reservedFileNames.includes(fileName) &&
allowedExtensions[projectType].includes(extension) &&
!componentNames.includes(fileName) &&
fileName.split(" ").length === 1
) {
return true;
} else {
return false;
}
};
export const validateFileName = (
fileName,
projectType = "python",
componentNames,
dispatch,
t,
callback,
currentFileName = null,
) => {
const extension = fileName.split(".").slice(1).join(".");
if (
isValidFileName(fileName, projectType, componentNames) ||
(currentFileName && fileName === currentFileName)
) {
callback();
} else if (reservedFileNames.includes(fileName)) {
dispatch(
setNameError(t("filePanel.errors.reservedFileName", { fileName })),
);
} else if (componentNames.includes(fileName)) {
dispatch(setNameError(t("filePanel.errors.notUnique")));
} else if (fileName.split(" ").length > 1) {
dispatch(setNameError(t("filePanel.errors.containsSpaces")));
} else if (!allowedExtensions[projectType].includes(extension)) {
dispatch(
setNameError(
t("filePanel.errors.unsupportedExtension", {
allowedExtensions: allowedExtensionsString(
projectType,
t,
allowedExtensions,
),
}),
),
);
} else {
dispatch(setNameError(t("filePanel.errors.generalError")));
}
};