Files
MikrotikManager/backend/src/services/firewall-address-input.ts
T

43 lines
1.2 KiB
TypeScript

import { z } from "zod"
const FamilySchema = z.enum(["ip", "ip6"])
const NativeIdSchema = z.string().regex(/^\*[A-Za-z0-9]+$/).max(100)
export const AddressKeySchema = z.object({
serverId: z.string().min(1),
family: FamilySchema,
rosId: NativeIdSchema,
})
export const AddressWriteSchema = z.object({
serverId: z.string().min(1),
family: FamilySchema,
rosId: NativeIdSchema.optional(),
list: z.string().min(1),
address: z.string().min(1),
comment: z.string().optional(),
timeout: z.string().optional(),
disabled: z.boolean().optional(),
})
export const AddressPatchSchema = AddressKeySchema.extend({
disabled: z.boolean(),
})
function toRosBody(obj: Record<string, string | undefined>): Record<string, string> {
const out: Record<string, string> = {}
for (const [k, v] of Object.entries(obj)) {
if (v !== undefined && v !== "") out[k] = v
}
return out
}
export function addressToRos(d: z.infer<typeof AddressWriteSchema>): Record<string, string> {
return toRosBody({
list: d.list,
address: d.address,
comment: d.comment,
timeout: d.timeout,
disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined,
})
}