1

我的 API 正在从客户端接收一些 JSON 数据。

我想使用 Schema 对收到的数据执行验证和强制,但还有一个额外要求:如果有任何未在模式中描述的映射键,请忽略并删除它而不是验证失败(这是因为我的客户可能会向我发送一些“垃圾”属性以及我关心的属性。我想对此保持宽容。)。

所以简而言之,我想select-keys在验证/强制之前使用我的模式对我的输入数据执行“深度”。

我需要的示例:

(require '[schema.core :as sc])
(def MySchema {:a sc/Int
               :b {:c sc/Str
                   (sc/optional-key :d) sc/Bool}
               :e [{:f sc/Inst}]})

(sanitize-and-validate
  MySchema
  {:a 2
   :b {:c "hello"
       :$$garbage-key 32}
   :e [{:f #inst "2015-07-23T12:29:51.822-00:00" :garbage-key 42}]
   :_garbage-key1 "woot"})
=> {:a 2
    :b {:c "hello"}
    :e [{:f #inst "2015-07-23T12:29:51.822-00:00"}]}

我还没有找到一种可靠的方法来做到这一点:

  1. 我似乎无法在自定义转换中执行此操作,因为步行者似乎无法让您访问密钥。
  2. 我没有运气尝试手动遍历模式,因为很难以通用方式区分映射模式和标量模式;也很难解释模式可以具有的所有可能的形状。

有没有明显的方法我没有看到?

谢谢!

4

4 回答 4

4

第三种解决方案,归功于abp:使用带有匹配器的 schema.coerce/coercer,该匹配器将从映射中删除未知键。

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

(defn filter-schema-keys
  [m schema-keys extra-keys-walker]
  (reduce-kv (fn [m k v]
               (if (or (contains? schema-keys k)
                       (and extra-keys-walker
                            (not (utils/error? (extra-keys-walker k)))))
                 m
                 (dissoc m k)))
             m
             m))

(defn map-filter-matcher
  [s]
  (when (or (instance? clojure.lang.PersistentArrayMap s)
            (instance? clojure.lang.PersistentHashMap s))
    (let [extra-keys-schema (#'s/find-extra-keys-schema s)
          extra-keys-walker (when extra-keys-schema (s/walker extra-keys-schema))
          explicit-keys (some->> (dissoc s extra-keys-schema)
                                 keys
                                 (mapv s/explicit-schema-key)
                                 (into #{}))]
      (when (or extra-keys-walker (seq explicit-keys))
        (fn [x]
          (if (map? x)
            (filter-schema-keys x explicit-keys extra-keys-walker)
            x))))))

被 Schema 的主要作者描述为最干净的解决方案,因为它不需要对模式本身进行任何更改即可工作。所以这可能是要走的路。

使用示例:

(def data {:a 2
           :b {:c "hello"
               :$$garbage-key 32}
           :e [{:f #inst "2015-07-23T12:29:51.822-00:00" :garbage-key 42}]
           :_garbage-key1 "woot"})
((coerce/coercer MySchema map-filter-matcher) data)
;=> {:a 2, :b {:c "hello"}, :e [{:f #inst "2015-07-23T12:29:51.822-00:00"}]}
于 2015-07-23T20:19:02.887 回答
1

架构自述文件

对于关键字的特殊情况,您可以省略 required-key,例如 {:foo s/Str :bar s/Keyword}。您还可以提供特定的可选键,并将特定键与通用模式组合用于剩余的键值映射:

(def FancyMap
  "If foo is present, it must map to a Keyword.  Any number of additional
   String-String mappings are allowed as well."
  {(s/optional-key :foo) s/Keyword
    s/Str s/Str})

(s/validate FancyMap {"a" "b"})

(s/validate FancyMap {:foo :f "c" "d" "e" "f"})

因此,除了您的特定键(可以s/optional-key像示例中那样,或者s/required-key似乎是您需要的那样)之外,您还可以拥有额外的“宽松”键,例如:

(def MySchema {:a sc/Int
               :b {:c sc/Str
                   (sc/optional-key :d) sc/Bool
                   s/Any s/Any}
               :e [{:f sc/Inst}]})

编辑:通过添加:garbage元数据并丢弃walker中的这些条目,找到了一种“hacky”方法:

(def Myschema {:a s/Int
               :b {:c s/Str
                   (s/optional-key :d) s/Bool
                   (with-meta s/Any {:garbage true}) s/Any}
               :e [{:f s/Inst}]
               (with-meta s/Any {:garbage true}) s/Any})

(defn garbage? [s]
  (and (associative? s)
       (:garbage (meta (:kspec s)))))

(defn discard-garbage [schema]
  (s/start-walker
    (fn [s]
      (let [walk (s/walker s)]
        (fn [x]
          (let [result (walk x)]
            (if (garbage? s)
              (do (println "found garbage" x)
                  nil)
              result)))))
    schema))

((discard-garbage Myschema) data)
;=> :a 2, :b {:c "hello"}, :e [{:f #inst "2015-07-23T12:29:51.822-00:00"}]}
于 2015-07-23T13:25:47.080 回答
1

有一个名为“select-schema”的模式工具。见https://github.com/metosin/schema-tools#select-schema

从页面:

选择架构

过滤掉非法模式键(使用强制):

(st/select-schema {:street "Keskustori 8"
                   :city "Tampere"
                   :description "Metosin HQ" ; disallowed-key
                   :country {:weather "-18" ; disallowed-key
                             :name "Finland"}}
                  Address)
; {:city "Tampere", :street "Keskustori 8", :country {:name "Finland"}}

使用强制和额外的 Json 强制过滤掉非法模式映射键 - 在一次扫描中:

(s/defschema Beer {:beer (s/enum :ipa :apa)})

(def ipa {:beer "ipa" :taste "good"})

(st/select-schema ipa Beer)
; clojure.lang.ExceptionInfo: Could not coerce value to schema: {:beer (not (#{:ipa :apa} "ipa"))}
;     data: {:type :schema.core/error,
;            :schema {:beer {:vs #{:ipa :apa}}},
;            :value {:beer "ipa", :taste "good"},
;            :error {:beer (not (#{:ipa :apa} "ipa"))}}

(require '[schema.coerce :as sc])

(st/select-schema ipa Beer sc/json-coercion-matcher)
; {:beer :ipa}
于 2017-09-27T16:05:30.747 回答
0

这是另一种方法(下面的代码):

  1. 定义自定义Garbage模式类型,以匹配要删除的属性;如果您想删除所有未知属性,您可以将schema.core/Any其用作架构中的键(感谢 Colin Yates 告诉我这一点)。
  2. 作为强制步骤,“标记”所有要删除的值,方法是将它们强制为垃圾类型的实例。
  3. 遍历数据结构以去除所有标志。

这样做的好处是对 Schema 的内部做很少的假设(在撰写本文时仍处于 alpha 阶段),并且至少有两个缺点:

  1. 假设数据是 Clojure 映射和序列的组合(在 JSON 输入的情况下不是真正的问题)
  2. 添加另一个数据结构遍历,从性能角度来看,这可能不是最佳的。

(require '[schema.core :as s])
(require '[schema.coerce :as sco])
(require '[schema.utils :as scu])

(deftype ^:private GarbageType [])
(def ^:private garbage-const (GarbageType.))

(def Garbage "Garbage schema, use it to flag schema attributes to be removed by `cleaner`." GarbageType)

(defn garbage-flagging-matcher "schema.coerce matcher to detect and flag garbage values." [schema]
  (cond (= schema Garbage) (constantly garbage-const)
        :else identity))

(defn- garbage-flagger "Accepts a schema (supposedly that uses Garbage as a sub-schema), and returns a function that flags garbage values by coercing them to `garbage-const`"
  [schema] (sco/coercer schema garbage-flagging-matcher))

(defn clean-garbage "Accepts a clojure data structures, and removes the values equal to `garbage-const."
  [v]
  (cond
    (= garbage-const v) nil
    (map? v) (->> v seq
                  (reduce (fn [m [k nv]]
                            (if (= garbage-const nv)
                              (dissoc m k)
                              (assoc m k (clean-garbage nv)))
                            ) v))
    (vector? v) (->> v (remove #(= % garbage-const)) (map clean-garbage) vec)
    (sequential? v) (->> v (remove #(= % garbage-const)) (map clean-garbage) doall)
    :else v
    ))

(defn cleaner "Accepts a Schema, which presumably uses Garbage to match illegal values, and returns a function that accepts a data structure (potentially an instance of the schema) and will remove its values that are not anticipated in the schema, e.g illegal map keys."
  [schema]
  (let [flag (garbage-flagger schema)]
    (fn [data]
      (-> data flag clean-garbage)
      )))

;; Example

(def MySchema {:a s/Int
               :b {:c  s/Str
                   (s/optional-key :d) s/Bool
                   s/Any Garbage}
               :e [{:f s/Inst
                    s/Any Garbage}]
               s/Any Garbage})

((cleaner MySchema) {:a 1
                       :garbage-key "hello"
                       :b {:c "Hellow world"
                           :d false
                           42432424 23/2}
                       :e [{:f #inst "2015-07-23T15:49:33.073-00:00"
                            'a-garbage-key "remove me!!"
                            "another garbage key" :remove-me!!}
                           {:f #inst "2015-07-23T15:53:33.073-00:00"}]})
  => {:a 1
      :b {:c "Hellow world"
          :d false}
      :e [{:f #inst "2015-07-23T15:49:33.073-00:00"}
          {:f #inst "2015-07-23T15:53:33.073-00:00"}]}
于 2015-07-23T17:41:03.890 回答