-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalidator.ts
103 lines (97 loc) · 2.86 KB
/
validator.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Validates that a string is a valid web URL.
*
* @param urlStr - The string to validate.
* @returns Whether the string is valid web URL or not.
*/
export function isURL(urlStr: any): boolean {
if (typeof urlStr !== 'string') {
return false;
}
// Lookup illegal characters.
const re = /[^a-z0-9:/?#[\]@!$&'()*+,;=.\-_~%]/i;
if (re.test(urlStr)) {
return false;
}
try {
const uri = new URL(urlStr);
const scheme = uri.protocol;
const hostname = uri.hostname;
const pathname = uri.pathname;
if (scheme !== 'http:' && scheme !== 'https:') {
return false;
}
// Validate hostname: Can contain letters, numbers, underscore and dashes separated by a dot.
// Each zone must not start with a hyphen or underscore.
if (!hostname || !/^[a-zA-Z0-9]+[\w-]*([.]?[a-zA-Z0-9]+[\w-]*)*$/.test(hostname)) {
return false;
}
// Allow for pathnames: (/chars+)*/?
// Where chars can be a combination of: a-z A-Z 0-9 - _ . ~ ! $ & ' ( ) * + , ; = : @ %
const pathnameRe = /^(\/[\w\-.~!$'()*+,;=:@%]+)*\/?$/;
// Validate pathname.
if (pathname && pathname !== '/' && !pathnameRe.test(pathname)) {
return false;
}
// Allow any query string and hash as long as no invalid character is used.
} catch (e) {
return false;
}
return true;
}
/**
* Validates that a value is a number.
*
* @param value - The value to validate.
* @returns Whether the value is a number or not.
*/
export function isNumber(value: any): value is number {
return typeof value === 'number';
}
/**
* Validates that a value is a string.
*
* @param value - The value to validate.
* @returns Whether the value is a string or not.
*/
export function isString(value: any): value is string {
return typeof value === 'string';
}
/**
* Validates that a value is a non-empty string.
*
* @param value - The value to validate.
* @returns Whether the value is a non-empty string or not.
*/
export function isNonEmptyString(value: any): value is string {
return isString(value) && value !== '';
}
/**
*
/**
* Validates that a value is a nullable object.
*
* @param value - The value to validate.
* @returns Whether the value is an object or not.
*/
export function isObject(value: any): boolean {
return typeof value === 'object' && !Array.isArray(value);
}
/**
* Validates that a value is a non-null object.
*
* @param value - The value to validate.
* @returns Whether the value is a non-null object or not.
*/
export function isNonNullObject<T>(value: T | null | undefined): value is T {
return isObject(value) && value !== null;
}
/**
* Validates that a string is a valid Firebase Auth uid.
*
* @param uid - The string to validate.
* @returns Whether the string is a valid Firebase Auth uid.
*/
export function isUid(uid: any): boolean {
return typeof uid === 'string' && uid.length > 0 && uid.length <= 128;
}