malli 2025-01-22

Hello Is there a way to remove a key from a schema or to create a mask and remove fields by merging the mask with the schema?

✅ 1

malli.util/dissoc

malli.util/select-keys

Thank you @joel.kaasinen 🙏 malli.util/dissoc seems just what I need

✅ 1

@joel.kaasinen from what I can see there is no dissoc in or way to remove a key inside a nested map, Would you a recursive function or is there a more idomatic / library supported way of doing it?

try combining the utils like (malli.util/update-in s [...] malli.util/dissoc ...)

🔥 1

Thx @ambrosebs

👍 1

Hello, Claude told me that I can do something like (! {:exception true}) to throw on bad parameters when instrumenting functions. I tried it, it works. I haven't found documentation for this behavior though. Is that something that is actually supported?

Looking in the source at ! , it says to see malli.core/-instrument for supported options. I don't see an :exception option there. It does call malli.core/-fail by default which throws an exception. Maybe this is working by accident and that :exception key is not used?

Yeah malli.core/-failsthrows, when you use malli.instrument/intrument you get an exception for calling a specced fn with bad args, but when I just did () without the {:exception true} bit I did get error messages but no exception thrown...

Ok. I don't see that :exception option anywhere.

yeah I know, it must work by accident somehow, I search the whole repo on github and found the keyword only once...

and it wasn't relevant...

Looks like with no args, the options default to {:report (pretty/reporter)}

And then in core/-nstrument it uses :report if present otherwise defaults to -fail

([props f options]
   (let [props (-> props
                   (update :scope #(or % #{:input :output :guard}))
                   (update :report #(or % -fail!)))
         s (-> props :schema (schema options))]
     (or (-instrument-f s props f options)
         (-fail! ::instrument-requires-function-schema {:schema s}))))

yeah, the -fail function is suposed to be redifined also, and is supposed to throw right after using that :report fn option, it just didn't throw the exception for me

What would be the best way to customize malli's internal caching of validator and explainer functions to take a ttl? The explainers in particular seem to get huge with large multi schemas.

I didn't get a chance yet. My first thought about the configuration is we might want both a global default to start and later add in the ability to override per schema. I'm not sure where the config would live. Caching is sort of a deployment concern and not part of the schema definition itself so am wondering if there is some way to keep it orthognal.

Thanks! I will experiment a bit.I saw that the Cache protocol assumes an atom but hand't considered using IAtom.

perhaps you could iterate over your registry and add-watch on every reachable -cache

(defn -add-ttl-cache-watcher [s ttl]
  (add-watch (-cache s) ::cache-ttl (fn [_ a o n]
                                      (cond
                                        (and (not (:validator o)) (:validator n))
                                        (future (Thread/sleep ttl)
                                                (swap! a dissoc :validator))

                                        (and (not (:explainer o)) (:explainer n))
                                        (future (Thread/sleep ttl)
                                                (swap! a dissoc :explainer)))))) 

or alter-var-root malli.core/-cache to something similar. I don't think there's a great way atm.

for the latter you can probably override -cache to be a core.cache cache, as long as you wrap it in an IAtom

looks like :ref has another internal cache that probably shouldn't be erased.

👍 1

@millettjon how'd it go? I wonder if we can flesh out the configuration of caches.

configuration in general: I would personally like to change the global (swappable) registry with global (swppable) options => one could easily configure from one place how these things work. .e.g

(malli.core/set-default-options!
 {:registry (malli.core/schemas)
  :malli.sci/options ...
  :malli.core/cache ...})

👍 1