Request validation for /api/integrations.* (#5638)

This commit is contained in:
Apoorv Mishra
2023-08-02 06:47:01 +05:30
committed by GitHub
parent 228d1faa9f
commit 2331bbbd36
6 changed files with 314 additions and 103 deletions

View File

@@ -0,0 +1,89 @@
import { z } from "zod";
import { IntegrationType } from "@shared/types";
import { Integration } from "@server/models";
import { UserCreatableIntegrationService } from "@server/models/Integration";
import BaseSchema from "../BaseSchema";
export const IntegrationsListSchema = BaseSchema.extend({
body: z.object({
/** Integrations sorting direction */
direction: z
.string()
.optional()
.transform((val) => (val !== "ASC" ? "DESC" : val)),
/** Integrations sorting column */
sort: z
.string()
.refine((val) => Object.keys(Integration.getAttributes()).includes(val), {
message: "Invalid sort parameter",
})
.default("updatedAt"),
/** Integration type */
type: z.nativeEnum(IntegrationType),
}),
});
export type IntegrationsListReq = z.infer<typeof IntegrationsListSchema>;
export const IntegrationsCreateSchema = BaseSchema.extend({
body: z.object({
/** Integration type */
type: z.nativeEnum(IntegrationType),
/** Integration service */
service: z.nativeEnum(UserCreatableIntegrationService),
/** Integration config/settings */
settings: z
.object({ url: z.string().url() })
.or(
z.object({
url: z.string().url(),
channel: z.string(),
channelId: z.string(),
})
)
.or(z.object({ measurementId: z.string() }))
.or(z.object({ serviceTeamId: z.string() }))
.optional(),
}),
});
export type IntegrationsCreateReq = z.infer<typeof IntegrationsCreateSchema>;
export const IntegrationsUpdateSchema = BaseSchema.extend({
body: z.object({
/** Id of integration that needs update */
id: z.string().uuid(),
/** Integration config/settings */
settings: z
.object({ url: z.string().url() })
.or(
z.object({
url: z.string().url(),
channel: z.string(),
channelId: z.string(),
})
)
.or(z.object({ measurementId: z.string() }))
.or(z.object({ serviceTeamId: z.string() }))
.optional(),
/** Integration events */
events: z.array(z.string()).optional().default([]),
}),
});
export type IntegrationsUpdateReq = z.infer<typeof IntegrationsUpdateSchema>;
export const IntegrationsDeleteSchema = BaseSchema.extend({
body: z.object({
/** Id of integration to be deleted */
id: z.string().uuid(),
}),
});
export type IntegrationsDeleteReq = z.infer<typeof IntegrationsDeleteSchema>;