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?
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.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}})}}])
Hi Everyone.
I have a question of Malli if I may. I have the following schema:
(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))]])
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.
but this returns nil.
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?
many thanks.
@shortlyportly would (m/get-in schema [0 :first-name])
work?
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).
:thinking_face:
anyway, wrote an issue, need to verify that all LensSchema imps have consistent paths: https://github.com/metosin/malli/issues/220
Here you could use [:first-name [:string {:min 3}]]
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.
Or use find-first
with non empty in
to make it generic, "find the first schema that forces to value that is deeper"
(mu/find-first
schema
(fn [s in _] (if (seq in) s)))
-> #malli
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?
@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.
you could run with request chain debugging on
okay thank you
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.
@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
;; 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)}}) }
http-swagger, and ring-spec-swagger work as expected, maybe there is a conflict on versions as @valtteri refered to me ?
@ikitommi please if you can help on that whenever you have the time, i appreciate it 🙂
@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
I got started looking at those but ran out of free time
@manutter51 great i'll start looking at spec-tools, thank you so much
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