admin管理员组

文章数量:1287582

I would like to apply an additional "required" property in an array sub schema based on the presence of a property in the root schema. I have my schema set like this:

{
    "$schema": "",
    "type": "object",
    "required":[
       "isParentDependency",
       "subArray"
    ],
    "properties": {
        "isParentDependency": {
            "$id": "#/properties/isParentDependency",
            "type": "boolean"
        },
        "subArray": {
            "$id": "#/properties/subArray",
            "type": "array",
            "items": {
                "$id": "#/properties/subArrayItem",
                "required": ["alwaysRequiredProp"],
                "dependencies": {
                    "isParentDependency":{
                        "required":["requiredPropIfIsParentDependency"]
                    }
                },
                "properties": {
                    "alwaysRequiredProp": {
                        "$id": "#/properties/subArray/items/properties/alwaysRequiredProp",
                        "type": "boolean"
                    },
                    "requiredPropIfIsParentDependency": {
                        "$id": "#/properties/subArray/items/properties/requiredPropIfIsParentDependency",
                        "type": "boolean"
                    }
                }
            }
        }
    }
}

Passing Cases

{
    "isParentDependency": false,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true,
        "requiredPropIfIsParentDependency":true
    }]
}

Failing Cases

{
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}

Clearly this is not going to work but I have been unable to figure out how to make a pointer to the root schema (or apply an if/else type solution with a $ref)

Any guidance greatly appreciated!

I would like to apply an additional "required" property in an array sub schema based on the presence of a property in the root schema. I have my schema set like this:

{
    "$schema": "http://json-schema/draft-07/schema#",
    "type": "object",
    "required":[
       "isParentDependency",
       "subArray"
    ],
    "properties": {
        "isParentDependency": {
            "$id": "#/properties/isParentDependency",
            "type": "boolean"
        },
        "subArray": {
            "$id": "#/properties/subArray",
            "type": "array",
            "items": {
                "$id": "#/properties/subArrayItem",
                "required": ["alwaysRequiredProp"],
                "dependencies": {
                    "isParentDependency":{
                        "required":["requiredPropIfIsParentDependency"]
                    }
                },
                "properties": {
                    "alwaysRequiredProp": {
                        "$id": "#/properties/subArray/items/properties/alwaysRequiredProp",
                        "type": "boolean"
                    },
                    "requiredPropIfIsParentDependency": {
                        "$id": "#/properties/subArray/items/properties/requiredPropIfIsParentDependency",
                        "type": "boolean"
                    }
                }
            }
        }
    }
}

Passing Cases

{
    "isParentDependency": false,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true,
        "requiredPropIfIsParentDependency":true
    }]
}

Failing Cases

{
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}

Clearly this is not going to work but I have been unable to figure out how to make a pointer to the root schema (or apply an if/else type solution with a $ref)

Any guidance greatly appreciated!

Share Improve this question edited Jul 23, 2019 at 8:25 Mike Miller asked Jul 22, 2019 at 7:55 Mike MillerMike Miller 3,1293 gold badges26 silver badges32 bronze badges 3
  • You can't "see up" the tree, so you have to define what you need at the root level and use subschemas. I'm happy to help, but could you clean up your example first please? Not plete JSON, and does not identify which draft it is using (I'm going to assume draft-7). Additionally, please provide a JSON instance for your pass and fail conditions (this will allow me to give you a quicker answer). – Relequestual Commented Jul 22, 2019 at 12:11
  • Thanks very much @Relequestual - I have (hopefully) fleshed out the Q with what you need – Mike Miller Commented Jul 23, 2019 at 8:25
  • Super. Working... – Relequestual Commented Jul 23, 2019 at 8:26
Add a ment  | 

1 Answer 1

Reset to default 11

With JSON Schema, each level of nested properties down the tree cannot see up the tree. Therefore you have to define your conditions at the top most level to which it needs to see. In this case, that's the root level.

Take a look at this schema.

{
  "$schema": "http://json-schema/draft-07/schema#",
  "type": "object",
  "definitions": {
    "subArray": {
      "type": "array",
      "items": {
        "required": [
          "alwaysRequiredProp"
        ],
        "properties": {
          "alwaysRequiredProp": {
            "type": "boolean"
          },
          "requiredPropIfIsParentDependency": {
            "type": "boolean"
          }
        }
      }
    }
  },
  "required": [
    "isParentDependency",
    "subArray"
  ],
  "properties": {
    "isParentDependency": {
      "type": "boolean"
    },
    "subArray": {
      "$ref": "#/definitions/subArray"
    }
  },
  "if": {
    "properties": {
      "isParentDependency": {
        "const": true
      }
    }
  },
  "then": {
    "properties": {
      "subArray": {
        "items": {
          "required": [
            "requiredPropIfIsParentDependency"
          ]
        }
      }
    }
  }
}

subArray has been moved to a definition and then referenced, so the conditional application logic can be seen easier.

The definition of subArray only determines the properties of the items structure if they are provided, and requires alwaysRequiredProp. It does not require requiredPropIfIsParentDependency.

The conditional application es in using the if and then keywords.

If the if schema is true, then the else schema is applied.

The value of if is a schema. It is applicable to the root level. It requires isParentDependency be true (it does not require the property itself, because that is ALWAYS required. Requiring the property inside the if statement would NOT mean the property is always required, unless else was false, but we're not even using else here).

When isParentDependency is true, the then schema is applied, which looks down the tree to subArray's items, requiring requiredPropIfIsParentDependency.

You can test this working in browser using jsonschema.dev (preloaded with this schema and your fail instance).

本文标签: javascriptJSONSchemaRequired property dependent on parent propertyStack Overflow