juxt / aero

A small library for explicit, intentful configuration.
MIT License
736 stars 60 forks source link

Integrate with clojure.spec #33

Open malcolmsparks opened 7 years ago

malcolmsparks commented 7 years ago

See this paper for why: https://blog.acolyer.org/2016/11/29/early-detection-of-configuration-errors-to-reduce-failure-damage/

bsima commented 7 years ago

Integration can be as simple as:

(def config
  (let [cfg  (aero/read-config (io/resource "config.edn"))]
    (if-not (spec/valid? ::conf-spec/config cfg)
      (throw (Exception. (spec/explain-str ::conf-spec/config cfg)))
      ;; if config is valid, return the map
      cfg)))

That's actually what I use in my codebase, albeit slightly modified (I removed mount's defstate and some custom error handling stuff, but the gist is the same).

::conf-spec/config is another namespace that is dedicated to specing my config map. It works really well for me.