Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema conditional: field is required based on value of another field

I'm trying to implement this condition: field is required based on value of another field i.e. if request with "index":"true" exists then "id" element required: true.

Here is a sample schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "test title",
  "type": "object",
  "properties": {
    "data": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Item"
      },
      "minItems": 0
    }
  },
  "required": [
    "data"
  ],
  "definitions": {
    "Item": {
      "type": "object",
      "properties": {
        "id": {
          "type": [
            "integer",
            "string"
          ]
        },
        "type": {
          "type": "string"
        }
      }
    }
  }
}

How it can be implemented?

Any pointers will be helpful.

like image 312
Artur Burnos Avatar asked Nov 23 '14 18:11

Artur Burnos


People also ask

How do you make a property required in JSON Schema?

Required Properties By default, the properties defined by the properties keyword are not required. However, one can provide a list of required properties using the required keyword. The required keyword takes an array of zero or more strings. Each of these strings must be unique.

What does $Ref mean in JSON Schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.

What is oneOf in JSON Schema?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.

How does JSON Schema validation work?

JSON Schema is a JSON-based format for defining the structure of JSON data. It provides a contract for what JSON data is required for a given application and how to interact with it. It can be used for validation, documentation, hyperlink navigation, and interaction control of JSON data.


1 Answers

Your sample chema is strange for me, but for validation case in ticket description your can use this part of json shema:

"properties": {
    "data": {
        "oneOf": [
            {
                "type": "object",
                "required": [
                    "index"
                ],
                "properties": {
                    "index": {
                        "type": "boolean",
                        "enum": [false]
                    }
                }
            },
            {
                "type": "object",
                "required": [
                    "index",
                    "id"
                ],
                "properties": {
                    "index": {
                        "type": "boolean",
                        "enum": [true]
                    },
                    "id": {
                        "type": "boolean"
                    }
                }
            }
        ]
    }
}

This trick helps for you, if you want validate one parameter, when other parameters equals to some value.

like image 159
Talkerbox Avatar answered Sep 23 '22 16:09

Talkerbox