最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make all properties optional in a Zod schema based on a specific product availability flag? - Stack Overflow

programmeradmin4浏览0评论

I have a Zod schema that validates product properties for an e-merce application. In my schema, I currently have a property called isLimitedEdition which indicates whether a product is available in limited quantities. However, when isLimitedEdition is set to true, I want all other properties in the schema to bee optional.

Here's the existing schema structure:

const productProperties = z.object({
  name: z.string().nonempty(),
  description: z.string().nonempty(),
  price: z.number().positive(),
  category: z.string().nonempty(),
  brand: z.string().nonempty(),
  isFeatured: z.boolean().default(false),
  isLimitedEdition: z.boolean().default(false),
});

In this schema, I want to achieve a behavior where all properties (name, description, price, category, brand, isFeatured) bee optional if isLimitedEdition is set to true.

How can I modify this schema to achieve the desired behavior? I would appreciate any guidance or code examples to help me implement this logic correctly. Thank you in advance!

I tried the refine method but it didn't work

I have a Zod schema that validates product properties for an e-merce application. In my schema, I currently have a property called isLimitedEdition which indicates whether a product is available in limited quantities. However, when isLimitedEdition is set to true, I want all other properties in the schema to bee optional.

Here's the existing schema structure:

const productProperties = z.object({
  name: z.string().nonempty(),
  description: z.string().nonempty(),
  price: z.number().positive(),
  category: z.string().nonempty(),
  brand: z.string().nonempty(),
  isFeatured: z.boolean().default(false),
  isLimitedEdition: z.boolean().default(false),
});

In this schema, I want to achieve a behavior where all properties (name, description, price, category, brand, isFeatured) bee optional if isLimitedEdition is set to true.

How can I modify this schema to achieve the desired behavior? I would appreciate any guidance or code examples to help me implement this logic correctly. Thank you in advance!

I tried the refine method but it didn't work

Share Improve this question asked Jul 1, 2023 at 9:17 Mustafa Ahmed MohammedMustafa Ahmed Mohammed 1032 silver badges9 bronze badges 1
  • 2 You could use z.discriminatedUnion with isLimitedEdition as the key. – Keith Commented Jul 1, 2023 at 9:44
Add a ment  | 

1 Answer 1

Reset to default 4

You can achieve this with discriminatedUnion:

const productProperties = z.object({
  name: z.string().nonempty(),
  description: z.string().nonempty(),
  price: z.number().positive(),
  category: z.string().nonempty(),
  brand: z.string().nonempty(),
  isFeatured: z.boolean().default(false),
})

const product = z.discriminatedUnion('isLimitedEdition', [
  productProperties.extend({ isLimitedEdition: z.literal(false) }),
  productProperties.partial().extend({ isLimitedEdition: z.literal(true) }),
])

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论