Fork me on GitHub

is it possible to evaluate spec description?


(def big-integer 1000)
(s/def ::small-integer {:spec (fn [n] (n < big-integer)) :description (str "Smaller than " big-integer)})


(s/explain-data ::small-integer 5000)




#:clojure.spec.alpha{:problems [{:path [],
                                 :pred {:spec (fn [n] (n < big-integer)),
                                        :description (str "Smaller than " big-integer)},
                                 :val 5000,
                                 :via [:xxx.spec/small-integer],
                                 :in []}],
                     :spec :xxx.spec/small-integer,
                     :value 5000}


I woud expect Smaller than 1000 in the description instead


the following one works, but I want to see this in the explanation


(-> (s/form ::small-integer) :description eval)


It's easy to hack a (simple) meta registry to do that without abusing the internals of clj.spec.


I did that and a few other horrors here if you re looking for inspiration


Look for spex/with-doc in the readme


gist of it:

(-> (s/def ::foo any?)
    (spex/with-doc "That's a foo"))

(spex/doc ::foo) => "That's a foo"


there's a coll-of but no corresponding one-of; i can go from a scalar spec to a collection spec, but i don't think i can go from a collection spec to an element spec. is that intentional? i'm wondering because i'm currently speccing a text editor, and it's going to have a collection of working documents as well an active document, which would naturally be one of the working documents


seems like you just need a set


or I am misunderstanding


i presently have this:

(s/def :world.sometimes.nota.editor/working-notes
             :kind vector?
             :distinct true))

(s/def :world.sometimes.nota.editor/active-note

  (s/keys :req [:world.sometimes.nota.editor/working-notes


but it feels wrong because active-note isn't just a random note, it's only ever going to be one of the working notes

Alex Miller (Clojure team)14:05:44

this would be easier if the working-notes was a set (not sure why it isn’t) but you could s/and an additional check into editor that working-notes contains active-note


it's not a set because the active-note is going to be pulled from the working-notes, and sets would be the wrong data structure for that, right? sets are just about membership, as far as i know


i hadn't thought about putting an s/and on editor, that makes a lot of sense. thanks 🙂

Alex Miller (Clojure team)14:05:24

you’re declaring in working-notes that its elements are distinct. that’s the same guarantee sets give you but they can check membership in (near) constant time rather than via a linear search


yeah, i know. i had the :kind as set? before, but then i realised you couldn't pull arbitrary members out of a set; there's going to be a UI that lists all the working notes, and then you click on one to open it. i think a vector is a better choice given that use-case. i suppose i could have a vector and a set, but then i'd have to keep them in sync

Alex Miller (Clojure team)14:05:33

what do you mean by “you couldn’t pull arbitrary members out of a set” ? it seems like you can do exactly that

Alex Miller (Clojure team)14:05:51

do you mean you need to retain ordering?


i meant that to pull an item out of a set you need the item itself; i thought you couldn't iterate over a set, but i realise i was mistaken. in any case, i think i do need to retain ordering

Alex Miller (Clojure team)14:05:38

and that’s a good reason to use a vector, not trying to necessarily change your mind, just probing


i understand; i appreciate the questions. it did make me more fully analyse why i wanted it to be a vector, so thanks 🙂