【问题标题】:Generating a Json schema with custom required生成具有自定义所需的 Json 模式
【发布时间】:2017-10-12 07:25:06
【问题描述】:

我尝试搜索,但没有一个适合我的案例。

这是我的模型架构:

{
    "formList": [{
        "type": "string",
        "fieldId": "string",
        "fieldLabel": "string",
        "value": "string",
        "depends": "string",
        "validation": {
            "mandatory": true
        },
        "dataValidation": "string",
        "helpText": "string",
        "key": "string"
    }],
    "action": "string",
    "mainScript": {
        "data": "string",
        "key": "string"
    }
}

输入的可能情况: 1. mainScript 可以为空

{
    "formList": [{
        "type": "Text box",
        "fieldId": "field_1",
        "fieldLabel": "Text box",
        "value": "",
        "depends": "",
        "validation": {
            "mandatory": false
        },
        "dataValidation": "",
        "helpText": "",
        "key": "field_1_1507792641393"
    }],
    "action": "add",
    "mainScript": ""
}
  1. formList 可以为空。

    { “表单列表”:[], “动作”:“添加”, “主脚本”:{ “数据”:“sdfsadf”, “关键”:“mainscript_1507793323369” } }

这是我的 json 架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "",
    "type": "object",
    "title": "Add Temp Configlet Builder Schema",
    "description": "Schema structure for adding temp configlet builder",
    "properties": {
        "formList": {
            "id": "formList",
            "type": "array",
            "title": "Form List values",
            "description": "The list of form entries",
            "items": {
                "type": "object",
                "properties": {
                    "type": {
                        "id": "type",
                        "type": "string",
                        "title": "The Type",
                        "description": "The type of form element"
                    },
                    "fieldId": {
                        "id": "fieldId",
                        "type": "string",
                        "title": "The Field Id",
                        "description": "The Id of the form field"
                    },
                    "fieldLabel": {
                        "id": "fieldLabel",
                        "type": "string",
                        "title": "The Field Label",
                        "description": "The label value of form field"
                    },
                    "value": {
                        "id": "value",
                        "type": "string",
                        "title": "The value",
                        "description": "The value of the form field"
                    },
                    "depends": {
                        "id": "depends",
                        "type": "string",
                        "title": "Depends on",
                        "description": "To indicate if the field depends on another field"
                    },
                    "validation": {
                        "id": "validation",
                        "type": "object",
                        "title": "Validation field",
                        "description": "To indicate if the field has any validation",
                        "properties": {
                            "mandatory": {
                                "id": "mandatory",
                                "type": "boolean"
                            }
                        }
                    },
                    "dataValidation": {
                        "id": "dataValidation",
                        "type": "string",
                        "title": "Data Validation field",
                        "description": "Contains the data validation condition"
                    },
                    "helpText": {
                        "id": "helpText",
                        "type": "string",
                        "title": "Help text",
                        "description": "Contains the help text for the field"
                    },
                    "key": {
                        "id": "key",
                        "type": "string"
                    }
                },
                "required": [
                    "type",
                    "fieldId",
                    "fieldLabel",
                    "value",
                    "depends",
                    "validation",
                    "dataValidation",
                    "helpText",
                    "key"
                ]
            }
        },
        "action": {
            "id": "action",
            "type": "string",
            "title": "Action type",
            "description": "Type of action to be performed",
            "enum": [
                "update",
                "add",
                "delete"
            ]
        },
        "mainScript": {
            "id": "mainScript",
            "type": [
                "object",
                "string"
            ],
            "properties": {
                "data": {
                    "id": "data",
                    "type": "string",
                    "title": "The mainscript data",
                    "description": "Contains mainscript data"
                },
                "key": {
                    "id": "mainscript_key",
                    "type": "string"
                }
            },
    "required":["data","key"],
        }
    },
    "required": [
        "formList",
        "action",
        "mainScript"
    ],
          "additionalProperties": false
}

我的 JsonSchema 在验证以下输入时应该抛出错误: {"formList":[],"action":"add","mainScript":{"data":""}} 因为 mainScript 中的参数之一丢失了。

【问题讨论】:

  • 对不起!你有什么问题?
  • 我只想生成一个 JsonSchema 可以适用于上述三个测试用例。但是我的 json Schema 应该允许空的 mainScript 。(第一个测试用例:mainScript 可以是空的)。但它不允许。
  • 我认为你的 json 架构可以通过 2 个案例(1 和 2)
  • 不..不是..
  • 啊,因为mainScript "required":["data","key"] 所以如果你不想要它可以删除。

标签: java json jsonschema json-schema-validator


【解决方案1】:

在我尝试了所有尝试之后,找到了一个解决方案,它也可以在 swagger 中工作。

感谢帮助我的人,

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "",
    "type": "object",
    "title": "Add Temp Configlet Builder Schema",
    "description": "Schema structure for adding temp configlet builder",
    "properties": {
        "formList": {
            "id": "formList",
            "type": "array",
            "title": "Form List values",
            "description": "The list of form entries",
            "items": {
                "type": "object",
                "properties": {
                    "type": {
                        "id": "type",
                        "type": "string",
                        "title": "The Type",
                        "description": "The type of form element"
                    },
                    "fieldId": {
                        "id": "fieldId",
                        "type": "string",
                        "title": "The Field Id",
                        "description": "The Id of the form field"
                    },
                    "fieldLabel": {
                        "id": "fieldLabel",
                        "type": "string",
                        "title": "The Field Label",
                        "description": "The label value of form field"
                    },
                    "value": {
                        "id": "value",
                        "type": "string",
                        "title": "The value",
                        "description": "The value of the form field"
                    },
                    "depends": {
                        "id": "depends",
                        "type": "string",
                        "title": "Depends on",
                        "description": "To indicate if the field depends on another field"
                    },
                    "validation": {
                        "id": "validation",
                        "type": "object",
                        "title": "Validation field",
                        "description": "To indicate if the field has any validation",
                        "properties": {
                            "mandatory": {
                                "id": "mandatory",
                                "type": "boolean"
                            }
                        }
                    },
                    "dataValidation": {
                        "id": "dataValidation",
                        "type": "string",
                        "title": "Data Validation field",
                        "description": "Contains the data validation condition"
                    },
                    "helpText": {
                        "id": "helpText",
                        "type": "string",
                        "title": "Help text",
                        "description": "Contains the help text for the field"
                    },
                    "key": {
                        "id": "key",
                        "type": "string"
                    }
                },
                "required": [
                    "type",
                    "fieldId",
                    "fieldLabel",
                    "value",
                    "depends",
                    "validation",
                    "dataValidation",
                    "helpText",
                    "key"
                ]
            }
        },
        "action": {
            "id": "action",
            "type": "string",
            "title": "Action type",
            "description": "Type of action to be performed",
            "enum": [
                "update",
                "add",
                "delete"
            ]
        },
        "mainScript": {
            "id": "mainScript",
            "type": [
                "object",
                "string"
            ],
            "properties": {
                "data": {
                    "id": "data",
                    "type": "string",
                    "title": "The mainscript data",
                    "description": "Contains mainscript data"
                },
                "key": {
                    "id": "mainscript_key",
                    "type": "string"
                }
            },
            "oneOf":[  
                {  

                },
                {  
                    "not":{  
                        "required":[  
                            "data",
                            "key"
                        ]
                    }
                }
            ]
        }
    },
    "required": [
        "formList",
        "action",
        "mainScript"
    ],
    "additionalProperties": false
}

检查这里。 working for all three cases

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多