Fork me on GitHub
#malli
<
2023-02-17
>
Bingen Galartza Iparragirre13:02:24

Hello! Any hints about how I could add a custom decoder to all the schemas of a certain type? We have a custom registry defined like this:

{:object (m/-map-schema)}
so then we use :object in our Malli schemas. Is it possible to add a custom decoder to all the :object schemas? without having to explicitly set it in all the schemas that use :object

Bingen Galartza Iparragirre13:02:41

I see the following could be a possible workaround:

(def test-schema
  (m/schema
   [::object
    [:test
     [::double]]]
   {:registry {::object (m/-map-schema)
               ::double (m/-double-schema)}}))

(m/decode test-schema {:test 1.0}
          (mt/transformer
           {:name :test
            :decoders {:map {:enter #(prn %)}}}))
But I would like to tie the tranformer to object and not map

ikitommi06:02:26

Hi, there is no easy to way to do this atm as the ::object is “just a map” after schema lookup . I could add support for :type-properties into m/-map-schema so this would work:

(def test-schema
  (m/schema
   [::object
    [:test
     [::double]]]
   {:registry {::object (m/-map-schema {:type-properties {:decode/test {:enter #(prn %)}}})
               ::double (m/-double-schema)}}))

(m/decode
 test-schema
 {:test 1.0}
 (mt/transformer {:name :test}))

ikitommi06:02:56

if that would be ok, please write issue. fix = 1 line + test.

ikitommi06:02:01

explained: each IntoSchema can have (shared and immutable) type-properties - like Schema instances have properties. Schema application that need properties, can fallback to reading the properties from it’s parent.

Bingen Galartza Iparragirre13:02:57

Yep, that would perfectly fit our use case. We will open an issue. Thank you

Bingen Galartza Iparragirre14:02:38

Let us know if you think we could help with a PR

camdez14:02:42

Hi all! Having a bit of trouble with function instrumentation via metadata, maybe I’m missing something… I’m using ! , and instrumentation works great initially, but is lost if I re-evaluate the function. My understanding was that the dev tooling would watch the var and re-instrument. Is this normal? If so, any good solutions?

steveb8n14:02:24

I had a similar experience. I found that rerunning dev/start refreshes it when registry and source change a lot in dev

steveb8n14:02:29

In my case my source is capturing the latest state of the registry and was not updating. I suspect the watcher assumes a particular style of registry and mine is not that

steveb8n14:02:27

Rerunning dev/start is a workaround that has worked for me. I added it to my repl fiddle forms i.e. everywhere I invoke the instrumented code

camdez16:02:09

Thanks. I similarly see that it picks up the changes on re-runs, but I’m aiming to just validation all function calls at development time and I was hoping to not have to litter my code with additional calls. Also I typically just re-evaluate individual functions. Perhaps it creates a new var so it’s not instrumented? I’m sure I can hook into CIDER or have a file-watcher based solution, I’m just not certain if that’s typically needed. I appreciate the response!

🙂 2
camdez17:02:59

Ah, I see now Malli just watches the registry, not the vars in the registry. So it seems like things are working as expected, but it’s not convenient… so I either need a workaround or to switch to a different approach that touches the registry on re-eval.

camdez17:02:39

In a similar vein, if you want to instrument automated tests… any code with :malli/schema metadata not loaded at the time you enabled instrumentation would never get instrumented. So unless I’m missing something, it seems like the other instrumentation approaches are the way to go.

camdez18:02:39

I suppose you could turn on the instrumentation in fixtures and it would work out fine.

Thomas Moerman17:02:09

Hi gang, a question regarding opinion: • I implemented a custom malli schema using the IntoSchema and other protocols, inspired by the implementation of the standard library schemas (e.g :map, :map-of, etc.) I basically wanted a kind-of higher-order schema that (in function of its children/parameters) creates an internal schema, which is then used for further validation. • My question: is implementing custom schemas something you would recommend against, or is this considered generally OK? E.g. from the point of view of Protocol stability of the current alpha version vs. a later version? • If there's anyone who implemented their custom generic malli types, would you consider sharing the case/motivation? Cheers!

ikitommi06:02:06

Morning. It’s OK. Trying to keep protocols stable, haven’t changed much and if there is a good reason to change, it’s a minor bump (described in the CHANGELOG & https://github.com/metosin/malli#alpha documentation.

gratitude 2
ikitommi06:02:31

e.g. part of the extender API

Thomas Moerman07:02:46

Great! Thanks for your feedback.