在数组包含对象的位置如何类型化JSON模式? [英] How type JSON schema where array contains objects?

查看:7
本文介绍了在数组包含对象的位置如何类型化JSON模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有包含字符串的数组的以下架构没有错误:

  interface ExpectedBody2 {
    push: {
      changes: string[]
    }
  }
  
  const expectedBodySchema2: JSONSchemaType<ExpectedBody2> = {
    type: 'object',
    properties: {
      push: {
        type: 'object',
        properties: {
          changes: {
            type: 'array',
            items: {
              type: 'string',
            },
          },
        },
        required: ['changes']
      }
    },
    required: ['push'],
  }

使用包含对象的数组略微修改的架构有错误:

  interface ExpectedBody3 {
    push: {
      changes: [{
        old: string,
      }]
    }
  }
  
  const expectedBodySchema3: JSONSchemaType<ExpectedBody3> = {
    type: 'object',
    properties: {
      push: {
        type: 'object',
        properties: {
          changes: {
            type: 'array',
            items: {
              type: 'object',
              properties: {
                old: {
                  type: 'string'
                },
              },
              required: ['old']
            },
          },
        },
        required: ['changes']
      }
    },
    required: ['push'],
  }
Type '{ type: "object"; properties: { push: { type: "object"; properties: { changes: { type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }; }; required: "changes"[]; }; }; required: "push"[]; }' is not assignable to type 'UncheckedJSONSchemaType<ExpectedBody3, false>'.
  The types of 'properties.push' are incompatible between these types.
    Type '{ type: "object"; properties: { changes: { type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }; }; required: "changes"[]; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<{ changes: [{ old: string; }]; }, false> & { const?: { changes: [{ old: string; }]; } | undefined; enum?: readonly { changes: [{ ...; }]; }[] | undefined; default?: { ...; } | undefined; })'.
      The types of 'properties.changes' are incompatible between these types.
        Type '{ type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<[{ old: string; }], false> & { const?: [{ old: string; }] | undefined; enum?: readonly [{ old: string; }][] | undefined; default?: [...] | undefined; })'.
          Type '{ type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }' is not assignable to type '{ type: "array"; items: readonly [UncheckedJSONSchemaType<{ old: string; }, false> & { const?: { old: string; } | undefined; enum?: readonly { old: string; }[] | undefined; default?: { ...; } | undefined; }] & { ...; }; minItems: 1; } & { ...; } & { ...; } & { ...; } & { ...; }'.
            Property 'minItems' is missing in type '{ type: "array"; items: { type: string; properties: { old: string; }; required: string[]; }; }' but required in type '{ type: "array"; items: readonly [UncheckedJSONSchemaType<{ old: string; }, false> & { const?: { old: string; } | undefined; enum?: readonly { old: string; }[] | undefined; default?: { ...; } | undefined; }] & { ...; }; minItems: 1; }'.ts(2322)
json-schema.d.ts(41, 5): 'minItems' is declared here.

我做错了什么?

推荐答案

我试过了..您能检查一下它对您是否有效吗...

interface ExpectedBody3 {
    push: IPush
}

interface IPush {
    changes: IOld[]
}

interface IOld {
    old: string
}

这篇关于在数组包含对象的位置如何类型化JSON模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆