When malli functions are instrumented, it will lint some of your function calls for you and let you know if there's a type mismatch. Is there a function I can run to output this check to the REPL instead? I like being able to see if I'm mismatching types on demand instead of looking at my warning list in my IDE.
Could you demonstrate how you'd like a session to look like?
That kind of output, yes. Or something similar. As far as I know m/explain checks if a value matches a schema, not if a namespace is mismatching function return types.
Malli only knows how to do that via runtime testing. You can do that via https://github.com/metosin/malli/blob/39244905665541c8d895c8b121b6ecf508fc2b18/src/malli/instrument.clj#L118-L119.
I was thinking of a malli function I could call manually that would read a namespace and see if I'm using those functions correctly. For instance, calling it on this namespace would return some kind of error.
(defn add-one
{:malli/schema [:=> [:cat :int] :int]}
[x]
(if (> x 3) 4 3))
(defn make-string {:malli/schema [:=> [:cat] :string]}
[]
"a")
(add-one (make-string)) ;; this would be an error
(comment
(malli/check *ns*) ;; Made up function that would read file for errors
)Perhaps try the clj-kondo integration. https://github.com/metosin/malli?tab=readme-ov-file#clj-kondo
That is pretty cool, but I was wondering if there'd be a way to get the actual check as data. kondo sometimes doesn't recognize every error and I don't like sorting through my IDE warnings for kondo errors. Considering Malli automatically does this for Kondo, I was wondering if there's some function that will show those errors.
do you mean like the output of m/explain?
I'm trying to get OpenAPI docs to generate correctly for an endpoint where there are two parameters, and one or both have to be supplied. Validation and coercion works fine with either of these schemas, but I can't get documentation to be correctly generated:
[:or [:map [:arg1 pos-int?]]
[:map [:arg2 pos-int?]]]
[:and [:map [:arg1 {:optional true} pos-int?]
[:arg2 {:optional true} pos-int?]]
[:fn #(or (some (:arg1 %)) (some (:arg2 %)))]]
[:multi {:dispatch :arg2}
[nil [:map [:arg1 pos-int?]]]
[::m/default [:map [:arg2 pos-int?]]]]This question might actually be more relevant in the #reitit channel, as it is reitit's OpenAPI handler that generates the JSON.
what are you expecting to see in the OpenAPI docs?
anyOf?
what is it outputting currently?
:or should translate to "anyOf", but I'm not sure how something like swagger ui handles that
If I generate Swagger 2, I get (EDN):
{:type "object",
:properties {:x {:type "integer", :format "int64", :minimum 1}},
:required [:x],
:x-anyOf [{:type "object", :properties {:x {:type "integer", :format "int64", :minimum 1}}, :required [:x]}
{:type "object", :properties {:y {:type "integer", :format "int64", :minimum 1}}, :required [:y]}]}
However, with OpenAPI 3, it's completely empty. This might be unrelated to Malli, and a pure reitit thing.{"get":{"parameters":[],"...interesting
In Swagger UI the Swagger 2 docs show only the "required" parameter.
is this a query parameter? those get special treatment compared to things like POST bodies.
Yes, it's a query param.
I'm not sure that's actually expressible in OpenAPI3 then, let me dig around
the query param case for OpenAPI3 is currently expecting the toplevel schema to be :map
I can easily enough have both as optional, and just handle the validation outside of the schema.
that should work nicely with the current openapi impl, at least
Yeah... by reading the OAS3.1 spec, I don't think there's any way to say "either this parameter or this parameter is required". Both of your args will be separate Parameter Objects, which can be either required or not, but there's nothing that can tie them together.
• https://spec.openapis.org/oas/latest.html#operation-object • https://spec.openapis.org/oas/latest.html#parameter-object
for something like a POST body on the other hand, you get the whole of json-schema for the body, so you can use something like anyOf