AJV schema validation for nested object

爷,独闯天下 提交于 2019-12-12 04:46:55

问题


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"
      ]
    }
  }
}


来源:https://stackoverflow.com/questions/44400237/ajv-schema-validation-for-nested-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!