This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-11
Channels
- # admin-announcements (26)
- # aws (1)
- # beginners (356)
- # boot (28)
- # cider (20)
- # clara (12)
- # cljs-dev (78)
- # cljsrn (22)
- # clojure (333)
- # clojure-brasil (1)
- # clojure-dev (15)
- # clojure-miami (1)
- # clojure-nl (3)
- # clojure-russia (61)
- # clojurecup (3)
- # clojurescript (137)
- # clojurex (4)
- # core-async (4)
- # data-science (3)
- # datavis (2)
- # datomic (31)
- # editors (1)
- # emacs (9)
- # hoplon (3)
- # juxt (8)
- # ldnclj (47)
- # leiningen (4)
- # luminus (4)
- # off-topic (20)
- # om (332)
- # onyx (1)
- # parinfer (23)
- # portland-or (4)
- # proton (161)
- # reagent (46)
- # ring-swagger (11)
- # specter (7)
- # yada (2)
Hi @juhoteperi... I got a little farther with that, but I'm a bit confused by how I access the actual data that is being considered for coercion in my (foo s)
function - ideally I'd have a way to access the ["company" 49]
bit of the incoming JSON
@timgilbert: For a good example matcher check: https://github.com/metosin/ring-swagger/blob/master/src/ring/swagger/coerce.clj#L25-L32
the matcher takes a schema as argument and returns a function, if schema is one that matcher wants to coerce, which takes in the value and returns either original or coerced value
Oh, I see. So I'm returning a function from data->data-which-matches-the-schema?
Pretty much, but if the value is one that matcher can't handle, you can just return the original data
Ok, gotcha. Thanks!
pluggable coercion is not the simplest thing around, but the tests should give nice intro: https://github.com/metosin/compojure-api/blob/master/test/compojure/api/coercion_test.clj
I got it more or less working, thanks for the help! I'm planning to write a little blog post about it for my company's tech blog at some point, but the gist of it was more or less this:
(defn descriptor-decode
"When the descriptor we're attempting to match is a descriptor schema, we try to convert its
first element from a String to a Keyword. If the resulting data structure matches the general
Descriptor schema, then we return it (so the value is coerced)."
[sc]
(when (re-matches #"^.*Descriptor$" (str (s/schema-name sc)))
(fn [data]
(if-not (sequential? data)
data
(let [coercion-attempt (into [(-> data first keyword)] (rest data))]
(if (nil? (s/check ds/Descriptor coercion-attempt))
coercion-attempt
data))))))
(defn descriptor-dispatch [sc]
(or (descriptor-decode sc)
(rsc/json-schema-coercion-matcher sc)))
I'm planning on just tagging the relevant schemas with metadata in the future instead of regex-matching on the name which seems kind of dumb, but that code does actually work
...where ds/Descriptor is a conditional schema that's sort of a supertype of eg ProductDescriptor and ManufacturerDescriptor