| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  | const invalidCharError = { | 
					
						
							|  |  |  |   valid: false, | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  |   error: 'contains invalid characters.' | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | const validationSuccess = { valid: true, error: null }; | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  | const usernameTooShort = { valid: false, error: 'is too short.' }; | 
					
						
							|  |  |  | const usernameIsHttpStatusCode = { | 
					
						
							|  |  |  |   valid: false, | 
					
						
							|  |  |  |   error: 'is a reserved error code.' | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  | const isNumeric = num => !isNaN(num); | 
					
						
							|  |  |  | const validCharsRE = /^[a-zA-Z0-9\-_+]*$/; | 
					
						
							|  |  |  | const isHttpStatusCode = str => | 
					
						
							|  |  |  |   isNumeric(str) && (parseInt(str, 10) >= 100 && parseInt(str, 10) <= 599); | 
					
						
							|  |  |  | const isValidUsername = str => { | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |   if (!validCharsRE.test(str)) return invalidCharError; | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  |   if (str.length < 3) return usernameTooShort; | 
					
						
							|  |  |  |   if (isHttpStatusCode(str)) return usernameIsHttpStatusCode; | 
					
						
							| 
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 |  |  |   return validationSuccess; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   isNumeric, | 
					
						
							|  |  |  |   isHttpStatusCode, | 
					
						
							|  |  |  |   isValidUsername, | 
					
						
							|  |  |  |   validationSuccess, | 
					
						
							|  |  |  |   usernameTooShort, | 
					
						
							|  |  |  |   usernameIsHttpStatusCode, | 
					
						
							|  |  |  |   invalidCharError | 
					
						
							|  |  |  | }; |