Add validation error message for inputbox component (#8909)

* add validation error message for inputbox component

* addressing comments

* remove copying entire definition for InputBoxProperties
This commit is contained in:
Kim Santiago
2020-01-24 11:38:49 -08:00
committed by GitHub
parent 7e0c7e35a1
commit 9e61c468d1
8 changed files with 112 additions and 21 deletions

View File

@@ -4,11 +4,12 @@
*--------------------------------------------------------------------------------------------*/
import * as os from 'os';
import * as path from 'path';
import * as nls from 'vscode-nls';
const WINDOWS_INVALID_FILE_CHARS = /[\\/:\*\?"<>\|]/g;
const UNIX_INVALID_FILE_CHARS = /[\\/]/g;
const isWindows = os.platform() === 'win32';
const WINDOWS_FORBIDDEN_NAMES = /^(con|prn|aux|clock\$|nul|lpt[0-9]|com[0-9])$/i;
const localize = nls.loadMessageBundle();
/**
* Determines if a given character is a valid filename character
@@ -88,4 +89,49 @@ export function isValidBasename(name: string | null | undefined): boolean {
}
return true;
}
}
/**
* Returns specific error message if file name is invalid
* Logic is copied from src\vs\base\common\extpath.ts
* @param name filename to check
*/
export function isValidBasenameErrorMessage(name: string | null | undefined): string {
const invalidFileChars = isWindows ? WINDOWS_INVALID_FILE_CHARS : UNIX_INVALID_FILE_CHARS;
if (!name) {
return localize('dacfx.undefinedFileNameErrorMessage', "Undefined name");
}
if (isWindows && name[name.length - 1] === '.') {
return localize('dacfx.fileNameEndingInPeriodErrorMessage', "File name cannot end with a period"); // Windows: file cannot end with a "."
}
let basename = path.parse(name).name;
if (!basename || basename.length === 0 || /^\s+$/.test(basename)) {
return localize('dacfx.whitespaceFilenameErrorMessage', "File name cannot be whitespace"); // require a name that is not just whitespace
}
invalidFileChars.lastIndex = 0;
if (invalidFileChars.test(basename)) {
return localize('dacfx.invalidFileCharsErrorMessage', "Invalid file characters"); // check for certain invalid file characters
}
if (isWindows && WINDOWS_FORBIDDEN_NAMES.test(basename)) {
console.error('here');
return localize('dacfx.reservedWindowsFileNameErrorMessage', "This file name is reserved for use by Windows. Choose another name and try again"); // check for certain invalid file names
}
if (basename === '.' || basename === '..') {
return localize('dacfx.reservedValueErrorMessage', "Reserved file name. Choose another name and try again"); // check for reserved values
}
if (isWindows && basename.length !== basename.trim().length) {
return localize('dacfx.trailingWhitespaceErrorMessage', "File name cannot end with a whitespace"); // Windows: file cannot end with a whitespace
}
if (basename.length > 255) {
return localize('dacfx.tooLongFileNameErrorMessage', "File name is over 255 characters"); // most file systems do not allow files > 255 length
}
return '';
}