Files
MikrotikManager/lib/evobgp-catalog.ts
T

23 lines
1.5 KiB
TypeScript

import { z } from "zod"
import type { EvoBgpCatalogSnapshot } from "./evobgp-context"
const text = z.string()
const id = text.min(1)
const count = z.number().int().nonnegative().safe()
function unique<T extends z.ZodType<{ id: string }>>(row: T) {
return z.array(row).refine(rows => new Set(rows.map(item => item.id)).size === rows.length)
}
const base = { id, asn: text, purpose: text, filter: text, updated: text, enabled: z.boolean() }
const schema = z.object({
fetchedAt: text.refine(value => Number.isFinite(Date.parse(value))),
modules: unique(z.object({ id, name: text, type: text })),
domains: unique(z.object({ ...base, domain: text, resolvedIp: text })),
ipRanges: unique(z.object({ ...base, cidr: text, country: text })),
asns: unique(z.object({ id, asn: text, org: text, country: text, prefixes: count.nullable(), filter: text, updated: text, enabled: z.boolean() })),
communities: unique(z.object({ id, value: text, name: text, description: text, type: z.enum(["standard", "no-export", "no-advertise", "local-as", "custom"]), filterIds: z.array(text), serverCount: count, prefixCount: count.nullable(), action: z.enum(["permit", "deny", "local-pref", "metric"]), actionValue: z.number().finite().optional(), enabled: z.boolean() })),
})
export function parseEvoBgpCatalog(raw: unknown): EvoBgpCatalogSnapshot {
const parsed = schema.safeParse(raw)
if (!parsed.success) throw new Error("Каталог Evolution не подтверждён: получен некорректный ответ сервера")
return parsed.data
}