forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnameValidator.ts
More file actions
51 lines (40 loc) · 2.06 KB
/
nameValidator.ts
File metadata and controls
51 lines (40 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as path from 'path';
import validator from 'validator';
import validPath = require('valid-path');
export function emptyName(message: string, value: string): string | null {
return validator.isEmpty(value) ? message : null;
}
export function lengthName(message: string, value: string, offset: number, minVal = 2, maxVal = 63): string | null {
return validator.isLength(value, { min: minVal, max: maxVal - offset }) ? null : message;
}
export function validateUrl(message: string, value: string): string | null {
return validator.isURL(value) ? null : message;
}
export function validateMatches(message: string, value: string): string | null {
return validator.matches(value, '^[a-z]([-a-z0-9]*[a-z0-9])*$') ? null : message;
}
export function validatePath(message: string, value: string): string | null {
return validPath(value).valid ? null : message;
}
export function validateFilePath(message: string, value: string): string | null {
const proposedPath = path.parse(value);
return /^devfile\.ya?ml$/i.test(proposedPath.base) ? null : message;
}
export function validateRFC1123DNSLabel(message: string, value: string): string | null {
return validator.matches(value, '^[a-z0-9]([-a-z0-9]*[a-z0-9])*$') ? null : message;
}
export function clusterURL(value: string): string | null {
const urlRegex = value.match(/--server=(https?:\/\/[^ ]*)/);
return urlRegex ? urlRegex[1] : null;
}
export function getToken(value: string): string | null {
const tokenRegex = value.match(/--token\s*=\s*(\S*).*/);
return tokenRegex ? tokenRegex[1] : null;
}
export function ocLoginCommandMatches(value: string): string | null {
return clusterURL(value) !== null && getToken(value) !== null ? value : null;
}