diff --git a/src/components/resource-policy/PolicyAccessRulesSection.tsx b/src/components/resource-policy/PolicyAccessRulesSection.tsx index 796ebbad0..e074c03c8 100644 --- a/src/components/resource-policy/PolicyAccessRulesSection.tsx +++ b/src/components/resource-policy/PolicyAccessRulesSection.tsx @@ -15,9 +15,11 @@ import { useTranslations } from "next-intl"; import { toast } from "@app/hooks/useToast"; import { createPolicyRulesSectionSchema, + type PolicyRuleMatchType, validatePolicyRulesForSave, type PolicyFormValues } from "."; +import { POLICY_RULE_MATCH_TYPES } from "./policy-access-rule-validation"; import { Button } from "@app/components/ui/button"; import { Plus } from "lucide-react"; @@ -69,6 +71,12 @@ export type PolicyAccessRulesSectionProps = | PolicyAccessRulesSectionEditProps | PolicyAccessRulesSectionCreateProps; +const POLICY_RULE_MATCH_SET = new Set(POLICY_RULE_MATCH_TYPES); + +function isPolicyRuleMatchType(value: string): value is PolicyRuleMatchType { + return POLICY_RULE_MATCH_SET.has(value); +} + export function PolicyAccessRulesSection(props: PolicyAccessRulesSectionProps) { if (props.mode === "create") { return ; @@ -270,7 +278,7 @@ function PolicyAccessRulesSectionEdit({ .map((r) => ({ ruleId: r.ruleId, action: r.action as "ACCEPT" | "DROP" | "PASS", - match: r.match, + match: isPolicyRuleMatchType(r.match) ? r.match : "PATH", value: r.value, priority: r.priority, enabled: r.enabled, diff --git a/src/components/resource-policy/index.ts b/src/components/resource-policy/index.ts index 6e867b017..18591491a 100644 --- a/src/components/resource-policy/index.ts +++ b/src/components/resource-policy/index.ts @@ -2,6 +2,7 @@ import z from "zod"; import { POLICY_RULE_MATCH_TYPES } from "./policy-access-rule-validation"; +import type { PolicyRuleMatchType } from "./policy-access-rule-validation"; export const createPolicySchema = z.object({ name: z.string().min(1).max(255), @@ -50,7 +51,7 @@ export type PolicyFormValues = z.infer; export type LocalRule = { ruleId: number; action: "ACCEPT" | "DROP" | "PASS"; - match: string; + match: PolicyRuleMatchType; value: string; priority: number; enabled: boolean; diff --git a/src/components/resource-policy/policy-access-rule-utils.ts b/src/components/resource-policy/policy-access-rule-utils.ts index 905023ff4..7a2e6f481 100644 --- a/src/components/resource-policy/policy-access-rule-utils.ts +++ b/src/components/resource-policy/policy-access-rule-utils.ts @@ -1,7 +1,9 @@ +import type { PolicyRuleMatchType } from "./policy-access-rule-validation"; + export type PolicyAccessRule = { ruleId: number; action: "ACCEPT" | "DROP" | "PASS"; - match: string; + match: PolicyRuleMatchType; value: string; priority: number; enabled: boolean;