Fork me on GitHub
#malli
<
2021-09-27
>
jkrasnay13:09:25

Hi, I have a schema that looks like this:

(def MySchema [:or
                 :string
                 [:and
                  [:vector any?]
                  [:fn {:error/fn
                        (fn [{:keys [value]} _]
                          (str "Not found: " (pr-str value)))}
                   my-lookup]]])

jkrasnay13:09:46

When I run (-> (m/explain MySchema 42) (me/humanize)) the result is ["should be a string" "invalid type" "Not found: 42"]

jkrasnay13:09:12

What I’m hoping for is a way to organize the schema so that the error is something like “should be a string or a vector”, and if the value is a vector and my-lookup fails the error is simply “Not found: [:whatever]“. Is there a way to do this?

jkrasnay13:09:21

(also it would be nice if my-lookup didn’t get called if the value wasn’t a vector. I suppose [:and] does not short-circuit)

jkrasnay13:09:29

I suppose I could replace my-lookup with #(or (not (vector? %)) (my-lookup %)), then put an :error/fn on the :vector check that returns “should be a vector”.

jkrasnay14:09:56

Still interested if there’s a more elegant way to do this, though.

ikitommi14:09:39

@jkrasnay there is a hidden feature where you can add the error-properties into parent-nodes: in case a child errors, the too-most error is used instead, once for all errors. I was not happy with the impl and didn't need it in my client project, so decided not to release it.

jkrasnay14:09:41

OK, thanks. I’ll take some time to study these.

ikitommi14:09:49

:and and :or short-circuit & collect all errors - it would be easy to inject extra data to those explanations and add options to either schema itself (via properties) or the me/humanize to allow control how to work with them.

Nicholas17:09:24

Hello there, I've just started a very basic template with Shadow-cljs and Malli , but as I run npx shadow-cljs watch :app I get an error saying : *Wrong number of args (3) passed to malli.core/-fail!* . The project itself is just here : https://github.com/bitbot123/shadow-cljs-malli/blob/master/src/core.cljs (on the master branch) , and I've stuck [metosin/malli "0.6.1"] in shadow-cljs.edn , is this correct? Any help is much appreciated.

ikitommi18:09:58

oh well, released version with just the minimal fixes for that @shamansandtheprimes :

➜  ~ clj -Sforce -Sdeps '{:deps {metosin/malli {:mvn/version "0.6.2"}}}'
Downloading: metosin/malli/0.6.2/malli-0.6.2.pom from clojars