Skip to content
Discussion options

You must be logged in to vote

Is this what you are looking for?

https://github.com/colinhacks/zod#record-key-type

when passing two arguments, valueType is the second argument

const REG = /^[a-z]([A-Za-z0-9]+)$/
const schema = z.record(
    z.string().regex( REG, 'Key must be camelCase' ),
    z.string(),
)

console.log( schema.parse( { camelCase: 'value' } ) ) // { camelCase: 'value' }

const result = schema.safeParse( { NotCamelCase: 'value' } )
!result.success && console.log( result.error.issues )
// [
//     {
//         validation: "regex",
//         code: "invalid_string",
//         message: "Key must be camelCase",
//         path: [ "NotCamelCase" ]
//     }
// ]

If you found my answer satisfactory, please …

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
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 #2706 on September 25, 2023 13:51.