skip to Main Content

Trying to validate the json in azure logic app using json schema validation.

This is my sample json:

{
    "address": [
        {
            "contact": {
                "firstName": "myFirstName",
                "lastName": "myLastName"
            },
            "type": "bill"
        }
    ]
}

This is my schema:

{
    "if": {
        "properties": {
            "address": {
                "type": "array",
                "items": {
                    "properties": {
                        "type": {
                            "const": "bill"
                        }
                    }
                }
            }
        }
    },
    "then": {
        "properties": {
            "address": {
                "type": "array",
                "items": {
                    "properties": {
                        "contact": {
                            "type": "object",
                            "required": [
                                "firstName"
                            ]
                        }
                    }
                }
            }
        }
    }
}

If I don’t pass firstName attribute in input json, the validation is getting failed which is expected, but the error message I am getting is not correct. It does not show which attribute is missing. Like in this example I am expecting the message should tell the firstName attribute is missing.

This is output of above validation:

"outputs": {
            "errors": [
                {
                    "message": "JSON does not match schema from 'then'.",
                    "lineNumber": 0,
                    "linePosition": 0,
                    "path": "",
                    "schemaId": "#/then",
                    "errorType": "then",
                    "childErrors": []
                }
            ]
        }

Any help would be appreciated

2

Answers


  1. Chosen as BEST ANSWER

    I was able to find the workaround and that is using oneOf.

    Modified schema:

    {
        "if": {
            "properties": {
                "address": {
                    "type": "array",
                    "items": {
                        "properties": {
                            "type": {
                                "const": "bill"
                            }
                        }
                    }
                }
            }
        },
        "then": {
            "oneOf": [
                {
                    "properties": {
                        "address": {
                            "type": "array",
                            "items": {
                                "properties": {
                                    "contact": {
                                        "type": "object",
                                        "required": [
                                            "firstName"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
    

    Here is the schema validation output for failed scenario.

    [
        {
            "message": "JSON does not match schema from 'then'.",
            "lineNumber": 0,
            "linePosition": 0,
            "path": "",
            "schemaId": "#/then",
            "errorType": "then",
            "childErrors": [
                {
                    "message": "JSON is valid against no schemas from 'oneOf'.",
                    "lineNumber": 0,
                    "linePosition": 0,
                    "path": "",
                    "schemaId": "#/then",
                    "errorType": "oneOf",
                    "childErrors": [
                        {
                            "message": "Required properties are missing from object: firstName.",
                            "lineNumber": 0,
                            "linePosition": 0,
                            "path": "address[0].contact",
                            "value": [
                                "firstName"
                            ],
                            "schemaId": "#/then/oneOf/0/properties/address/items/properties/contact",
                            "errorType": "required",
                            "childErrors": []
                        }
                    ]
                }
            ]
        }
    ]
    

    firstName missing attribute is coming up in error response for conditional schema validation


  2. I have reproduced in my environment and got expected results as below:

    Firstly, I have taken a http trigger and then taken parse json step and gave the below schema in it.

    {
        "type": "object",
        "properties": {
            "address": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "contact": {
                            "type": "object",
                            "properties": {
                                "firstName": {
                                    "type": "string",
                                    "message": {
                                        "required": "name is required"
                                    },
                                    "required": true
                                },
                                "lastName": {
                                    "type": "string"
                                }
                            }
                        },
                        "type": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "contact",
                        "type"
                    ]
                }
            }
        }
    }
    

    enter image description here

    Now in response action:

    outputs('Parse_JSON')['errors'][0]['message']
    

    enter image description here

    After i have kept runAfter Failed to get the message and then you can use any parallel action also.

    Output:

    enter image description here

    Output In logic app:

    enter image description here

    enter image description here

    enter image description here

    Code view:

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Parse_JSON": {
                    "inputs": {
                        "content": "@triggerBody()",
                        "schema": {
                            "properties": {
                                "address": {
                                    "items": {
                                        "properties": {
                                            "contact": {
                                                "properties": {
                                                    "firstName": {
                                                        "message": {
                                                            "required": "name is required"
                                                        },
                                                        "required": true,
                                                        "type": "string"
                                                    },
                                                    "lastName": {
                                                        "type": "string"
                                                    }
                                                },
                                                "type": "object"
                                            },
                                            "type": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "contact",
                                            "type"
                                        ],
                                        "type": "object"
                                    },
                                    "type": "array"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "runAfter": {},
                    "type": "ParseJson"
                },
                "Response": {
                    "inputs": {
                        "body": "@outputs('Parse_JSON')['errors'][0]['message']",
                        "statusCode": 200
                    },
                    "kind": "Http",
                    "runAfter": {
                        "Parse_JSON": [
                            "Failed"
                        ]
                    },
                    "type": "Response"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {}
    }
    

    You can do only this and i do agree with @skin about this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search