mirror of
https://github.com/fosrl/pangolin.git
synced 2026-06-25 08:41:55 +00:00
refactor: dedupe resource rule value validation
This commit is contained in:
committed by
GitHub
parent
0d960181a2
commit
a7c99f336f
@@ -37,6 +37,29 @@ const updateResourceRuleSchema = z
|
|||||||
error: "At least one field must be provided for update"
|
error: "At least one field must be provided for update"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function getRuleValueValidationError(
|
||||||
|
match: "CIDR" | "IP" | "PATH" | "COUNTRY" | "ASN" | "REGION",
|
||||||
|
value: string
|
||||||
|
): string | null {
|
||||||
|
if (match === "CIDR" && !isValidCIDR(value)) {
|
||||||
|
return "Invalid CIDR provided";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (match === "IP" && !isValidIP(value)) {
|
||||||
|
return "Invalid IP provided";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (match === "PATH" && !isValidUrlGlobPattern(value)) {
|
||||||
|
return "Invalid URL glob pattern provided";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (match === "REGION" && !isValidRegionId(value)) {
|
||||||
|
return "Invalid region ID provided";
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
registry.registerPath({
|
registry.registerPath({
|
||||||
method: "post",
|
method: "post",
|
||||||
path: "/resource/{resourceId}/rule/{ruleId}",
|
path: "/resource/{resourceId}/rule/{ruleId}",
|
||||||
@@ -155,42 +178,14 @@ export async function updateResourceRule(
|
|||||||
const { value } = updateData;
|
const { value } = updateData;
|
||||||
|
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
if (match === "CIDR") {
|
const validationError = getRuleValueValidationError(
|
||||||
if (!isValidCIDR(value)) {
|
match,
|
||||||
return next(
|
value
|
||||||
createHttpError(
|
);
|
||||||
HttpCode.BAD_REQUEST,
|
if (validationError) {
|
||||||
"Invalid CIDR provided"
|
return next(
|
||||||
)
|
createHttpError(HttpCode.BAD_REQUEST, validationError)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
} else if (match === "IP") {
|
|
||||||
if (!isValidIP(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid IP provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "PATH") {
|
|
||||||
if (!isValidUrlGlobPattern(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid URL glob pattern provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "REGION") {
|
|
||||||
if (!isValidRegionId(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid region ID provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,42 +238,11 @@ export async function updateResourceRule(
|
|||||||
const { value } = updateData;
|
const { value } = updateData;
|
||||||
|
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
if (match === "CIDR") {
|
const validationError = getRuleValueValidationError(match, value);
|
||||||
if (!isValidCIDR(value)) {
|
if (validationError) {
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(HttpCode.BAD_REQUEST, validationError)
|
||||||
HttpCode.BAD_REQUEST,
|
);
|
||||||
"Invalid CIDR provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "IP") {
|
|
||||||
if (!isValidIP(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid IP provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "PATH") {
|
|
||||||
if (!isValidUrlGlobPattern(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid URL glob pattern provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "REGION") {
|
|
||||||
if (!isValidRegionId(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid region ID provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user