clojureplumatic-schema

cannot validate values with string keys in map using clojure schema library


I tried validating a map using the prismatic/schema library for clojure. here is my shape

(require '[schema.core :as s])
(def d {"a" s/Str "b" s/Int})

When I tried to validate it against a map, it throws the following exception

(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)

Am I do something wrong, or can't the schema library not validate against String keys ?


Solution

  • You have to use

    (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=>