Z
Zod5mo ago
Steve

Stephan Meijer - What was the deal with union a...

What was the deal with union and transform again?
import { Heading, Paragraph, Text } from '@toegang-voor-iedereen/document-spec-types';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';

// ...

export const TextDto = z.object({
type: z.literal('text'),
text: z.string(),
marks: z.array(
z.union([BoldMarkDto, ItalicMarkDto, IdentifierMarkDto])
)
}).transform(({ text, marks }): Text => {
return {
id: marks.find(mark => mark.type === 'identifier')?.attrs.identifier ?? uuidv4();
type: 'https://spec.nldoc.nl/Resource/Text',
text,
styling: marksToStyling(marks),
descriptors: []
}
})

export const HeadingDto = z.object({
type: z.literal('heading'),
attrs: z.object({
identifier: z.string(),
level: z.number(),
}),
content: z.array(
Te
)
}).transform(({ attrs, content }): Heading => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Heading',
level: attrs.level,
descriptors: [],
children: content
}
});

export const ParagraphDto = z.object({
type: z.literal('paragraph'),
attrs: z.object({
identifier: z.string(),
}),
content: z.array(TextDto)
}).transform(({ attrs, content }): Paragraph => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Paragraph',
descriptors: [],
children: content
}
});

export const DocumentDto = z.object({
type: z.literal('doc'),
attrs: z.object({
identifier: z.string().nullable(),
}),
content: z.array(
z.union([HeadingDto, ParagraphDto])
)
}).transform(({ attrs, content }): Document => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Document',
descriptors: [],
children: content
}
}
import { Heading, Paragraph, Text } from '@toegang-voor-iedereen/document-spec-types';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';

// ...

export const TextDto = z.object({
type: z.literal('text'),
text: z.string(),
marks: z.array(
z.union([BoldMarkDto, ItalicMarkDto, IdentifierMarkDto])
)
}).transform(({ text, marks }): Text => {
return {
id: marks.find(mark => mark.type === 'identifier')?.attrs.identifier ?? uuidv4();
type: 'https://spec.nldoc.nl/Resource/Text',
text,
styling: marksToStyling(marks),
descriptors: []
}
})

export const HeadingDto = z.object({
type: z.literal('heading'),
attrs: z.object({
identifier: z.string(),
level: z.number(),
}),
content: z.array(
Te
)
}).transform(({ attrs, content }): Heading => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Heading',
level: attrs.level,
descriptors: [],
children: content
}
});

export const ParagraphDto = z.object({
type: z.literal('paragraph'),
attrs: z.object({
identifier: z.string(),
}),
content: z.array(TextDto)
}).transform(({ attrs, content }): Paragraph => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Paragraph',
descriptors: [],
children: content
}
});

export const DocumentDto = z.object({
type: z.literal('doc'),
attrs: z.object({
identifier: z.string().nullable(),
}),
content: z.array(
z.union([HeadingDto, ParagraphDto])
)
}).transform(({ attrs, content }): Document => {
return {
id: attrs.identifier,
type: 'https://spec.nldoc.nl/Resource/Document',
descriptors: [],
children: content
}
}
3 Replies
Steve
SteveOP5mo ago
This kills my types at the transform in DocumentDto, as the union seem to block correct typing of it.
Scott Trinh
Scott Trinh5mo ago
What's th etype of Document? Does it work if you don't specify the type?

Did you find this page helpful?