Fork me on GitHub
#malli
<
2024-01-24
>
Marius12:01:38

I think I found a bug regarding Swagger Schema generation: Only definitions from schemas referred to in responses are shown in /definitions, but those from requests are missing. Example: With the following route config

:parameters {:body [:map [:company ::validation/company1]]}
        :responses {200 {:body ::validation/company2}}
only company2 will be in definitions (although the ref$to both def鈥檚 are there). I looked through the issues on Github and could not find anything related - is this a known issue or should I open a new one?

tomc15:01:34

A philosophical question about Malli, and schema checking in general: how much validation is too much? In my application, we have a datomic database wherein entities reference each other with non-component refs. In the maps I'm validating with Malli, these reference attributes have values that look like {:db/id int}. To only check that the map looks like that (has a :db/id key with an integer value) isn't quite sufficient to know the data is valid - I'd also like to check that the referenced entity is valid to be referenced from this attribute (perhaps by checking the attributes on the referenced entity). I can do this by getting access to a Datomic db instance in a :fn schema, and doing arbitrary queries. This leads to my conundrum. Should I do that? Am I setting myself up for problems later? My instinct is to include that validation code but make it conditional on whether or not there's a Datomic db in a *db* dynamic variable. Perhaps someone can convince me this is a good or bad idea.

Stig Brautaset15:01:18

I'm leaning towards validating (with Malli) that the entities are of the right shape, but not validate that correctly-shaped identifiers point to valid entities themselves (i.e. exists in a DB).

馃憤 1
tomc16:01:01

Thanks for the feedback. I'm going to try out the db query approach and I'll report back in a few months if it turns out I have driven myself insane

Stig Brautaset16:01:46

This has come up before in this channel, btw. You might want to try a search, in case the past recommendations were more convincing than mine 馃檪

tomc16:01:57

thanks, will do

Noah Bogart16:01:55

I would recommend against validating for valid/existing data, that will be 1) an incredible performance loss, and 2) harder to test (no generative testing, no stubs, etc)

Noah Bogart16:01:10

your integration test suite should cover that ground for you

Karol W贸jcik15:01:30

Is it possible to deref the referenced schema fully with refs as well?

ikitommi16:01:20

:refs can be recursive, so it could blow the stack.

Karol W贸jcik17:01:25

Hmm, that鈥檚 right but if the user could specify max-depth of recursion then the stack would not blow.

Karol W贸jcik17:01:40

But yeah, totally get the rationale! Thank you 馃檹

ikitommi17:01:35

+4loc later, a quick prototype of a input-output guard for :=>

nice 5
鉂わ笍 2
馃槏 2
馃敟 1
Karol W贸jcik21:01:31

Hmm, Why in function schema using {:ref "something"} in [:cat] is not allowed. The error message: :malli.core/potentially-recursive-seqex . I don't understand why malli suggest the function schema to be potentially recursive in this case 馃槷

ikitommi21:01:43

sequence schemas inside sequences are inlined - if the :ref is recursive, it could expand to unlimited sequence:

(def Schema [:cat :int [:ref #'Schema]])
would be expanded like this:
[:cat :int [:ref #'Schema]]
... expanded to ...
[:cat :int :int [:ref #'Schema]]
... expanded to ...
[:cat :int :int :int [:ref #'Schema]]
... expanded to ...
[:cat :int :int :int :int [:ref #'Schema]]
...

ikitommi21:01:29

you can wrap the :ref with something like :schema to mark it just to use one slot in sequence.

ikitommi21:01:34

from README:

(def Hiccup
  [:schema {:registry {"hiccup" [:orn
                                 [:node [:catn
                                         [:name keyword?]
                                         [:props [:? [:map-of keyword? any?]]]
                                         [:children [:* [:schema [:ref "hiccup"]]]]]]
                                 [:primitive [:orn
                                              [:nil nil?]
                                              [:boolean boolean?]
                                              [:number number?]
                                              [:text string?]]]]}}
   "hiccup"])

(def parse-hiccup (m/parser Hiccup))

(parse-hiccup
  [:div {:class [:foo :bar]}
   [:p "Hello, world of data"]])
;[:node
; {:name :div
;  :props {:class [:foo :bar]}
;  :children [[:node
;              {:name :p
;               :props nil
;               :children [[:primitive [:text "Hello, world of data"]]]}]]}]

鉂わ笍 1
Karol W贸jcik23:01:11

Wow. This really works! Thank you!

Karol W贸jcik23:01:40

Thank you as well for all of the examples. Really helpful!

馃憤 1