This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-03-12
Channels
- # aleph (10)
- # beginners (62)
- # boot (12)
- # cider (97)
- # cljs-dev (171)
- # clojars (1)
- # clojure (224)
- # clojure-italy (4)
- # clojure-nl (2)
- # clojure-russia (1)
- # clojure-spec (41)
- # clojure-uk (68)
- # clojured (7)
- # clojurescript (115)
- # community-development (4)
- # cursive (2)
- # data-science (1)
- # datomic (18)
- # duct (40)
- # emacs (1)
- # events (1)
- # fulcro (148)
- # funcool (2)
- # graphql (2)
- # immutant (3)
- # jobs (3)
- # keechma (1)
- # luminus (2)
- # numerical-computing (1)
- # off-topic (19)
- # om (6)
- # parinfer (10)
- # pedestal (15)
- # precept (86)
- # reagent (12)
- # ring (3)
- # ring-swagger (2)
- # shadow-cljs (42)
- # spacemacs (19)
- # specter (17)
- # sql (11)
- # tools-deps (78)
- # unrepl (62)
- # vim (28)
I’m working on writing a spec for a model that uses multi-spec
. Is it possible to use unqualified keywords in a multi-spec? I found this thread https://www.mail-archive.com/[email protected]/msg99107.html and tried altering the example in the docs with no success.
> Is it possible to use unqualified keywords in a multi-spec? @oconn Can you explain this a little bit more? I’m not sure I understand the question.
@oconn it’s possible, and the example in the multi-spec
doc string uses an unqualified keyword
https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html#clojure.spec.alpha/multi-spec
@oconn Can you expand a bit on what is happening when you call explain
and what you expect to happen?
Yeah, not seeing that but I think it’s because I stripped out some keys before pasting here.
I'm currently using gen/generate
to make some sample data for myself, including fairly nested structures which use s/*
at multiple levels. It'd be nice during dev to have the produced data be fairly short so it doesn't take up a whole repl screen. Wasn't there some way to tell it to keep those short? I vaguely thought there was. From the documentation it seems like it would be s/*recursion-limit*
, but if I do
(binding [s/*recursion-limit* 1] (sgen/generate (s/gen ::my-spec)))
I still get large numbers of items on the s/*
specs.Oh, I see why it's not *recursion-limit*
, I think; it's not actually recurring many layers deep, just creating a long sequence at each layer.
But it'd be really nice to have an equivalent way to force shorter sequences. Am I missing something?
I'm afraid you are not missing anything, @eggsyntax
you can provide overrides, which wrap original seq specs with just items count upper limit
It'd make a terrific addition to a future version of spec, to have s/*sequence-limit*
alongside s/*recursion-limit*
.
I doubt it is a good idea to have a single global count limit. I'd rather had a way to override :min-count/:max-count opts for maps and seqs specs
From my POV, that'd be nice too -- but for the case of just quickly generating a piece of data in the REPL, it'd be lovely to just bind a global limit.
Hmm, test.check
does have :min-elements
and :max-elements
on a number of its generators, I see....
https://github.com/clojure/spec.alpha/blob/master/src/main/clojure/clojure/spec/alpha.clj#L1318
why are all the facilities for defining specs macros? I’m tempted to use spec for pattern matching but the syntax is too heavy…
they’re macros to capture the form for explanations
that said, some of that is likely to change in the next rev
@alexmiller How can I validate a schema like this using Spec?
{:billing_contact
{
(schema/optional-key :street_address) schema/Str
(schema/optional-key :city) schema/Str
(schema/optional-key :region) schema/Str
(schema/optional-key :postal_code) schema/Str}}
I have the following spec but if ::billing_contact
has keys other than the optional keys, it considers the map to be valid. I just want to check if the map has either those specified keys in opt or no keys.
(s/def ::street_address string?)
(s/def ::city string?)
(s/def ::region string?)
(s/def ::postal_code string?)
(s/def ::billing_contact (s/keys :opt [::street_address ::city ::region ::postal_code]))
(s/def ::changes (s/keys :req [::billing_contact]))
You can s/and with an every? predicate that checks every key is a known key set
@alexmiller Thank you for that. I am not sure how exactly I could use that here. Could you please exhibit that with my example?
@U7MMSSQKC Perhaps something like (s/def ::billing_contact (s/and (s/keys :opt [::street_address ::city ::region ::postal_code]) #(every? {::street_address ::city ::region ::postal_code} %)))
?
Whoops, I forgot the call to keys
. Would this work: (s/def ::billing_contact (s/and (s/keys :opt [::street_address ::city ::region ::postal_code]) #(every? #{::city ::street_address ::region ::postal_code} (keys %))))
Thanks a lot @U08EKSQMS