Skip to content

Commit 5be17af

Browse files
committed
Used 1 validation schema for all platforms.
1 parent 03a938d commit 5be17af

File tree

2 files changed

+50
-58
lines changed

2 files changed

+50
-58
lines changed

Server/controllers/notificationController.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {
22
triggerNotificationBodyValidation,
3-
telegramWebhookConfigValidation,
4-
discordWebhookConfigValidation,
5-
slackWebhookConfigValidation
63
} from '../validation/joi.js';
74
import { handleError, handleValidationError } from './controllerUtils.js';
85

Server/validation/joi.js

+50-55
Original file line numberDiff line numberDiff line change
@@ -475,39 +475,53 @@ const imageValidation = joi
475475
"any.required": "Image file is required",
476476
});
477477

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();
509523

510-
const triggerNotificationBodyValidation = joi.object({
524+
const triggerNotificationBodyValidation = joi.object({
511525
monitorId: joi.string().required().messages({
512526
'string.empty': 'Monitor ID is required',
513527
'any.required': 'Monitor ID is required'
@@ -517,31 +531,14 @@ const imageValidation = joi
517531
'any.required': 'Notification type is required',
518532
'any.only': 'Notification type must be webhook'
519533
}),
520-
521534
platform: joi.string().valid('telegram', 'discord', 'slack').required().messages({
522535
'string.empty': 'Platform type is required',
523536
'any.required': 'Platform type is required',
524537
'any.only': 'Platform must be telegram, discord, or slack'
525538
}),
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+
})
545542
});
546543

547544
export {
@@ -604,7 +601,5 @@ export {
604601
getStatusPageQueryValidation,
605602
imageValidation,
606603
triggerNotificationBodyValidation,
607-
telegramWebhookConfigValidation,
608-
discordWebhookConfigValidation,
609-
slackWebhookConfigValidation
604+
webhookConfigValidation,
610605
};

0 commit comments

Comments
 (0)