无法使用 clojure 模式库在映射中使用字符串键验证值
cannot validate values with string keys in map using clojure schema library
我尝试使用 prismatic/schema clojure 库验证地图。这是我的形状
(require '[schema.core :as s])
(def d {"a" s/Str "b" s/Int})
当我尝试根据地图对其进行验证时,它抛出以下异常
(s/validate d {"a" "@@#$" "b" 2})
RuntimeException More than one non-optional/required key schemata: ["a" "b"] schema.core/find-extra-keys-schema (core.clj:705)
我是不是做错了什么,或者模式库不能根据字符串键进行验证?
你必须使用
(def d {(s/required-key "a") s/Str (s/required-key "b") s/Int})
Only when using keywords as keys you can omit the required-key
.
example=> (def d {(s/required-key "a") s/Str (s/required-key "b") s/Int})
#'schema-examples/d
example=> (s/validate d {"a" "@@#$" "b" 2})
{"a" "@@#$", "b" 2}
examples=>
我尝试使用 prismatic/schema clojure 库验证地图。这是我的形状
(require '[schema.core :as s])
(def d {"a" s/Str "b" s/Int})
当我尝试根据地图对其进行验证时,它抛出以下异常
(s/validate d {"a" "@@#$" "b" 2})
RuntimeException More than one non-optional/required key schemata: ["a" "b"] schema.core/find-extra-keys-schema (core.clj:705)
我是不是做错了什么,或者模式库不能根据字符串键进行验证?
你必须使用
(def d {(s/required-key "a") s/Str (s/required-key "b") s/Int})
Only when using keywords as keys you can omit the required-key
.
example=> (def d {(s/required-key "a") s/Str (s/required-key "b") s/Int})
#'schema-examples/d
example=> (s/validate d {"a" "@@#$" "b" 2})
{"a" "@@#$", "b" 2}
examples=>