Search code examples
javajsonjsonschemajson-schema-validator

Generating a Json schema with custom required


I tried searching , none of them are working for my cases.

this is my model schema :

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

Possible cases for the input : 1. mainScript can be empty

{
    "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 can be empty .

    { "formList": [], "action": "add", "mainScript": { "data": "sdfsadf", "key": "mainscript_1507793323369" } }

here is my json schema :

{
    "$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 should throw error while validating the following input: {"formList":[],"action":"add","mainScript":{"data":""}} Because one of param in mainScript is missing.


Solution

  • After I tried all the attempts ,found a solution, which is also working in swagger also.

    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
    }
    

    check here . working for all three cases