This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-10
Channels
- # admin-announcements (32)
- # announcements (4)
- # aws (25)
- # beginners (296)
- # boot (1)
- # cider (87)
- # clara (16)
- # cljs-dev (7)
- # cljsrn (41)
- # clojure (121)
- # clojure-art (26)
- # clojure-japan (4)
- # clojure-miami (190)
- # clojure-russia (168)
- # clojure-sg (3)
- # clojure-sweden (13)
- # clojurescript (138)
- # clojurex (7)
- # cursive (98)
- # data-science (2)
- # datomic (129)
- # devcards (10)
- # editors (5)
- # funcool (1)
- # hoplon (31)
- # jobs (1)
- # ldnclj (4)
- # lein-figwheel (3)
- # off-topic (2)
- # om (213)
- # onyx (33)
- # parinfer (7)
- # portland-or (1)
- # re-frame (19)
- # reagent (2)
- # ring-swagger (27)
- # slack-help (3)
Hey, I have a random compojure-api question...
I have a POST endpoint which takes a JSON body resembling {"organization": ["manufacturer" 3]}
or {"organization": ["company" 45]}
, where the values are tagged unions
...and I'd like to coerce the input so that in Clojure-land I get back {:organization [:company 45]}
It seems like this should be possible with the :coercion
keys, but I'm at a bit of a loss about where to hook in to it. I don't want to convert every right-hand-side string to a keyword, but I do want to do it for everything matching a specific set of schemas
@timgilbert: Default coercion already does string to keyword coercion so only thing you should need is a correct schema
For some reason it's giving me back {:organization ["company" 42]}
and then failing the schema validation
What does your Schema look like?
I'll try to see if I can come up with a minimal case
The tuple part looks like this:
(def CompanyDescriptor
[(s/one (s/eq :company) "type")
(s/one s/Int "company_id")])
Ah, coercion doesn't probably work with s/eq
Oh, hmm
you could use (s/one s/Keyword "type")
(s/enum :company)
might work also, I think there is some enum to keyword coercer
I also have an (s/either CompanyDescriptor ManufacturerDescriptor)
higher up, with several similar types
either
doesn't work with coercion either
Dang. Ok, thanks for looking
s/conditional
is preferred to s/either
Is there some point I can hook into the coercion logic and supply a function that accepts the input subtree and the schema it's trying to match to?
Yeah, I haven't got around to updating that, though I am on the most recent Schema
You can set :coercion
option to a map with differenet coercers: https://github.com/metosin/compojure-api/blob/master/src/compojure/api/middleware.clj#L106
The value should be a function of Schema to function: https://github.com/metosin/ring-swagger/blob/master/src/ring/swagger/coerce.clj#L83
Your own coercion-matcher could look something like this:
(defn foo-coercion-matcher [s]
(or (foo s)
(rsc/json-schema-coercion-matcher s)))
Where foo would do your own logic
I see. Is s the schema in that example?
Awesome. Thanks a lot for the help! I'll try that and let you know how it turns out