Given this schema, mutate the key "user_id" to "user":
const schema = z.object({
user_id: z.string() // <--- some method here to mutate the key,
});
let input = { user_id: 1234qwer5678 }
let output = schema.parse( input )
console.log(output) // returns { id: 1234qwer1234 }
You can use transform
to modify the data after it has been parsed by the underlying schema. So for example:
const schema = z.object({
user_id: z.string(),
}).transform(({ user_id, ...rest }) => ({
user: user_id,
...rest
});
This will accept an object with user_id
as the field but when parsing will return the value under the user
key instead.