如何在数组包含对象的地方键入 JSON 模式?
How type JSON schema where array contains objects?
以下具有包含字符串的数组的架构没有错误:
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
}
以下具有包含字符串的数组的架构没有错误:
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
}