-
Notifications
You must be signed in to change notification settings - Fork 362
/
validator.ts
296 lines (260 loc) · 7.79 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*!
* @license
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import url = require('url');
/**
* Validates that a value is a byte buffer.
*
* @param value - The value to validate.
* @returns Whether the value is byte buffer or not.
*/
export function isBuffer(value: any): value is Buffer {
return value instanceof Buffer;
}
/**
* Validates that a value is an array.
*
* @param value - The value to validate.
* @returns Whether the value is an array or not.
*/
export function isArray<T>(value: any): value is T[] {
return Array.isArray(value);
}
/**
* Validates that a value is a non-empty array.
*
* @param value - The value to validate.
* @returns Whether the value is a non-empty array or not.
*/
export function isNonEmptyArray<T>(value: any): value is T[] {
return isArray(value) && value.length !== 0;
}
/**
* Validates that a value is a boolean.
*
* @param value - The value to validate.
* @returns Whether the value is a boolean or not.
*/
export function isBoolean(value: any): boolean {
return typeof value === 'boolean';
}
/**
* 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): boolean {
return typeof value === 'number' && !isNaN(value);
}
/**
* 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 base64 string.
*
* @param value - The value to validate.
* @returns Whether the value is a base64 string or not.
*/
export function isBase64String(value: any): boolean {
if (!isString(value)) {
return false;
}
return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value);
}
/**
* 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' && !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;
}
/**
* Validates that a string is a valid Firebase Auth password.
*
* @param password - The password string to validate.
* @returns Whether the string is a valid Firebase Auth password.
*/
export function isPassword(password: any): boolean {
// A password must be a string of at least 6 characters.
return typeof password === 'string' && password.length >= 6;
}
/**
* Validates that a string is a valid email.
*
* @param email - The string to validate.
* @returns Whether the string is valid email or not.
*/
export function isEmail(email: any): boolean {
if (typeof email !== 'string') {
return false;
}
// There must at least one character before the @ symbol and another after.
const re = /^[^@]+@[^@]+$/;
return re.test(email);
}
/**
* Validates that a string is a valid phone number.
*
* @param phoneNumber - The string to validate.
* @returns Whether the string is a valid phone number or not.
*/
export function isPhoneNumber(phoneNumber: any): boolean {
if (typeof phoneNumber !== 'string') {
return false;
}
// Phone number validation is very lax here. Backend will enforce E.164
// spec compliance and will normalize accordingly.
// The phone number string must be non-empty and starts with a plus sign.
const re1 = /^\+/;
// The phone number string must contain at least one alphanumeric character.
const re2 = /[\da-zA-Z]+/;
return re1.test(phoneNumber) && re2.test(phoneNumber);
}
/**
* Validates that a string is a valid ISO date string.
*
* @param dateString - The string to validate.
* @returns Whether the string is a valid ISO date string.
*/
export function isISODateString(dateString: any): boolean {
try {
return isNonEmptyString(dateString) &&
(new Date(dateString).toISOString() === dateString);
} catch (e) {
return false;
}
}
/**
* Validates that a string is a valid UTC date string.
*
* @param dateString - The string to validate.
* @returns Whether the string is a valid UTC date string.
*/
export function isUTCDateString(dateString: any): boolean {
try {
return isNonEmptyString(dateString) &&
(new Date(dateString).toUTCString() === dateString);
} catch (e) {
return false;
}
}
/**
* 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 = url.parse(urlStr);
const scheme = uri.protocol;
const slashes = uri.slashes;
const hostname = uri.hostname;
const pathname = uri.pathname;
if ((scheme !== 'http:' && scheme !== 'https:') || !slashes) {
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 the provided topic is a valid FCM topic name.
*
* @param topic - The topic to validate.
* @returns Whether the provided topic is a valid FCM topic name.
*/
export function isTopic(topic: any): boolean {
if (typeof topic !== 'string') {
return false;
}
const VALID_TOPIC_REGEX = /^(\/topics\/)?(private\/)?[a-zA-Z0-9-_.~%]+$/;
return VALID_TOPIC_REGEX.test(topic);
}
/**
* Validates that the provided string can be used as a task ID
* for Cloud Tasks.
*
* @param taskId - the task ID to validate.
* @returns Whether the provided task ID is valid.
*/
export function isTaskId(taskId: any): boolean {
if (typeof taskId !== 'string') {
return false;
}
const VALID_TASK_ID_REGEX = /^[A-Za-z0-9_-]+$/;
return VALID_TASK_ID_REGEX.test(taskId);
}