prevent users from adding extension to folder name

This commit is contained in:
Cassie Tarakajian 2019-07-17 12:30:26 -04:00
parent 1e5a2b7c51
commit cc456200a2
2 changed files with 7 additions and 3 deletions

View file

@ -67,13 +67,15 @@ export class FileNode extends React.Component {
validateFileName() { validateFileName() {
const oldFileExtension = this.originalFileName.match(/\.[0-9a-z]+$/i); const oldFileExtension = this.originalFileName.match(/\.[0-9a-z]+$/i);
const newFileExtension = this.props.name.match(/\.[0-9a-z]+$/i); const newFileExtension = this.props.name.match(/\.[0-9a-z]+$/i);
const hasPeriod = this.props.name.match(/\.+/);
const newFileName = this.props.name; const newFileName = this.props.name;
const hasNoExtension = oldFileExtension && !newFileExtension; const hasNoExtension = oldFileExtension && !newFileExtension;
const notSameExtension = oldFileExtension && newFileExtension && const hasExtensionIfFolder = this.props.fileType === 'folder' && hasPeriod;
oldFileExtension[0].toLowerCase() !== newFileExtension[0].toLowerCase(); const notSameExtension = oldFileExtension && newFileExtension
&& oldFileExtension[0].toLowerCase() !== newFileExtension[0].toLowerCase();
const hasEmptyFilename = newFileName === ''; const hasEmptyFilename = newFileName === '';
const hasOnlyExtension = newFileExtension && newFileName === newFileExtension[0]; const hasOnlyExtension = newFileExtension && newFileName === newFileExtension[0];
if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension) { if (hasEmptyFilename || hasNoExtension || notSameExtension || hasOnlyExtension || hasExtensionIfFolder) {
this.props.updateFileName(this.props.id, this.originalFileName); this.props.updateFileName(this.props.id, this.originalFileName);
} }
} }

View file

@ -38,6 +38,8 @@ function validate(formProps) {
errors.name = 'Please enter a name'; errors.name = 'Please enter a name';
} else if (formProps.name.trim().length === 0) { } else if (formProps.name.trim().length === 0) {
errors.name = 'Folder name cannot contain only spaces'; errors.name = 'Folder name cannot contain only spaces';
} else if (formProps.name.match(/\.+/i)) {
errors.name = 'Folder name cannot contain an extension';
} }
return errors; return errors;