Fork me on GitHub

the use case is to re-use ui-approved-spec in a select-keys to not leak any sensitive or just extra information about entity, exactly because of s/keys's support of open maps


@misha try spec-tools:

(require '[clojure.spec.alpha :as s])
(require '[spec-tools.core :as st])

(s/def ::a int?)
(s/def ::b int?)
(s/def ::foo (st/spec (s/keys :req-un [::a ::b])))
(s/valid? ::foo {:a 1 :b 2 :c 3})  ;; => true

(st/select-spec ::foo {:a 1 :b 2 :c 3}) ;; => {:b 2, :a 1}


@ikitommi will check it out, thanks


Hi! I have a quick question. I have a spec for a map, which defines a set of required and a set of optional keys (`(s/keys :req [...] :opt [...])`). I would like to generate a version of that spec suitable for “partial updates”. By that I mean that the spec should pass if a required key is missing, but it should not pass if the required key has value nil.


Is that possible?


For a real-world use-case, think of a CRUD scenario. When creating an entity I need to validate the presence of some attributes, but when updating I only need to make sure that an attribute marked as required isn’t set to nil

Alex Miller (Clojure team)12:08:58

The vals for the required keys (actually all keys) in s/keys will be verified according to the spec for that key. If the spec does not allow nil, it is not valid.


Ok, thanks!

Alex Miller (Clojure team)12:08:44

user=> (s/def ::i int?)
user=> (s/def ::m (s/keys :opt [::i]))
user=> (s/valid? ::m {::i nil})
user=> (s/valid? ::m {})

Alex Miller (Clojure team)12:08:39

If the key is required, then its absence will cause validation to fail as required keys are … required. :)


Hi again… I am getting this error whenever I run the “doc” command (and others) in the REPL: java.lang.NoClassDefFoundError: clojure/spec/alpha$get_spec. Any ideas?


there's already a ticket + patch on jira