| 
									
										
										
										
											2016-09-09 00:42:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Gajus Kuizinas @gajus | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | var webpackOptionsSchema = require("../schemas/webpackOptionsSchema.json"); | 
					
						
							| 
									
										
										
										
											2016-09-09 00:42:26 +08:00
										 |  |  | var Ajv = require("ajv"); | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | var ajv = new Ajv({ | 
					
						
							|  |  |  | 	errorDataPath: "configuration", | 
					
						
							|  |  |  | 	allErrors: true, | 
					
						
							|  |  |  | 	verbose: true | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 22:32:00 +08:00
										 |  |  | function validateWebpackOptions(options, customSchema) { | 
					
						
							|  |  |  | 	var schema = customSchema || webpackOptionsSchema; | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 	if(Array.isArray(options)) { | 
					
						
							| 
									
										
										
										
											2016-11-01 22:32:00 +08:00
										 |  |  | 		var errors = options.map(validateObject.bind(this, schema)); | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 		errors.forEach(function(list, idx) { | 
					
						
							|  |  |  | 			list.forEach(function applyPrefix(err) { | 
					
						
							|  |  |  | 				err.dataPath = "[" + idx + "]" + err.dataPath; | 
					
						
							|  |  |  | 				if(err.children) { | 
					
						
							|  |  |  | 					err.children.forEach(applyPrefix); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		return errors.reduce(function(arr, items) { | 
					
						
							|  |  |  | 			return arr.concat(items); | 
					
						
							|  |  |  | 		}, []); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-11-01 22:32:00 +08:00
										 |  |  | 		return validateObject(options, schema); | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 22:32:00 +08:00
										 |  |  | function validateObject(options, schema) { | 
					
						
							|  |  |  | 	var validate = ajv.compile(schema); | 
					
						
							| 
									
										
										
										
											2016-09-09 00:42:26 +08:00
										 |  |  | 	var valid = validate(options); | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 	return valid ? [] : filterErrors(validate.errors); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function filterErrors(errors) { | 
					
						
							|  |  |  | 	var errorsByDataPath = {}; | 
					
						
							|  |  |  | 	var newErrors = []; | 
					
						
							|  |  |  | 	errors.forEach(function(err) { | 
					
						
							|  |  |  | 		var dataPath = err.dataPath; | 
					
						
							|  |  |  | 		var key = "$" + dataPath; | 
					
						
							|  |  |  | 		if(errorsByDataPath[key]) { | 
					
						
							|  |  |  | 			var oldError = errorsByDataPath[key]; | 
					
						
							|  |  |  | 			var idx = newErrors.indexOf(oldError); | 
					
						
							|  |  |  | 			newErrors.splice(idx, 1); | 
					
						
							|  |  |  | 			if(oldError.children) { | 
					
						
							|  |  |  | 				var children = oldError.children; | 
					
						
							|  |  |  | 				delete oldError.children; | 
					
						
							|  |  |  | 				children.push(oldError); | 
					
						
							|  |  |  | 				err.children = children; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				err.children = [oldError]; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		errorsByDataPath[key] = err; | 
					
						
							|  |  |  | 		newErrors.push(err); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return newErrors; | 
					
						
							| 
									
										
										
										
											2016-09-09 00:42:26 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-19 06:54:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 00:42:26 +08:00
										 |  |  | module.exports = validateWebpackOptions; |