我尝试在zod中创建一个这样的对象类型:
{
default: string,
[string]: string,
}
我试过使用z.union
来合并z.object
和z.record
,但它并没有像我预期的那样有效。
const LocaleString = z.union([
z
.object({
default: z.string(),
})
.required(),
z.record(z.string()),
]);
LocaleString.safeParse({
testing: 'abc',
});
/**
* it will return `{ success: true, data: { testing: 'abc' } }`,
* where I expect it to fail when the data is without default value
*/
1条答案
按热度按时间pcww981p1#
我发现了这一点,它对我的案子很有效。