Fork me on GitHub
#reitit
<
2020-07-14
>
William R. Arellano00:07:16

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?

ninjure07:07:24

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. Arellano22:07:04

Thanks @U23D6CRJN, 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}})}}])

🌻 3
Dave Simmons07:07:22

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

Dave Simmons07:07:00

(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 Simmons07:07:05

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 Simmons07:07:18

but this returns nil.

Dave Simmons07:07:22

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?

ikitommi07:07:56

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

Dave Simmons07:07:09

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

ikitommi08:07:08

:thinking_face:

ikitommi08:07:52

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

ikitommi08:07:01

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

ikitommi08:07:22

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.

ikitommi08:07:00

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

ikitommi08:07:32

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

abdullahibra08:07:17

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?

ikitommi09:07:41

@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.

ikitommi09:07:51

you could run with request chain debugging on

abdullahibra09:07:36

okay thank you

Dave Simmons10:07:19

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.

abdullahibra12:07:17

@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

abdullahibra12:07:48

;; 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)}}) }

abdullahibra13:07:50

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

abdullahibra16:07:56

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

manutter5116:07:35

@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

manutter5116:07:27

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

abdullahibra17:07:06

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

kenny22:07:03

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