【问题标题】:How to remove extra keys from internal map using spec-tools如何使用规范工具从内部映射中删除额外的键
【发布时间】:2017-12-14 22:32:55
【问题描述】:

我正在尝试使用 clojure.spec 和 metosin/spec-tools 来验证和符合我的应用程序中的数据。在阅读了 spec-tools 文档后,我不清楚我应该如何使用 spec-tools.core/spec 包装我的规范,以便符合的数据没有额外的键(它适用于顶级地图,但不适用于内部结构的地图)。

一些代码可以帮助澄清问题:

(ns prodimg.spec
  (:require [clojure.spec.alpha :as s]
            [spec-tools.core :as st]
            [spec-tools.spec :as st.spec]))

(def ^:private not-blank? #(and (string? %)
                                (not (clojure.string/blank? %))))

(s/def :db/id integer?)

(s/def :model.image/id :db/id)
(s/def :model.image/type not-blank?)
(s/def :model.image/product-id :db/id)

(s/def :model.product/id :db/id)
(s/def :model.product/parent-id (s/nilable :db/id))
(s/def :model.product/name not-blank?)
(s/def :model.product/description string?)
(s/def :model.product/price (s/nilable decimal?))

; ----- request specs -----

; create product

(s/def :req.product.create/images (s/* (s/keys :req-un [:model.image/type])))
(s/def :req.product.create/children
  (s/* (s/keys :req-un [:model.product/name :model.product/description]
               :opt-un [:model.product/price])))

(s/def :req.product/create
  (st/spec (s/keys :req-un [:model.product/name :model.product/description]
                   :opt-un [:model.product/price
                            :model.product/parent-id
                            :req.product.create/images
                            :req.product.create/children])))

现在假设我有以下要验证/符合的数据:

(def data {:name "Product"
           :description "Product description"
           :price (bigdec "399.49")
           :extra-key "something"
           :images [{:type "PNG" :extra-key "something else"}])

(st/conform :req.product/create data st/strip-extra-keys-conforming)
; below is the result
; {:name "Product"
   :description "Product description"
   :price 399.49M
   :images [{:type "PNG" :extra-key "something else"}]

我尝试更改 :req.product.create/images 声明以包含 st/spec 调用包装 s/* 表单或 s/keys 表单,或两者兼而有之,但这些更改并没有改变结果。

有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: clojure clojure.spec


    【解决方案1】:

    奇怪,最新版本 [metosin/spec-tools "0.5.1"] 发布于 2017 年 10 月 31 日(所以在你发帖之前),我要做的唯一更改是按照文档中的示例进行操作 Map conforming 部分,这似乎是一个您已经尝试过的尝试:将s/keysst/spec 包装如下:

    改变

    (s/def :req.product.create/images (s/* (s/keys :req-un [:model.image/type])))
    

    (s/def :req.product.create/images (s/* (st/spec (s/keys :req-un [:model.image/type]))))
    

    我得到了预期的输出:

    (st/conform :req.product/create data st/strip-extra-keys-conforming)
    =>
    {:description "Product description",
     :images [{:type "PNG"}],
     :name "Product",
     :price 399.49M}
    

    【讨论】:

      猜你喜欢
      • 2020-09-09
      • 1970-01-01
      • 2012-04-19
      • 2017-09-13
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多