生成需要自定义的 Json 模式

Generating a Json schema with custom required

我尝试搜索,none 个正在为我的案例工作。

这是我的模型架构:

{
    "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": [], "action": "add", "mainScript":{ "data": "sdfsadf", "key": "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
}

my JsonSchema 应该在验证以下输入时抛出错误: {"formList":[],"action":"add","mainScript":{"data":""}} 因为缺少 mainScript 中的一个参数。

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

Thanks for the folks who are helping me,

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