AJV schema validation for nested object

2019-08-17 23:48发布

问题:

Functions returns object which looks something like this:

    {
        "answer": {
           "vehicle_type": 1,
           "message": "Car"
        },
        "model": "VW",
        "color": "red"
    }

'Answer' object is always there. Other fields are there based on 'vehicle_type'.

E.g.

if vehicle_type = 1 there are 'model' and 'color'.

if vehicle_type = 2 there are 'engine_count', 'seat_count' and 'wing_count'.

I am trying to write JSON-schema which I will use to validate returned object.

I would like to set 'model' and 'color' as required properties if 'vehicle_type' is 1. And if 'vehicle_type' is 2, then 'engine_count', 'seat_count' and 'wing_count' are required.

I am using AJV (https://github.com/epoberezkin/ajv) schema validator.

For me, it's problematic because vehicle_type is nested inside 'answer', and properties which I want to mark as required are on the parent object. In other words, 'validation_type' is not on the same level as 'model' or 'engine_count'.

I already several different approached... I also tried with ajv-keywords (switch, if/else/then) but i didn't have any luck

Any ideas?

回答1:

You can use "oneOf" property for that.

Where you will have "one of" a vehicle type 1, or a type 2. Type 1 has certain required properties while type 2 has different required properties.

For example:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://some.site.somewhere/entry-schema#",
  "oneOf": [
    {"$ref": "#/definitions/type1"},
    {"$ref": "#/definitions/type2"}
  ],
  "definitions": {
    "type1": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "object",
          "properties": {
            "vehicle_type": {
              "type": "integer",
              "enum": [1]
            },
            "message": {
              "type": "string"
            }
          }
        },
        "model": {
          "type": "string"
        },
        "color": {
          "type": "string"
        }
      },
      "required": [
        "model",
        "color"
      ]
    },
    "type2": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "object",
          "properties": {
            "vehicle_type": {
              "type": "integer",
              "enum": [2]
            },
            "message": {
              "type": "string"
            }
          }
        },
        "engine_count": {
          "type": "integer"
        },
        "seat_count": {
          "type": "integer"
        },
        "wing_count": {
          "type": "integer"
        }
      },
      "required": [
        "engine_count",
        "seat_count",
        "wing_count"
      ]
    }
  }
}