These are chat archives for etorreborre/specs2

22nd
Jun 2016
Mehdi Mollaverdi
@mmollaverdi
Jun 22 2016 06:32
Hi everyone, is there a way to check if a key does not exist in a JSON using specs2 JSON matchers?
Eric Torreborre
@etorreborre
Jun 22 2016 06:37
@mmollaverdi you should be able to do jsonString must not(*/(“key”))
Mehdi Mollaverdi
@mmollaverdi
Jun 22 2016 06:40
The documentation says */(value) applies to values though, not keys, and that's what I'm experiencing now
Eric Torreborre
@etorreborre
Jun 22 2016 07:19
Right, I think the proper syntax is jsonString must not(*/(“key” -> “.*”.r))
Mehdi Mollaverdi
@mmollaverdi
Jun 22 2016 07:21
Yeah, I tried that but it passes also when the value is a JSON object
Seems like “key” -> “.*”.r checks that it's not any string, so arrays or maps (objects) won't match
Eric Torreborre
@etorreborre
Jun 22 2016 07:57
try this
import org.specs2.matcher._
 def anyJson: Matcher[JsonType] = (actual: JsonType) =>
    (true, “")
json must */(“key").andHave(anyJson)
Mehdi Mollaverdi
@mmollaverdi
Jun 22 2016 08:34
Thanks Eric. That works :thumbsup:
Eric Torreborre
@etorreborre
Jun 22 2016 08:40
Sorry for the approximations, I don’t use the json matchers very often and they have been developed a bit organically so the dsl might not be the best
Mehdi Mollaverdi
@mmollaverdi
Jun 22 2016 08:41
Ah, no worries. Thanks for your help