validation refactoring
This commit is contained in:
1 parent
c3a41272a3
commit
a5d3c95145
7 files changed
+53
-74
No files matched your search
@@ -7,9 +7,6 @@ import {
|
|||||||
deleteChecksParamValidation,
|
deleteChecksParamValidation,
|
||||||
deleteChecksByTeamIdParamValidation,
|
deleteChecksByTeamIdParamValidation,
|
||||||
updateChecksTTLBodyValidation,
|
updateChecksTTLBodyValidation,
|
||||||
ackCheckBodyValidation,
|
|
||||||
ackAllChecksParamValidation,
|
|
||||||
ackAllChecksBodyValidation,
|
|
||||||
} from "@/validation/checkValidation.js";
|
} from "@/validation/checkValidation.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "checkController";
|
const SERVICE_NAME = "checkController";
|
||||||
@@ -28,8 +25,8 @@ class CheckController {
|
|||||||
|
|
||||||
getChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
await getChecksParamValidation.validateAsync(req.params);
|
getChecksParamValidation.parse(req.params);
|
||||||
await getChecksQueryValidation.validateAsync(req.query);
|
getChecksQueryValidation.parse(req.query);
|
||||||
|
|
||||||
const result = await this.checkService.getChecksByMonitor({
|
const result = await this.checkService.getChecksByMonitor({
|
||||||
monitorId: req?.params?.monitorId,
|
monitorId: req?.params?.monitorId,
|
||||||
@@ -49,7 +46,7 @@ class CheckController {
|
|||||||
|
|
||||||
getChecksByTeam = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksByTeam = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
await getTeamChecksQueryValidation.validateAsync(req.query);
|
getTeamChecksQueryValidation.parse(req.query);
|
||||||
const checkData = await this.checkService.getChecksByTeam({
|
const checkData = await this.checkService.getChecksByTeam({
|
||||||
teamId: req?.user?.teamId,
|
teamId: req?.user?.teamId,
|
||||||
query: req?.query,
|
query: req?.query,
|
||||||
@@ -80,7 +77,7 @@ class CheckController {
|
|||||||
|
|
||||||
deleteChecks = async (req: Request, res: Response, next: NextFunction) => {
|
deleteChecks = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
await deleteChecksParamValidation.validateAsync(req.params);
|
deleteChecksParamValidation.parse(req.params);
|
||||||
|
|
||||||
const deletedCount = await this.checkService.deleteChecks({
|
const deletedCount = await this.checkService.deleteChecks({
|
||||||
monitorId: req.params.monitorId as string,
|
monitorId: req.params.monitorId as string,
|
||||||
@@ -99,7 +96,7 @@ class CheckController {
|
|||||||
|
|
||||||
deleteChecksByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
deleteChecksByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
await deleteChecksByTeamIdParamValidation.validateAsync(req.params);
|
deleteChecksByTeamIdParamValidation.parse(req.params);
|
||||||
|
|
||||||
const deletedCount = await this.checkService.deleteChecksByTeamId({ teamId: req?.user?.teamId });
|
const deletedCount = await this.checkService.deleteChecksByTeamId({ teamId: req?.user?.teamId });
|
||||||
|
|
||||||
@@ -115,7 +112,7 @@ class CheckController {
|
|||||||
|
|
||||||
updateChecksTTL = async (req: Request, res: Response, next: NextFunction) => {
|
updateChecksTTL = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
await updateChecksTTLBodyValidation.validateAsync(req.body);
|
updateChecksTTLBodyValidation.parse(req.body);
|
||||||
|
|
||||||
await this.checkService.updateChecksTTL({
|
await this.checkService.updateChecksTTL({
|
||||||
teamId: req?.user?.teamId,
|
teamId: req?.user?.teamId,
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ class GeoCheckController {
|
|||||||
|
|
||||||
getGeoChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
getGeoChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
await getChecksParamValidation.validateAsync(req.params);
|
getChecksParamValidation.parse(req.params);
|
||||||
await getChecksQueryValidation.validateAsync(req.query);
|
getChecksQueryValidation.parse(req.query);
|
||||||
|
|
||||||
const result = await this.geoChecksService.getGeoChecksByMonitor({
|
const result = await this.geoChecksService.getGeoChecksByMonitor({
|
||||||
monitorId: req?.params?.monitorId as string,
|
monitorId: req?.params?.monitorId as string,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { updateAppSettingsBodyValidation } from "@/validation/settingsValidation.js";
|
import { updateAppSettingsBodyValidation } from "@/validation/settingsValidation.js";
|
||||||
import { sendTestEmailBodyValidation } from "@/validation/announcementValidation.js";
|
import { sendTestEmailBodyValidation } from "@/validation/notificationValidation.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "SettingsController";
|
const SERVICE_NAME = "SettingsController";
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
//****************************************
|
|
||||||
// Announcement Validations
|
|
||||||
//****************************************
|
|
||||||
|
|
||||||
export const sendTestEmailBodyValidation = z.object({
|
|
||||||
to: z.string().min(1, "To field is required"),
|
|
||||||
systemEmailHost: z.string().optional(),
|
|
||||||
systemEmailPort: z.number().optional(),
|
|
||||||
systemEmailSecure: z.boolean().optional(),
|
|
||||||
systemEmailPool: z.boolean().optional(),
|
|
||||||
systemEmailAddress: z.string().optional(),
|
|
||||||
systemEmailPassword: z.string().optional(),
|
|
||||||
systemEmailUser: z.string().optional(),
|
|
||||||
systemEmailConnectionHost: z.union([z.string(), z.literal("")]).optional(),
|
|
||||||
systemEmailIgnoreTLS: z.boolean().optional(),
|
|
||||||
systemEmailRequireTLS: z.boolean().optional(),
|
|
||||||
systemEmailRejectUnauthorized: z.boolean().optional(),
|
|
||||||
systemEmailTLSServername: z.union([z.string(), z.literal("")]).optional(),
|
|
||||||
});
|
|
||||||
@@ -1,56 +1,43 @@
|
|||||||
import joi from "joi";
|
import { z } from "zod";
|
||||||
import { GeoContinents } from "@/types/geoCheck.js";
|
import { GeoContinents } from "@/types/geoCheck.js";
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Check Validations
|
// Check Validations
|
||||||
//****************************************
|
//****************************************
|
||||||
|
|
||||||
export const ackCheckBodyValidation = joi.object({
|
export const getChecksParamValidation = z.object({
|
||||||
ack: joi.boolean(),
|
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ackAllChecksParamValidation = joi.object({
|
export const getChecksQueryValidation = z.object({
|
||||||
monitorId: joi.string().optional(),
|
type: z.enum(["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc"]).optional(),
|
||||||
path: joi.string().valid("monitor", "team").required(),
|
sortOrder: z.enum(["asc", "desc"]).optional(),
|
||||||
|
limit: z.number().optional(),
|
||||||
|
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]).optional(),
|
||||||
|
filter: z.enum(["all", "up", "down", "resolve"]).optional(),
|
||||||
|
ack: z.boolean().optional(),
|
||||||
|
page: z.number().optional(),
|
||||||
|
rowsPerPage: z.number().optional(),
|
||||||
|
status: z.boolean().optional(),
|
||||||
|
continent: z.union([z.enum(GeoContinents), z.array(z.enum(GeoContinents))]).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ackAllChecksBodyValidation = joi.object({
|
export const getTeamChecksQueryValidation = z.object({
|
||||||
ack: joi.boolean(),
|
sortOrder: z.enum(["asc", "desc"]).optional(),
|
||||||
|
limit: z.number().optional(),
|
||||||
|
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]).optional(),
|
||||||
|
filter: z.enum(["all", "up", "down", "resolve"]).optional(),
|
||||||
|
ack: z.boolean().optional(),
|
||||||
|
page: z.number().optional(),
|
||||||
|
rowsPerPage: z.number().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getChecksParamValidation = joi.object({
|
export const deleteChecksParamValidation = z.object({
|
||||||
monitorId: joi.string().required(),
|
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getChecksQueryValidation = joi.object({
|
export const deleteChecksByTeamIdParamValidation = z.object({});
|
||||||
type: joi.string().valid("http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc"),
|
|
||||||
sortOrder: joi.string().valid("asc", "desc"),
|
|
||||||
limit: joi.number(),
|
|
||||||
dateRange: joi.string().valid("recent", "hour", "day", "week", "month", "all"),
|
|
||||||
filter: joi.string().valid("all", "up", "down", "resolve"),
|
|
||||||
ack: joi.boolean(),
|
|
||||||
page: joi.number(),
|
|
||||||
rowsPerPage: joi.number(),
|
|
||||||
status: joi.boolean(),
|
|
||||||
continent: joi.alternatives().try(joi.string().valid(...GeoContinents), joi.array().items(joi.string().valid(...GeoContinents))),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const getTeamChecksQueryValidation = joi.object({
|
export const updateChecksTTLBodyValidation = z.object({
|
||||||
sortOrder: joi.string().valid("asc", "desc"),
|
ttl: z.number().min(1, "TTL is required"),
|
||||||
limit: joi.number(),
|
|
||||||
dateRange: joi.string().valid("recent", "hour", "day", "week", "month", "all"),
|
|
||||||
filter: joi.string().valid("all", "up", "down", "resolve"),
|
|
||||||
ack: joi.boolean(),
|
|
||||||
page: joi.number(),
|
|
||||||
rowsPerPage: joi.number(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const deleteChecksParamValidation = joi.object({
|
|
||||||
monitorId: joi.string().required(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const deleteChecksByTeamIdParamValidation = joi.object({});
|
|
||||||
|
|
||||||
export const updateChecksTTLBodyValidation = joi.object({
|
|
||||||
ttl: joi.number().required(),
|
|
||||||
});
|
});
|
||||||
@@ -16,5 +16,4 @@ export * from "./maintenanceWindowValidation.js";
|
|||||||
export * from "./settingsValidation.js";
|
export * from "./settingsValidation.js";
|
||||||
export * from "./statusPageValidation.js";
|
export * from "./statusPageValidation.js";
|
||||||
export * from "./notificationValidation.js";
|
export * from "./notificationValidation.js";
|
||||||
export * from "./announcementValidation.js";
|
|
||||||
export * from "./userValidation.js";
|
export * from "./userValidation.js";
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import joi from "joi";
|
import joi from "joi";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Notification Validations
|
// Notification Validations
|
||||||
@@ -76,3 +77,19 @@ export const createNotificationBodyValidation = joi.object({
|
|||||||
otherwise: joi.string().allow("").optional(),
|
otherwise: joi.string().allow("").optional(),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const sendTestEmailBodyValidation = z.object({
|
||||||
|
to: z.string().min(1, "To field is required"),
|
||||||
|
systemEmailHost: z.string().optional(),
|
||||||
|
systemEmailPort: z.number().optional(),
|
||||||
|
systemEmailSecure: z.boolean().optional(),
|
||||||
|
systemEmailPool: z.boolean().optional(),
|
||||||
|
systemEmailAddress: z.string().optional(),
|
||||||
|
systemEmailPassword: z.string().optional(),
|
||||||
|
systemEmailUser: z.string().optional(),
|
||||||
|
systemEmailConnectionHost: z.union([z.string(), z.literal("")]).optional(),
|
||||||
|
systemEmailIgnoreTLS: z.boolean().optional(),
|
||||||
|
systemEmailRequireTLS: z.boolean().optional(),
|
||||||
|
systemEmailRejectUnauthorized: z.boolean().optional(),
|
||||||
|
systemEmailTLSServername: z.union([z.string(), z.literal("")]).optional(),
|
||||||
|
});
|
||||||
Reference in new issue
Block a user