Search code examples
karate

How to validate schema with nullable properties in karate 1.4


I'm updating tests from karate 0.9.5 to 1.4.1. We have a lot of schema validation tests for objects with properties that can be null, but the tests are not working in the newer version of Karate.

Here's a very simplified example that works as expected when the sub property has content.

* def nested = {'name': '#string'}
* def schema = {'sub': '##(nested)'}
Then match {'sub': {'name': 'I am here'}} == schema

My understanding of the karate docs is that the ## on nested makes it an optional field, but when the sub property is null then this fails:

Then match {'sub': null} == '#(schema)'

What am I missing?


Solution

  • Yes, this behavior changed slightly. What to do is explained here: https://github.com/karatelabs/karate#remove-if-null

    Let me know if this now works for you.

    * def nested = {'name': '#string'}
    * def schema = ({'sub': '##(nested)'})
    * def response = { sub: { name: 123 } } 
    * match response == schema