zod icon indicating copy to clipboard operation
zod copied to clipboard

Is it possible to custom validate the allowed keys when. using .passthrough or .strip?

Open doberkofler opened this issue 1 year ago • 2 comments

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

doberkofler avatar Aug 22 '22 14:08 doberkofler

Can you give an example of what you're trying to do? I'm not following I'm afraid.

colinhacks avatar Sep 06 '22 04:09 colinhacks

I updated the original question with an example

doberkofler avatar Sep 06 '22 04:09 doberkofler

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.

stale[bot] avatar Nov 05 '22 05:11 stale[bot]