clojure-spec

Lycheese 2022-07-08T08:57:11.488459Z

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?

reefersleep 2022-07-08T10:36:42.092509Z

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

Lycheese 2022-07-08T11:48:21.420099Z

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

reefersleep 2022-07-08T11:56:26.037939Z

(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

reefersleep 2022-07-08T12:10:55.517869Z

something like that?

reefersleep 2022-07-08T12:11:11.825429Z

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

reefersleep 2022-07-08T12:11:39.420209Z

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

reefersleep 2022-07-08T12:13:06.737299Z

Or simpler, without the map thing:

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

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

reefersleep 2022-07-08T12:18:38.750069Z

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

reefersleep 2022-07-08T12:19:12.362609Z

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

Lycheese 2022-07-08T12:59:07.666339Z

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