Skip to content
Discussion options

You must be logged in to vote

Is this what you are looking for?

import { z } from 'zod'
import _ from 'lodash'

const snakeCaseKeys = ( data: object ) => _.mapKeys( data, ( val, key ) => _.snakeCase( key ) )

const Post = z.object( {
    id: z.string(),
    body: z.string(),
    userId: z.string(),
} )

console.log(
    Post.parse( {
        id: '1',
        body: 'body',
        userId: '1',
    } )
)
// {
//     id: "1",
//     body: "body",
//     userId: "1"
// }

const snakeCasedPost = Post.transform( snakeCaseKeys )
console.log(
    snakeCasedPost.parse( {
        id: '1',
        body: 'body',
        userId: '1',
    } )
)
// {
//     id: "1",
//     body: "body",
//     user_id: "1"
// }

const snakeCasedPart…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2470 on September 25, 2023 15:35.