Validate-content policy in APIM - Is there any way to include property name in error details which triggered the validation?

Ajay Anandrao Gote 30 Reputation points
2023-08-10T13:36:19.21+00:00

I am working on APIM validate-content policy. This policy validate the incoming request body against the JSON schema we defined. So, when we pass invalid request body it returns 400 status code, response body has Message property and it has details about the error. However, in some cases this Message property doesn't have more details like which property in request body triggered validation.

For example, we have following schema,

{
    "type": "object",
        "ClientId"
    ],
    "properties": {
        "ClientId": { 
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "countryCode": {
            "type": "string",
            "pattern": "^[+][0-9]{1,4}$"  
            "description": "Country code"
        },
        "mobileNumber": {
            "type": "string",
            "pattern": "^([0-9]{10})$"
        }, 
        "dob": {
            "type": "string",
            "format": "date"
        }
        "mfj": {
            "type": "boolean"
        }
        "address": {
            "$ref": "#/components/schemas/Address"
        }
    }
}

When we pass some invalid values for CountryCode i.e. 91, the policy validate the request body and return following details in Message property.

Body of the request does not conform to the definition which is associated with the content type application/json. String '91' does not match regex pattern '^[+][0-9]{1,4}$'. Line: 5, Position: 22

If we pass invalid values in mobile i.e. 123, the response will be,

Body of the request does not conform to the definition which is associated with the content type application/json. String '123' does not match regex pattern '^([0-9]{10})$'. Line: 5, Position: 24

The details does not tell about which property has triggered validation. It is good to say that CountryCode or Mobilenumber is invalid in message.

So, is there any way/configuration which will include this information as well. So, caller will get some more meaningful error message.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,366 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,421 Reputation points
    2023-08-10T20:33:36.6966667+00:00

    Ajay Anandrao Gote Thanks for posting your question in Microsoft Q&A. This message format is described in doc: Validation errors and unfortunately, the public response/details are not customizable to include the property name. I agree that this might be a good idea to add it in the details section/trace and will share feedback internally with the product team. You can also submit it via https://aka.ms/apimwish and others with similar interests can upvote it too.

    User's image

    I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.