创建一个具有一些动态键和一些静态键的架构,使用zod。

huangapple go评论107阅读模式
英文:

Create a schema that has some dynamic keys and some static keys with zod

问题

我想要实现类似这种类型的结果,但使用zod。

  1. type a = {
  2. [key: string]: string;
  3. name: string;
  4. surname: string;
  5. }

因此,这是一个具有一些静态字段但接受其他动态字段的类型。

我知道 z.record(z.string(), z.string()),但似乎它只能重新创建类型只有动态键的情况,如:

  1. type zod = {
  2. [key: string]: string;
  3. }

我还尝试使用 merge:

  1. const dynamicPropertiesSchema = z.record(z.string());
  2. export const PersonSchema = z.object({
  3. anagraphics: z.object({
  4. name: z.string(),
  5. surname: z.string(),
  6. }).merge(dynamicPropertiesSchema),
  7. notes: z.array(z.string()),
  8. });

但我收到以下错误,强调了 .merge() 参数:

“类型参数 'ZodRecord <ZodString, ZodString>' 无法分配给类型参数 'AnyZodObject'。 类型 'ZodRecord <ZodString, ZodString>' 缺少类型 'ZodObject <any, any, any, { [x: string]: any; }, { [x: string]: any; }>' 的以下属性:_cached,_getCached,shape,strict,以及其他14个.ts(2345)”

总之,我想要类似这样的东西:

  1. const mixedSchema = z.object({
  2. z.record(z.string(), z.string()),
  3. name: z.string(),
  4. surname: z.string(),
  5. });

是否有办法实现这个?
感谢您的帮助!

英文:

I want to achieve a result similar to this type, but using zod.

  1. type a = {
  2. [key: string]: string;
  3. name: string;
  4. surname: string;
  5. }

So a type that has some static fields but accepts other dynamic ones.

I know about z.record(z.string(), z.string()), but it seems like it can only recreate the case where the type only has dynamic keys, as in:

  1. type zod = {
  2. [key: string]: string;
  3. }

I also tried using merge:

  1. const dynamicPropertiesSchema = z.record(z.string());
  2. export const PersonSchema = z.object({
  3. anagraphics: z.object({
  4. name: z.string(),
  5. surname: z.string(),
  6. }).merge(dynamicPropertiesSchema),
  7. notes: z.array(z.string()),
  8. });

But I get the following error highlighting the .merge() parameter:

Argument of type &#39;ZodRecord&lt;ZodString, ZodString&gt;&#39; is not assignable to parameter of type &#39;AnyZodObject&#39;. Type &#39;ZodRecord&lt;ZodString, ZodString&gt;&#39; is missing the following properties from type &#39;ZodObject&lt;any, any, any, { [x: string]: any; }, { [x: string]: any; }&gt;&#39;: _cached, _getCached, shape, strict, and 14 more.ts(2345)

So, all in all, I'd like something like this:

  1. const mixedSchema = z.object({
  2. z.record(z.string(), z.string()),
  3. name: z.string(),
  4. surname: z.string(),
  5. });

Would this be able to be achieved somehow?
Thanks for your help!

答案1

得分: 3

  1. 我认为实现这个的正确方式是使用交集:
  2. ```ts
  3. const schema = z.intersection(z.object({
  4. name: z.string(),
  5. surname: z.string(),
  6. }), z.record(z.string(), z.string()));
  7. type T = z.infer<typeof schema>;
  8. // ^? { name: string; surname: string } & Record<string, string>

类似于在普通的TS类型中使用的方式:

  1. type T = { name: string; surname: string } & Record<string, string>;

Playground

  1. <details>
  2. <summary>英文:</summary>
  3. I believe the correct way to accomplish this would be to use an intersection:
  4. ```ts
  5. const schema = z.intersection(z.object({
  6. name: z.string(),
  7. surname: z.string(),
  8. }), z.record(z.string(), z.string()));
  9. type T = z.infer&lt;typeof schema&gt;;
  10. // ^? { name: string; surname: string } &amp; Record&lt;string, string&gt;

Similar to how you would do it with plain TS types:

  1. type T = { name: string; surname: string } &amp; Record&lt;string, string&gt;;

Playground

huangapple
  • 本文由 发表于 2023年2月23日 22:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定