reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
wombawomba 2020-06-02T13:33:31.037500Z

I’m using Ring, and I’m looking to declare my routes and handlers separately (as in, I would like to have a routes structure with no handlers do something like (add-handlers route handlers)). Is there a straightforward way to do this?

wombawomba 2020-06-02T14:22:20.038400Z

Guess I’ll just go with making sure each endpoint has a :name and doing a postwalk to add the appropriate handlers 🙂

wombawomba 2020-06-02T16:05:13.039300Z

Follow-up question, if I have a map of routes, is there any utility in reitit that I can use to flatten it? I don’t want to create a router, I just want to iterate over the routes

wombawomba 2020-06-02T16:15:10.039700Z

Seems like (reitit.impl/resolve-routes my-routes (reitit.core/default-router-options) does the trick 🙂

Matheus Moreira 2020-06-02T19:05:47.040900Z

hello, people! can someone help me figure out why an error happens?

{
    "schema": {
        "action": "(conditional patrulleros.onering.http/fn--30490 {:type (eq :spaces-money-transfer), :amount (constrained java.math.BigDecimal patrulleros.onering.http/fn--30487), :reference-text Str, :source-space-id Uuid, :target-space-id Uuid})",
        "trigger": "(conditional patrulleros.onering.http/fn--30493 {:type (eq :calendar), :start-date Inst, (optional-key :end-date) Inst, (optional-key :frequency) (enum \"biweekly\" \"weekly\" \"daily\" \"monthly\")})"
    },
    "errors": {
        "action": "(not (patrulleros.onering.http/fn--30490 a-clojure.lang.PersistentHashMap))",
        "trigger": "(not (patrulleros.onering.http/fn--30493 a-clojure.lang.PersistentHashMap))"
    },
    "type": "reitit.coercion/request-coercion",
    "coercion": "schema",
    "value": {
        "action": {
            "amount": 10,
            "type": "spaces-money-transfer",
            "source-space-id": "937bcca6-d482-4b93-9fa1-540773eb9ede",
            "reference-text": "reftext",
            "target-space-id": "52dbc61b-1409-46f6-bad9-5136eeab913f"
        },
        "trigger": {
            "end-date": "2020-12-15",
            "frequency": "monthly",
            "type": "calendar",
            "start-date": "2020-06-15"
        }
    },
    "in": [
        "request",
        "body-params"
    ]
}

ikitommi 2020-06-03T17:38:06.045600Z

You could run the schema coercion from repl to see why the s/conditional fails.

ikitommi 2020-06-03T17:38:34.045800Z

what is the guard function in those conditional?

ikitommi 2020-06-03T17:39:12.046100Z

the code is in reitit.coercion.schema.

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 %)))

Matheus Moreira 2020-06-02T19:07:11.041Z

if i understood correctly, the error says that action and trigger are not maps but they are sent in the request as a json value where both properties are json objects.

Matheus Moreira 2020-06-02T19:08:27.041200Z

the middlewares that i use are the same as those shown in https://github.com/metosin/reitit/blob/master/examples/ring-swagger/src/example/server.clj minus multipart.

Matheus Moreira 2020-06-02T19:08:53.041500Z

i use schema for validation and coercion.

Matheus Moreira 2020-06-02T19:15:06.041700Z

request: curl -X PUT --data-binary @rule.json -H "user-id: 1368d863-c5e6-40e4-9c74-9e7e1aedcb61" -H "Content-Type: application/json" -H "Accept: application/json" <http://localhost:3000/api/rules>