Fork me on GitHub
#ring-swagger
<
2022-02-12
>
Dumch07:02:08

Is there an option to provide optional file spec? I am using :require [[reitit.ring.middleware.multipart :as multipart]] provide middleware multipart/multipart-middleware and set up parameters as :parameters {:multipart {:logs multipart/temp-file-part}} when I tried something like (spec/nilable multipart/temp-file-part) , it doesn’t work.

Dumch07:02:33

So I have to provide two routes instead of one with the ability to pass files optionally

ikitommi07:02:44

looking at https://cljdoc.org/d/metosin/spec-tools/0.10.5/doc/data-specs, maybe :parameters {(ds/opt :logs) multipart/temp-file-part}

👍 1
Dumch08:02:44

thank you, but it still

Dumch09:02:00

but your answer helped me to find a solution {:logs (ds/maybe multipart/temp-file-part)}

Dumch09:02:13

oh, now when I send emty value, it sends empty string “” and spec predicate failes

Dumch09:02:41

tried (ds/opt :rules) (ds/maybe multipart/temp-file-part) output is

{
  "spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$28868/logs] :opt-un [:spec$28868/rules]), :type :map, :leaf? false})",
  "problems": [
    {
      "path": [
        "rules",
        "clojure.spec.alpha/nil"
      ],
      "pred": "nil?",
      "val": "",
      "via": [
        "spec$28868/rules"
      ],
      "in": [
        "rules"
      ]
    },
    {
      "path": [
        "rules",
        "clojure.spec.alpha/pred"
      ],
      "pred": "clojure.core/map?",
      "val": "",
      "via": [
        "spec$28868/rules"
      ],
      "in": [
        "rules"
      ]
    }
  ],
  "type": "reitit.coercion/request-coercion",
  "coercion": "spec",
  "value": {
    "logs": {
      "filename": "bugreport-33345431500100001233071f-1.74.52-01-16-2022-11-46-19.zip",
      "content-type": "application/zip",
      "tempfile": "/var/folders/gm/sgh_2g690cn85ybhmv1phs5c0000gq/T/ring-multipart-1380586888493316976.tmp",
      "size": 4644776
    },
    "rules": ""
  },
  "in": [
    "request",
    "multipart-params"
  ]
} 

Dumch11:02:14

I figured out how to force it to work. This is the final spec

:rules 
(ds/maybe
 (spec-tools.core/spec
   {:spec         (spec/or
                    :file multipart/temp-file-part
                    :nan string?)
    :swagger/type "file"}))
and in my handler I check if (string? rules)