| 
									
										
										
										
											2015-08-04 01:25:34 -07:00
										 |  |  | import validator from 'express-validator'; | 
					
						
							| 
									
										
										
										
											2016-06-01 15:52:08 -07:00
										 |  |  | import { isPoly } from '../../common/utils/polyvinyl'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const isObject = val => !!val && typeof val === 'object'; | 
					
						
							| 
									
										
										
										
											2015-08-04 01:25:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 22:10:06 -08:00
										 |  |  | export default function() { | 
					
						
							|  |  |  |   return validator({ | 
					
						
							|  |  |  |     customValidators: { | 
					
						
							|  |  |  |       matchRegex(param, regex) { | 
					
						
							|  |  |  |         return regex.test(param); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       isString(value) { | 
					
						
							|  |  |  |         return typeof value === 'string'; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       isNumber(value) { | 
					
						
							|  |  |  |         return typeof value === 'number'; | 
					
						
							| 
									
										
										
										
											2016-06-01 15:52:08 -07:00
										 |  |  |       }, | 
					
						
							|  |  |  |       isFiles(value) { | 
					
						
							|  |  |  |         if (!isObject(value)) { | 
					
						
							|  |  |  |           return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const keys = Object.keys(value); | 
					
						
							|  |  |  |         return !!keys.length && | 
					
						
							|  |  |  |           // every key is a file
 | 
					
						
							|  |  |  |           keys.every(key => isObject(value[key])) && | 
					
						
							|  |  |  |           // every file has contents
 | 
					
						
							|  |  |  |           keys.map(key => value[key]).every(file => isPoly(file)); | 
					
						
							| 
									
										
										
										
											2016-02-10 22:10:06 -08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-12-15 02:54:59 +05:30
										 |  |  |     }, | 
					
						
							|  |  |  |     customSanitizers: { | 
					
						
							|  |  |  |       // Refer : http://stackoverflow.com/a/430240/1932901
 | 
					
						
							|  |  |  |       trimTags(value) { | 
					
						
							| 
									
										
										
										
											2017-12-26 13:17:54 -08:00
										 |  |  |         const tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*'; | 
					
						
							|  |  |  |         const tagOrComment = new RegExp( | 
					
						
							|  |  |  |           '<(?:' | 
					
						
							|  |  |  |           // Comment body.
 | 
					
						
							|  |  |  |           + '!--(?:(?:-*[^->])*--+|-?)' | 
					
						
							|  |  |  |           // Special "raw text" elements whose content should be elided.
 | 
					
						
							|  |  |  |           + '|script\\b' + tagBody + '>[\\s\\S]*?</script\\s*' | 
					
						
							|  |  |  |           + '|style\\b' + tagBody + '>[\\s\\S]*?</style\\s*' | 
					
						
							|  |  |  |           // Regular name
 | 
					
						
							|  |  |  |           + '|/?[a-z]' | 
					
						
							|  |  |  |           + tagBody | 
					
						
							|  |  |  |           + ')>', | 
					
						
							|  |  |  |           'gi' | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         let rawValue; | 
					
						
							|  |  |  |         do { | 
					
						
							|  |  |  |           rawValue = value; | 
					
						
							|  |  |  |           value = value.replace(tagOrComment, ''); | 
					
						
							|  |  |  |         } while (value !== rawValue); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return value.replace(/</g, '<'); | 
					
						
							| 
									
										
										
										
											2016-12-15 02:54:59 +05:30
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-08-04 01:25:34 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-02-10 22:10:06 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | } |