Fork me on GitHub
#malli
<
2020-11-14
>
ikitommi17:11:50

Schema knows now the IntoSchema that creates it. Fixes the issue @maciej.falski and makes schema copying (= all utils) faster.

👍 3
ikitommi19:11:54

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"]}

ikitommi19:11:28

no macros = awesome

ikitommi19:11:20

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.

ikitommi19:11:55

it’s bit like derived types schemas?