metosin / spec-tools

Clojure(Script) tools for clojure.spec

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`strip-extra-keys-transformer` don't strip keys in lists

renanpalmeira opened this issue · comments

commented

I trying strip extra keys of lists using strip-extra-keys-transformer but don't working, follow a example:

(s/def ::city string?)
(s/def ::address (s/keys :req-un [::city]))
(s/def ::addresses (st/spec (s/* ::address)))
(s/def ::user (st/spec (s/keys :req-un [::name ::address ::addresses])))

(def rick
  {:name "Rick"
   :address {:city "Seattle"
             :state "Washington"}
   :addresses [{:city "Seattle"
                :state "Washington"}]})

;; i trying with 'decode'
(st/decode ::user rick st/strip-extra-keys-transformer)
=> {:address {:city "Seattle"}, :addresses [{:city "Seattle", :state "Washington"}], :name "Rick"}

;; i trying with 'coerce'
(st/coerce ::user rick st/strip-extra-keys-transformer)
=> {:address {:city "Seattle"}, :addresses [{:city "Seattle", :state "Washington"}], :name "Rick"}

When is a map the keyword :state was striped (:address {:city "Seattle"}) but when is a list
the key :city continue after run decode or coerce (:addresses [{:city "Seattle", :state "Washington"}])

Versions

  • Clojure 1.10.1
  • metosin/spec-tools 0.10.1

Hello @renanpalmeira , I could reproduce your situation at master too. However, as an alternative implementation could you change your addresses spec to:

(s/def ::addresses (spec (s/coll-of ::address)))

and the decode will remove the undesired state key from your list of maps.

I investigate this problem a little further and found some interesting bits, by now there are no implementation of parse-form for the following specs:

; *
; +
; ?
; alt
; cat
; &
; keys*

therefore the type assigned to these specs are always nil and the transformer cannot find the appropriate fn to perform the transformation..

Seems like you can always re-write your specs in a different way to avoid the regexp ones. I would like to hear from @ikitommi if any previous discussions was done about these specs.