Fork me on GitHub
#clojure-spec
<
2017-07-18
>
stathissideris13:07:27

is there any way to “merge” two map generators?

mpenet13:07:14

I guess you can cheat your way into it with (s/gen (s/merge x y))

stathissideris13:07:18

well, I don’t have specs for the maps I need to merge (so it’s not strictly a spec question!)

gfredericks13:07:19

(gen/let {m1 gen-m1, m2 gen-m2} (merge m1 m2))

stathissideris13:07:31

oh there’s gen/let!

stathissideris13:07:46

that’s great, thanks @gfredericks!

plins19:07:44

hello everyone, how can i validate a month inside a string? is the regexp approach the best? it should be a string containing 2 digits ranging from “01” to “12"

gfredericks19:07:58

it's certainly easy as a regex #"0[1-9]|1[0-2]"

jcf22:07:06

(into #{} (map #(format "%02d" %)) (range 1 13))
You could build a set of the strings as the count is small too. 🙂

jcf22:07:50

Then with clojure.spec you can do something like (s/def ::month-number-str (into #{} (map #(format "%02d" %)) (range 1 13))). And/or just write it out like so:

(s/def month-number-str
  #{"01" "02" "03" ...}

creese23:07:45

I'd like to generate a spec dynamically, where the name of the spec is returned from a function. Is there any prior art on this?

creese23:07:05

(defn register-spec
  [spec-k attr-ks]
  (s/def (keyword "foo" spec-k) (s/keys :req-un attr-ks)))

jcf23:07:57

@creese I'm not sure it's a good idea or that it'd work as expected, but the spec registry is an atom you can swap! etc.