This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-15
Channels
- # announcements (13)
- # beginners (106)
- # cider (70)
- # cljdoc (1)
- # cljsjs (1)
- # clojure (97)
- # clojure-finland (1)
- # clojure-italy (13)
- # clojure-mexico (16)
- # clojure-russia (1)
- # clojure-spec (53)
- # clojure-uk (146)
- # clojurescript (44)
- # core-async (5)
- # cryogen (1)
- # css (1)
- # cursive (11)
- # datomic (89)
- # duct (10)
- # emacs (4)
- # figwheel-main (58)
- # fulcro (5)
- # hispano (35)
- # hyperfiddle (1)
- # jobs (2)
- # jobs-discuss (1)
- # lambdaisland (1)
- # leiningen (3)
- # off-topic (13)
- # onyx (50)
- # parinfer (3)
- # pedestal (4)
- # reagent (9)
- # ring-swagger (56)
- # rum (3)
- # shadow-cljs (85)
- # spacemacs (4)
- # vim (4)
https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc/spec.clj
Hello!
Can someone tell me what :opt
is for? How is validation affected by its presence?
I'm talking about :opt
as an option to keys
As far as i’ve understood it,
if you have say (s/keys :opt [::name])
If the key ::name
is present it will check it against the spec.
So i’ve used it in the past to say, If this thing is present then it has to look like this spec. Otherwise carry on.
(s/def ::name string?)
(s/def ::type string?)
(s/def ::person (s/keys :opt [::name] :req [::type]))
(s/valid? ::person {::type "human})
=> probably true
(s/valid? ::person {::name 124 ::type "human"})
=> false because name is not a string
For more reading i would recommend https://clojure.org/guides/spec#_entity_maps
This registers a ::person spec with the required keys ::first-name, ::last-name, and ::email, with optional key ::phone. The map spec never specifies the value spec for the attributes, only what attributes are required or optional.
When conformance is checked on a map, it does two things - checking that the required attributes are included, and checking that every registered key has a conforming value. We'll see later where optional attributes can be useful. Also note that ALL attributes are checked via keys, not just those listed in the :req and :opt keys. Thus a bare (s/keys) is valid and will check all attributes of a map without checking which keys are required or optional.
A rather large snippetHey @guy! Thanks!
However, if I do (s/keys)
, it still checks that ::name
is valid.
(s/def ::name string?)
(s/def ::person (s/keys))
(s/valid? ::person {}) ;=> true
(s/valid? ::person {::name "Eric"}) ;=> true
(s/valid? ::person {::name 15}) ;=> false
yes, documentation is good
thanks
okay, thanks
the spec guide on http://clojure.org hints that it'll explain later, but it never does
I thought it might be more than just docs
good call
I think when it comes to speccing out ur fn as well it might be awkward depending on how u use (s/keys)
for sure
but that would also lead to harder reading when it comes to spec messages when they have failed
is there also a difference when the spec is not defined?
i.e., putting things in :opt
forces you to define the specs you listed (i.e., prevents you from accidentally forgetting them)?
@gfredericks a little repling shows that opts does not check if the spec exists