Fork me on GitHub
#malli
<
2022-07-29
>
pppaul02:07:08

this looks like what multi is designed for, i haven't used multi, i'm curious where it fails in this case.

Mateo Difranco14:07:14

Well, I might be wrong, but take a look at this:

(def Something
  [:map
   [:something string?]
   [:type [:enum :a :b]]
   [:deep
    [:multi {:dispatch :type}
     [:a [:map [:depends int?]]]
     [:b [:map [:another [:vector int?]]]]]]])

(m/validate
 Something
 {:something "test"
 :type :a
 :deep {:depends 3}}) ;=> false
And if I do a really ugly side effect, we can look at what exactly the dispatch function takes:
(def Something
  [:map
   [:something string?]
   [:type [:enum :a :b]]
   [:deep
    [:multi {:dispatch (fn [x] (or (println x) :type))} <-------------
     [:a [:map [:depends int?]]]
     [:b [:map [:another [:vector int?]]]]]]])

(m/validate
 Something
 {:something "test"
 :type :a
 :deep {:depends 3}}) ;=> false, but also prints {:depends 3}

valtteri16:07:53

You probably should do the multi for the parent of :deep

valtteri16:07:34

If you need to dispatch on the :type of the parent

Mateo Difranco16:07:16

I thought about that, but then how would I avoid duplication of the structure? For example, if I move :multi to the top of the schema, I would need to make each possibility contain the whole structure right? In this case, :a would have a schema matching :something for example. Maybe I can merge in each possibility though, but I was wondering if there was a better solution.

valtteri16:07:55

Yep, I guess that's whats you gotta do. You can splice the sub-schemas into smaller chunks for reuse

valtteri16:07:12

There might be some better way I'm not aware of 🙂

annarcana16:07:42

are there any SQL libraries that work with malli?

valtteri16:07:08

Depends on what you mean with that. :) We have experimental tool that derives malli schemas from sql table schema

annarcana18:07:11

Yes! This looks like what I had in mind (data-driven SQL schemas)

annarcana18:07:19

We are already using malli to generate reitit routes from data models, so one thought was to also generate SQL table schemas from same

annarcana18:07:11

I suppose any data-driven library like gungnir could work but something that already can do malli->SQL type translation was something I was hoping would be out there somewhere

colliderwriter18:07:24

afaict, this does not do schemas but it seems to have all the parts

annarcana18:07:12

it can do table create/drop migrations which should be enough for my purposes, but I'll have to dig into it

colliderwriter18:07:03

i'd be interested to hear your thoughts