| 
									
										
										
										
											2017-02-02 17:13:15 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const getErrorFor = (shouldBeAbsolute, data, schema) => { | 
					
						
							|  |  |  | 	const message = shouldBeAbsolute ? | 
					
						
							| 
									
										
										
										
											2017-03-22 19:24:18 +08:00
										 |  |  | 		`The provided value ${JSON.stringify(data)} is not an absolute path!` | 
					
						
							|  |  |  | 		: `A relative path is expected. However the provided value ${JSON.stringify(data)} is an absolute path!`; | 
					
						
							| 
									
										
										
										
											2017-02-02 17:13:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		keyword: "absolutePath", | 
					
						
							|  |  |  | 		params: { absolutePath: data }, | 
					
						
							|  |  |  | 		message: message, | 
					
						
							|  |  |  | 		parentSchema: schema, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | module.exports = (ajv) => ajv.addKeyword("absolutePath", { | 
					
						
							|  |  |  | 	errors: true, | 
					
						
							|  |  |  | 	type: "string", | 
					
						
							|  |  |  | 	compile(expected, schema) { | 
					
						
							|  |  |  | 		function callback(data) { | 
					
						
							| 
									
										
										
										
											2017-03-22 14:14:31 +08:00
										 |  |  | 			const passes = expected === /^(?:[A-Za-z]:\\|\/)/.test(data); | 
					
						
							| 
									
										
										
										
											2017-02-02 17:13:15 +08:00
										 |  |  | 			if(!passes) { | 
					
						
							|  |  |  | 				callback.errors = [getErrorFor(expected, data, schema)]; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return passes; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		callback.errors = []; | 
					
						
							|  |  |  | 		return callback; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }); |