Open irigarae opened 8 months ago
I've had to do something similar in the past for accumulating state between :error fns.
(defn mk-stateful-schema
[]
(let [state (atom {})]
[:and
[:map
[:scheme :string]
[:colours [:vector
[:map
[:colour :string]
[:hex #"^#[a-z0-9]{6}$"]]]]]
[:fn
{:error/fn
(fn [& _]
(format "The following schemes have duplicate colors: %s" (:dupes @state)))}
(fn [{:keys [scheme colours]}]
(->> colours
(map :colour)
(reduce
(fn [{:keys [seen] :as acc} m]
(if (contains? seen m)
(update acc :dupes conj {:scheme scheme :colour m})
(update acc :seen conj m)))
{:seen #{} :dupes #{}})
(reset! state)
(#(empty? (:dupes %)))))]]))
(malli-error/humanize
(malli/explain (mk-stateful-schema)
{:scheme "foo"
:colours [{:colour "duplicated" :hex "#000000"}
{:colour "secondary" :hex "#111111"}
{:colour "highlight" :hex "#222222"}
{:colour "duplicated" :hex "#333333"}]}))
This should be obsolete once https://github.com/metosin/malli/issues/975 is done.
Hi,
I wonder if it's possible to output multiple errors from a single
:fn
and if not, how do you usually handle it. Here's an example in which I have a function to validate the uniqueness of names in an array:How do you usually handle these kind of validations? How do you report the individual places that fail validation?
I think what I would expect is an
:fn
that behaves the opposite way, instead of returning truthy for valid, would returnnil
, otherwise return a bunch of[{:error/message "" :error/path []}]
, but maybe that's far from the way schemas are walked and errors reported in malli.Anyway interested to see what's the recommended approach for these kind of validations.
Thank you