如何获得 'at-least' 架构?

How to get 'at-least' schema?

'at-least' 我的意思是将忽略所有 disallowed-key 错误的模式。 考虑以下片段:

(require '[schema.core :as s])

(def s {:a s/Int})

(s/check s {:a 1}) ;; => nil (check passed)
(s/check s {:a 1 :b 2}) ;; => {:b disallowed-key}

(def at-least-s (at-least s))

(s/check at-least-s {:a 1}) ;; => nil
(s/check at-least-s {:a 1 :b 2}) ;; => nil

关于实现 at-least 函数的第一个想法是将 [s/Any s/Any] 条目连接到初始模式:

(defn at-least [s]
  (conj s [s/Any s/Any]))

但不幸的是,这种实现不适用于嵌套地图:

(def another-s {:a {:b s/Int}})

(s/check (at-least another-s) {:a {:b 1} :c 2}) ;; => nil
(s/check (at-least another-s) {:a {:b 1 :d 3} :c 2}) ;; => {:a {:d disallowed-key}}

是否有可能获得也适用于嵌套地图的 at-least 模式?或者 prismatic/schema 提供了一些我所缺少的开箱即用的东西?

您可以使用 metosin/schema-tools: schema-tools.walk. There is even a code that you need in the test:

中的一些东西
(defn recursive-optional-keys [m]
  (sw/postwalk (fn [s]
                 (if (and (map? s) (not (record? s)))
                   (st/optional-keys s)
                   s))
               m))