Home > Mobile >  How to prevent additions properties when using oneOf in JSON Schema
How to prevent additions properties when using oneOf in JSON Schema

Time:03-09

I have a JSON configuration with an array of "specs", with many different kinds of "path" properties. The "kind" property is the selector of the variant.

I want to restrict the validation to not allow addition properties. In the JSON example, the properties "noAllowedInt" and "notAllowedObject" must report a validation error.

How can I add this rule to the JSON schema?

{
    "specs": [
        {
            "id": 12,
            "label": "Serial",
            "path": {
                "kind": "serial",
                "speed": 9600,
                "parity": "even"
            }
        },
        {
            "id": 13,
            "label": "Memory",
            "path": {
                "kind": "memory",
                "storage": "permanent",
                "location": "external"
            },
            "noAllowedInt": 42,
            "notAllowedObject": {
                "value": 3.1415
            }
        }
    ]
}

There are two Json Schema to validate this, a simple one and one with "definitions"

Simple JSON Schema:

    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "specs": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": true,
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "label": {
                        "type": "string"
                    }
                },
                "required": [
                    "id",
                    "label"
                ],
                "oneOf": [
                    {
                        "type": "object",
                        "properties": {
                            "path": {
                                "type": "object",
                                "additionalProperties": false,
                                "properties": {
                                    "kind": {
                                        "type": "string",
                                        "enum": [
                                            "serial"
                                        ]
                                    },
                                    "speed": {
                                        "type": "integer"
                                    },
                                    "parity": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "kind"
                                ]
                            }
                        }
                    },
                    {
                        "type": "object",
                        "properties": {
                            "path": {
                                "type": "object",
                                "additionalProperties": false,
                                "properties": {
                                    "kind": {
                                        "type": "string",
                                        "enum": [
                                            "memory"
                                        ]
                                    },
                                    "storage": {
                                        "type": "string"
                                    },
                                    "location": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "kind"
                                ]
                            }
                        }
                    }
                ]
            }
        }
    },
    "required": [
        "specs"
    ]
}

JSON Schema with "definitions"

    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "specs": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": true,
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "label": {
                        "type": "string"
                    }
                },
                "required": [
                    "id",
                    "label"
                ],
                "oneOf": [
                    {
                        "$ref": "#/definitions/PathKindSerialType"
                    },
                    {
                        "$ref": "#/definitions/PathKindMemory"
                    }
                ]
            }
        }
    },
    "required": [
        "specs"
    ],
    "definitions": {
        "PathKindSerialType": {
            "type": "object",
            "additionalProperties": true,
            "properties": {
                "path": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                        "kind": {
                            "type": "string",
                            "enum": [
                                "serial"
                            ]
                        },
                        "speed": {
                            "type": "integer"
                        },
                        "parity": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "kind"
                    ]
                }
            }
        },
        "PathKindMemory": {
            "type": "object",
            "additionalProperties": true,
            "properties": {
                "path": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                        "kind": {
                            "type": "string",
                            "enum": [
                                "memory"
                            ]
                        },
                        "storage": {
                            "type": "string"
                        },
                        "location": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "kind"
                    ]
                }
            }
        }
    }
}

CodePudding user response:

I guess you need to keep the path definition as a property at the specs array level. This would be my proposal:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "specs": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "integer"
          },
          "label": {
            "type": "string"
          },
          "path": {
            "oneOf": [
              {
                "$ref": "#/definitions/PathKindSerialType"
              },
              {
                "$ref": "#/definitions/PathKindMemory"
              }
            ]
          }
        },
        "required": [ "id", "label" ]
      }
    }
  },
  "required": [ "specs" ],
  "definitions": {
    "PathKindSerialType": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "type": "string",
          "enum": [ "serial" ]
        },
        "speed": {
          "type": "integer"
        },
        "parity": {
          "type": "string"
        }
      },
      "required": [ "kind" ]
    },
    "PathKindMemory": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "type": "string",
          "enum": [ "memory" ]
        },
        "storage": {
          "type": "string"
        },
        "location": {
          "type": "string"
        }
      },
      "required": [ "kind" ]
    }
  }
}

enter image description here

  • Related