remove joi

This commit is contained in:
Alex Holliday
2026-03-02 19:42:47 +00:00
parent 75a7e144cd
commit 9fd006cfff
5 changed files with 52 additions and 125 deletions
@@ -42,9 +42,7 @@ class NotificationController {
createNotification = async (req: Request, res: Response, next: NextFunction) => {
try {
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
createNotificationBodyValidation.parse(req.body);
const body = req.body;
@@ -134,9 +132,7 @@ class NotificationController {
editNotification = async (req: Request, res: Response, next: NextFunction) => {
try {
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
createNotificationBodyValidation.parse(req.body);
const teamId = requireTeamId(req.user?.teamId);
const notificationId = req.params.id as string;
@@ -3,7 +3,6 @@ import { NotificationModel, type NotificationDocument } from "@/db/models/index.
import { INotificationsRepository } from "@/repositories/index.js";
import type { Notification } from "@/types/index.js";
import { AppError } from "@/utils/AppError.js";
import { not } from "joi";
class MongoNotificationsRepository implements INotificationsRepository {
private mapDocuments = (documents: NotificationDocument[]): Notification[] => {
+50 -67
View File
@@ -1,82 +1,65 @@
import joi from "joi";
import { z } from "zod";
//****************************************
// Notification Validations
//****************************************
export const createNotificationBodyValidation = joi.object({
notificationName: joi.string().required().messages({
"string.empty": "Notification name is required",
"any.required": "Notification name is required",
export const createNotificationBodyValidation = z.discriminatedUnion("type", [
// Email notification
z.object({
notificationName: z.string().min(1, "Notification name is required"),
type: z.literal("email"),
address: z.email("Please enter a valid e-mail address"),
homeserverUrl: z.union([z.string(), z.literal("")]).optional(),
roomId: z.union([z.string(), z.literal("")]).optional(),
accessToken: z.union([z.string(), z.literal("")]).optional(),
}),
type: joi.string().valid("email", "webhook", "slack", "discord", "pager_duty", "matrix").required().messages({
"string.empty": "Notification type is required",
"any.required": "Notification type is required",
"any.only": "Notification type must be email, webhook, slack, discord, pager_duty, or matrix",
// Webhook notification
z.object({
notificationName: z.string().min(1, "Notification name is required"),
type: z.literal("webhook"),
address: z.string().url("Please enter a valid Webhook URL"),
homeserverUrl: z.union([z.string(), z.literal("")]).optional(),
roomId: z.union([z.string(), z.literal("")]).optional(),
accessToken: z.union([z.string(), z.literal("")]).optional(),
}),
address: joi.when("type", {
switch: [
{
is: "email",
then: joi.string().email().required().messages({
"string.empty": "E-mail address cannot be empty",
"any.required": "E-mail address is required",
"string.email": "Please enter a valid e-mail address",
}),
},
{
is: "pager_duty",
then: joi.string().required().messages({
"string.empty": "PagerDuty integration key cannot be empty",
"any.required": "PagerDuty integration key is required",
}),
},
{
is: joi.string().valid("webhook", "slack", "discord"),
then: joi.string().uri().required().messages({
"string.empty": "Webhook URL cannot be empty",
"any.required": "Webhook URL is required",
"string.uri": "Please enter a valid Webhook URL",
}),
},
{
is: "matrix",
then: joi.string().allow("").optional(),
},
],
// Slack notification
z.object({
notificationName: z.string().min(1, "Notification name is required"),
type: z.literal("slack"),
address: z.string().url("Please enter a valid Webhook URL"),
homeserverUrl: z.union([z.string(), z.literal("")]).optional(),
roomId: z.union([z.string(), z.literal("")]).optional(),
accessToken: z.union([z.string(), z.literal("")]).optional(),
}),
homeserverUrl: joi.when("type", {
is: "matrix",
then: joi.string().uri().required().messages({
"string.empty": "Homeserver URL cannot be empty",
"any.required": "Homeserver URL is required",
"string.uri": "Please enter a valid Homeserver URL",
}),
otherwise: joi.string().allow("").optional(),
// Discord notification
z.object({
notificationName: z.string().min(1, "Notification name is required"),
type: z.literal("discord"),
address: z.string().url("Please enter a valid Webhook URL"),
homeserverUrl: z.union([z.string(), z.literal("")]).optional(),
roomId: z.union([z.string(), z.literal("")]).optional(),
accessToken: z.union([z.string(), z.literal("")]).optional(),
}),
roomId: joi.when("type", {
is: "matrix",
then: joi.string().required().messages({
"string.empty": "Room ID cannot be empty",
"any.required": "Room ID is required",
}),
otherwise: joi.string().allow("").optional(),
// PagerDuty notification
z.object({
notificationName: z.string().min(1, "Notification name is required"),
type: z.literal("pager_duty"),
address: z.string().min(1, "PagerDuty integration key is required"),
homeserverUrl: z.union([z.string(), z.literal("")]).optional(),
roomId: z.union([z.string(), z.literal("")]).optional(),
accessToken: z.union([z.string(), z.literal("")]).optional(),
}),
accessToken: joi.when("type", {
is: "matrix",
then: joi.string().required().messages({
"string.empty": "Access Token cannot be empty",
"any.required": "Access Token is required",
}),
otherwise: joi.string().allow("").optional(),
// Matrix notification
z.object({
notificationName: z.string().min(1, "Notification name is required"),
type: z.literal("matrix"),
address: z.union([z.string(), z.literal("")]).optional(),
homeserverUrl: z.string().url("Please enter a valid Homeserver URL"),
roomId: z.string().min(1, "Room ID is required"),
accessToken: z.string().min(1, "Access Token is required"),
}),
});
]);
export const sendTestEmailBodyValidation = z.object({
to: z.string().min(1, "To field is required"),