malli

Dallas Surewood 2024-08-15T01:45:29.668369Z

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.

2024-08-15T02:17:34.763069Z

Could you demonstrate how you'd like a session to look like?

Dallas Surewood 2024-08-16T07:35:47.576879Z

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.

2024-08-16T17:11:23.141429Z

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.

Dallas Surewood 2024-08-15T14:26:37.494509Z

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 
  )

2024-08-15T17:14:58.381429Z

Perhaps try the clj-kondo integration. https://github.com/metosin/malli?tab=readme-ov-file#clj-kondo

Dallas Surewood 2024-08-15T18:04:35.040389Z

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.

2024-08-15T19:45:50.016609Z

do you mean like the output of m/explain?

Sardtok 2024-08-15T07:45:02.938699Z

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?]]]]

Sardtok 2024-08-15T08:18:01.408389Z

This question might actually be more relevant in the #reitit channel, as it is reitit's OpenAPI handler that generates the JSON.

opqdonut 2024-08-15T09:13:38.255339Z

what are you expecting to see in the OpenAPI docs?

opqdonut 2024-08-15T09:14:20.738379Z

anyOf?

opqdonut 2024-08-15T09:14:36.198069Z

what is it outputting currently?

opqdonut 2024-08-15T09:15:45.650679Z

:or should translate to "anyOf", but I'm not sure how something like swagger ui handles that

Sardtok 2024-08-15T09:46:46.971309Z

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.

Sardtok 2024-08-15T09:47:24.188059Z

{"get":{"parameters":[],"...

opqdonut 2024-08-15T09:48:33.334839Z

interesting

Sardtok 2024-08-15T09:48:58.655789Z

In Swagger UI the Swagger 2 docs show only the "required" parameter.

opqdonut 2024-08-15T09:49:56.149339Z

is this a query parameter? those get special treatment compared to things like POST bodies.

Sardtok 2024-08-15T09:50:11.419889Z

Yes, it's a query param.

opqdonut 2024-08-15T09:51:04.622369Z

I'm not sure that's actually expressible in OpenAPI3 then, let me dig around

opqdonut 2024-08-15T09:51:28.106769Z

the query param case for OpenAPI3 is currently expecting the toplevel schema to be :map

Sardtok 2024-08-15T09:51:30.197689Z

I can easily enough have both as optional, and just handle the validation outside of the schema.

opqdonut 2024-08-15T09:52:13.863439Z

that should work nicely with the current openapi impl, at least

opqdonut 2024-08-15T09:54:21.009779Z

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.

opqdonut 2024-08-15T09:55:02.416259Z

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

👍 1