Home > Blockchain >  How type JSON schema where array contains objects?
How type JSON schema where array contains objects?

Time:11-09

The following schema with an array that contains strings has no errors:

  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'],
  }

This slightly modified schema with an array that contains objects has errors:

  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.

What am I doing wrong?

CodePudding user response:

I tried this.. can you check if it works for you...

interface ExpectedBody3 {
    push: IPush
}

interface IPush {
    changes: IOld[]
}

interface IOld {
    old: string
}
  • Related