2

Suppose you have the schema for valid payload defined in accordance to json-schema.org and you'd like to validate it in a proxy prior to handling the payload or passing it to backend, how can you properly validate the contents of the payload?

2 Answers2

2

Apigee doesn't have a JSON schema validator built in, so your best bet is to create a Javascript something like tv4 or another javascript based validator. Then you need to create a Javascript callout which has your script to validate the Apigee flow variable and includes your library (for example, tv4.js)

<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="JSO- Validate-JSON">
    <DisplayName>JS-Validate-JSON</DisplayName>
    <FaultRules/>
    <Properties/>
    <ResourceURL>jsc://validatejson.js</ResourceURL>
    <IncludeURL>jsc://tv4.js</IncludeURL>
</Javascript>

tv4 is available on github at https://github.com/geraintluff/tv4

Michael Bissell
  • 1,210
  • 1
  • 8
  • 14
1

To expand a bit on Michael B. response validatejson.js will be a JavaScript policy that will load the schema into schema variable, which will be validated against the response.content:

var valid = tv4.validate(response.content, schema);
if(valid){
    log.info("Schema is valid!" + valid);
} else {
    context.setVariable("raiseFaultRuleSchemaValidation", "true");
    context.setVariable("raiseFaultRuleSchemaValidationMessage", tv4.error)
  }
}
Diego
  • 1,678
  • 1
  • 16
  • 20