X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/435d259b65ca110065a96581c1d568fb3ddd205d..28052f8d76348c19acf601d8db779aaf4baa3de2:/src/validators/valid-name.tsx diff --git a/src/validators/valid-name.tsx b/src/validators/valid-name.tsx index 468811d831..89bb3f9688 100644 --- a/src/validators/valid-name.tsx +++ b/src/validators/valid-name.tsx @@ -2,13 +2,36 @@ // // SPDX-License-Identifier: AGPL-3.0 +export const disallowDotName = /^\.{1,2}$/; +export const disallowSlash = /\//; +export const disallowLeadingWhitespaces = /^\s+/; +export const disallowTrailingWhitespaces = /\s+$/; -const ERROR_MESSAGE = "Name cannot be '.' or '..' or contain '/' characters"; +export const validName = (value: string) => { + return [disallowDotName, disallowSlash].find(aRule => value.match(aRule) !== null) + ? "Name cannot be '.' or '..' or contain '/' characters" + : undefined; +}; -export const invalidNamingRules = [/\//, /^\.{1,2}$/]; +export const validNameAllowSlash = (value: string) => { + return [disallowDotName].find(aRule => value.match(aRule) !== null) + ? "Name cannot be '.' or '..'" + : undefined; +}; -export const validName = (value: string) => { - return invalidNamingRules.find(aRule => value.match(aRule) !== null) - ? ERROR_MESSAGE +export const validFileName = (value: string) => { + return [ + disallowLeadingWhitespaces, + disallowTrailingWhitespaces + ].find(aRule => value.match(aRule) !== null) + ? `Leading/trailing whitespaces not allowed on '${value}'` : undefined; }; + +export const validFilePath = (filePath: string) => { + const errors = filePath.split('/').map(pathPart => { + if (pathPart === "") { return "Empty dir name not allowed"; } + return validNameAllowSlash(pathPart) || validFileName(pathPart); + }); + return errors.filter(e => e !== undefined)[0]; +}; \ No newline at end of file