This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-05
Channels
- # announcements (5)
- # beginners (49)
- # boot (9)
- # calva (34)
- # cider (9)
- # clara (18)
- # clj-kondo (1)
- # cljsrn (7)
- # clojure (196)
- # clojure-dev (4)
- # clojure-europe (11)
- # clojure-france (3)
- # clojure-nl (13)
- # clojure-norway (2)
- # clojure-spec (8)
- # clojure-uk (85)
- # clojurescript (87)
- # clojurex (202)
- # core-logic (6)
- # data-science (6)
- # datomic (9)
- # dirac (1)
- # duct (93)
- # emacs (9)
- # events (1)
- # fulcro (22)
- # hoplon (30)
- # jackdaw (10)
- # keechma (1)
- # leiningen (5)
- # malli (17)
- # off-topic (9)
- # other-languages (3)
- # pedestal (7)
- # re-frame (3)
- # reagent (5)
- # reitit (9)
- # remote-jobs (6)
- # rewrite-clj (80)
- # ring (2)
- # shadow-cljs (191)
- # tools-deps (54)
- # vim (14)
- # xtdb (7)
I am trying to specify that atleast one key should be present, ex -
(def ::city string?)
(der ::name string?)
(def my-input (s/keys :opt-un [::city ::state])
The above makes both of them optional, I want to specify that atleast one should be present in the input.(s/def ::a string?)
(s/def ::b string?)
(s/def ::m (s/keys :req-un [(or ::a ::b)]))
(s/explain ::m {:a "1" :b "2"})
;; Success!
(s/explain ::m {:a "1" :b 2})
;; 2 - failed: string? in: [:a] at: [:b] spec: :user/b
(s/explain ::m {:a "1"})
;; Success!
(s/explain ::m {})
;; {} - failed: (or (contains? % :a) (contains? % :b)) spec: :user/m
thanks @U051HUZLD
Hey guys I ran into something I’d like to ask you about. I wanted to “merge” to s/keys specs using s/and. It didn’t work as I thought it would. Here is an example showing what I wanted to do:
(ns example
(:require [clojure.spec.alpha :as s]))
(s/def ::type (s/or :s string? :i int?))
(s/def ::a ::type)
(s/def ::b ::type)
(s/def ::t1 (s/keys :req [::a]))
(s/def ::t2 (s/keys :req [::b]))
(s/def ::v1 (s/and ::t1 ::t2))
(s/def ::v2 (s/and #(s/valid? ::t1 %)
#(s/valid? ::t2 %)))
(def example {::a "1" ::b 2})
(s/valid? ::v1 example)
;=> false
(s/valid? ::v2 example)
;=> true
(s/explain ::v1 example)
;[:s "1"] - failed: string? in: [:example/a] at: [:example/a :s] spec: :example/type
;[:s "1"] - failed: int? in: [:example/a] at: [:example/a :i] spec: :example/type
;[:i 2] - failed: string? in: [:example/b] at: [:example/b :s] spec: :example/type
;[:i 2] - failed: int? in: [:example/b] at: [:example/b :i] spec: :example/type
;=> nil
Do I use s/and
in an unintended way? Is ::v2
the correct way to do it? Did I miss something in spec’s api?