zod icon indicating copy to clipboard operation
zod copied to clipboard

Allow specific properties to be `.required`

Open ftzi opened this issue 3 years ago • 4 comments

I have a mySchema with ~10 properties.

In my API, I use mySchema.partial() as all props are optional, BUT one.

I wish I could simply mySchema.partial().required({ myRequired: true }), as partial allows.

It would be a better solution than .partial({ 9 props }) or merging with the mySchema.shape.myRequired.

ftzi avatar May 30 '22 02:05 ftzi

+1 fo this, it is very common scenario.

For example I have a orderSchema and I have an updateOrder() function that takes orderSchema.partial() (listing each key in the partial() function would be too tedious), but I want the orderSchema.shape.id property to be required, so I would like to do orderSchema.partial().required({id:true}) instead of the hack I am currently using orderSchema.partial().omit({id:true}).extend({id: orderIdSchema})

ivosabev avatar Jun 28 '22 04:06 ivosabev

+1 -- would like this so zod can mirror the TypeScript Required utility type https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype

Antman261 avatar Jul 13 '22 05:07 Antman261

+1, I also needed this feature, thanks for your workaround @ivosabev

yannbcf avatar Aug 04 '22 17:08 yannbcf

Just implemented it in #1315.

Can you please take a look, @colinhacks?

ftzi avatar Aug 05 '22 13:08 ftzi

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 Oct 04 '22 19:10 stale[bot]

Up

ftzi avatar Oct 04 '22 19:10 ftzi

Merged in #1315

ftzi avatar Oct 05 '22 03:10 ftzi