Fork me on GitHub
#ring-swagger
<
2015-12-10
>
timgilbert22:12:34

Hey, I have a random compojure-api question...

timgilbert22:12:23

I have a POST endpoint which takes a JSON body resembling {"organization": ["manufacturer" 3]} or {"organization": ["company" 45]}, where the values are tagged unions

timgilbert22:12:20

...and I'd like to coerce the input so that in Clojure-land I get back {:organization [:company 45]}

timgilbert22:12:50

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

juhoteperi22:12:18

@timgilbert: Default coercion already does string to keyword coercion so only thing you should need is a correct schema

timgilbert22:12:00

For some reason it's giving me back {:organization ["company" 42]} and then failing the schema validation

juhoteperi22:12:10

What does your Schema look like?

timgilbert22:12:12

I'll try to see if I can come up with a minimal case

timgilbert22:12:48

The tuple part looks like this:

(def CompanyDescriptor
  [(s/one (s/eq :company) "type")
   (s/one s/Int "company_id")])

juhoteperi22:12:18

Ah, coercion doesn't probably work with s/eq

juhoteperi22:12:33

you could use (s/one s/Keyword "type")

juhoteperi22:12:21

(s/enum :company) might work also, I think there is some enum to keyword coercer

timgilbert22:12:22

I also have an (s/either CompanyDescriptor ManufacturerDescriptor) higher up, with several similar types

juhoteperi22:12:35

either doesn't work with coercion either

timgilbert22:12:55

Dang. Ok, thanks for looking

juhoteperi22:12:15

s/conditional is preferred to s/either

timgilbert22:12:52

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?

timgilbert22:12:15

Yeah, I haven't got around to updating that, though I am on the most recent Schema

juhoteperi22:12:08

Your own coercion-matcher could look something like this:

(defn foo-coercion-matcher [s]
  (or (foo s)
      (rsc/json-schema-coercion-matcher s)))

juhoteperi22:12:53

Where foo would do your own logic

timgilbert22:12:23

I see. Is s the schema in that example?

timgilbert22:12:32

Awesome. Thanks a lot for the help! I'll try that and let you know how it turns out