Fork me on GitHub
#reitit
<
2021-07-09
>
n2o15:07:55

Hello, I’ve got a question regarding the spec coercion. When I define a value of a parameter as a vector, it gets not coerced and remains a string, which is why there occurs an error when using the route. Let’s assume this spec:

(s/def ::reaction #{:attack :support :neutral})
and this as part of the ring/router
["/foo" {:post doesnt-matter-handler
         :parameters {:body {:reaction ::reaction}}}]
When I then curl the route with "attack" packed in a json request, I get a spec coercion error:
curl -X POST "" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"reaction\": \"attack\"}"

{
  "spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$46451/reaction]), :type :map, :leaf? false})",
  "problems": [
    {
      "path": [
        "reaction"
      ],
      "pred": "#{:support :neutral :attack}",
      "val": "attack",
      "via": [
        "spec$46451/reaction"
      ],
      "in": [
        "reaction"
      ]
    }
  ],
  "type": "reitit.coercion/request-coercion",
  "coercion": "spec",
  "value": {
    "reaction": "attack"
  },
  "in": [
    "request",
    "body-params"
  ]
}
Also, when I post ":attack" (prefixed with a : ), I get the same error. How can I achieve spec coercion to these three specified values defined in the spec ::reaction ? Thanks for your help 🙂

n2o15:07:51

for the other specs etc., coercion works wonderful. But for these values it just does not. I can not find examples for this case neither in the reitit nor in the spec-tools docs.

n2o15:07:19

When I use transit from my cljs frontend, everything works as expected. But I need to open my API for other applications and they use ‘json’.