简体   繁体   中英

How do I do JSON schema validation using Apigee Edge policies?

假设您具有根据json-schema.org定义的有效有效负载的架构,并且想要在处理有效负载或将其传递给后端之前在代理中对其进行验证,那么如何正确验证有效负载的内容?

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

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)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM