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()