-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Description
Consider the case where you want to build an API response with camelCase properties in JS, and then send it over the wire with snake_case properties:
const Post =
z.object({
id: z.string(),
body: z.string(),
userId: z.string(),
})
.transform(
({ userId, ...rest }) => ({ ...rest, user_id: userId })
)
.pipe(z.object({
id: z.string(),
body: z.string(),
user_id: z.string(),
}))You might want to do .partial() if you have another API that allows the user to pick a selection of fields from a Post:
const PostSelection = Post.partial() // plz?One could create the following helper function for this. But this all feels very cumbersome. Maybe in an alternate universe, Zod has a more ergonomic way to do all of this?
function pipelinePartial(schema) {
if (schema instanceof z.ZodObject) {
return schema.partial()
}
if (schema instanceof z.ZodPipeline) {
return z.pipeline(pipelinePartial(schema._def.in), pipelinePartial(schema._def.out))
}
if (schema instanceof z.ZodEffects) {
return z.effects(pipelinePartial(schema._def.schema), schema._def.effect)
}
throw new Error(`unsupported schema type: ${schema.constructor.name}`)
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels