Used 1 validation schema for all platforms.
This commit is contained in:
1 parent
03a938de8a
commit
5be17af4c1
2 files changed
+50
-58
No files matched your search
@@ -1,8 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
triggerNotificationBodyValidation,
|
triggerNotificationBodyValidation,
|
||||||
telegramWebhookConfigValidation,
|
|
||||||
discordWebhookConfigValidation,
|
|
||||||
slackWebhookConfigValidation
|
|
||||||
} from '../validation/joi.js';
|
} from '../validation/joi.js';
|
||||||
import { handleError, handleValidationError } from './controllerUtils.js';
|
import { handleError, handleValidationError } from './controllerUtils.js';
|
||||||
|
|
||||||
|
|||||||
+50
-55
@@ -475,39 +475,53 @@ const imageValidation = joi
|
|||||||
"any.required": "Image file is required",
|
"any.required": "Image file is required",
|
||||||
});
|
});
|
||||||
|
|
||||||
const telegramWebhookConfigValidation = joi.object({
|
const webhookConfigValidation = joi.object({
|
||||||
webhookUrl: joi.string().uri().optional(),
|
webhookUrl: joi.string().uri()
|
||||||
botToken: joi.string().required().messages({
|
.when('$platform', {
|
||||||
'string.empty': 'Telegram bot token is required',
|
switch: [
|
||||||
'any.required': 'Telegram bot token is required'
|
{
|
||||||
}),
|
is: 'telegram',
|
||||||
chatId: joi.string().required().messages({
|
then: joi.optional()
|
||||||
'string.empty': 'Telegram chat ID is required',
|
},
|
||||||
'any.required': 'Telegram chat ID is required'
|
{
|
||||||
})
|
is: 'discord',
|
||||||
});
|
then: joi.required().messages({
|
||||||
|
'string.empty': 'Discord webhook URL is required',
|
||||||
|
'string.uri': 'Discord webhook URL must be a valid URL',
|
||||||
|
'any.required': 'Discord webhook URL is required'
|
||||||
|
})
|
||||||
|
},
|
||||||
|
{
|
||||||
|
is: 'slack',
|
||||||
|
then: joi.required().messages({
|
||||||
|
'string.empty': 'Slack webhook URL is required',
|
||||||
|
'string.uri': 'Slack webhook URL must be a valid URL',
|
||||||
|
'any.required': 'Slack webhook URL is required'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}),
|
||||||
|
botToken: joi.string()
|
||||||
|
.when('$platform', {
|
||||||
|
is: 'telegram',
|
||||||
|
then: joi.required().messages({
|
||||||
|
'string.empty': 'Telegram bot token is required',
|
||||||
|
'any.required': 'Telegram bot token is required'
|
||||||
|
}),
|
||||||
|
otherwise: joi.optional()
|
||||||
|
}),
|
||||||
|
chatId: joi.string()
|
||||||
|
.when('$platform', {
|
||||||
|
is: 'telegram',
|
||||||
|
then: joi.required().messages({
|
||||||
|
'string.empty': 'Telegram chat ID is required',
|
||||||
|
'any.required': 'Telegram chat ID is required'
|
||||||
|
}),
|
||||||
|
otherwise: joi.optional()
|
||||||
|
})
|
||||||
|
}).required();
|
||||||
|
|
||||||
const discordWebhookConfigValidation = joi.object({
|
const triggerNotificationBodyValidation = joi.object({
|
||||||
webhookUrl: joi.string().uri().required().messages({
|
|
||||||
'string.empty': 'Discord webhook URL is required',
|
|
||||||
'string.uri': 'Discord webhook URL must be a valid URL',
|
|
||||||
'any.required': 'Discord webhook URL is required'
|
|
||||||
}),
|
|
||||||
botToken: joi.string().optional(),
|
|
||||||
chatId: joi.string().optional()
|
|
||||||
});
|
|
||||||
|
|
||||||
const slackWebhookConfigValidation = joi.object({
|
|
||||||
webhookUrl: joi.string().uri().required().messages({
|
|
||||||
'string.empty': 'Slack webhook URL is required',
|
|
||||||
'string.uri': 'Slack webhook URL must be a valid URL',
|
|
||||||
'any.required': 'Slack webhook URL is required'
|
|
||||||
}),
|
|
||||||
botToken: joi.string().optional(),
|
|
||||||
chatId: joi.string().optional()
|
|
||||||
});
|
|
||||||
|
|
||||||
const triggerNotificationBodyValidation = joi.object({
|
|
||||||
monitorId: joi.string().required().messages({
|
monitorId: joi.string().required().messages({
|
||||||
'string.empty': 'Monitor ID is required',
|
'string.empty': 'Monitor ID is required',
|
||||||
'any.required': 'Monitor ID is required'
|
'any.required': 'Monitor ID is required'
|
||||||
@@ -517,31 +531,14 @@ const imageValidation = joi
|
|||||||
'any.required': 'Notification type is required',
|
'any.required': 'Notification type is required',
|
||||||
'any.only': 'Notification type must be webhook'
|
'any.only': 'Notification type must be webhook'
|
||||||
}),
|
}),
|
||||||
|
|
||||||
platform: joi.string().valid('telegram', 'discord', 'slack').required().messages({
|
platform: joi.string().valid('telegram', 'discord', 'slack').required().messages({
|
||||||
'string.empty': 'Platform type is required',
|
'string.empty': 'Platform type is required',
|
||||||
'any.required': 'Platform type is required',
|
'any.required': 'Platform type is required',
|
||||||
'any.only': 'Platform must be telegram, discord, or slack'
|
'any.only': 'Platform must be telegram, discord, or slack'
|
||||||
}),
|
}),
|
||||||
config: joi.alternatives()
|
config: webhookConfigValidation.required().messages({
|
||||||
.conditional('platform', [
|
'any.required': 'Webhook configuration is required'
|
||||||
{
|
})
|
||||||
is: 'telegram',
|
|
||||||
then: telegramWebhookConfigValidation
|
|
||||||
},
|
|
||||||
{
|
|
||||||
is: 'discord',
|
|
||||||
then: discordWebhookConfigValidation
|
|
||||||
},
|
|
||||||
{
|
|
||||||
is: 'slack',
|
|
||||||
then: slackWebhookConfigValidation
|
|
||||||
}
|
|
||||||
])
|
|
||||||
.required()
|
|
||||||
.messages({
|
|
||||||
'any.required': 'Webhook configuration is required'
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
@@ -604,7 +601,5 @@ export {
|
|||||||
getStatusPageQueryValidation,
|
getStatusPageQueryValidation,
|
||||||
imageValidation,
|
imageValidation,
|
||||||
triggerNotificationBodyValidation,
|
triggerNotificationBodyValidation,
|
||||||
telegramWebhookConfigValidation,
|
webhookConfigValidation,
|
||||||
discordWebhookConfigValidation,
|
|
||||||
slackWebhookConfigValidation
|
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user