clojureplumatic-schema

Why can't I use regular expressions to validate strings as map keys?


In my Clojure project, I have these:

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

This is considered valid:

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

So based on that, I would have thought the following would also be valid (but it isn't):

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

Solution

  • And as often happens with these things, I discovered the answer soon after posting this question; I got what I expected when I used the below definition for structure instead:

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