Is use of clojure.spec for coercion idiomatic?

Viewed 2367

I've seen use of clojure conformers to coerce data in various gists, but have also picked up an impression (I can't recall where though) that coercion (e.g. as follows) is not idiomatic use of conformers.

(s/def :conformers/int
  (s/conformer (fn [x]
                 (cond
                  (integer? x) x
                  (re-matches #"\d+" x) (edn/read-string x)
                 :else :cljs.spec.alpha/invalid))))

(s/def :data.subscription/quantity :conformers/int)

(s/def :data/subscription (s/keys :req-un [:data.subscription/quantity]))

Is it true that the above is unidiomatic/unintended? And if so, what would be appropriate/idiomatic use. Where do the boundaries of the intended use lie?

3 Answers
Related