'Query for Assertion using match for comparing double values in json

I am having one usecase where I am calculating some values in js and trying to assert those using match. Datatype for comparison is double. Is there any way I can use match for comparing double in json with some delta provided?

For now, I have written custom javascript function which perform this comparison. But I am more inclined towards using match as it is very cleaner approach.



Solution 1:[1]

Yes this is easy, first read: https://github.com/karatelabs/karate#self-validation-expressions

And here is a step by step implementation:

* def valid = (x, y) => Math.abs(x - y) < 0.2
* assert valid(1, 1.1)
* assert !valid(1, 1.3)

* def response = { value: 1.5 }
* match response == { value: '#? valid(_, 1)' } 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Thomas