JSON Schema validation

Hi There,

Our requirement is to validate the JSON request payload before forwarding the request to the back end servers, could you pls refer me to the appropriate article for it. Thanks

0 4 831
4 REPLIES 4

Thanks for the info.Bit unlcear with the steps, do i need to import both java script TV4 and uses tv4-min.js in to my proxy?

One more question,Isn't that APIGEE developer team planning to build any builtin policy for this schema validation,since it quite tedious to manage this code

Yes - Apigee is doing some work on an OpenAPI Spec validator.

You can also try this Java version:

https://github.com/DinoChiesa/ApigeeEdge-Java-Validate-JSON-Schema

@Popleys you can use json-schema draft v4 to validate simple values and complex objects using a rich validation vocabulary: https://github.com/geraintluff/tv4

  • Add a JS Policy in your API request flow including tv4.js:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="JS-Subscriber-ParamsValidation">
    <DisplayName>JS-ParamsValidation</DisplayName>
    <Properties/>
    <ResourceURL>jsc://JS-ParamsValidation.js</ResourceURL>
    <IncludeURL>jsc://tv4.js</IncludeURL>
</Javascript>
  • In JS-ParamsValidation:
    • Define your API Schema (required & properties)
    • Validate the request params:
if(tv4.validate(requestPayload, apiSchema)){
    context.setVariable("validRequest" ,true);
}else{
context.setVariable("validRequest" ,false);
context.setVariable("tv4ErrorCode" ,tv4.error.code);
context.setVariable("tv4ErrorDataPath" ,tv4.error.dataPath);
context.setVariable("tv4ErrorSchemaPath" ,tv4.error.schemaPath);
context.setVariable("ERRORMSG" ,tv4.error.message); 

}
  • Add a Raise fault policy after the JS-ParamsValidation policy with condition flow:
                <Step>
                    <Name>RF-ParamsValidation</Name>
                    <Condition>validRequest=false</Condition>
                </Step>
  • You can add an assign message policy after the RF for custom response using the variables set:
    (ERRORMSG, tv4ErrorSchemaPath, tv4ErrorDataPath, tv4ErrorCode)