This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-03
Channels
- # aws (1)
- # beginners (5)
- # boot (46)
- # cider (6)
- # cljs-dev (421)
- # cljsrn (74)
- # clojure (77)
- # clojure-greece (1)
- # clojure-italy (7)
- # clojure-russia (12)
- # clojure-serbia (42)
- # clojure-sg (1)
- # clojure-spec (10)
- # clojure-uk (38)
- # clojurescript (40)
- # core-async (14)
- # data-science (1)
- # datomic (18)
- # emacs (2)
- # events (1)
- # garden (4)
- # gorilla (4)
- # graphql (5)
- # hoplon (69)
- # luminus (1)
- # lumo (1)
- # off-topic (31)
- # om (31)
- # om-next (2)
- # overtone (3)
- # pedestal (1)
- # precept (4)
- # re-frame (23)
- # reagent (2)
- # remote-jobs (1)
- # ring-swagger (23)
- # rum (7)
- # spacemacs (7)
- # sql (4)
- # unrepl (9)
- # untangled (5)
- # vim (11)
- # yada (5)
@mpenet I forgot to mention it is either a or b not the two so currently I'm using a xor
(defn xor [x y]
(or (and x (not y))
(and y (not x))))
Here too: [metosin/spec-tools "0.3.0"]
is out with support for Swagger2. More info at #announcements
@yenda your xor
function used in this way gives you what you want
(s/def ::a (s/nilable neg-int?))
(s/def ::b (s/nilable pos-int?))
(s/def ::x string?)
(s/def ::mapspec (s/keys :req-un [::x] :opt-un [::a ::b]))
(s/def ::abspec #(let [a (find % :a)
b (find % :b)]
(xor a b)))
(s/def ::finalspec (s/and ::mapspec ::abspec))
using find
to ensure that even if a
and b
are nilable
, the xor will still be enforcedand using opt-un
in the mapspec
ensures that non-namespaced keywords a
and b
will still be validated
(s/valid? ::finalspec {:x "yo" :a 100})
=> false
(s/valid? ::finalspec {:x "yo" :a -100})
=> true
(s/valid? ::finalspec {:x "yo" :a 100})
=> false
(s/valid? ::finalspec {:x "yo" :a -100 :b 100})
=> false
(s/valid? ::finalspec {:x "yo" :a nil :b nil})
=> false
(s/valid? ::finalspec {:x "yo" :b 42})
=> true