mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
feat: add UUID validation methods to ObjectID and update BaseAPI to use them
This commit is contained in:
@@ -347,7 +347,9 @@ export default class BaseAPI<
|
||||
req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
): Promise<void> {
|
||||
const objectId: ObjectID = new ObjectID(req.params["id"] as string);
|
||||
const idParam: string = req.params["id"] as string;
|
||||
ObjectID.validateUUID(idParam);
|
||||
const objectId: ObjectID = new ObjectID(idParam);
|
||||
await this.onBeforeGet(req, res);
|
||||
let select: Select<BaseModel> = {};
|
||||
|
||||
@@ -372,7 +374,9 @@ export default class BaseAPI<
|
||||
res: ExpressResponse,
|
||||
): Promise<void> {
|
||||
await this.onBeforeDelete(req, res);
|
||||
const objectId: ObjectID = new ObjectID(req.params["id"] as string);
|
||||
const idParam: string = req.params["id"] as string;
|
||||
ObjectID.validateUUID(idParam);
|
||||
const objectId: ObjectID = new ObjectID(idParam);
|
||||
|
||||
await this.service.deleteOneById({
|
||||
id: objectId,
|
||||
@@ -388,7 +392,9 @@ export default class BaseAPI<
|
||||
res: ExpressResponse,
|
||||
): Promise<void> {
|
||||
await this.onBeforeUpdate(req, res);
|
||||
const objectId: ObjectID = new ObjectID(req.params["id"] as string);
|
||||
const idParam: string = req.params["id"] as string;
|
||||
ObjectID.validateUUID(idParam);
|
||||
const objectId: ObjectID = new ObjectID(idParam);
|
||||
const objectIdString: string = objectId.toString();
|
||||
const body: JSONObject = req.body;
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@ import { JSONObject, ObjectType } from "./JSON";
|
||||
import { FindOperator } from "typeorm";
|
||||
import Zod from "../Utils/Schema/Zod";
|
||||
|
||||
// UUID validation regex - matches standard UUID format
|
||||
const UUID_REGEX: RegExp =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export default class ObjectID extends DatabaseProperty {
|
||||
private _id: string = "";
|
||||
public get id(): string {
|
||||
@@ -112,6 +116,27 @@ export default class ObjectID extends DatabaseProperty {
|
||||
return new ObjectID(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is a valid UUID format
|
||||
*/
|
||||
public static isValidUUID(id: string): boolean {
|
||||
if (!id || typeof id !== "string") {
|
||||
return false;
|
||||
}
|
||||
return UUID_REGEX.test(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a string is a valid UUID, throw BadDataException if not
|
||||
*/
|
||||
public static validateUUID(id: string): void {
|
||||
if (!ObjectID.isValidUUID(id.toString())) {
|
||||
throw new BadDataException(
|
||||
`Invalid ID format: "${id}". Expected a valid UUID (e.g., "550e8400-e29b-41d4-a716-446655440000").`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static override getSchema(): any {
|
||||
return Zod.string().openapi({
|
||||
type: "string",
|
||||
|
||||
Reference in New Issue
Block a user