| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 13:51:12 +02:00
										 |  |  | const { yup } = require('@strapi/utils'); | 
					
						
							| 
									
										
										
										
											2020-06-09 17:45:53 +02:00
										 |  |  | const validators = require('./common-validators'); | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  | const registerProviderActionSchema = yup | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |   .array() | 
					
						
							|  |  |  |   .required() | 
					
						
							|  |  |  |   .of( | 
					
						
							|  |  |  |     yup | 
					
						
							|  |  |  |       .object() | 
					
						
							|  |  |  |       .shape({ | 
					
						
							| 
									
										
										
										
											2020-06-05 14:25:44 +02:00
										 |  |  |         uid: yup | 
					
						
							|  |  |  |           .string() | 
					
						
							|  |  |  |           .matches( | 
					
						
							|  |  |  |             /^[a-z]([a-z|.|-]+)[a-z]$/, | 
					
						
							|  |  |  |             v => `${v.path}: The id can only contain lowercase letters, dots and hyphens.` | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |           .required(), | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |         section: yup | 
					
						
							|  |  |  |           .string() | 
					
						
							|  |  |  |           .oneOf(['contentTypes', 'plugins', 'settings']) | 
					
						
							|  |  |  |           .required(), | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |         pluginName: yup.mixed().when('section', { | 
					
						
							|  |  |  |           is: 'plugins', | 
					
						
							| 
									
										
										
										
											2020-06-09 17:45:53 +02:00
										 |  |  |           then: validators.isAPluginName.required(), | 
					
						
							|  |  |  |           otherwise: validators.isAPluginName, | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |         }), | 
					
						
							| 
									
										
										
										
											2020-06-04 18:30:26 +02:00
										 |  |  |         subjects: yup.mixed().when('section', { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |           is: 'contentTypes', | 
					
						
							| 
									
										
										
										
											2020-06-04 18:30:26 +02:00
										 |  |  |           then: yup | 
					
						
							|  |  |  |             .array() | 
					
						
							| 
									
										
										
										
											2020-06-10 15:42:32 +02:00
										 |  |  |             .of(yup.string()) | 
					
						
							| 
									
										
										
										
											2021-06-17 09:55:32 +02:00
										 |  |  |             .required(), | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |           otherwise: yup | 
					
						
							|  |  |  |             .mixed() | 
					
						
							|  |  |  |             .oneOf([undefined], 'subjects should only be defined for the "contentTypes" section'), | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |         }), | 
					
						
							|  |  |  |         displayName: yup.string().required(), | 
					
						
							| 
									
										
										
										
											2020-06-04 18:30:26 +02:00
										 |  |  |         category: yup.mixed().when('section', { | 
					
						
							| 
									
										
										
										
											2020-06-10 15:42:32 +02:00
										 |  |  |           is: 'settings', | 
					
						
							|  |  |  |           then: yup.string().required(), | 
					
						
							|  |  |  |           otherwise: yup | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |             .mixed() | 
					
						
							| 
									
										
										
										
											2020-06-10 15:42:32 +02:00
										 |  |  |             .test( | 
					
						
							|  |  |  |               'settingsCategory', | 
					
						
							|  |  |  |               'category should only be defined for the "settings" section', | 
					
						
							|  |  |  |               cat => cat === undefined | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2020-06-04 18:30:26 +02:00
										 |  |  |         }), | 
					
						
							|  |  |  |         subCategory: yup.mixed().when('section', { | 
					
						
							| 
									
										
										
										
											2020-06-10 15:42:32 +02:00
										 |  |  |           is: section => ['settings', 'plugins'].includes(section), | 
					
						
							|  |  |  |           then: yup.string(), | 
					
						
							|  |  |  |           otherwise: yup | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |             .mixed() | 
					
						
							| 
									
										
										
										
											2020-06-10 15:42:32 +02:00
										 |  |  |             .test( | 
					
						
							|  |  |  |               'settingsSubCategory', | 
					
						
							|  |  |  |               'subCategory should only be defined for "plugins" and "settings" sections', | 
					
						
							|  |  |  |               subCat => { | 
					
						
							|  |  |  |                 return subCat === undefined; | 
					
						
							|  |  |  |               } | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |             ), | 
					
						
							| 
									
										
										
										
											2020-06-04 18:30:26 +02:00
										 |  |  |         }), | 
					
						
							| 
									
										
										
										
											2020-08-11 16:39:05 +02:00
										 |  |  |         options: yup.object({ | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |           applyToProperties: yup.array().of(yup.string()), | 
					
						
							| 
									
										
										
										
											2020-08-11 16:39:05 +02:00
										 |  |  |         }), | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |       }) | 
					
						
							|  |  |  |       .noUnknown() | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  | const validateRegisterProviderAction = data => { | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |     registerProviderActionSchema.validateSync(data, { strict: true, abortEarly: false }); | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2020-06-04 18:30:26 +02:00
										 |  |  |     if (e.errors.length > 0) { | 
					
						
							|  |  |  |       throw new yup.ValidationError(e.errors.join(', ')); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       throw e; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-06-08 11:01:20 +02:00
										 |  |  |   validateRegisterProviderAction, | 
					
						
							| 
									
										
										
										
											2020-06-02 17:59:57 +02:00
										 |  |  | }; |