Steve
Steve
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I was wondering if there is a certain w...
😦 too bad
7 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I was wondering if there is a certain w...
The first optimization of this is ofcourse rewriting it to this:
export const TipTapTextDto = z
.object({
type: z.literal('text'),
text: z.string(),
marks: z
.array(TipTapMarkDto)
.default([])
.refine((marks) => marks.some((mark) => mark.type === 'identifier'), {
message: 'Should have an identifier',
}),
})
.strict()

export type TipTapTextDto = z.infer<typeof TipTapTextDto>
export const TipTapTextDto = z
.object({
type: z.literal('text'),
text: z.string(),
marks: z
.array(TipTapMarkDto)
.default([])
.refine((marks) => marks.some((mark) => mark.type === 'identifier'), {
message: 'Should have an identifier',
}),
})
.strict()

export type TipTapTextDto = z.infer<typeof TipTapTextDto>
7 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
sweet
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
You're a genius
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
type DocumentContentMember = TipTapParagraphDto | TipTapHeadingDto

export const TipTapDocumentDto = z
.object({
type: z.literal('doc'),
attrs: z.object({
identifier: z.string(),
}),
/**
* @TODO remove logic for filtering out unsupported element types
*/
content: z
.array(z.union([TipTapParagraphDto, TipTapHeadingDto]).nullable().catch(null))
.default([])
.transform((xs: (DocumentContentMember | null)[]): DocumentContentMember[] => {
return xs.filter((x): x is DocumentContentMember => x !== null)
}),
})
.strict()
export type TipTapDocumentDto = z.infer<typeof TipTapDocumentDto>
type DocumentContentMember = TipTapParagraphDto | TipTapHeadingDto

export const TipTapDocumentDto = z
.object({
type: z.literal('doc'),
attrs: z.object({
identifier: z.string(),
}),
/**
* @TODO remove logic for filtering out unsupported element types
*/
content: z
.array(z.union([TipTapParagraphDto, TipTapHeadingDto]).nullable().catch(null))
.default([])
.transform((xs: (DocumentContentMember | null)[]): DocumentContentMember[] => {
return xs.filter((x): x is DocumentContentMember => x !== null)
}),
})
.strict()
export type TipTapDocumentDto = z.infer<typeof TipTapDocumentDto>
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
This works brilliantly, thanks @Scott Trinh !
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
I am afraid it would, let me test
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
Would it modify my output type to also include null or not?
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
.catch would be possible for now.
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
I want to filter it out of the input for now
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
I would probably use catch and transform with a filter
Transform would be executed on the output, right?
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
import { z } from 'zod'

const SimpleMarkDto = <T extends string>(typeName: T) =>
z.object({
type: z.literal(typeName),
})

const IdentifierMarkDto = z
.object({
type: z.literal('identifier'),
attrs: z.object({
identifier: z.string(),
}),
})
.strict()

const BoldMarkDto = SimpleMarkDto('bold')
const ItalicMarkDto = SimpleMarkDto('italic')
const StrikeMarkDto = SimpleMarkDto('strike')
const CodeMarkDto = SimpleMarkDto('code')
const SubscriptMarkDto = SimpleMarkDto('subscript')
const SuperscriptMarkDto = SimpleMarkDto('superscript')
const UnderlineMarkDto = SimpleMarkDto('underline')

export const TipTapMarkDto = z.union([
SuperscriptMarkDto,
SubscriptMarkDto,
CodeMarkDto,
StrikeMarkDto,
BoldMarkDto,
ItalicMarkDto,
UnderlineMarkDto,
IdentifierMarkDto,
])
export type TipTapMarkDto = z.infer<typeof TipTapMarkDto>

export const TipTapTextDto = z
.object({
type: z.literal('text'),
text: z.string(),
marks: z.array(TipTapMarkDto).default([]),
})
.strict()
.refine((dto) => dto.marks.some((mark) => mark.type === 'identifier'), {
message: 'Should have an identifier',
})
export type TipTapTextDto = z.infer<typeof TipTapTextDto>

const content = z.array(TipTapTextDto).default([])

export const TipTapHeadingDto = z
.object({
type: z.literal('heading'),
attrs: z.object({
identifier: z.string(),
level: z.number(),
}),
content,
})
.strict()
export type TipTapHeadingDto = z.infer<typeof TipTapHeadingDto>

export const TipTapParagraphDto = z
.object({
type: z.literal('paragraph'),
attrs: z.object({
identifier: z.string(),
}),
content,
})
.strict()
export type TipTapParagraphDto = z.infer<typeof TipTapParagraphDto>
import { z } from 'zod'

const SimpleMarkDto = <T extends string>(typeName: T) =>
z.object({
type: z.literal(typeName),
})

const IdentifierMarkDto = z
.object({
type: z.literal('identifier'),
attrs: z.object({
identifier: z.string(),
}),
})
.strict()

const BoldMarkDto = SimpleMarkDto('bold')
const ItalicMarkDto = SimpleMarkDto('italic')
const StrikeMarkDto = SimpleMarkDto('strike')
const CodeMarkDto = SimpleMarkDto('code')
const SubscriptMarkDto = SimpleMarkDto('subscript')
const SuperscriptMarkDto = SimpleMarkDto('superscript')
const UnderlineMarkDto = SimpleMarkDto('underline')

export const TipTapMarkDto = z.union([
SuperscriptMarkDto,
SubscriptMarkDto,
CodeMarkDto,
StrikeMarkDto,
BoldMarkDto,
ItalicMarkDto,
UnderlineMarkDto,
IdentifierMarkDto,
])
export type TipTapMarkDto = z.infer<typeof TipTapMarkDto>

export const TipTapTextDto = z
.object({
type: z.literal('text'),
text: z.string(),
marks: z.array(TipTapMarkDto).default([]),
})
.strict()
.refine((dto) => dto.marks.some((mark) => mark.type === 'identifier'), {
message: 'Should have an identifier',
})
export type TipTapTextDto = z.infer<typeof TipTapTextDto>

const content = z.array(TipTapTextDto).default([])

export const TipTapHeadingDto = z
.object({
type: z.literal('heading'),
attrs: z.object({
identifier: z.string(),
level: z.number(),
}),
content,
})
.strict()
export type TipTapHeadingDto = z.infer<typeof TipTapHeadingDto>

export const TipTapParagraphDto = z
.object({
type: z.literal('paragraph'),
attrs: z.object({
identifier: z.string(),
}),
content,
})
.strict()
export type TipTapParagraphDto = z.infer<typeof TipTapParagraphDto>
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
for context:
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
In the content property, I have an array of a union of objects, also having a type property. And for now I just don't want to bother with anything that does not have type being equal to 'heading' or 'paragraph'
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
export const TipTapDocumentDto = z
.object({
type: z.literal('doc'),
attrs: z.object({
identifier: z.string(),
}),
content: z.array(z.union([TipTapParagraphDto, TipTapHeadingDto])).default([]),
})
.strict()
export type TipTapDocumentDto = z.infer<typeof TipTapDocumentDto>
export const TipTapDocumentDto = z
.object({
type: z.literal('doc'),
attrs: z.object({
identifier: z.string(),
}),
content: z.array(z.union([TipTapParagraphDto, TipTapHeadingDto])).default([]),
})
.strict()
export type TipTapDocumentDto = z.infer<typeof TipTapDocumentDto>
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
Oh yeah, sorry, correct.
23 replies
ZZod
Created by Steve on 9/12/2024 in #questions
Steve - I have this unique case where I want to...
Currently I am thinking of using z.preprocess, would this be the way to go?
23 replies
ZZod
Created by Steve on 9/4/2024 in #questions
Stephan Meijer - What was the deal with union a...
4 replies