Magic SQL type parse into zod schema

wondering if it's possible to parse a value from the select using magic sql into a zod schema to parse into validated object you need to call the schema.parse() function, however the magic sql only give typehints
const timeSchema = z.object({
joinTime: z
.string()
.datetime({ offset: true })
.transform((val) => new Date(val)),
leaveTime: z
.string()
.datetime({ offset: true })
.transform((val) => new Date(val))
})

const arrayTimeSchema = z.array(timeSchema)

// this will not work since it only give type hint into the returned object but doens't actually pase it
db.select({
timeData : sql<z.infer<typeof timeSchema>>`SQL QUERY`
})
const timeSchema = z.object({
joinTime: z
.string()
.datetime({ offset: true })
.transform((val) => new Date(val)),
leaveTime: z
.string()
.datetime({ offset: true })
.transform((val) => new Date(val))
})

const arrayTimeSchema = z.array(timeSchema)

// this will not work since it only give type hint into the returned object but doens't actually pase it
db.select({
timeData : sql<z.infer<typeof timeSchema>>`SQL QUERY`
})
it's not a big problem for a single query selector, but the thing is that actually i'm using it to aggregate an array
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server