Fork me on GitHub
#clojure-spec
<
2022-07-08
>
Lycheese08:07:11

How would I go about making a certain key necessary when that key can exist at different depths in a map (and it only needs to exist at least once)? Do I need to normalize the map and put it at one level or is there another way?

reefersleep10:07:42

Use clojure.walk to see if exists at least once?

Lycheese11:07:21

How would I go about incorporating that into a spec validity check?

reefersleep11:07:26

(s/def ::guarantee-key-in-map (write-your-fn-here))

 (s/def ::my-map
   (s/and ::guarantee-key-in-map
          (s/keys  :req [:other-key
... and so on

reefersleep12:07:55

something like that?

reefersleep12:07:11

(s/valid? ::my-map some-map)

reefersleep12:07:39

I don’t write that many specs, so don’t copy verbatim 😄 it probably doesn’t compile

reefersleep12:07:06

Or simpler, without the map thing:

(s/def ::guarantee-key-in-map (write-your-fn-here))

(s/valid? ::guarantee-key-in-map some-map)

reefersleep12:07:38

The key takeaway is that your spec is something separate from e.g. s/keys. I don’t think you can use s/keys to say arbitrary things about certain values in your map. But you can do so with a separate spec that you can s/and

reefersleep12:07:12

Anybody, feel free to take the wheel and show @U02FM0NNZAB how I’m wrong 🙂

Lycheese12:07:07

I think I only now got what Alex Miller was alluding to in the post above. Feel kinda dumb now. Thank you^^ I think I got my test case working the way I want it to. Now I just need to remember where I wanted to use this 😅

👌 1