| 
									
										
										
										
											2018-03-23 14:35:29 +00:00
										 |  |  | const Joi = require('joi'); | 
					
						
							|  |  |  | Joi.objectId = require('joi-objectid')(Joi); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 12:43:09 +03:00
										 |  |  | const { challengeTypes } = require('../../client/utils/challengeTypes'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 20:09:18 +03:00
										 |  |  | function getSchemaForLang(lang) { | 
					
						
							|  |  |  |   let schema = Joi.object().keys({ | 
					
						
							|  |  |  |     block: Joi.string(), | 
					
						
							|  |  |  |     blockId: Joi.objectId(), | 
					
						
							|  |  |  |     challengeOrder: Joi.number(), | 
					
						
							|  |  |  |     challengeType: Joi.number() | 
					
						
							|  |  |  |       .min(0) | 
					
						
							|  |  |  |       .max(9) | 
					
						
							|  |  |  |       .required(), | 
					
						
							|  |  |  |     checksum: Joi.number(), | 
					
						
							|  |  |  |     dashedName: Joi.string(), | 
					
						
							| 
									
										
										
										
											2019-02-18 12:43:09 +03:00
										 |  |  |     description: Joi.when('challengeType', { | 
					
						
							|  |  |  |       is: challengeTypes.step, | 
					
						
							|  |  |  |       then: Joi.string().allow(''), | 
					
						
							|  |  |  |       otherwise: Joi.string().required() | 
					
						
							|  |  |  |     }), | 
					
						
							| 
									
										
										
										
											2018-12-05 20:09:18 +03:00
										 |  |  |     fileName: Joi.string(), | 
					
						
							|  |  |  |     files: Joi.array().items( | 
					
						
							|  |  |  |       Joi.object().keys({ | 
					
						
							|  |  |  |         key: Joi.string(), | 
					
						
							|  |  |  |         ext: Joi.string(), | 
					
						
							|  |  |  |         name: Joi.string(), | 
					
						
							|  |  |  |         head: [ | 
					
						
							|  |  |  |           Joi.array().items(Joi.string().allow('')), | 
					
						
							|  |  |  |           Joi.string().allow('') | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         tail: [ | 
					
						
							|  |  |  |           Joi.array().items(Joi.string().allow('')), | 
					
						
							|  |  |  |           Joi.string().allow('') | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         contents: [ | 
					
						
							|  |  |  |           Joi.array().items(Joi.string().allow('')), | 
					
						
							|  |  |  |           Joi.string().allow('') | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ), | 
					
						
							|  |  |  |     guideUrl: Joi.string().uri({ scheme: 'https' }), | 
					
						
							|  |  |  |     videoUrl: Joi.string().allow(''), | 
					
						
							|  |  |  |     helpRoom: Joi.string(), | 
					
						
							|  |  |  |     id: Joi.objectId().required(), | 
					
						
							| 
									
										
										
										
											2019-02-18 12:43:09 +03:00
										 |  |  |     instructions: Joi.string().allow(''), | 
					
						
							| 
									
										
										
										
											2018-12-05 20:09:18 +03:00
										 |  |  |     isBeta: Joi.bool(), | 
					
						
							|  |  |  |     isComingSoon: Joi.bool(), | 
					
						
							|  |  |  |     isLocked: Joi.bool(), | 
					
						
							|  |  |  |     isPrivate: Joi.bool(), | 
					
						
							|  |  |  |     isRequired: Joi.bool(), | 
					
						
							|  |  |  |     name: Joi.string(), | 
					
						
							|  |  |  |     order: Joi.number(), | 
					
						
							|  |  |  |     required: Joi.array().items( | 
					
						
							|  |  |  |       Joi.object().keys({ | 
					
						
							|  |  |  |         link: Joi.string(), | 
					
						
							|  |  |  |         raw: Joi.bool(), | 
					
						
							|  |  |  |         src: Joi.string(), | 
					
						
							|  |  |  |         crossDomain: Joi.bool() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ), | 
					
						
							|  |  |  |     solutions: Joi.array().items(Joi.string().optional()), | 
					
						
							|  |  |  |     superBlock: Joi.string(), | 
					
						
							|  |  |  |     superOrder: Joi.number(), | 
					
						
							|  |  |  |     suborder: Joi.number(), | 
					
						
							|  |  |  |     tests: Joi.array().items( | 
					
						
							|  |  |  |       // public challenges
 | 
					
						
							|  |  |  |       Joi.object().keys({ | 
					
						
							|  |  |  |         text: Joi.string().required(), | 
					
						
							|  |  |  |         testString: Joi.string() | 
					
						
							|  |  |  |           .allow('') | 
					
						
							|  |  |  |           .required() | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |       // our tests used in certification verification
 | 
					
						
							|  |  |  |       Joi.object().keys({ | 
					
						
							|  |  |  |         id: Joi.string().required(), | 
					
						
							|  |  |  |         title: Joi.string().required() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ), | 
					
						
							|  |  |  |     template: Joi.string().allow(''), | 
					
						
							|  |  |  |     time: Joi.string().allow(''), | 
					
						
							|  |  |  |     title: Joi.string().required() | 
					
						
							| 
									
										
										
										
											2018-10-30 18:48:54 +03:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 20:09:18 +03:00
										 |  |  |   if (lang !== 'english') { | 
					
						
							|  |  |  |     schema = schema.append({ | 
					
						
							|  |  |  |       localeTitle: Joi.string().required() | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return schema; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | exports.challengeSchemaValidator = lang => { | 
					
						
							|  |  |  |   const schema = getSchemaForLang(lang); | 
					
						
							|  |  |  |   return challenge => Joi.validate(challenge, schema); | 
					
						
							| 
									
										
										
										
											2018-03-23 14:35:29 +00:00
										 |  |  | }; |