Merge branch '15685-file-renaming-empty-name'
[arvados-workbench2.git] / src / validators / valid-name.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 export const disallowDotName = /^\.{1,2}$/;
6 export const disallowSlash = /\//;
7 export const disallowLeadingWhitespaces = /^\s+/;
8 export const disallowTrailingWhitespaces = /\s+$/;
9
10 export const validName = (value: string) => {
11     return [disallowDotName, disallowSlash].find(aRule => value.match(aRule) !== null)
12         ? "Name cannot be '.' or '..' or contain '/' characters"
13         : undefined;
14 };
15
16 export const validNameAllowSlash = (value: string) => {
17     return [disallowDotName].find(aRule => value.match(aRule) !== null)
18         ? "Name cannot be '.' or '..'"
19         : undefined;
20 };
21
22 export const validFileName = (value: string) => {
23     return [
24         disallowLeadingWhitespaces,
25         disallowTrailingWhitespaces
26     ].find(aRule => value.match(aRule) !== null)
27         ? `Leading/trailing whitespaces not allowed on '${value}'`
28         : undefined;
29 };
30
31 export const validFilePath = (filePath: string) => {
32     const errors = filePath.split('/').map(pathPart => {
33         if (pathPart === "") { return "Empty dir name not allowed"; }
34         return validNameAllowSlash(pathPart) || validFileName(pathPart);
35     });
36     return errors.filter(e => e !== undefined)[0];
37 };