Hi, I want to know how to customize the error message while using validate-content policy.
The intention is to show the customized message when users input wrong field name or additional fields.
Schema used
{
"type": "object",
"properties": {
"abc": {
"type": "string"
}
},
"required": [
"MasterNo"
],
"additionalProperties": false
}
So far the validate-content works, but the default error message is not clear.
{
"error": {
"code": "BadRequest",
"message": "Invalid Request Body"
}
}
So I want to customize the error message.
Here's my attempt but it does not work, still return the default error message.
<policies>
<inbound>
<base />
<set-backend-service base-url="@("{{ERP-Cloud}}")" />
<authentication-basic username="{{ERP-Cloud-Id}}" password="{{ERP-Cloud-Password}}" />
<validate-content unspecified-content-type-action="ignore" max-size="500" size-exceeded-action="prevent" errors-variable-name="validationErrors">
<content type="application/json" validate-as="json" action="prevent" schema-id="erp-check" schema-ref="#/components/schemas/erp-check" />
</validate-content>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<choose>
<when condition="@(context.Variables.ContainsKey("validationErrors"))">
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var error = new JObject();
error["code"] = "400";
error["message"] = "The request body is invalid. Only the field 'MasterNo' is allowed.";
return error.ToString();
}</set-body>
</when>
<otherwise>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var error = new JObject();
error["code"] = "500";
error["message"] = "An unexpected error occurred.";
return error.ToString();
}</set-body>
</otherwise>
</choose>
</on-error>
</policies>
Is there anything I get wrong ?
Need your advice.
Thank your