小编Lzo*_*zok的帖子

有没有办法拥有一个包含其中一些文字的对象数组?

我正在考虑使用 zod 进行以下验证,但我不知道如何做到这一点(或者是否可以使用 zod)。我想要一个对象数组,所有对象都具有相同的形状,其中一些带有文字道具,我需要它们始终存在于数组中。

示例:我始终需要在数组中包含名称为 required1 和 required2 的对象,然后是具有相同形状的其他对象选项。

[
    {
      name: z.literal('required1'),
      otherprop: z.number()
    },
    {
      name: z.literal('required2'),
      otherprop: z.number()
    },
    // I want to include one or more of the following too (optionals).
    {
      name: z.string(),
      otherprop: z.number()
    },
]
Run Code Online (Sandbox Code Playgroud)

由于缺少 required2,因此需要抛出另一个示例

[
    {
      name: z.literal('required1'),
      otherprop: z.number()
    },
    // I want to include one or more of the following too.
    {
      name: z.string(),
      otherprop: z.number()
    },
]
Run Code Online (Sandbox Code Playgroud)

有什么线索吗?

typescript zod

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

typescript ×1

zod ×1