clojure-spec 2025-04-09

Is there a way to pass context to rules when running s/valid?

I often times find myself needing extra info, such as checking the referential integrity of a key to some other part of the map, or perhaps even a call to something outside of the data-structure, but I have no way that I know of to do this with spec so I end up doing it another way

As a trivial example, say I have a structure like {:foo {“bar” 42 “baz” 43} :bat “bar”}

and I want some rule structure like

(s/def ::bat bat-valid?)
(s/def ::x (s/keys :req-un [::bat]))

but bat-valid? can only appropriately be written if it can check that [:foo “bar”] exists, can I somehow pass a context such that (in this case), I can check other parts of the tree via bat )

In the most general sense that means you should be using s/and with a predicate that checks the whole map

yeah, i was just thinking about that as I was asking my question

ty…ill give that a go

If you need more information to validate, then your spec should be applied at the level where the information is

That makes sense

There are maybe hacks you could do using binding, but they won't be very clean

Got it. I wasnt sure if there was something built-in to spec that I didnt know about

but the top-level validator makes sense, so ill try that approach