metis-desk/modules/assets/src/api/domain/infrastructure-values.ts
2026-10-17 17:01:16 +02:00

52 lines
1.5 KiB
TypeScript

import { z } from "zod/v4";
export const InfrastructureRoleSchema = z.enum([
"network",
"server",
"storage",
"shared_peripheral",
]);
export const InterfaceAddressInputSchema = z.strictObject({
id: z.uuid().optional(),
address: z
.union([z.ipv4(), z.ipv6()])
.transform((value) =>
value.includes(":") ? new URL(`http://[${value}]`).hostname.slice(1, -1) : value
),
});
export const InfrastructureInterfaceInputSchema = z.strictObject({
id: z.uuid().optional(),
name: z.string().trim().min(1).max(100),
kind: z.enum(["ethernet", "wifi", "other"]),
mac: z
.string()
.trim()
.max(17)
.refine((value) => !value || /^(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2}$/i.test(value))
.transform((value) => value.replaceAll("-", ":").toUpperCase()),
notes: z.string().trim().max(2000),
addresses: z.array(InterfaceAddressInputSchema).max(32),
});
export const InfrastructureInputSchema = z.strictObject({
roles: z.array(InfrastructureRoleSchema).max(4),
interfaces: z.array(InfrastructureInterfaceInputSchema).max(32),
});
export const InfrastructureValuesSchema = InfrastructureInputSchema.extend({
interfaces: z
.array(
InfrastructureInterfaceInputSchema.extend({
id: z.uuid(),
addresses: z.array(InterfaceAddressInputSchema.extend({ id: z.uuid() })).max(32),
})
)
.max(32),
});
export type InfrastructureInput = z.infer<typeof InfrastructureInputSchema>;
export type InfrastructureData = z.infer<typeof InfrastructureValuesSchema>;