使用TV4-Java脚本的可选JSON验证 [英] Optional Json validation using tv4 - Javascript

查看:7
本文介绍了使用TV4-Java脚本的可选JSON验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tv4验证我的JSON架构。 它正在工作,并且验证返回True

但是,在我的情况下,JSON集合"first, second, and third"不会始终可用。

在这种情况下如何编写架构?

我的JSON数据

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

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
                            ]
                        }
                    }
                }
            }
        }
    }
}

推荐答案

在JSON架构中,默认情况下所有属性都是可选的。您的模式根据需要显式声明这些属性。要使它们成为可选的,请删除该关键字:"required": ["first", "second", "third"]

这篇关于使用TV4-Java脚本的可选JSON验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆