reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
William R. Arellano 2020-07-14T00:01:16.245800Z

Hi, I am setting up a route with a path parameter and a query parameters I was wondering if it possible to set the query parameter as optional?

ninjure 2020-07-14T07:42:24.252Z

I just got same problem, and solved like this.

(:require [spec-tools.data-spec :as ds])
["/sites/:site-id/articles"
 {:get {:parameters {:path {:site-id int?}
                     :query {(ds/opt :newer_than) int?
                             (ds/opt :limit) int?}}
        :handler (fn [{{:keys [query], {:keys [site-id]} :path} :parameters}]
                   (let [body (articles/list-articles-in-json-by-site-id site-id query)]
                     {:status 200
                      :body body}))}}]
Hope it works for you too.

William R. Arellano 2020-07-14T22:23:04.280500Z

Thanks @ninjure, I did something similar:

(s/def ::field
  #{"address" "name"})

(s/def ::fields
  (s/coll-of ::field :into []))

(s/def ::get-consumer-query-params
  (s/keys :opt-un [::fields]))

(def consumer-with-fields-route
  ["/consumers/:id/"
   {:get {:summary "Get the consumer by id with the given fields if provided"
          :parameters {:path {:id int?}
                       :query ::get-consumer-query-params}
          :handler (fn [{{{:keys [fields]} :query
                          {:keys [id]} :path} :parameters}]
                     {:status 200
                      :body {:id id
                             :flds fields}})}}])

🌻 1
Dave Simmons 2020-07-14T07:11:45.246Z

Hi Everyone.

Dave Simmons 2020-07-14T07:12:22.246600Z

I have a question of Malli if I may. I have the following schema:

Dave Simmons 2020-07-14T07:13:00.246900Z

(def my-schema
  [:and
   [:map
    [:first-name [:and
                  string?
                  [:fn {:error/message "First Name: should be more than 3"} '(fn [value] (< 3 (count value)))]]]
    [:last-name [:and
                 string?
                 [:fn {:error/message "Last Name: should be less than 3"} '(fn [value] (> 3 (count value)))]]]
    [:small-number int?]
    [:large-number int?]]
   [:fn '(fn [{:keys [small-number large-number]}] (> small-number large-number))]])

Dave Simmons 2020-07-14T07:14:05.248100Z

I'd like to get access to the :first-name rules and I'm using Malli.util so (mu/get-in my-schema [:first-name] - as in the example docs.

Dave Simmons 2020-07-14T07:14:18.248400Z

but this returns nil.

Dave Simmons 2020-07-14T07:15:22.249600Z

If I remove the first [:and clause (so I then just have the :map - then everything works as expected. Does anyone know how I can just get the individual :map items i.e. first-name, last-name for my full schema above?

Dave Simmons 2020-07-14T07:15:27.249800Z

many thanks.

ikitommi 2020-07-14T07:29:56.251900Z

@shortlyportly would (m/get-in schema [0 :first-name]) work?

Dave Simmons 2020-07-14T07:58:09.254700Z

That works @ikitommi - many thanks for the suggestion. Unfortunately it doesn't work with the "[:and" removed (so for a simpler schema. I'm trying to get a generic solution so that whatever my schema definition I can pull out individual rules (which I then apply at the front end for individual fields).

ikitommi 2020-07-14T08:01:08.254900Z

:thinking_face:

ikitommi 2020-07-14T08:01:52.256300Z

anyway, wrote an issue, need to verify that all LensSchema imps have consistent paths: https://github.com/metosin/malli/issues/220

ikitommi 2020-07-14T08:06:01.259400Z

Here you could use [:first-name [:string {:min 3}]]

ikitommi 2020-07-14T08:11:22.263400Z

there is also mu/find-first you could use a the top to look up the first :map schema that has empty in argument (e.g. "at the top"). This way, you can do [:and rule1 [:map. ..] rule2] and it would still find it.

ikitommi 2020-07-14T08:13:00.266200Z

Or use find-first with non empty in to make it generic, "find the first schema that forces to value that is deeper"

ikitommi 2020-07-14T08:15:32.268100Z

(mu/find-first 
  schema 
  (fn [s in _] (if (seq in) s)))

ikitommi 2020-07-14T08:15:57.268500Z

-> #malli

abdullahibra 2020-07-14T08:52:17.270200Z

Hi @ikitommi, can you help me out with that https://github.com/metosin/reitit/tree/master/examples/pedestal-swagger, i can't handle invalid inputs, it gets broken if you pass invalid inputs?

ikitommi 2020-07-14T09:45:41.272500Z

@abdullahibra my guess is that there is a coercion error, which can’t be serialized as JSON, because there non-serializable. sorry, don’t have time to dig deeper right now.

ikitommi 2020-07-14T09:45:51.272800Z

you could run with request chain debugging on

abdullahibra 2020-07-14T09:46:36.273200Z

okay thank you

Dave Simmons 2020-07-14T10:52:19.273900Z

Many thanks @ikitommi - I'll have a play around with your suggestions. If I get a vote on the issue you raised I much prefer (m/get-in schema [:first-name]). Cheers.

abdullahibra 2020-07-14T12:59:17.274900Z

@ikitommi there is something i have observed too, which maybe related to the problem, i have changed parameters and response to use spec, but that make swagger not loaded

abdullahibra 2020-07-14T12:59:48.275600Z

;; New version (s/def ::request (s/keys :req-un [::x ::y])) (s/def ::response (s/keys :req-un [::total])) :post {:summary "plus with spec body parameters" :parameters {:body ::request} :responses {200 {:body ::response}} :handler (fn [{{{:keys [x y]} :body} :parameters}] {:status 200 :body {:total (+ x y)}}) }

abdullahibra 2020-07-14T13:00:15.275700Z

abdullahibra 2020-07-14T13:02:50.277100Z

http-swagger, and ring-spec-swagger work as expected, maybe there is a conflict on versions as @valtteri refered to me ?

abdullahibra 2020-07-14T16:51:56.277900Z

@ikitommi please if you can help on that whenever you have the time, i appreciate it 🙂

2020-07-14T16:58:35.278900Z

@abdullahibra I think there may be some answers in the spec-tools docs, at least as far as understanding the :parameters and :responses tags https://github.com/metosin/spec-tools

2020-07-14T16:59:27.279800Z

I got started looking at those but ran out of free time

abdullahibra 2020-07-14T17:00:06.280400Z

@manutter51 great i'll start looking at spec-tools, thank you so much

kenny 2020-07-14T22:56:03.281600Z

Updated to reitit 0.5.4 from 0.5.1 and received the below message on CI. Rolling back for now to fix the issue. Is there a reason why Trie needs to be compiled against Java 11?

UnsupportedClassVersionError: reitit/Trie has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0