Untitled
Anonymous
typescript
02/25/2026 8:04 PM
3.1 KB
17
Indexable
import { z } from "zod";
// 1. Pomocnicze schematy (Leaf nodes)
// Definiujemy najpierw te obiekty, które nie mają cyklicznych zależności
const OnuModelTypeSchema = z.object({
name: z.string(),
type: z.enum(["ZTE", "Huawei"]), // Z notatki: "type ZTE/Huawei"
});
const UserGroupSchema = z.object({
name: z.string(),
// related users - uproszczone do tablicy stringów (ID) lub prostej struktury,
// by nie gmatwać głównego przykładu
relatedUsers: z.array(z.string()).optional(),
});
// 2. Definicja interfejsów TypeScript (Opcjonalne, ale zalecane przy .lazy)
// Pomaga to TypeScriptowi zrozumieć typy zanim Zod domknie definicję runtime.
export type Onu = {
name: string;
relatedOlt: Olt; // Cykl
managementGroup: z.infer<typeof UserGroupSchema>;
modelOnu: z.infer<typeof OnuModelTypeSchema>;
state: "online" | "offline";
lastShutdown: Date | null;
lastBoot: Date | null;
clientId: string | number;
whoCreated: string;
createdAt: Date;
longLat: string; // Zakładam string, np. "50.12, 19.00"
};
export type Olt = {
name: string;
model: string;
clients: Onu[]; // Cykl
};
// 3. Główna Schema z Circular Dependency (z.lazy)
// Musimy użyć zmiennej let lub const z jawnym typowaniem, aby lazy mogło się odnieść
// do schematu zdefiniowanego "niżej" lub w tym samym bloku.
export const OnuSchema: z.ZodType<Onu> = z.object({
name: z.string(),
// KLUCZOWE: z.lazy opóźnia ewaluację OltSchema do momentu wywołania.
// Dzięki temu OltSchema może być zdefiniowane linijkę niżej.
relatedOlt: z.lazy(() => OltSchema),
managementGroup: UserGroupSchema,
modelOnu: OnuModelTypeSchema,
state: z.enum(["online", "offline"]),
// Używam .nullable() i .coerce.date(), bo daty z API często przychodzą jako stringi
lastShutdown: z.coerce.date().nullable(),
lastBoot: z.coerce.date().nullable(),
clientId: z.union([z.string(), z.number()]), // "id from lms system"
whoCreated: z.string(),
createdAt: z.coerce.date(),
longLat: z.string(), // lub z.object({ lat: z.number(), lng: z.number() })
});
export const OltSchema: z.ZodType<Olt> = z.object({
name: z.string(),
model: z.string(), // "separate object" - uprościłem do nazwy/id
// KLUCZOWE: Relacja zwrotna. OLT ma listę ONU.
clients: z.array(z.lazy(() => OnuSchema)),
});Editor is loading...
Leave a Comment