Fork me on GitHub
#malli
<
2022-06-16
>
pinkfrog05:06:40

Hi, is there some tool like https://github.com/Provisdom/spectomic but generates datomic schema from malli instead of spec ?

Ben Sless05:06:41

I tried writing one once, I guess you could port some ideas from here and write a transformer like there is for json schema

pinkfrog07:06:30

Are you saying porting ideas from the above repo?

eskos07:06:00

I don’t think Malli’s transformers can do 1-to-n replacements (eg. single value to map), although I haven’t tested the behavior for such case. That said, I don’t think it should be that hard to create equivalent behavior with some helper functions and whatnot, plus one area of malli I haven’t explored is the raw walk api, which may work just fine for this 🙂

juhoteperi08:06:08

Should work. E.g.: [:map {:decode/string (fn [s] (if (string? s) (js/JSON.parse s) s))} [:foo :keyword]] Decode function for the map schema is run before checking map properties, and decoding those. At least I've used this in cases where API takes comma separated string, and it is decoded to seq/vec and then items are coerced to keywords or such. Not sure if this answers the original question, but 1-to-n transfomer tranformations should work.

👍 1
juhoteperi08:06:17

(decoding is one use/type of transformer)

eskos11:06:07

Okay, that’s great. Tangentially, I just did a Thing™ which I didn’t mean to but can be abused to do something like this 😅 I’ll create another thread about it since it’s sort of a pre-release now.

eskos11:06:59

There’s that post. So yeah, https://github.com/esuomi/muotti/ can be abused like so:

(require '[malli.core :as malli])
(require '[muotti.core :as muotti])
(require '[muotti.malli :as mm])

(malli/decode
  [:string]
  1
  (mm/transformer (muotti/->transformer 
                    {:transformations {[:int :string] {:transformer (constantly {:a 1 :b "wow"})}}})))
Obviously custom schema registry support needs to be added to make this even remotely sensible :rolling_on_the_floor_laughing:

eskos11:06:07

Just released https://github.com/esuomi/muotti/, a graph-based value transformation chain library which has integration with malli’s transformers.

awesome 2
🏆 2
eskos11:06:18

Malli support is still somewhat in progress as malli has tons of schemas and some can be considered quite subjective (eg. for value transformation, what does [:and pos? :double] mean, if input is “3”) but maybe those can be supported eventually as well. For now core types are supported.

eskos11:06:37

And this is a sort of pre-release for now which is why I put this here, not #announcements - have to let the dust settle first 🙂

ikitommi12:06:29

Looks great! like byte-streams, but for (literal) data. Malli will have effective and derived types, will simplify things a lot, https://github.com/metosin/malli/issues/264

eskos12:06:38

Thanks! 😸 Figuring out which schemas to support and how is going to be an on-going process, I think - I’m going to exclusively avoid doing validation with muotti, naturally. My intention is to start using this in an actual project quite soonish, so I’m hoping that’ll let me refine the native support and providing sensible defaults. And as curiosity, muotti was made almost entirely during a few train trips between Helsinki and Tampere… 🙂