【问题标题】:How type JSON schema where array contains objects?数组包含对象的 JSON 模式如何输入?
【发布时间】:2021-11-08 08:53:10
【问题描述】:

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

  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.

我做错了什么?

【问题讨论】:

  • 泛型类型JSONSchemaType的定义是什么?
  • 它来自 import Ajv,{ JSONSchemaType } from 'ajv'。

标签: typescript jsonschema ajv


【解决方案1】:

我试过这个..你能检查它是否适合你......

interface ExpectedBody3 {
    push: IPush
}

interface IPush {
    changes: IOld[]
}

interface IOld {
    old: string
}

【讨论】:

  • 谢谢!由于缺乏知识,我似乎在 TypeScript 类型上犯了一个错误。我使用了更改:[{ old: string, }],当我应该使用更改时:{ old: string, }[]
  • 没关系,错误会把我们带到一些地方.. :-)
猜你喜欢
  • 2019-11-25
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多