简体   繁体   English

使用 tv4 的可选 Json 验证 - Javascript

[英]Optional Json validation using tv4 - Javascript

I am trying to validate my JSON Schema using tv4 .我正在尝试使用tv4验证我的 JSON 架构。 It is working and validation returns True .它正在工作并且验证返回True

But, in my case the the JSON collection "first, second, and third" will not be available all time.但是,在我的情况下,JSON 系列"first, second, and third"将不会一直可用。

How do I write the schema in this situation?在这种情况下如何编写模式?

My JSON Data我的 JSON 数据

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

JSON Schema JSON 架构

{
    "$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
                            ]
                        }
                    }
                }
            }
        }
    }
}

In JSON Schema, all properties are optional by default.在 JSON Schema 中,默认情况下所有属性都是可选的。 Your schema explicitly declares those properties as required.您的架构根据需要显式声明这些属性。 To make them optional, remove that keyword: "required": ["first", "second", "third"]要使它们成为可选,请删除该关键字: "required": ["first", "second", "third"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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