How can verify minItems based on other field by AJV?

Viewed 16

I have a schema, and using AJV to verify object. Now I can check required some field accords to other field value. But I want to check more than 1 field, how I can do it?

Schema:

{
    type: 'object',
    required: [
      'notify',
    ],
    additionalProperties: true,
    properties: {
      notify: { type: 'array' },
      mode: { type: 'number', enum: [0, 1, 2, 3] }
    },
    if: {
      properties: {
        mode: {
          enum: [1, 2],
        },
      },
    },
    then: {
      required: ['temperature'],
    },
    else: {
      if: {
        properties: {
          mode: {
            const: 3,
          },
        },
      },
      then: {
        required: ['temperature', 'humidity'],
      },
    },
  }

this schema can check if mode: 1 or 2 then require temperature else if mode: 3 then require both temperature and humidity.

But I want to check notify length accords to mode. I tried to add if else in notify defincation but it do not work:

{
    type: 'object',
    required: [
      'notify',
    ],
    additionalProperties: true,
    properties: {
      notify: {
        type: 'array',
        if: {
          properties: {
            mode: {
              enum: [1, 2],
            },
          },
        },
        then: {
          minItems: 1,
        },
        else: {
          minItems: 0,
        },
      },
      mode: { type: 'number', enum: [0, 1, 2, 3] }
    },
    if: {
      properties: {
        mode: {
          enum: [1, 2],
        },
      },
    },
    then: {
      required: ['temperature'],
    },
    else: {
      if: {
        properties: {
          mode: {
            const: 3,
          },
        },
      },
      then: {
        required: ['temperature', 'humidity'],
      },
    },
  }
0 Answers
Related