Fork me on GitHub
#clojure-spec
<
2018-03-31
>
gabriele15:03:53

how can i write a spec to validate a map like this: {[:type1 "args"] {:key-type1 value} [:type2 "args"] {:key-type2 value} ...(type-n)}

gabriele15:03:18

where the value is dependent on the first element of the list that composes the key

borkdude15:03:54

@gabriele.carrettoni First of all I would try to get the source of that data in a more workable format

borkdude15:03:35

I usually try to go from dynamic keys to something more regular

gabriele15:03:45

@borkdude i see, i wanted to use specs to do that

borkdude15:03:47

E.g.

(def data [{:type 1
            :args “args”
            :value value}
           {:type 2
            :args “args”
            :value value}])

borkdude15:03:07

And then you can spec that after transforming the awkwardly shaped data and verify if your normalization worked

gabriele15:03:19

thanks, i'll do that

borkdude15:03:15

@gabriele.carrettoni E.g.:

(def data {[:type1 "args"] {:key-type1 1}})

(defn transform-data
  [data]
  (for [[k v] data]
    (let [[type-key args] k
          other-key (keyword (str "key-"
                                  (name type-key)))
          value (get v other-key)]
      {:type type-key
       :args args
       :value value})))

(transform-data data) ;;=> ({:type :type1, :args "args", :value 1})