I need to validate json schema only when there is 200 status code for the API. Can we have a script assetion created from which different assertions are called depends on the status code condition.
there is a page that in the soapui.org help that includes some code to assert against an xml schema (the .xsd) - you can likely tailor this to do your json schema validation, but before you start looking at that, I have a question.
What's the reason you want to add a schema validation in your tests? The reason I ask is that be definition - if your request is validated (which it is if you have a .json schema) each time you publish to the web service the request is validated, so it's kindof moot that you'd add extra hassle by adding this in groovy script assertion to test this in your testing when you can do the schema valid/invalid testing without the hassle.
What I mean is - you are going to tailor requests that are both valid and a number of negative tests to ensure the invalid requests are handled correctly - right? With this in mind - I don't know why you don't setup your requests (both positive (schema valid) and negative (schema invalid)) - then say create 2 separate looping tests (1 positive (schema valid) returning a 204 or whatever is the expected response and 1 negative (schema invalid) returning a 401 or whatever is the expected response).<-- this would satisfy your schema testing without having to add in a script assertion to do this
If you execute these tests you'll assert that only the valid ones return a 204 and the invalid ones return 401 or whatever. This doesn't require script assertions at all - the OTB/embedded functionality will handle alll this using either the 'Valid HTTP Status Codes' and the 'Invalid HTTP Status Codes' assertions or just the 'Valid HTTP Status Codes' assertion.
To be honest - I've always struggled with the purpose of using schema validation in my tests as unless the validator isn't present in the environment you are publishing to (which it nearly always is), there doesn't appear to be any additional value adding in the code to actually do schema validation assertions to validate against a schema as all your requests are being validated before being consumed by the webservice anyway and you can do your testing without going through that headache.
I'm not saying schema validation checks aren't useful - but there's easier ways to do this testing without bothering adding in the schema validation groovy to do this - that's all.
Just an opinion....