reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
orestis 2020-06-04T11:39:44.047200Z

I’m looking at reitit.frontend.controllers — is there a way to “hang” some data to the match object? Or are the start/stop functions only for side effects?

orestis 2020-06-04T11:45:12.047600Z

Looking at the code, it seems not.

2020-06-04T12:05:35.048500Z

I’d look at the identity function, it takes a match and returns an arbitrary value, so a good place to hook in some extra data before it gets passed to start/stop

juhoteperi 2020-06-04T12:09:18.049800Z

Apply-controllers is also just a 50 lines of code and you are calling it from your route change handler, you could just call your own code from the same place, no need to use controllers for this.

orestis 2020-06-04T12:17:18.050400Z

@juhoteperi yeah this is what I ended up doing. Literally 2 lines of code 😄

1👍
Matheus Moreira 2020-06-04T15:37:34.050600Z

@ikitommi i solved the problem, one coercion wasn’t working well: (schema/constrained (schema/pred decimal?) #(> % 0M)), i am not sure why. i changed (schema/pred decimal?) to java.lang.BigDecimal and it started to behave better.

Matheus Moreira 2020-06-04T15:42:01.050800Z

now i have a different problem: validation and coercion work for the request but not the response and i suspect this is the culprit:

(def reitit-coercion
 (reitit.coercion.schema/create
  (assoc-in reitit.coercion.schema/default-options
   [:matchers :body :formats "application/json"]
    (some-fn amount-matcher
             date-matcher
             schema-tools.coerce/+json-coercions+
             schema.coerce/keyword-enum-matcher
             schema.coerce/set-matcher))))

Matheus Moreira 2020-06-04T15:43:58.051100Z

i don’t know if this is the correct way to “extend” the default coercion. what i want is to register coercions for BigDecimals and java.time.LocalDate.

Matheus Moreira 2020-06-04T15:47:21.051300Z

the schemas are:

(def Amount (schema/constrained java.math.BigDecimal #(> % 0M)))

(def Date java.time.LocalDate)
and the corresponding matchers:
(defn amount-matcher [schema]
  (when (and (instance? schema.core.Constrained schema)
             (= (.schema schema) java.math.BigDecimal))
    bigdec))

(defn date-matcher [schema]
  (when (= schema java.time.LocalDate)
    #(java.time.LocalDate/parse %)))

Tanel 2020-06-04T18:07:26.052Z

Is there a way to have optional query parameters when coercing with clojure.spec? I have the following, but the request fails if parameters are missing:

{:get {:parameters {:query {:limit int?
                            :offset int?}}
Edit: Nevermind, I solved it by using your `spec-tools.data-spec/opt`