Skip to content

Open-source status page, incident response and uptime monitoring

fix: api depreacted regions monitor schema (#1442)

Maximilian Kasked814002dc436
diff --git a/apps/server/src/routes/v1/monitors/post.test.ts b/apps/server/src/routes/v1/monitors/post.test.ts
index 3bf77cb54..f3aefcf44 100644
--- a/apps/server/src/routes/v1/monitors/post.test.ts
+++ b/apps/server/src/routes/v1/monitors/post.test.ts
@@ -1,6 +1,7 @@
import { expect, test } from "bun:test";
import { app } from "@/index";
+import { createErrorSchema } from "@/libs/errors";
import { MonitorSchema } from "./schema";
test("create a valid monitor", async () => {
@@ -91,3 +92,30 @@ test("no auth key should return 401", async () => {
expect(res.status).toBe(401);
});
+
+test("create a monitor with deprecated regions should return 400", async () => {
+ const res = await app.request("/v1/monitor", {
+ method: "POST",
+ headers: {
+ "x-openstatus-key": "1",
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ regions: ["ams", "jnb", "hkg", "waw"],
+ name: "Testing Deprecated Regions",
+ description: "Testing Deprecated Regions",
+ url: "https://www.openstatus.dev",
+ method: "GET",
+ active: true,
+ }),
+ });
+
+ const json = await res.json();
+ const errorSchema = createErrorSchema("BAD_REQUEST").safeParse(json);
+
+ expect(res.status).toBe(400);
+ expect(errorSchema.success).toBe(true);
+ expect(errorSchema.data?.message).toMatch(
+ "Deprecated regions are not allowed: hkg, waw",
+ );
+});
diff --git a/apps/server/src/routes/v1/monitors/schema.ts b/apps/server/src/routes/v1/monitors/schema.ts
index d528cad55..0e2098fd8 100644
--- a/apps/server/src/routes/v1/monitors/schema.ts
+++ b/apps/server/src/routes/v1/monitors/schema.ts
@@ -6,6 +6,7 @@ import {
monitorPeriodicitySchema,
monitorRegions,
} from "@openstatus/db/src/schema/constants";
+import { regionDict } from "@openstatus/utils";
import { ZodError } from "zod";
const statusAssertion = z
@@ -94,21 +95,34 @@ export const MonitorSchema = z
regions: z
.preprocess(
(val) => {
- try {
- if (Array.isArray(val)) return val;
- if (String(val).length > 0) {
- return String(val).split(",");
- }
- return [];
- } catch (e) {
+ let regions: Array<unknown> = [];
+ if (Array.isArray(val)) {
+ regions = val;
+ }
+ if (String(val).length > 0) {
+ regions = String(val).split(",");
+ }
+
+ const deprecatedRegions = regions.filter((r) => {
+ const deprecated =
+ regionDict[r as keyof typeof regionDict].deprecated;
+ if (deprecated) return true;
+ return false;
+ });
+
+ if (deprecatedRegions.length > 0) {
throw new ZodError([
{
code: "custom",
- path: ["headers"],
- message: e instanceof Error ? e.message : "Invalid value",
+ path: ["regions"],
+ message: `Deprecated regions are not allowed: ${deprecatedRegions.join(
+ ", ",
+ )}`,
},
]);
}
+
+ return regions;
},
z.array(z.enum(monitorRegions)),
)
@@ -343,9 +357,45 @@ const baseRequest = z.object({
description: "Whether the monitor is public",
default: false,
}),
- regions: z.array(z.enum(monitorRegions)).openapi({
- description: "Regions to run the request in",
- }),
+ regions: z
+ .preprocess(
+ (val) => {
+ let regions: Array<unknown> = [];
+ if (Array.isArray(val)) {
+ regions = val;
+ }
+ if (String(val).length > 0) {
+ regions = String(val).split(",");
+ }
+
+ const deprecatedRegions = regions.filter((r) => {
+ const deprecated =
+ regionDict[r as keyof typeof regionDict].deprecated;
+ if (deprecated) return true;
+ return false;
+ });
+
+ if (deprecatedRegions.length > 0) {
+ throw new ZodError([
+ {
+ code: "custom",
+ path: ["regions"],
+ message: `Deprecated regions are not allowed: ${deprecatedRegions.join(
+ ", ",
+ )}`,
+ },
+ ]);
+ }
+
+ return regions;
+ },
+ z.array(z.enum(monitorRegions)),
+ )
+ .default([])
+ .openapi({
+ example: ["ams"],
+ description: "Where we should monitor it",
+ }),
openTelemetry: z
.object({
endpoint: z