I have a spec definition that validates contents of incoming data. Since the data is a map of fields, I use spec/keys for validating it. For example:
(def person-data {:name "Jon Doe", :age 30})
(s/def ::name string?)
(s/def ::age pos-int?)
(s/def ::person-info (s/keys :req-un [::name ::age])
...
;validate data via spec and make sure no additional keys are included
(s/valid? ::person-spec some-input)
But an additional need I have is to make sure incoming data only contains the keys I want. (in this case :name and :age keys only. For that, I do something like:
(def permitted-keys [:age :name])
(select-keys some-input permitted-keys)
, ensuring only those keys get filtered in.
Is there a way I can reuse some code between my spec definition for the map structure (s/keys) and this additional step I take for filtering the allowed keys (permitted-keys)?
Perhaps by either extracting the list of keys from the s/keys definition, or by passing an existing vector of keys to s/keys?