zod
zod copied to clipboard
Allow `.extract`/`.exclude` for `nativeEnum`s
Hey there! We're swapping to zod in our library for validation and we have the following use case:
// some-dependency
enum Something {
X,
Y,
Z
}
We're using this numeric enum from our dependency (which has more like 14 available members), and we happen to want, say, 6 of them as allowed values to a record field.
My instinct was to use z.nativeEnum(Something).extract(...)
, but I was surprised to learn it doesn't exist. My next idea was to do what I thought was the next most-correct thing, which is z.enum([Something.X, Something.Y, ...])
, but I was quickly reminded it only supports strings (fair).
Which leaves me with using literals. https://github.com/colinhacks/zod/issues/2686#issuecomment-2073511939 would be of big help already, since I ended up having to do something like this: https://github.com/discordjs/discord.js/blob/6d44a77180fc52e6b87278b8054c54cb3a501600/packages/builders/src/interactions/slashCommands/Assertions.ts#L61-L75
I think ultimately though, it'd be ideal if "native enums" supported extract & exclude. I'd be willing to try to implement this, just figured I'd make an issue first in case there's a clear-cut sort of blocker for something like this.