简体   繁体   中英

Require property to all childen in json schema v4

Let say I have a schema like this:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 4,
      "maxLength": 50,
      "pattern": "[a-zA-Z0-9\\-\\s]+"
    },
    "object": {
      "type": "object"
    }
  },
  "required": [
    "name",
    "object"
  ],
  "additionalProperties": false
}

Where 'object' can be an instance of any valid JSON object, but I want to enforce that every child object of the top-level object MUST have a property of 'sort_default' with values of enum ['ASC','DESC]

So a valid example instance might could be:

{
  "name": "Example Object",
  "object": {
    "prop1": {
      "value": 2547,
      "sort_default": "ASC"
    },
    "prop2": {
      "value": 3658,
      "sort_default": "DESC",
      "prop2.1": {
        "value": 147,
        "sort_default": "ASC"
      }
    }
  }
}

but if an object node were missing the sort_default property, it would not validate.

Is this possible?

是的,您可以使用对象架构的递归定义, 如此处所述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM