zod
zod copied to clipboard
Is it possible to custom validate the allowed keys when. using .passthrough or .strip?
I first want to congratulate you to a very well designed and implemented library!
I'm currently evaluating the use of zod in my application and have a small problem when having to parse an object that can contain optional keys. I'm using .passthrough to allow the keys to stay in the object but would like to custom validate the keys or at least make sure that the key names and types are valid. The .catchall only allows to specify a type of all optional keys but I would require to custom validate each optional key.
import {z} from 'zod';
// mandatory user information
const user = z.object({
id: z.number(),
name: z.string(),
});
// additional keys like:
// string: key in the format /^add_\d{3}_s$/
// number: key in the format /^add_\d{3}_n$/
add_001_s: z.string()
add_002_s: z.string()
add_003_n: z.number()
add_004_n: z.number()
Can you give an example of what you're trying to do? I'm not following I'm afraid.
I updated the original question with an example
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.