You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an object. I want to validate that the given string is a keyof that object I have.
I tried the following:
z.enum(Object.keys(Items)) z.enum(Object.keys(Items) as (keyof typeof Items)[])
but they won't work. There is this answer elsewhere but it won't work: #839 (comment)
How can I achieve it in the best way?
There is the z.object({}).keyof(). Should Zod have the z.keyof() to achieve what I am trying to do? Seems a good thing to have in the library.
Edit: I managed to get this working z.custom<keyof typeof Items>((v) => Object.keys(Items).includes(v as string)), but I still think we should have z.keyof!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an object. I want to validate that the given string is a keyof that object I have.
I tried the following:
z.enum(Object.keys(Items))z.enum(Object.keys(Items) as (keyof typeof Items)[])but they won't work. There is this answer elsewhere but it won't work: #839 (comment)
z.object({}).keyof(). Should Zod have thez.keyof()to achieve what I am trying to do? Seems a good thing to have in the library.Edit: I managed to get this working
z.custom<keyof typeof Items>((v) => Object.keys(Items).includes(v as string)), but I still think we should havez.keyof!Beta Was this translation helpful? Give feedback.
All reactions