You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用tv4实现JavaScript可选JSON字段的Schema校验规则编写咨询

JSON Schema 字段可选配置方案

你当前的校验规则强制要求first、second、third存在的原因是:这三个字段被列入了result节点的required必填数组中,调整规则只需修改该配置即可:

  • 移除result节点required数组内的first、second、third字段
  • 调整后三个字段变为可选:不存在时不会触发校验报错,存在时会自动匹配你已定义的结构规则(必须包含label字符串、value整数)

核心修改示例

原result节点的必填配置:

"required": [
    "first",
    "second",
    "third"
]

修改后配置:

"required": []

如果你有「三个字段至少存在一个」的额外要求,可以在result节点下新增"minProperties": 1配置即可。

完整修改后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": [],
            "minProperties": 0,
            "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
                            ]
                        }
                    }
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Vineesh TP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 12:15:02