This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-18
Channels
- # aws (3)
- # beginners (18)
- # boot (3)
- # cider (47)
- # clara (54)
- # cljs-dev (62)
- # clojure (104)
- # clojure-berlin (1)
- # clojure-denver (1)
- # clojure-italy (1)
- # clojure-nl (22)
- # clojure-russia (30)
- # clojure-spec (28)
- # clojure-uk (95)
- # clojurescript (31)
- # cloverage (1)
- # cursive (1)
- # datomic (17)
- # duct (4)
- # emacs (27)
- # fulcro (36)
- # graphql (1)
- # hoplon (1)
- # jobs-discuss (1)
- # lein-figwheel (1)
- # lumo (2)
- # off-topic (44)
- # om-next (5)
- # onyx (29)
- # precept (1)
- # re-frame (8)
- # reagent (7)
- # ring (1)
- # ring-swagger (2)
- # schema (4)
- # shadow-cljs (185)
- # spacemacs (21)
- # specter (59)
- # tools-deps (7)
- # vim (15)
- # yada (1)
Hello @ikitommi finally following on our conversation from https://clojurians-log-staging.clojureverse.org/schema/2017-10-16.html I gave your fn a try - it is not exactly the behaviour I am after. I would like to know if there is something that would help in schema-tools?
(require '[schema-tools.core :as st]
'[schema.coerce]
'[schema.core :as s])
(defn coercer [schema matcher {:keys [open?]}]
(let [f (if open? st/open-schema identity)]
(schema.coerce/coercer (f schema) matcher)))
(def schema {:a Long, :b [(s/maybe {:a Long, s/Keyword s/Keyword})]})
(def schema-coercer (coercer schema (constantly nil) {:open? true}))
(deftest coerce-data
(testing "coerces values correctly"
;; OK
(is (= {:a 1, :b [{:a 1, "kikka" "kukka"}], "kukka" "kakka"}
(schema-coercer {:a 1, :b [{:a 1, "kikka" "kukka"}], "kukka" "kakka"}))))
(testing "returns coerced data even if missing keys/errors"
;; KO
(is (= {:a 1}
(schema-coercer {:a 1}))))
(testing "leaves extra keys"
;; KO
(is (= {:a 1 :z "extra"}
(schema-coercer {:a 1
:z "extra"}))))
(testing "coerces nested data"
;; OK
(is (= {:a 1, :b [{:a 1, "kikka" "kukka"}], "kukka" "kakka"}
(schema-coercer {:a 1, :b [{:a 1, "kikka" "kukka"}], "kukka" "kakka"}))))
(testing "leaves extra nested data"
;; OK
(is (= {:a 1, :b [{:a 1, "kikka" "kukka"
:nested "keep-me-please"}], "kukka" "kakka"}
(schema-coercer {:a 1, :b [{:a 1, "kikka" "kukka"
:nested "keep-me-please"}], "kukka" "kakka"}))))
(testing "leave data"
;; KO
(is (= {:a 1, :b [{:a 1, "kikka" "kukka"}
{:b "keep-me-too"}], "kukka" "kakka"}
(schema-coercer {:a 1, :b [{:a 1, "kikka" "kukka"}
{:b "keep-me-too"}], "kukka" "kakka"}))))
)
Basically I am after a coerce that always returns the data, and coerces as
much as possible of it.
Validation is a secondary, although what I currently do is return {:data {...} :errors {}}
(and just log the errors).
However my code currently doesn’t handle nested data (was fine since it was only used on data from an SQL database, which was flat).
I would like to know if there is something that would help build the above in schema-tools?