mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
feat: add Zod schema validation for Domain, Email, IP, MonitorCriteria, MonitorCriteriaInstance, MonitorStep, MonitorSteps, Name, ObjectID, Phone, Port, and Version classes
This commit is contained in:
@@ -2,6 +2,7 @@ import DatabaseProperty from "./Database/DatabaseProperty";
|
||||
import BadDataException from "./Exception/BadDataException";
|
||||
import { JSONObject, ObjectType } from "./JSON";
|
||||
import { FindOperator } from "typeorm/find-options/FindOperator";
|
||||
import Zod, { ZodSchema } from "../Utils/Schema/Zod";
|
||||
|
||||
export default class Domain extends DatabaseProperty {
|
||||
private _domain: string = "";
|
||||
@@ -91,4 +92,18 @@ export default class Domain extends DatabaseProperty {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.Domain),
|
||||
value: Zod.string().openapi({
|
||||
type: "string",
|
||||
example: "example.com",
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "Domain object",
|
||||
example: { _type: ObjectType.Domain, value: "example.com" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import DatabaseProperty from "./Database/DatabaseProperty";
|
||||
import BadDataException from "./Exception/BadDataException";
|
||||
import { JSONObject, ObjectType } from "./JSON";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../Utils/Schema/Zod";
|
||||
|
||||
const nonBusinessEmailDomains: Array<string> = [
|
||||
"gmail",
|
||||
@@ -123,4 +124,19 @@ export default class Email extends DatabaseProperty {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.Email),
|
||||
value: Zod.string().email().openapi({
|
||||
type: "string",
|
||||
format: "email",
|
||||
example: "user@example.com",
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "Email object",
|
||||
example: { _type: ObjectType.Email, value: "user@example.com" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { JSONObject, ObjectType } from "../JSON";
|
||||
import Typeof from "../Typeof";
|
||||
import IPType from "./IPType";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../../Utils/Schema/Zod";
|
||||
|
||||
export default class IP extends DatabaseProperty {
|
||||
private _ip: string = "";
|
||||
@@ -199,4 +200,18 @@ export default class IP extends DatabaseProperty {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.IP),
|
||||
value: Zod.string().openapi({
|
||||
type: "string",
|
||||
example: "192.168.1.1",
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "IP object",
|
||||
example: { _type: ObjectType.IP, value: "192.168.1.1" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import ObjectID from "../ObjectID";
|
||||
import MonitorCriteriaInstance from "./MonitorCriteriaInstance";
|
||||
import MonitorType from "./MonitorType";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../../Utils/Schema/Zod";
|
||||
|
||||
export interface MonitorCriteriaType {
|
||||
monitorCriteriaInstanceArray: Array<MonitorCriteriaInstance>;
|
||||
@@ -179,4 +180,22 @@ export default class MonitorCriteria extends DatabaseProperty {
|
||||
public override toString(): string {
|
||||
return JSON.stringify(this.toJSON());
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.MonitorCriteria),
|
||||
value: Zod.object({
|
||||
monitorCriteriaInstanceArray: Zod.array(Zod.any()), // Could be improved with MonitorCriteriaInstance.getSchema()
|
||||
}).openapi({
|
||||
type: "object",
|
||||
example: {
|
||||
monitorCriteriaInstanceArray: [],
|
||||
},
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "MonitorCriteria object",
|
||||
example: { _type: ObjectType.MonitorCriteria, value: { monitorCriteriaInstanceArray: [] } },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import { CriteriaIncident } from "./CriteriaIncident";
|
||||
import MonitorType from "./MonitorType";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../../Utils/Schema/Zod";
|
||||
|
||||
export interface MonitorCriteriaInstanceType {
|
||||
monitorStatusId: ObjectID | undefined;
|
||||
@@ -1018,6 +1019,41 @@ export default class MonitorCriteriaInstance extends DatabaseProperty {
|
||||
return monitorCriteriaInstance;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.MonitorCriteriaInstance),
|
||||
value: Zod.object({
|
||||
id: Zod.string(),
|
||||
monitorStatusId: Zod.any(),
|
||||
filterCondition: Zod.any(),
|
||||
filters: Zod.array(Zod.any()),
|
||||
incidents: Zod.array(Zod.any()),
|
||||
alerts: Zod.array(Zod.any()),
|
||||
name: Zod.string(),
|
||||
description: Zod.string(),
|
||||
changeMonitorStatus: Zod.boolean().optional(),
|
||||
createIncidents: Zod.boolean().optional(),
|
||||
createAlerts: Zod.boolean().optional(),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
example: {
|
||||
id: "id",
|
||||
monitorStatusId: "statusId",
|
||||
filterCondition: "All",
|
||||
filters: [],
|
||||
incidents: [],
|
||||
alerts: [],
|
||||
name: "Criteria Name",
|
||||
description: "Description",
|
||||
},
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "MonitorCriteriaInstance object",
|
||||
example: { _type: ObjectType.MonitorCriteriaInstance, value: { id: "id", monitorStatusId: "statusId", filterCondition: "All", filters: [], incidents: [], alerts: [], name: "Criteria Name", description: "Description" } },
|
||||
});
|
||||
}
|
||||
|
||||
public static isValid(_json: JSONObject): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import MonitorStepTraceMonitor, {
|
||||
import MonitorStepMetricMonitor, {
|
||||
MonitorStepMetricMonitorUtil,
|
||||
} from "./MonitorStepMetricMonitor";
|
||||
import Zod, { ZodSchema } from "../../Utils/Schema/Zod";
|
||||
|
||||
export interface MonitorStepType {
|
||||
id: string;
|
||||
@@ -425,6 +426,40 @@ export default class MonitorStep extends DatabaseProperty {
|
||||
return monitorStep;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.MonitorStep),
|
||||
value: Zod.object({
|
||||
id: Zod.string(),
|
||||
monitorDestination: Zod.any().optional(),
|
||||
monitorCriteria: Zod.any(),
|
||||
requestType: Zod.any(),
|
||||
requestHeaders: Zod.any().optional(),
|
||||
requestBody: Zod.string().optional(),
|
||||
doNotFollowRedirects: Zod.boolean().optional(),
|
||||
monitorDestinationPort: Zod.any().optional(),
|
||||
customCode: Zod.string().optional(),
|
||||
screenSizeTypes: Zod.any().optional(),
|
||||
browserTypes: Zod.any().optional(),
|
||||
logMonitor: Zod.any().optional(),
|
||||
traceMonitor: Zod.any().optional(),
|
||||
metricMonitor: Zod.any().optional(),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
example: {
|
||||
id: "stepId",
|
||||
monitorDestination: undefined,
|
||||
monitorCriteria: {},
|
||||
requestType: "GET",
|
||||
},
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "MonitorStep object",
|
||||
example: { _type: ObjectType.MonitorStep, value: { id: "stepId", monitorDestination: undefined, monitorCriteria: {}, requestType: "GET" } },
|
||||
});
|
||||
}
|
||||
|
||||
public isValid(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import ObjectID from "../ObjectID";
|
||||
import MonitorStep from "./MonitorStep";
|
||||
import MonitorType from "./MonitorType";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../../Utils/Schema/Zod";
|
||||
|
||||
export interface MonitorStepsType {
|
||||
monitorStepsInstanceArray: Array<MonitorStep>;
|
||||
@@ -192,4 +193,24 @@ export default class MonitorSteps extends DatabaseProperty {
|
||||
public override toString(): string {
|
||||
return JSON.stringify(this.toJSON());
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.MonitorSteps),
|
||||
value: Zod.object({
|
||||
monitorStepsInstanceArray: Zod.array(Zod.any()),
|
||||
defaultMonitorStatusId: Zod.string().optional(),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
example: {
|
||||
monitorStepsInstanceArray: [],
|
||||
defaultMonitorStatusId: undefined,
|
||||
},
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "MonitorSteps object",
|
||||
example: { _type: ObjectType.MonitorSteps, value: { monitorStepsInstanceArray: [], defaultMonitorStatusId: undefined } },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import DatabaseProperty from "./Database/DatabaseProperty";
|
||||
import BadDataException from "./Exception/BadDataException";
|
||||
import { JSONObject, ObjectType } from "./JSON";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../Utils/Schema/Zod";
|
||||
|
||||
export default class Name extends DatabaseProperty {
|
||||
private _title: string = "";
|
||||
@@ -83,4 +84,18 @@ export default class Name extends DatabaseProperty {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.Name),
|
||||
value: Zod.string().openapi({
|
||||
type: "string",
|
||||
example: "John Doe",
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "Name object",
|
||||
example: { _type: ObjectType.Name, value: "John Doe" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import DatabaseProperty from "./Database/DatabaseProperty";
|
||||
import BadDataException from "./Exception/BadDataException";
|
||||
import { JSONObject, ObjectType } from "./JSON";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../Utils/Schema/Zod";
|
||||
|
||||
export default class ObjectID extends DatabaseProperty {
|
||||
private _id: string = "";
|
||||
@@ -110,4 +111,18 @@ export default class ObjectID extends DatabaseProperty {
|
||||
public static fromString(id: string): ObjectID {
|
||||
return new ObjectID(id);
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.ObjectID),
|
||||
value: Zod.string().openapi({
|
||||
type: "string",
|
||||
example: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "ObjectID object",
|
||||
example: { _type: ObjectType.ObjectID, value: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import DatabaseProperty from "./Database/DatabaseProperty";
|
||||
import BadDataException from "./Exception/BadDataException";
|
||||
import { JSONObject, ObjectType } from "./JSON";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../Utils/Schema/Zod";
|
||||
|
||||
export default class Phone extends DatabaseProperty {
|
||||
private _phone: string = "";
|
||||
@@ -131,4 +132,18 @@ export default class Phone extends DatabaseProperty {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.Phone),
|
||||
value: Zod.string().openapi({
|
||||
type: "string",
|
||||
example: "+1-555-123-4567",
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "Phone object",
|
||||
example: { _type: ObjectType.Phone, value: "+1-555-123-4567" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { JSONObject, ObjectType } from "./JSON";
|
||||
import PositiveNumber from "./PositiveNumber";
|
||||
import Typeof from "./Typeof";
|
||||
import { FindOperator } from "typeorm/find-options/FindOperator";
|
||||
import Zod, { ZodSchema } from "../Utils/Schema/Zod";
|
||||
|
||||
export default class Port extends DatabaseProperty {
|
||||
private _port: PositiveNumber = new PositiveNumber(0);
|
||||
@@ -89,4 +90,18 @@ export default class Port extends DatabaseProperty {
|
||||
public toNumber(): number {
|
||||
return this.port.toNumber();
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.Port),
|
||||
value: Zod.number().int().min(0).max(65535).openapi({
|
||||
type: "number",
|
||||
example: 8080,
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "Port object",
|
||||
example: { _type: ObjectType.Port, value: 8080 },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import DatabaseProperty from "./Database/DatabaseProperty";
|
||||
import BadDataException from "./Exception/BadDataException";
|
||||
import { JSONObject, ObjectType } from "./JSON";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod, { ZodSchema } from "../Utils/Schema/Zod";
|
||||
|
||||
export default class Version extends DatabaseProperty {
|
||||
private _version: string = "";
|
||||
@@ -63,4 +64,18 @@ export default class Version extends DatabaseProperty {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static override getSchema(): ZodSchema {
|
||||
return Zod.object({
|
||||
_type: Zod.literal(ObjectType.Version),
|
||||
value: Zod.string().openapi({
|
||||
type: "string",
|
||||
example: "1.0.0",
|
||||
}),
|
||||
}).openapi({
|
||||
type: "object",
|
||||
description: "Version object",
|
||||
example: { _type: ObjectType.Version, value: "1.0.0" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user