Schema
knows now the IntoSchema
that creates it. Fixes the issue @maciej.falski and makes schema copying (= all utils) faster.
two ways to create reusable rules for mutually exclusive keys for maps: 1. just data:
(defn exclusive-keys [keys]
[:fn {:error/message (str "the following keys are mutually exclusive: " (str/join ", " keys))}
(fn [m] (not (every? (partial contains? m) keys)))])
(-> [:and
[:map
[:foo {:optional true} :int]
[:blabla {:optional true} :int]
[:hah {:optional true} :int]
[:bar {:optional true} :int]]
(exclusive-keys [:hah :bar])
(exclusive-keys [:hah :foo])]
(m/explain {:foo 1 :hah 2})
(me/humanize))
; => #:malli{:error ["the following keys are mutually exclusive: :hah, :foo"]}
2. new Schema
impl (with pretty form):
(def Exclusive
(m/-simple-schema
(fn [_ [keys]]
{:type 'Exclusive
:min 1
:max 1
:pred (fn [m] (not (every? (partial contains? m) keys)))
:type-properties {:error/message (str "the following keys are mutually exclusive: " (str/join ", " keys))}})))
(-> [:and
[:map
[:foo {:optional true} :int]
[:blabla {:optional true} :int]
[:hah {:optional true} :int]
[:bar {:optional true} :int]]
[Exclusive #{:hah :bar}]
[Exclusive #{:hah :foo}]]
(m/explain {:foo 1 :hah 2})
(me/humanize))
; => #:malli{:error ["the following keys are mutually exclusive: :hah, :foo"]}
no macros = :awesome:
the latter is the not-well-documented reagent-style thing, any IntoSchema
can be used in first position in Schema AST and -simple-schema
allows one to read the properties & children at creation time and return a Schema instance based on those.
itโs bit like derived types schemas?