zod icon indicating copy to clipboard operation
zod copied to clipboard

Support for schemas that is an enum of the keys of an object?

Open bravely opened this issue 3 years ago • 3 comments

Essentially what I'm looking for would be something akin to this:

const Inputs = Z.object({
  email: Z.string().email(),
  password: Z.string().min(8),
  username: Z.string().min(3),
  id: Z.string().uuid(),
  base: Z.unknown().optional(),
})
const InputFields: Z.ZodEnum<['email', 'password', 'username', 'id', 'base']> = Z.keyof(Inputs)

Is any form of the above currently possible, and if not, would anything bar its creation?

bravely avatar Apr 15 '22 03:04 bravely

Hmmm.. Nothing comes to mind already. One tricky thing here is that you could have a schema like z.object({}).passthrough() which should make the keyof type never, which might be a little tricky.

I don't think there is anything impossible here, and PRs are welcome!

scotttrinh avatar Apr 15 '22 18:04 scotttrinh

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 Jun 14 '22 19:06 stale[bot]

Hello, i'm also in need for this feature. As i need to use an enum to validate user input based only on a provided schema for zodios library. Could you point to me where to start or check if i want to propose a Pull Request for this .

ecyrbe avatar Jun 16 '22 17:06 ecyrbe

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 Aug 16 '22 00:08 stale[bot]