0

I am trying to validate my JSON Schema using tv4. It is working and validation returns True.

But, in my case the the JSON collection "first, second, and third" will not be available all time.

How do I write the schema in this situation?

My JSON Data

{
    "checked": "OK",
    "result": {
        "first": {
            "label": "First Label",
            "value": 1
        },
        "second": {
            "label": "second Label",
            "value": 34
        },
        "third": {
            "label": "Third Label",
            "value": 28
        }
    }
}

JSON Schema

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},

    "required": [
        "checked",
        "result"
    ],
    "properties": {
        "checked": {
            "$id": "#/properties/checked",
            "type": "string",
            "title": "The checked schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "OK"
            ]
        },
        "result": {
            "$id": "#/properties/result",
            "type": "object",
            "title": "The result schema",
            "description": "An explanation about the purpose of this instance.",
            "default": {},
           
            "required": [
                "first",
                "second",
                "third"
            ],
            "properties": {
                "first": {
                    "$id": "#/properties/result/properties/first",
                    "type": "object",
                    "title": "The first schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": {},
                    "examples": [
                        {
                            "label": "First Label",
                            "value": 1
                        }
                    ],
                    "required": [
                        "label",
                        "value"
                    ],
                    "properties": {
                        "label": {
                            "$id": "#/properties/result/properties/first/properties/label",
                            "type": "string",
                            "title": "The label schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": "",
                            "examples": [
                                "First Label"
                            ]
                        },
                        "value": {
                            "$id": "#/properties/result/properties/first/properties/value",
                            "type": "integer",
                            "title": "The value schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": 0,
                            "examples": [
                                1
                            ]
                        }
                    },
                    "additionalProperties": true
                },
                "second": {
                    "$id": "#/properties/result/properties/second",
                    "type": "object",
                    "title": "The second schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": {},
                    "examples": [
                        {
                            "label": "second Label",
                            "value": 34
                        }
                    ],
                    "required": [
                        "label",
                        "value"
                    ],
                    "properties": {
                        "label": {
                            "$id": "#/properties/result/properties/second/properties/label",
                            "type": "string",
                            "title": "The label schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": "",
                            "examples": [
                                "second Label"
                            ]
                        },
                        "value": {
                            "$id": "#/properties/result/properties/second/properties/value",
                            "type": "integer",
                            "title": "The value schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": 0,
                            "examples": [
                                34
                            ]
                        }
                    },
                    "additionalProperties": true
                },
                "third": {
                    "$id": "#/properties/result/properties/third",
                    "type": "object",
                    "title": "The third schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": {},
                    "examples": [
                        {
                            "label": "Third Label",
                            "value": 28
                        }
                    ],
                    "required": [
                        "label",
                        "value"
                    ],
                    "properties": {
                        "label": {
                            "$id": "#/properties/result/properties/third/properties/label",
                            "type": "string",
                            "title": "The label schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": "",
                            "examples": [
                                "Third Label"
                            ]
                        },
                        "value": {
                            "$id": "#/properties/result/properties/third/properties/value",
                            "type": "integer",
                            "title": "The value schema",
                            "description": "An explanation about the purpose of this instance.",
                            "default": 0,
                            "examples": [
                                28
                            ]
                        }
                    }
                }
            }
        }
    }
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • 1
    https://json-schema.org/understanding-json-schema/ is a good resource I suggest you read through to learn more about JSON Schema. – Jason Desrosiers Dec 06 '21 at 17:52
  • I highly suggest you _not_ use the website you used to generate that schema. It's known to create bad schemas that can cause problems and confusion in some cases. You'll want to remove all of `$id`, `title`, `description`, and `default`. – Jason Desrosiers Dec 06 '21 at 17:57
  • I notice that your schema declares that it uses draft-07, but tv4 only supports draft-03 and draft-04. You'll want to either find a validator that supports newer versions, or change the `$schema` declaration. – Jason Desrosiers Dec 06 '21 at 18:00

1 Answers1

2

In JSON Schema, all properties are optional by default. Your schema explicitly declares those properties as required. To make them optional, remove that keyword: "required": ["first", "second", "third"]

Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53