生成需要自定义的Json模式

时间:2017-10-12 07:25:07

标签: java json jsonschema json-schema-validator

我尝试搜索,但没有一个能为我的案件工作。

这是我的模型架构:

{
    "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可以为空。

    {         “formList”:[],         “行动”:“添加”,         “mainScript”:{             “data”:“sdfsadf”,             “key”:“mainscript_1507793323369”         }     }

  2. 这是我的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”:[], “动作”: “添加”, “mainScript”:{ “数据”: “”}} 因为缺少mainScript中的一个param。

1 个答案:

答案 0 :(得分:0)

在我尝试了所有的尝试之后,找到了一个解决方案,这也是在招摇也工作。

  

感谢那些帮助我的人,

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