malli

https://github.com/metosin/malli :malli:
ikitommi 2020-11-14T17:26:50.050900Z

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

๐Ÿ‘ 1
ikitommi 2020-11-14T19:47:54.056700Z

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

ikitommi 2020-11-14T19:49:28.057200Z

no macros = :awesome:

ikitommi 2020-11-14T19:52:20.058900Z

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.

ikitommi 2020-11-14T19:52:55.059500Z

itโ€™s bit like derived types schemas?