简体   繁体   中英

JSON response validation against JSON schema using groovy script

I have a rest service which returns a json response. I need to validate response against my predefined json schema using groovy script. All the options I have found on the net describe validating json response using groovy.json.JsonSlurper against some pre conditions not the schema. So I am kinda confused where to start from. But I roughly know that I need the following steps get done.

  1. Define custom json schema
  2. Importing some json validator library
  3. And validate response against schema in groovy

I'd be highly grateful if anybody helps out on the steps 2 and 3. For your notice, I am using Soap ui tool and here is my custom schema:

{
"$schema": "http://json-schema.org/schema#",
"type":"array",
"items":{
"type": "string"
}
}

Loading libraries in Groovy depends on the tool. In SoapUI-5.6.0 the standard way is to put jar files in (from SoapUI base installation path) bin/ext .

Validation differs depending on the library. For example with groovy-json-schema :

def json = new JsonSlurper().parseText('{"an": "example"}')
use(JsonSchema) {
    json.schema = 'file://path/to/your/json/schema.json'
    json.conformsSchema()
}

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