How do use fuzzy matching validation for either a non-present key or an empty array?

Viewed 177

In karate version 0.9.6 I used the following match statement in a .feature file and it worked for validating the value to be an empty array or a key that was not present.

def example = {}
match example.errors == '##[0]'

In 1.0 the documentation example suggests that this should check for the key being present and null or an empty array and testing this fails with a validation error that the value is not present.

From https://karatelabs.github.io/karate/#schema-validation

# should be null or an array of strings
* match foo == '##[] #string'

This appears to be an undocumented breaking change from pre-1.0 to 1.0.

My question is: how do I construct a validator to cover this case correctly when the key is allowed to be absent but if it is present it must be an empty array?

2 Answers

I've found an undesirable solution for now but am leaving this open in case someone has a better answer.

I'm validating the entire parent object with a minimal schema:

Replace

match $.errors == '##[0]'

With

* match $ == { data: '#object', extensions: '##object', errors: '##[0]' }

While more brittle and verbose it is technically working.

This indeed looks like an in-intended breaking change. Here is another workaround:

* def example = {}
* def expected = example.errors ? '#[0]' : '#notpresent'
* match example.errors == expected

I see you have opened an issue here: https://github.com/karatelabs/karate/issues/1825

EDIT: this might be an improvement over the workaround you came up with in your answer:

* match example contains { errors: '##[0]' }
Related