Fixed file name validation unit test + added newline

- fixed file name validation unit test
- added "\n" as forbidden character in isFileNameValid()
This commit is contained in:
Vincent Petry 2014-01-30 11:42:43 +01:00
parent d36a2ff9ee
commit ba1b9df8a9
2 changed files with 16 additions and 7 deletions

View File

@ -79,17 +79,25 @@ var Files = {
return fileName; return fileName;
}, },
isFileNameValid:function (name) { /**
if (name === '.') { * Checks whether the given file name is valid.
throw t('files', '\'.\' is an invalid file name.'); * @param name file name to check
} else if (name.length === 0) { * @return true if the file name is valid.
* Throws a string exception with an error message if
* the file name is not valid
*/
isFileNameValid: function (name) {
var trimmedName = name.trim();
if (trimmedName === '.' || trimmedName === '..') {
throw t('files', '"{name}" is an invalid file name.', {name: name});
} else if (trimmedName.length === 0) {
throw t('files', 'File name cannot be empty.'); throw t('files', 'File name cannot be empty.');
} }
// check for invalid characters // check for invalid characters
var invalid_characters = ['\\', '/', '<', '>', ':', '"', '|', '?', '*']; var invalid_characters =
['\\', '/', '<', '>', ':', '"', '|', '?', '*', '\n'];
for (var i = 0; i < invalid_characters.length; i++) { for (var i = 0; i < invalid_characters.length; i++) {
if (name.indexOf(invalid_characters[i]) !== -1) { if (trimmedName.indexOf(invalid_characters[i]) !== -1) {
throw t('files', "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed."); throw t('files', "Invalid name, '\\', '/', '<', '>', ':', '\"', '|', '?' and '*' are not allowed.");
} }
} }

View File

@ -73,6 +73,7 @@ describe('Files tests', function() {
var threwException = false; var threwException = false;
try { try {
Files.isFileNameValid(fileNames[i]); Files.isFileNameValid(fileNames[i]);
console.error('Invalid file name not detected:', fileNames[i]);
} }
catch (e) { catch (e) {
threwException = true; threwException = true;