shop
Version:
15 lines (11 loc) • 499 B
text/typescript
import { z, ZodTypeAny } from 'zod'
export const deserialize = <TZod extends ZodTypeAny>(target: unknown, zType: TZod): z.TypeOf<TZod> => {
if (typeof target === 'object' && target !== null && target.hasOwnProperty('body')) {
target = JSON.parse((<{ body: string }>target).body)
}
const parse = zType.safeParse(target)
if (parse.success === false) {
throw new Error(`Body is not in correct structure: '${parse.error}'`)
}
return parse.data
}