Fork me on GitHub
#clojure-spec
<
2017-08-14
>
misha06:08:56

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

ikitommi08:08:16

@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}

misha08:08:58

@ikitommi will check it out, thanks

hmaurer12:08:57

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.

hmaurer12:08:02

Is that possible?

hmaurer12:08:00

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.

hmaurer16:08:28

Ok, thanks!

Alex Miller (Clojure team)12:08:44

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

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. :)

hmaurer16:08:18

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?

bronsa16:08:13

there's already a ticket + patch on jira