Forum Discussion

pauldzy's avatar
pauldzy
Occasional Contributor
10 years ago

SoapUI JSON schema validation with Rhino Javascript

Hi folks,

I've been interested in finding a simple method to do JSON schema validation via a SoapUI script. I couldn't find a clean Groovy approach that had the features I wanted (schema references) but there are a number of very complete solutions in JavaScript. In particular, tv4 seems very well done.
https://github.com/geraintluff/tv4

So thus I wandered off the SmartBear reservation into Rhino land. I think its safe to say there is not a single working example of a JavaScript script for SoapUI anywhere to be found.

So even though mine is a bit half-baked I thought I'd place it here for anyone else interested in the topic. Any feedback is appreciated. The top part is just the minified tv4 code, the second blob is my schema and then its just the lines that grab the test result and run the validation. The schema is not the tightest by any means but I ended up more focused on the process itself. I remain confused how best to do an assertion so that SoapUI picks up and passes along info to the logs but just unceremoniously throwing an error works well enough for now.

Cheers,
Paul

/*
Tiny Validator for JSON Schema v4
Geraint Luff and others
2013
https://github.com/geraintluff/tv4
This code is released into the "public domain" by its author(s). Anybody may use, alter and distribute the code without restriction. The author makes no guarantees, and takes no liability of any kind for use of this code.
If you find a bug or make an improvement, it would be courteous to let the author know, but it is not compulsory.
*/

//// ??? AHEM SOAPUI FORUM ONLY ALLOWS 20000 BYTE MESSAGES ???
//// Paste in the minified function from https://github.com/geraintluff/tv4/blob/master/tv4.min.js

var tschema = {"$schema":"http://json-schema.org/draft-04/schema#","description":"EPA Office of Water Catchment Impairment Status JSON Schema","type":"object","required":["output","status"],"properties":{"output":{"type":"object","properties":{"submission_id":{"type":["string","null"]},"catchments":{"type":"array","items":{"type":"object","properties":{"catchment_comid":{"type":"integer"},"nhd_waters":{"type":"array","items":{"type":"object","properties":{"gnis_id":{"type":["string","null"]},"gnis_name":{"type":["string","null"]},"ftype":{"type":"string"},"listed_waters":{"type":"array","items":{"type":"object","properties":{"listed_water_id":{"type":"string"},"cycle_year":{"type":"string"},"state":{"type":"string"},"listed_water_name":{"type":"string"},"snapshot_date":{"type":"string"},"listed_water_causes":{"type":"array","items":{"type":"object","properties":{"lw_detailed_cause_id":{"type":"integer"},"lw_detailed_cause_name":{"type":"string"},"lw_parent_cause_id":{"type":"integer"},"lw_parent_cause_name":{"type":"string"},"tmdl_flag":{"type":["string","null"]},"icis_parameter_code":{"type":["string","null"]},"icis_unit_code":{"type":["string","null"]},"icis_description":{"type":["string","null"]}},"required":["lw_detailed_cause_id","lw_detailed_cause_name","lw_parent_cause_id","lw_parent_cause_name","tmdl_flag","icis_parameter_code","icis_unit_code","icis_description"]}},"required":["listed_water_id","cycle_year","state","listed_water_name","snapshot_date","listed_water_causes"]}}},"listed_waters_with_tmdls":{"type":"array","items":{"type":"object","properties":{"listed_water_id":{"type":"string"},"cycle_year":{"type":"string"},"state":{"type":"string"},"listed_water_name":{"type":"string"},"snapshot_date":{"type":"string"},"tmdls":{"type":"array","items":{"type":"object","properties":{"tmdl_id":{"type":"integer"},"tmdl_name":{"type":"string"},"tmdl_status":{"type":"string"},"tmdl_date":{"type":"string"},"tmdl_pollutants":{"type":"array","items":{"type":"object","properties":{"tmdl_type":{"type":"string"},"tmdl_pollutant_id":{"type":"integer"},"tmdl_pollutant_name":{"type":"string"},"tmdl_parent_pollutant_id":{"type":"integer"},"tmdl_parent_pollutant_name":{"type":"string"}},"required":["tmdl_type","tmdl_pollutant_id","tmdl_pollutant_name","tmdl_parent_pollutant_id","tmdl_parent_pollutant_name"]}},"tmdl_lw_causes":{"type":"array","items":{"type":"object","properties":{"lw_detailed_cause_id":{"type":"integer"},"lw_detailed_cause_name":{"type":"string"},"lw_parent_cause_id":{"type":"integer"},"lw_parent_cause_name":{"type":"string"},"tmdl_flag":{"type":["string","null"]},"icis_parameter_code":{"type":["string","null"]},"icis_unit_code":{"type":["string","null"]},"icis_description":{"type":["string","null"]}},"required":["lw_detailed_cause_id","lw_detailed_cause_name","lw_parent_cause_id","lw_parent_cause_name","tmdl_flag","icis_parameter_code","icis_unit_code","icis_description"]}}},"required":["tmdl_id","tmdl_name","tmdl_status","tmdl_date","tmdl_pollutants","tmdl_lw_causes"]}}},"required":["listed_water_id","cycle_year","state","listed_water_name","snapshot_date","tmdls"]}}},"required":["gnis_id","gnis_name","ftype","listed_waters","listed_waters_with_tmdls"]}}}}}}},"status":{"type":"object","properties":{"submission_id":{"type":["string","null"]},"status_code":{"type":"integer"},"status_message":{"type":["string","null"]},"execution_time":{"type":"number"},"output_bytes":{"type":"number"}},"required":["submission_id","status_code","status_message","execution_time","output_bytes"]}}};

var tcase = testRunner.getTestCase();
var tsuite = tcase.getTestSuite();
var tproject = tsuite.getProject();
var tsuiteX = tproject.getTestSuiteByName("CatchmentImpairmentStatus");
var tcaseX = tsuiteX.getTestCaseByName("Random Point JSON Schema NP21");
var tstep = tcaseX.getTestStepByName("Random Point Hit NP21");
var tresp = tstep.testRequest.getResponse();

var tjson = eval("(" + tresp.getContentAsString() + ")");

var tvalid = tv4.validate(tjson, tschema, true);

if (tvalid == false) {
log.info(tv4.error.toString());
throw new Error (tv4.error.toString());
}

tvalid;