spec-tools
spec-tools copied to clipboard
Nested data and s/map-of with #{} produced error when using decode or select-spec
Stumbled upon this situation where a certain spec fails when trying to use select-spec - but I don't yet understand why and how documentation or code should be improved. I managed to circumvent the problem by describing the spec differently.
What did not work:
(s/def ::title2 (st/spec (s/map-of #{:fi :en :se :sv} string?)))
What eventually worked:
(s/def ::title2 (st/spec (s/map-of (s/and keyword? #{:fi :en :se :sv}) string?)))
Below code that shows the behaviour.
(require '[clojure.spec.alpha :as s]
'[spec-tools.core :as st])
(s/def ::title2 (st/spec (s/map-of #{:fi :en :se :sv} string?)))
(s/def ::subject2 (st/spec (s/keys :opt-un [::title2])))
(s/def ::subjects2 (st/spec (s/coll-of ::subject2)))
(s/def ::demo (st/spec (s/keys :req-un [::subjects2 ] )))
(def demodoc {:subjects2 [{:id "xxx" :title2 {:fi "TURVE" }}]})
(st/select-spec ::demo demodoc)
AssertionError Assert failed: missing spec predicate
spec spec-tools.core/create-spec (core.cljc:418)
(s/def ::title2 (st/spec (s/map-of (s/and keyword? #{:fi :en :se :sv}) string?)))
(st/select-spec ::demo demodoc)
=> {:subjects2 [{:title2 {:fi "TURVE"}}]}
Was this also related to https://dev.clojure.org/jira/browse/CLJ-2251 and I just did not spot that in the docs.