Fork me on GitHub
#clojure-spec
<
2022-10-18
>
kpav20:10:25

Is there a way to generate only required or optional keys?

kpav20:10:12

e,g, Given this spec:

(s/def ::required-key string?)
  (s/def ::optional-key string?)
  (s/def ::foo (s/keys :req [::required-key]
                       :opt [::optional-key]))
I'd like a way to force generation of:
{:required-key "foo"}
And also:
{:required-key "foo"
   :optional-key "bar"}

Alex Miller (Clojure team)20:10:32

the latter should be what you get by default - required and sometimes optional

Alex Miller (Clojure team)20:10:43

you could filter with fmap to get the former

kpav20:10:50

This is assuming I am given an unknown spec. Basically, I'd like to know which keys are required, and which keys are optional

Alex Miller (Clojure team)20:10:35

there are some 3rd party libraries that have functions to parse that from the spec form

kpav20:10:00

ah ok thanks! I'll take a look into those