mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 15:48:32 -06:00
adding file name validation
This commit is contained in:
28
src/services/fileNameValidation.ts
Normal file
28
src/services/fileNameValidation.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
export function validateFileName(fileName: string): string {
|
||||
if (!fileName || fileName.trim() === "") {
|
||||
return "Name cannot be empty";
|
||||
}
|
||||
|
||||
const invalidChars = [":", "/", "\\", "*", "?", '"', "<", ">", "|"];
|
||||
|
||||
for (const char of fileName) {
|
||||
if (invalidChars.includes(char)) {
|
||||
return `Name contains invalid character: "${char}". Please avoid: ${invalidChars.join(
|
||||
" "
|
||||
)}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (fileName !== fileName.trimEnd()) {
|
||||
return "Name cannot end with whitespace";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
export function assertValidFileName(fileName: string): void {
|
||||
const error = validateFileName(fileName);
|
||||
if (error) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user