Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in Spec by

I want to create a spec that is like spec/keys, but that enforces that only keys in req-un and opt-un are allowed.

I was told that there is no easy way to do that with clojure.spec.alpha, and that I need to add an s/and with a function that will check that the given keys exist in req-un and opt-un.

My plan is to simply create another spec using the spec already defined with spec/keys, since it seems to me that it's impossible to define that the spec is exhaustive in one single spec. So what I want is something like this:

(spec/def ::person (spec/keys :req-un [::name ::age]))
(spec/def ::person-exhaustive (s/and ::person #(only-allowed-keys %))

However, it is not clear to me how to do that properly. I noticed there is a spec/form function that returns the spec as data, but since it is a Cons it seems clunky to use it. Is there a better way to get that data from a spec?

1 Answer

0 votes
by

spec.alpha do not support this ATM.
You can do that with that s/and trick

You can see more about this problem here
https://github.com/clojure/spec-alpha2/wiki/Schema-and-select

...