Include the Missing Fields in Error Response

Hi,

I just want to include the fields that are null in my error response. Any suggestion?

Sample request

{

"username": "" ,

"name": "Joe",

"email" : ""

}
My desired error response

{

"timestamp": "2020-06-17T13:26.724",

"message": "Mandatory Fields missing",

"detail": {["username is mandatory", "email is mandatory" ]}


Thank you!

Solved Solved
0 2 872
1 ACCEPTED SOLUTION

Yes

there are three ways that I can think of,  to accomplish that goal within an Apigee API Proxy.

The most formal is probably via the OASValidation policy. This is a builtin policy in Apigee which validates the inbound message against an OpenAPI Specification which includes schema for the JSON payloads. To make this happen, you need the specification. I think the error message generated by this policy will include descriptive error messages indicating what is wrong with the payload.

One step down from that, in terms of formality, would be using a JSON Schema Validator. Something like this callout can do that. For this approach, you need the JSON Schema. (Which is strictly a subset of the OpenAPI Specification that I mentioned above.) Then just configure the callout and it should emit the error messages you want for your response.

Finally, you can implement "do it yourself" validation within a JavaScript callout in the API Proxy.  This approach is relatively easy to implement in JavaScript, and you don't need a formal schema to do so.  But because you wouldn't use a formal schema, this approach won't scale well with lots and lots of different kinds of payloads.

An example of the last would be something like this: 

var requiredNonEmptyFields = ['username', 'name', 'email'];
var payload = JSON.parse(context.getVariable('request.content'));
var missingFields =
  requiredNonEmptyFields
  .filter(function(fieldName) { return !payload[fieldName]; });

if (missingFields.length) { 
  var errorMessages =
    missingFields
      .map(function(fieldName) { return fieldName + " is null or empty"; });
  context.setVariable('errormessages', JSON.stringify(errorMessages));
  throw new Error('invalid request payload');
}

 

View solution in original post

2 REPLIES 2

Yes

there are three ways that I can think of,  to accomplish that goal within an Apigee API Proxy.

The most formal is probably via the OASValidation policy. This is a builtin policy in Apigee which validates the inbound message against an OpenAPI Specification which includes schema for the JSON payloads. To make this happen, you need the specification. I think the error message generated by this policy will include descriptive error messages indicating what is wrong with the payload.

One step down from that, in terms of formality, would be using a JSON Schema Validator. Something like this callout can do that. For this approach, you need the JSON Schema. (Which is strictly a subset of the OpenAPI Specification that I mentioned above.) Then just configure the callout and it should emit the error messages you want for your response.

Finally, you can implement "do it yourself" validation within a JavaScript callout in the API Proxy.  This approach is relatively easy to implement in JavaScript, and you don't need a formal schema to do so.  But because you wouldn't use a formal schema, this approach won't scale well with lots and lots of different kinds of payloads.

An example of the last would be something like this: 

var requiredNonEmptyFields = ['username', 'name', 'email'];
var payload = JSON.parse(context.getVariable('request.content'));
var missingFields =
  requiredNonEmptyFields
  .filter(function(fieldName) { return !payload[fieldName]; });

if (missingFields.length) { 
  var errorMessages =
    missingFields
      .map(function(fieldName) { return fieldName + " is null or empty"; });
  context.setVariable('errormessages', JSON.stringify(errorMessages));
  throw new Error('invalid request payload');
}

 

Thank you very much dchiesa1, we are just new in Apigee, this will definitely help us on our project. I will now explore and apply each of your suggestions.