Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 如何使用等级库工具从内部贴图中删除额外的关键点_Clojure_Clojure.spec - Fatal编程技术网

Clojure 如何使用等级库工具从内部贴图中删除额外的关键点

Clojure 如何使用等级库工具从内部贴图中删除额外的关键点,clojure,clojure.spec,Clojure,Clojure.spec,我正在尝试使用clojure.spec并验证和整合应用程序中的数据。在阅读了spec tools文档之后,我不清楚应该如何使用spec tools.core/spec包装我的规范,这样符合要求的数据就不会有额外的键(它适用于顶级映射,但不适用于内部结构的映射) 一些代码有助于澄清问题: (ns prodimg.spec (:require [clojure.spec.alpha :as s] [spec-tools.core :as st] [

我正在尝试使用clojure.spec并验证和整合应用程序中的数据。在阅读了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
表单,或两者,但更改并没有改变结果


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

奇怪,2017-10-31发布了最新版本的
[metosin/spec tools“0.5.1”]
(所以在你发帖之前),我唯一要做的改变就是遵循第节文档中的示例,这似乎是您已经尝试过的尝试之一:使用
st/spec
包装
s/keys
,如下所示:

改变

(s/def :req.product.create/images (s/* (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}
(st/conform :req.product/create data st/strip-extra-keys-conforming)
=>
{:description "Product description",
 :images [{:type "PNG"}],
 :name "Product",
 :price 399.49M}