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

javascript - How to Extend a Zod Object with Another Object and Pick Certain Entries? - Stack Overflow

programmeradmin1浏览0评论

I'm using Zod, a TypeScript schema validation library, to validate objects in my application. I have a scenario where I need to validate an object with nested properties and extend it with another object while picking only certain entries from the second object.

Here's what I'm trying to achieve:

logValidation.pick({
        level: true,
        event: true,
        userId: true,
        ipAddress: true,
        statusCode: true,
}).extend(validation.pick({
    limit: true,
    offset: true
}))

In the above code:

logValidation represents the schema for validating log objects.

  • I want to extend logValidation with another object containing pagination parameters (limit and offset).
  • However, I want to pick only limit and offset from the second object to extend logValidation.

But this code doesn't work as expected. Zod's extend method doesn't seem to support picking certain entries from the extending object.

Is there a way to achieve this functionality with Zod? I

I'm using Zod, a TypeScript schema validation library, to validate objects in my application. I have a scenario where I need to validate an object with nested properties and extend it with another object while picking only certain entries from the second object.

Here's what I'm trying to achieve:

logValidation.pick({
        level: true,
        event: true,
        userId: true,
        ipAddress: true,
        statusCode: true,
}).extend(validation.pick({
    limit: true,
    offset: true
}))

In the above code:

logValidation represents the schema for validating log objects.

  • I want to extend logValidation with another object containing pagination parameters (limit and offset).
  • However, I want to pick only limit and offset from the second object to extend logValidation.

But this code doesn't work as expected. Zod's extend method doesn't seem to support picking certain entries from the extending object.

Is there a way to achieve this functionality with Zod? I

Share Improve this question asked May 11, 2024 at 11:04 hantorenhantoren 1,2757 gold badges27 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You're using extend (which expects you to give it an object of properties, and will make a schema) when you should be using merge (which expects to to give it a schema).

See https://github./colinhacks/zod?tab=readme-ov-file#extend

发布评论

评论列表(0)

  1. 暂无评论