Zod:如何允许架构中未指定的对象字段的传递?

问题描述 投票:0回答:0

我正在尝试使用

zod
验证我的请求数据的一个子集。但是,它会过滤掉我没有明确指定的所有内容。

const { z } = require('zod');

const schema = z.object({
  params: z.object({ dependent_id: z.string() }),
})

const req = {
  params: { dependent_id: "blah", bar: "baz" },
  body: { foo: "bar" },
  query: {}
}

const test = async () => {
 const { params, body, query } = await schema.parseAsync(req);
  console.log("params: ", params)
  console.log("body: ", body)
  console.log("query: ", query)
}

test()

这是打印出来的:

params:  { dependent_id: 'blah' }         // expect it to print { dependent_id: "blah", "bar": baz }
body:  undefined                          // expect it to print { foo: "bar" }
query:  undefined                         // expect it to print {},

有没有办法告诉它忽略不属于模式的所有内容?

javascript express zod
© www.soinside.com 2019 - 2024. All rights reserved.