| 
									
										
										
										
											2015-08-04 01:25:34 -07:00
										 |  |  | import validator from 'express-validator'; | 
					
						
							| 
									
										
										
										
											2020-06-13 11:27:15 +02:00
										 |  |  | import { isPoly } from '../../../utils/polyvinyl'; | 
					
						
							| 
									
										
										
										
											2016-06-01 15:52:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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); | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |         return ( | 
					
						
							|  |  |  |           !!keys.length && | 
					
						
							| 
									
										
										
										
											2016-06-01 15:52:08 -07:00
										 |  |  |           // every key is a file
 | 
					
						
							|  |  |  |           keys.every(key => isObject(value[key])) && | 
					
						
							|  |  |  |           // every file has contents
 | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |           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( | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |           '<(?:' + | 
					
						
							|  |  |  |             // 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 + | 
					
						
							|  |  |  |             ')>', | 
					
						
							| 
									
										
										
										
											2017-12-26 13:17:54 -08:00
										 |  |  |           '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
										 |  |  |   }); | 
					
						
							|  |  |  | } |