Skip to content

Case insensitive keys on objects #2920

Discussion options

You must be logged in to vote

Is this what you are looking for?

import _ from 'lodash'

const jsonObjSchema = z.record( z.string(), z.any() )
const jsonToObj = z.string().transform( string => JSON.parse( string ) ).pipe( jsonObjSchema )
const jsonToCaseInsensitiveKeysRecord = jsonToObj.transform( obj => _.mapKeys( obj, ( val, key ) => _.camelCase( key ) ) )
const User = z.object( { name: z.string() } )
const jsonToUser = jsonToCaseInsensitiveKeysRecord.pipe( User )

console.log( jsonToUser.parse( '{"name": "foo"}' ) ) // { name: 'foo' }
console.log( jsonToUser.parse( '{"Name": "foo"}' ) ) // { name: 'foo' }

If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. …

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