Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Created February 28, 2017 05:01
Show Gist options
  • Save olivergeorge/aeaf318439c745570a65c5be92aacae6 to your computer and use it in GitHub Desktop.
Save olivergeorge/aeaf318439c745570a65c5be92aacae6 to your computer and use it in GitHub Desktop.
(def reg
{::doc '(keys [::id ::title ::summary ::authors])
::doc2 '(keys [::title])
::title :field/text
::summary :field/text
::authors '(and (coll-of ::author) :field/many)
::author '(keys [::id ::name])
:field/text (fn [data] {:value data :type :text})
:field/many (fn [data] {:value data :type :many})})
(declare conform)
(defn conform-key
[data k]
(println :conform-key data k)
(let [simple-k (keyword (name k))]
(assert (contains? data simple-k) (str "Expected" simple-k " in " (keys data)))
(update data simple-k conform k)))
(defn conform
[data spec]
(let [c (get reg spec)]
(cond
(keyword? c) (conform data c)
(seq? c) (let [[a b] c]
(println :a a :b b)
(cond
(= a 'keys) (reduce conform-key data b)
(= a 'coll-of) (do (assert (coll? data) (str "Expected a collection, got " (pr-str data)))
(map #(conform % b) data))
(= a 'and) (reduce conform data (rest c))))
(ifn? c) (c data)
:else data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment