@@ -475,39 +475,53 @@ const imageValidation = joi
475
475
"any.required" : "Image file is required" ,
476
476
} ) ;
477
477
478
- const telegramWebhookConfigValidation = joi . object ( {
479
- webhookUrl : joi . string ( ) . uri ( ) . optional ( ) ,
480
- botToken : joi . string ( ) . required ( ) . messages ( {
481
- 'string.empty' : 'Telegram bot token is required' ,
482
- 'any.required' : 'Telegram bot token is required'
483
- } ) ,
484
- chatId : joi . string ( ) . required ( ) . messages ( {
485
- 'string.empty' : 'Telegram chat ID is required' ,
486
- 'any.required' : 'Telegram chat ID is required'
487
- } )
488
- } ) ;
489
-
490
- const discordWebhookConfigValidation = joi . object ( {
491
- webhookUrl : joi . string ( ) . uri ( ) . required ( ) . messages ( {
492
- 'string.empty' : 'Discord webhook URL is required' ,
493
- 'string.uri' : 'Discord webhook URL must be a valid URL' ,
494
- 'any.required' : 'Discord webhook URL is required'
495
- } ) ,
496
- botToken : joi . string ( ) . optional ( ) ,
497
- chatId : joi . string ( ) . optional ( )
498
- } ) ;
499
-
500
- const slackWebhookConfigValidation = joi . object ( {
501
- webhookUrl : joi . string ( ) . uri ( ) . required ( ) . messages ( {
502
- 'string.empty' : 'Slack webhook URL is required' ,
503
- 'string.uri' : 'Slack webhook URL must be a valid URL' ,
504
- 'any.required' : 'Slack webhook URL is required'
505
- } ) ,
506
- botToken : joi . string ( ) . optional ( ) ,
507
- chatId : joi . string ( ) . optional ( )
508
- } ) ;
478
+ const webhookConfigValidation = joi . object ( {
479
+ webhookUrl : joi . string ( ) . uri ( )
480
+ . when ( '$platform' , {
481
+ switch : [
482
+ {
483
+ is : 'telegram' ,
484
+ then : joi . optional ( )
485
+ } ,
486
+ {
487
+ is : 'discord' ,
488
+ then : joi . required ( ) . messages ( {
489
+ 'string.empty' : 'Discord webhook URL is required' ,
490
+ 'string.uri' : 'Discord webhook URL must be a valid URL' ,
491
+ 'any.required' : 'Discord webhook URL is required'
492
+ } )
493
+ } ,
494
+ {
495
+ is : 'slack' ,
496
+ then : joi . required ( ) . messages ( {
497
+ 'string.empty' : 'Slack webhook URL is required' ,
498
+ 'string.uri' : 'Slack webhook URL must be a valid URL' ,
499
+ 'any.required' : 'Slack webhook URL is required'
500
+ } )
501
+ }
502
+ ]
503
+ } ) ,
504
+ botToken : joi . string ( )
505
+ . when ( '$platform' , {
506
+ is : 'telegram' ,
507
+ then : joi . required ( ) . messages ( {
508
+ 'string.empty' : 'Telegram bot token is required' ,
509
+ 'any.required' : 'Telegram bot token is required'
510
+ } ) ,
511
+ otherwise : joi . optional ( )
512
+ } ) ,
513
+ chatId : joi . string ( )
514
+ . when ( '$platform' , {
515
+ is : 'telegram' ,
516
+ then : joi . required ( ) . messages ( {
517
+ 'string.empty' : 'Telegram chat ID is required' ,
518
+ 'any.required' : 'Telegram chat ID is required'
519
+ } ) ,
520
+ otherwise : joi . optional ( )
521
+ } )
522
+ } ) . required ( ) ;
509
523
510
- const triggerNotificationBodyValidation = joi . object ( {
524
+ const triggerNotificationBodyValidation = joi . object ( {
511
525
monitorId : joi . string ( ) . required ( ) . messages ( {
512
526
'string.empty' : 'Monitor ID is required' ,
513
527
'any.required' : 'Monitor ID is required'
@@ -517,31 +531,14 @@ const imageValidation = joi
517
531
'any.required' : 'Notification type is required' ,
518
532
'any.only' : 'Notification type must be webhook'
519
533
} ) ,
520
-
521
534
platform : joi . string ( ) . valid ( 'telegram' , 'discord' , 'slack' ) . required ( ) . messages ( {
522
535
'string.empty' : 'Platform type is required' ,
523
536
'any.required' : 'Platform type is required' ,
524
537
'any.only' : 'Platform must be telegram, discord, or slack'
525
538
} ) ,
526
- config : joi . alternatives ( )
527
- . conditional ( 'platform' , [
528
- {
529
- is : 'telegram' ,
530
- then : telegramWebhookConfigValidation
531
- } ,
532
- {
533
- is : 'discord' ,
534
- then : discordWebhookConfigValidation
535
- } ,
536
- {
537
- is : 'slack' ,
538
- then : slackWebhookConfigValidation
539
- }
540
- ] )
541
- . required ( )
542
- . messages ( {
543
- 'any.required' : 'Webhook configuration is required'
544
- } )
539
+ config : webhookConfigValidation . required ( ) . messages ( {
540
+ 'any.required' : 'Webhook configuration is required'
541
+ } )
545
542
} ) ;
546
543
547
544
export {
@@ -604,7 +601,5 @@ export {
604
601
getStatusPageQueryValidation ,
605
602
imageValidation ,
606
603
triggerNotificationBodyValidation ,
607
- telegramWebhookConfigValidation ,
608
- discordWebhookConfigValidation ,
609
- slackWebhookConfigValidation
604
+ webhookConfigValidation ,
610
605
} ;
0 commit comments