How can I write this type with Zod?

Can someone pls help me to write this type with Zod?
type MyType = {
transcript: {
text: Array<{
"#text": string;
start: string;
end: string
}>;
[key: string]: Record<string, unknown> | Array<Record<string, unknown>>;
};
[key: string]: Record<string, unknown>;
};
type MyType = {
transcript: {
text: Array<{
"#text": string;
start: string;
end: string
}>;
[key: string]: Record<string, unknown> | Array<Record<string, unknown>>;
};
[key: string]: Record<string, unknown>;
};
TLDR: the object and all nested objects need to have the listed attributes but they cann all be extended. I tried something like this but when I try to safe parse it, it says that the data is unknown.
const ZodTranscriptXMLFormat = z.union([
z.record(z.string(), z.unknown()),
z.object({
transcript: z.object({
text: z.array(
z.object({
text: z.array(
z.object({
"#text": z.string(),
start: z.string(),
end: z.string(),
}),
),
}),
),
}),
}),
]);
const ZodTranscriptXMLFormat = z.union([
z.record(z.string(), z.unknown()),
z.object({
transcript: z.object({
text: z.array(
z.object({
text: z.array(
z.object({
"#text": z.string(),
start: z.string(),
end: z.string(),
}),
),
}),
),
}),
}),
]);
2 Replies
LittleLily
LittleLily12mo ago
You have an extra level of nesting of a text property with an array + object inside, try removing one of the { text: z.array(z.object(...)) } levels
JulieCezar
JulieCezar12mo ago
tyty ❤️
Want results from more Discord servers?
Add your server