4

在我的 Clojure 项目中,我有这些:

:dependencies [
  [org.clojure/clojure "1.8.0"]
  [prismatic/schema "1.0.5"]]

这被认为是有效的:

(require '[schema.core :as s])
(def pos (s/pred #(re-matches #"\d+,\d+" %)))
(s/validate pos "0,0")
; "0,0"

因此,基于此,我认为以下内容也是有效的(但事实并非如此):

(require '[schema.core :as s])
(def pos (s/pred #(re-matches #"\d+,\d+" %)))
(def structure {(s/optional-key pos) s/Any})
(s/validate structure {"0,0" true, "2,0" false})
; Value does not match schema: {"0,0" disallowed-key, "2,0" disallowed-key}
4

1 回答 1

6

就像这些事情经常发生的那样,我在发布这个问题后不久就找到了答案;当我使用以下定义structure代替时,我得到了我的预期:

(require '[schema.core :as s])
(def pos (s/pred #(re-matches #"\d+,\d+" %)))
(def structure {pos s/Any})
(s/validate structure {"0,0" true, "2,0" false})
; {"0,0" true, "2,0" false}
于 2016-03-11T02:09:43.210 回答