This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-06
Channels
- # announcements (7)
- # aws (8)
- # babashka (9)
- # babashka-sci-dev (11)
- # beginners (37)
- # calva (50)
- # cider (15)
- # clj-kondo (30)
- # clj-otel (3)
- # cljdoc (16)
- # cljs-dev (26)
- # cljsrn (4)
- # clojure (168)
- # clojure-doc (1)
- # clojure-europe (17)
- # clojure-gamedev (4)
- # clojure-nl (3)
- # clojure-norway (1)
- # clojure-spec (17)
- # clojure-uk (16)
- # clojurescript (27)
- # community-development (3)
- # css (3)
- # cursive (9)
- # datomic (25)
- # emacs (1)
- # events (4)
- # fulcro (2)
- # google-cloud (2)
- # graphql (11)
- # gratitude (9)
- # humbleui (16)
- # hyperfiddle (2)
- # jobs (1)
- # london-clojurians (1)
- # lsp (16)
- # malli (2)
- # off-topic (71)
- # pedestal (4)
- # polylith (9)
- # portal (94)
- # reagent (6)
- # reitit (2)
- # releases (1)
- # remote-jobs (2)
- # sci (9)
- # shadow-cljs (49)
- # spacemacs (8)
- # tools-build (2)
- # tools-deps (39)
- # vim (7)
- # xtdb (6)
(s/def ::params (s/or :string string?
:map map?))
(defn is-null? [val]
(or (empty? val)
(= "null" val)
(nil? val)))
(s/def ::non-null-params
(s/and ::params
#(not (is-null? %))))
(gen/sample (s/gen ::non-null-params))
Why does ::non-null-params
still give me empty maps and empty strings?Because s/or
produces a pair. Either [:string s]
or [:map m]
is-null?
could have [[_ val]]
as its arglist and it will work.
Or you could test (is-null? (second %))
(technically it's a MapEntry
so (val %)
is "more correct" than (second %)
s/or
is how you do it, but it returns a tagged pair to tell you which branch matched.
(s/def ::non-null-params
(s/and ::params
#(not (is-null? (val %)))))
Thanks Sean.
It's working just as expected!I think Alex has said that Spec 2 will have a non-conforming or
that doesn't do that, or at least some easy way to avoid it...
It's only available via git deps and it's kind of buggy.
What you can do with Spec 1 is used the undocumented nonconforming
function, which is currently documented for Spec 2 (but might go away -- it will still be in Spec 1 tho'):
(s/def ::params (s/nonconforming (s/or :string string? :map map?)))
Spec 1 isn't going away. Spec 2 will most likely morph into the actual "core" Spec at some point and folks will either migrate from Spec 1 to "Spec" or use both side-by-side.
For a while at work, we maintained a branch of our code base migrated to use Spec 2, and help Alex test stuff, but it was such a moving target and several places were buggy and/or incomplete so after several months we abandoned trying to keep our branch updated and decided to wait for Spec 2 to mature.
Spec 2 has a lot of improvements over Spec 1 but it is also quite different in several places.