为什么我不能使用正则表达式将字符串验证为映射键?
Why can't I use regular expressions to validate strings as map keys?
在我的 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}
正如这些事情经常发生的那样,我在发布这个问题后很快就找到了答案;当我对 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}
在我的 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}
正如这些事情经常发生的那样,我在发布这个问题后很快就找到了答案;当我对 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}