This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-22
Channels
- # architecture (30)
- # beginners (56)
- # cider (16)
- # cljs-dev (12)
- # cljsrn (21)
- # clojure (169)
- # clojure-austin (1)
- # clojure-estonia (1)
- # clojure-italy (3)
- # clojure-russia (1)
- # clojure-spec (56)
- # clojure-uk (46)
- # clojurescript (53)
- # consulting (3)
- # core-async (3)
- # cursive (14)
- # data-science (16)
- # datascript (1)
- # datomic (26)
- # defnpodcast (11)
- # docs (3)
- # emacs (6)
- # fulcro (4)
- # graphql (24)
- # hoplon (8)
- # instaparse (4)
- # java (2)
- # jobs (1)
- # jobs-rus (1)
- # jobs_rus (1)
- # keechma (1)
- # luminus (2)
- # lumo (1)
- # mount (36)
- # off-topic (30)
- # om-next (5)
- # onyx (29)
- # precept (23)
- # re-frame (20)
- # reagent (2)
- # remote-jobs (9)
- # ring (2)
- # ring-swagger (3)
- # rum (3)
- # shadow-cljs (100)
- # spacemacs (17)
- # sql (10)
- # timbre (2)
- # unrepl (29)
- # yada (2)
I want to spec a command vector, given (s/def ::command-name keyword?)
. Now the command-vector needs to be a vector where the first element must be the ::command-name
. All other elements are optional arguments. Any suggestions? Thank you.
(s/def ::command-name keyword?)
(s/def ::command (s/cat :name ::command-name
:args (s/* any?)))
(s/conform ::command [:foo "bar"])
if you really need the input to be a vector then
(s/def ::command
(s/and
vector?
(s/cat :name ::command-name
:args (s/* any?))))
I have a datastructure that looks something like this:
(def my-invoice-address
{
:use-delivery-address false
:street "asdf"
:zip "123"})
If :use-delivery-address
is true, :street
and :zip
should not be there. If :use-delivery-address
is false, :street
and :zip
should be there
I find it really difficult to write a spec for this. Anyone who have any ideas?@karl.jakob.lind I guess :use-delivery-address
is :req
, the others are :opt
and then you wrap your keys
with an s/and
to enforce the rest of the logic that you described
Well, the others are required if :use-delivery-address
is true. So it's not that simple
yeah, but I’m proposing that this is enforced by the extra predicate that you will pass to s/and
hm.. something like this?
(s/def ::invoice-address
(s/keys
:req-un [::use-delivery-address]
:opt-un [::street ::zip]))
where do the s/and
belong ?something like this (I’m sure there’s a more elegant way to do it):
(s/def ::invoice-address
(s/and
(s/keys
:req-un [::use-delivery-address]
:opt-un [::street ::zip])
(fn [{:keys [use-deliver-address street zip]}]
(or (and (not use-deliver-address)
(not street)
(not zip))
(and use-deliver-address
street
zip)))))
Perhaps use https://clojure.org/guides/spec#_multi_spec ?
(defmulti address-type :address/use-deliver-address)
(defmethod address-type true [_]
(s/keys :req []))
(defmethod address-type false [_]
(s/keys :req [ :address/street ...]))
@karl.jakob.lindgood suggestion @karl.jakob.lind. will read about that!
let's say i want to write a spec for a string that should be splitted in two, imagine i have a split function that can be called with that string and returns a pair of strings... i have a ::check-whole-thing spec and also a ::check-part spec ...how would i write that ::check-whole-thing part ?
i think what i'm asking here is how to transform the data being specced before speccing it further
have you read the spec guide? https://clojure.org/guides/spec If not, maybe take a spin through that first and then refine the question.
i did ... actually found a related question to which you responded on the forum ... the conformer function looks like it fits the use case
that does not seem like a use case where conformers should be used to me
I’m confused by several aspects of your question so not sure how best to answer it
i'm conforming a string, i want to have it transformed into a vector of strings before further conforming
well, I would recommend not doing that
do that in code explicitly
don’t use spec for it
I think you are better off if you spec the inputs and outputs of the transformation
and explicitly invoke the transformation
and don’t use spec to actually do the transformation part
the thing is that i'm not really interested in that transformation at all, i'm only validating it to pass the whole thing merilly along
if you have to do that transformation in able to pass it along, then it seems like you are interested in it after all
let's imagine a use case for one of those html dsls ... they have this way of specifying class attributes like so [:div.app-container ...] ...that's not what i'm doing at all but that's a good example... are you saying that spec wouldn't fit to do the "destructuring" of that keyword ?
or maybe to destructure an udp packet or you know go inside a data structure and destructure it for me
what's the issue here ? the fact that one could not rebuild the data structure from the conformed result ?
no problem. Lots of other validation libraries provide "mutative validation".... conflating transformation into its API
but now i first need to conform a big data structure, then walk it and validate parts of it
I'm trying to spec polymorphic maps where a particular key is used for dispatch. Is there a way to spec the dispatch key to specify the dispatch value?
(s/def ::foo
(s/keys :req [:data/type :foo/stuff]))
(s/def ::bar
(s/keys :req [:data/type :bar/stuff]))
e.g. ::foo
's :data/type
should have a value of :foo
, and ::bar
's :data/type
have a value of :bar
@scallions I think you are looking for multi-spec: https://clojure.org/guides/spec#_multi_spec
Thanks, I'll look into it. That retag
argument looks interesting.