4

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}
T.W.R. Cole
  • 4,106
  • 1
  • 19
  • 26

1 Answers1

6

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}
T.W.R. Cole
  • 4,106
  • 1
  • 19
  • 26
  • 1
    Your initial intention was to keep keys optional, I suppose. Did you figure out how to make optional key that matches predicate? – OlegTheCat Mar 11 '16 at 05:25
  • @OlegTheCat I believe what's going on here is that the use of a predicate in the place of a map key implies "0 or more keys that return truthy" so in a certain way they are. It's the behavior I was trying to describe originally anyhow. – T.W.R. Cole Mar 11 '16 at 14:30