Merge pull request #3640 from bluewave-labs/fix/maintenance-window-validation
fix(maintenance): reject past-dated one-time windows at creation
This commit is contained in:
6 files changed
+234
-29
No files matched your search
@@ -85,10 +85,6 @@ const CreateMaintenanceWindowPage = () => {
|
||||
repeat,
|
||||
};
|
||||
|
||||
if (repeat === 0) {
|
||||
payload.expiry = endDateTime.toISOString();
|
||||
}
|
||||
|
||||
return payload;
|
||||
};
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ const config: Config = {
|
||||
"^@/(.*)\\.ts$": "<rootDir>/src/$1.ts",
|
||||
"^@/(.*)\\.js$": "<rootDir>/src/$1.ts",
|
||||
"^@/(.*)$": "<rootDir>/src/$1",
|
||||
"^(\\.{1,2}/.*)\\.js$": "$1",
|
||||
},
|
||||
testMatch: ["<rootDir>/test/**/*.test.ts"],
|
||||
setupFilesAfterEnv: [],
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Schema, model, type Types } from "mongoose";
|
||||
import type { MaintenanceWindow } from "@/types/maintenanceWindow.js";
|
||||
import { DurationUnits, type MaintenanceWindow } from "@/types/maintenanceWindow.js";
|
||||
|
||||
type MaintenanceWindowDocumentBase = Omit<MaintenanceWindow, "id" | "monitorId" | "teamId" | "start" | "end" | "createdAt" | "updatedAt"> & {
|
||||
monitorId: Types.ObjectId;
|
||||
@@ -39,7 +39,7 @@ const MaintenanceWindowSchema = new Schema<MaintenanceWindowDocument>(
|
||||
},
|
||||
durationUnit: {
|
||||
type: String,
|
||||
enum: ["seconds", "minutes", "hours", "days"],
|
||||
enum: DurationUnits,
|
||||
},
|
||||
repeat: {
|
||||
type: Number,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export type DurationUnit = "seconds" | "minutes" | "hours" | "days";
|
||||
export const DurationUnits = ["seconds", "minutes", "hours", "days"] as const;
|
||||
export type DurationUnit = (typeof DurationUnits)[number];
|
||||
|
||||
export interface MaintenanceWindow {
|
||||
id: string;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { z } from "zod";
|
||||
import { booleanCoercion } from "./shared.js";
|
||||
import { DurationUnits } from "@/types/maintenanceWindow.js";
|
||||
|
||||
const dateToString = z.coerce.date().transform((d) => d.toISOString());
|
||||
|
||||
@@ -7,17 +8,37 @@ const dateToString = z.coerce.date().transform((d) => d.toISOString());
|
||||
// Maintenance Window Validations
|
||||
//****************************************
|
||||
|
||||
export const createMaintenanceWindowBodyValidation = z.object({
|
||||
monitors: z.array(z.string()).min(1, "At least one monitor is required"),
|
||||
name: z.string().min(1, "Name is required"),
|
||||
active: z.boolean().optional(),
|
||||
duration: z.number().min(1, "Duration is required"),
|
||||
durationUnit: z.enum(["seconds", "minutes", "hours", "days"]),
|
||||
start: dateToString,
|
||||
end: dateToString,
|
||||
repeat: z.number().min(0, "Repeat must be a non-negative number"),
|
||||
expiry: dateToString.optional(),
|
||||
});
|
||||
export const createMaintenanceWindowBodyValidation = z
|
||||
.object({
|
||||
monitors: z.array(z.string()).min(1, "At least one monitor is required"),
|
||||
name: z.string().min(1, "Name is required"),
|
||||
active: z.boolean().optional(),
|
||||
duration: z.number().min(1, "Duration is required"),
|
||||
durationUnit: z.enum(DurationUnits),
|
||||
start: dateToString,
|
||||
end: dateToString,
|
||||
repeat: z.number().min(0, "Repeat must be a non-negative number"),
|
||||
expiry: dateToString.optional(),
|
||||
})
|
||||
.strict()
|
||||
.superRefine((data, ctx) => {
|
||||
const start = new Date(data.start).getTime();
|
||||
const end = new Date(data.end).getTime();
|
||||
if (end <= start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "End must be after start",
|
||||
path: ["end"],
|
||||
});
|
||||
}
|
||||
if (data.repeat === 0 && end <= Date.now()) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "End must be in the future for one-time maintenance windows",
|
||||
path: ["end"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const getMaintenanceWindowByIdParamValidation = z.object({
|
||||
id: z.string().min(1, "ID is required"),
|
||||
@@ -43,14 +64,29 @@ export const editMaintenanceWindowByIdParamValidation = z.object({
|
||||
id: z.string().min(1, "ID is required"),
|
||||
});
|
||||
|
||||
export const editMaintenanceByIdWindowBodyValidation = z.object({
|
||||
active: z.boolean().optional(),
|
||||
name: z.string().optional(),
|
||||
repeat: z.number().optional(),
|
||||
start: dateToString.optional(),
|
||||
end: dateToString.optional(),
|
||||
expiry: dateToString.optional(),
|
||||
monitors: z.array(z.string()).optional(),
|
||||
duration: z.number().optional(),
|
||||
durationUnit: z.enum(["seconds", "minutes", "hours", "days"]).optional(),
|
||||
});
|
||||
export const editMaintenanceByIdWindowBodyValidation = z
|
||||
.object({
|
||||
active: z.boolean().optional(),
|
||||
name: z.string().optional(),
|
||||
repeat: z.number().optional(),
|
||||
start: dateToString.optional(),
|
||||
end: dateToString.optional(),
|
||||
expiry: dateToString.optional(),
|
||||
monitors: z.array(z.string()).optional(),
|
||||
duration: z.number().optional(),
|
||||
durationUnit: z.enum(DurationUnits).optional(),
|
||||
})
|
||||
.strict()
|
||||
.superRefine((data, ctx) => {
|
||||
if (data.start && data.end) {
|
||||
const start = new Date(data.start).getTime();
|
||||
const end = new Date(data.end).getTime();
|
||||
if (end <= start) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "End must be after start",
|
||||
path: ["end"],
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,171 @@
|
||||
import { describe, expect, it, jest, afterEach } from "@jest/globals";
|
||||
import {
|
||||
createMaintenanceWindowBodyValidation,
|
||||
editMaintenanceByIdWindowBodyValidation,
|
||||
} from "../../../src/validation/maintenanceWindowValidation.ts";
|
||||
|
||||
const FROZEN_NOW = new Date("2026-05-07T12:00:00Z").getTime();
|
||||
|
||||
const baseCreateBody = (overrides: Record<string, unknown> = {}) => ({
|
||||
monitors: ["mon-1"],
|
||||
name: "Scheduled Maintenance",
|
||||
duration: 60,
|
||||
durationUnit: "minutes",
|
||||
start: new Date(FROZEN_NOW + 60 * 60 * 1000).toISOString(),
|
||||
end: new Date(FROZEN_NOW + 2 * 60 * 60 * 1000).toISOString(),
|
||||
repeat: 0,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
describe("createMaintenanceWindowBodyValidation", () => {
|
||||
it("accepts a valid one-time window with end in the future", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(baseCreateBody());
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts a recurring window even when end is in the past", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(
|
||||
baseCreateBody({
|
||||
repeat: 1,
|
||||
start: new Date(FROZEN_NOW - 2 * 60 * 60 * 1000).toISOString(),
|
||||
end: new Date(FROZEN_NOW - 60 * 60 * 1000).toISOString(),
|
||||
})
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects when end is before start", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(
|
||||
baseCreateBody({
|
||||
start: new Date(FROZEN_NOW + 2 * 60 * 60 * 1000).toISOString(),
|
||||
end: new Date(FROZEN_NOW + 60 * 60 * 1000).toISOString(),
|
||||
})
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues).toEqual(expect.arrayContaining([expect.objectContaining({ message: "End must be after start", path: ["end"] })]));
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects when end equals start", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const sameInstant = new Date(FROZEN_NOW + 60 * 60 * 1000).toISOString();
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(baseCreateBody({ start: sameInstant, end: sameInstant }));
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues).toEqual(expect.arrayContaining([expect.objectContaining({ message: "End must be after start", path: ["end"] })]));
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects a one-time window with end in the past", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(
|
||||
baseCreateBody({
|
||||
start: new Date(FROZEN_NOW - 2 * 60 * 60 * 1000).toISOString(),
|
||||
end: new Date(FROZEN_NOW - 60 * 60 * 1000).toISOString(),
|
||||
})
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
message: "End must be in the future for one-time maintenance windows",
|
||||
path: ["end"],
|
||||
}),
|
||||
])
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects a one-time window with end exactly equal to now", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(
|
||||
baseCreateBody({
|
||||
start: new Date(FROZEN_NOW - 60 * 60 * 1000).toISOString(),
|
||||
end: new Date(FROZEN_NOW).toISOString(),
|
||||
})
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
message: "End must be in the future for one-time maintenance windows",
|
||||
path: ["end"],
|
||||
}),
|
||||
])
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects unknown fields under .strict()", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(baseCreateBody({ teamId: "team-1" }));
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects an invalid durationUnit value", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(baseCreateBody({ durationUnit: "weeks" }));
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects a missing required field", () => {
|
||||
jest.useFakeTimers().setSystemTime(FROZEN_NOW);
|
||||
const { name: _name, ...withoutName } = baseCreateBody();
|
||||
const result = createMaintenanceWindowBodyValidation.safeParse(withoutName);
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("editMaintenanceByIdWindowBodyValidation", () => {
|
||||
it("accepts an empty body (all fields optional)", () => {
|
||||
const result = editMaintenanceByIdWindowBodyValidation.safeParse({});
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts a partial update with only active toggled", () => {
|
||||
const result = editMaintenanceByIdWindowBodyValidation.safeParse({ active: false });
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects unknown fields under .strict()", () => {
|
||||
const result = editMaintenanceByIdWindowBodyValidation.safeParse({
|
||||
active: false,
|
||||
id: "mw-1",
|
||||
});
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects when start and end are both present and end <= start", () => {
|
||||
const start = new Date("2026-05-08T10:00:00Z").toISOString();
|
||||
const end = new Date("2026-05-08T09:00:00Z").toISOString();
|
||||
const result = editMaintenanceByIdWindowBodyValidation.safeParse({ start, end });
|
||||
expect(result.success).toBe(false);
|
||||
if (!result.success) {
|
||||
expect(result.error.issues).toEqual(expect.arrayContaining([expect.objectContaining({ message: "End must be after start", path: ["end"] })]));
|
||||
}
|
||||
});
|
||||
|
||||
it("does not apply the start/end ordering rule when only end is provided", () => {
|
||||
const result = editMaintenanceByIdWindowBodyValidation.safeParse({
|
||||
end: new Date("2020-01-01T00:00:00Z").toISOString(),
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("does not apply the start/end ordering rule when only start is provided", () => {
|
||||
const result = editMaintenanceByIdWindowBodyValidation.safeParse({
|
||||
start: new Date("2099-01-01T00:00:00Z").toISOString(),
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user