<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged spec</title>
<link>https://ask.clojure.org/index.php/tag/spec</link>
<description></description>
<item>
<title>How to force clojure.spec generator to skip a field</title>
<link>https://ask.clojure.org/index.php/14893/how-to-force-clojure-spec-generator-to-skip-a-field</link>
<description>&lt;pre&gt;&lt;code&gt;(gen/let [x (s/gen (s/keys :opt [::a]))
          y (s/gen (s/keys :opt [::a]) {::a ???})])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If &lt;code&gt;x&lt;/code&gt; generates without &lt;code&gt;::a&lt;/code&gt;, I'd also like to generate &lt;code&gt;y&lt;/code&gt; without &lt;code&gt;::a&lt;/code&gt;.  I can't presently see a way to express that.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14893/how-to-force-clojure-spec-generator-to-skip-a-field</guid>
<pubDate>Thu, 15 Jan 2026 07:58:46 +0000</pubDate>
</item>
<item>
<title>`(s/gen ...)` caches its failure to load generators and thus is incompatible with `add-lib`</title>
<link>https://ask.clojure.org/index.php/14718/gen-caches-failure-load-generators-thus-incompatible-with</link>
<description>&lt;pre&gt;&lt;code&gt;Clojure 1.12.0
user=&amp;gt; (require '[clojure.spec.alpha :as s])
nil
user=&amp;gt; (s/def ::x #{1 2 3})
:user/x
user=&amp;gt; (s/gen ::x)
Execution error (FileNotFoundException) at user/eval145 (REPL:1).
Could not locate clojure/test/check/generators__init.class, clojure/test/check/generators.clj or clojure/test/check/generators.cljc on classpath.
user=&amp;gt; (add-lib 'org.clojure/test.check)
[org.clojure/test.check]
user=&amp;gt; (s/gen ::x)
Execution error (FileNotFoundException) at user/eval145 (REPL:1).
Could not locate clojure/test/check/generators__init.class, clojure/test/check/generators.clj or clojure/test/check/generators.cljc on classpath.
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>REPL</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14718/gen-caches-failure-load-generators-thus-incompatible-with</guid>
<pubDate>Tue, 23 Sep 2025 16:23:58 +0000</pubDate>
</item>
<item>
<title>What is an ideomatic way to use spec for API input validation?</title>
<link>https://ask.clojure.org/index.php/14225/what-is-an-ideomatic-way-to-use-spec-for-api-input-validation</link>
<description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I'm pretty new to Clojure and recently got interested in using spec. One use-case I'm interested in, is to use it for validation of API inputs. As far as I understood I have the following possibilities to correctly declare / use specs in that case:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create specs with unqualified names and use plain keywords in API input. I guess that's prone to error as one can think of overlapping specs with different requirements (e. g. having IDs with different scheme in two entities).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create specs with qualified names and also use qualified names in API input. That would work as expected but feels like unnecessary extra work on the client side as all clients now need to specify the namespace for all keys while its usually clear which entity is expected (e. g. API endpoint /customer-address is expecting a customer address but still all fields need to be namespaced in the input like :customer-address/street).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create specs with qualified names but use :req-un instead of :req to specify required keys in specs. Works as expected unless someone tries to validate a map with qualified names (shouldn't usually happen in API requests but might occur within the application where namespaces might not be a bad idea).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create specs with qualified names and lift all keywords in API inputs into the expected namespace. Would work as expected and has the benefit that namespaced keywords can be used in the application. Drawback is that every API input needs to be transformed before validation can happen.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So, my question is, what is the ideomatic way to validate API input with spec?&lt;br&gt;
And maybe as a bonus question: why is spec using the global &quot;database&quot; for specs? It looks like a lot of problems/confusion arise from that design decision (e. g. name clashes / overrides) and it also doesn't look like a very &quot;functional&quot; way. To clarify, I'm also not an expert in functional programming, so would be great if someone could highlight the benefits that led to that decision.&lt;/p&gt;
&lt;p&gt;Best&lt;br&gt;
Ben&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14225/what-is-an-ideomatic-way-to-use-spec-for-api-input-validation</guid>
<pubDate>Tue, 05 Nov 2024 20:58:16 +0000</pubDate>
</item>
<item>
<title>`(s/gen ratio?)` throws exceptions</title>
<link>https://ask.clojure.org/index.php/13877/s-gen-ratio-throws-exceptions</link>
<description>&lt;p&gt;&lt;code&gt;(s/gen ratio?)&lt;/code&gt; is &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/spec.alpha/blob/c630a0b8f1f47275e1a476dcdf77507316bad5bc/src/main/clojure/clojure/spec/gen/alpha.clj#L187&quot;&gt;implemented&lt;/a&gt; as &lt;code&gt;(gen/such-that ratio? gen/ratio)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The problem is that about 10% of the time &lt;code&gt;gen/ratio&lt;/code&gt; returns a whole number, which fails the &lt;code&gt;ratio?&lt;/code&gt; predicate. If that happens 10 times in a row, &lt;code&gt;gen/such-that&lt;/code&gt; throws an exception.&lt;/p&gt;
&lt;p&gt;With a large enough sample size, &lt;code&gt;(s/gen ratio?)&lt;/code&gt; reliably fails:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(dorun (gen/sample (s/gen ratio?) 10000000))

Execution error (ExceptionInfo) at clojure.test.check.generators/fn (generators.cljc:435).
Couldn't satisfy such-that predicate after 10 tries.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A quick fix is to increase the &lt;code&gt;max-tries&lt;/code&gt; param for &lt;code&gt;such-that&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(dorun (gen/sample (gen/such-that ratio? gen/ratio 100) 10000000))

=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I do however also find it odd that &lt;code&gt;(s/gen ratio?)&lt;/code&gt; always generates &lt;code&gt;Ratio&lt;/code&gt;s, but &lt;code&gt;gen/ratio&lt;/code&gt; generates a mixture of ratios and integers. So perhaps a better fix would be to update &lt;code&gt;gen/ratio&lt;/code&gt; to not generate integers?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13877/s-gen-ratio-throws-exceptions</guid>
<pubDate>Wed, 08 May 2024 07:44:49 +0000</pubDate>
</item>
<item>
<title>clojure spec vs plumatic schema</title>
<link>https://ask.clojure.org/index.php/13817/clojure-spec-vs-plumatic-schema</link>
<description>&lt;p&gt;A colleague brought to my attention that Clojure Spec is still alpha and that Plumatic Schema might be a better way to do the same tipe of spec.&lt;/p&gt;
&lt;p&gt;Do you folks have any advice?&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13817/clojure-spec-vs-plumatic-schema</guid>
<pubDate>Wed, 17 Apr 2024 04:11:04 +0000</pubDate>
</item>
<item>
<title>spec for extend-protocol</title>
<link>https://ask.clojure.org/index.php/13723/spec-for-extend-protocol</link>
<description>&lt;p&gt;Many macros in clojure have a spec which get executed by the compiler at macro-expansion time.&lt;/p&gt;
&lt;p&gt;At this time &lt;code&gt;extend-protocol&lt;/code&gt; doesn't have such a spec.&lt;br&gt;
I'd like to know if &lt;code&gt;extend-protocol&lt;/code&gt; is supposed to work with &lt;code&gt;(Class/forName ..)&lt;/code&gt; or that the class name to which the protocol is extended must always be a symbol. A spec would be the natural place to get this answer from. &lt;code&gt;extend-protocol&lt;/code&gt; is known to not work with &lt;code&gt;Class/forName&lt;/code&gt; when you provide more than one:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(extend-protocol IFoo (Class/forName &quot;[B&quot;) (Class/forName &quot;[D&quot;))
Syntax error (IllegalArgumentException) compiling at (REPL:1:1).
Don't know how to create ISeq from: java.lang.Character&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If the spec says anything about this, I can also make linting for this in clj-kondo. At this time it seems to be ambiguous: &lt;code&gt;(extend-protocol IFoo (Class/forName &quot;[B&quot;))&lt;/code&gt; does work.&lt;/p&gt;
&lt;p&gt;Related issue: &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJ-1381&quot;&gt;https://clojure.atlassian.net/browse/CLJ-1381&lt;/a&gt;&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13723/spec-for-extend-protocol</guid>
<pubDate>Wed, 14 Feb 2024 14:05:57 +0000</pubDate>
</item>
<item>
<title>`(s/explain-data (s/coll-of any? :count 1) (range))` hangs</title>
<link>https://ask.clojure.org/index.php/13660/s-explain-data-s-coll-of-any-count-1-range-hangs</link>
<description>&lt;p&gt;&lt;code&gt;(s/explain-data (s/coll-of any? :count 1) (range))&lt;/code&gt; hangs.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;coll-prob&lt;/code&gt; impl has these lines:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(cond
  ...
  (c/and count (not= count (bounded-count count x)))
  [{:path path :pred `(= ~count (c/count ~'%)) :val x :via via :in in}]
  ...)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So for all non-counted collections with number of items larger than &lt;code&gt;count&lt;/code&gt;, this branch is never triggered.&lt;br&gt;
Maybe it should use &lt;code&gt;(bounded-count (inc count) x)&lt;/code&gt; instead?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13660/s-explain-data-s-coll-of-any-count-1-range-hangs</guid>
<pubDate>Fri, 02 Feb 2024 20:59:37 +0000</pubDate>
</item>
<item>
<title>spec 1 :count fails uninformatively</title>
<link>https://ask.clojure.org/index.php/13656/spec-1-count-fails-uninformatively</link>
<description>&lt;p&gt;Using Spec 1, it looks like the count validation failure could be reported better - it's not clear what the actual count was&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (s/assert (s/coll-of any? :count 1)
            [])
Execution error - invalid arguments to user/eval148 at (REPL:1).
[] - failed: (= 1 (count %))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13656/spec-1-count-fails-uninformatively</guid>
<pubDate>Fri, 02 Feb 2024 19:17:54 +0000</pubDate>
</item>
<item>
<title>Should s/keys error on bad arg keys (not :req, :req-un, etc)?</title>
<link>https://ask.clojure.org/index.php/13200/should-s-keys-error-on-bad-arg-keys-not-req-req-un-etc</link>
<description>&lt;p&gt;An &lt;code&gt;s/keys&lt;/code&gt; spec will silently do nothing if it is defined without any valid arg key ( &lt;code&gt;:req&lt;/code&gt;, &lt;code&gt;:req-un&lt;/code&gt;, &lt;code&gt;:opt&lt;/code&gt;, &lt;code&gt;:opt-un&lt;/code&gt;, &lt;code&gt;:gen&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Using &lt;code&gt;req-un&lt;/code&gt; instead of &lt;code&gt;:req-un&lt;/code&gt; (symbol instead of keyword, perhaps accidentally):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user&amp;gt; (s/def ::foo (s/or :string string? :number number?))
:user/foo

user&amp;gt; (s/def ::foo-map-broken (s/keys req-un [::foo]))
:user/foo-map-broken

user&amp;gt; (s/conform ::foo-map-broken {:foo []})
{:foo []}

;;arg key can be anything
user&amp;gt; (s/def ::foo-map-broken2 (s/keys 42 [::foo]))
:user/foo-map-broken2

user&amp;gt; (s/conform ::foo-map-broken2 {:foo []})
{:foo []}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My two cents: I tend to think calls to &lt;code&gt;s/keys&lt;/code&gt; should be validated more aggressively so this produces an error. The only argument I can think of in favor of the current behavior is that the args to &lt;code&gt;s/keys&lt;/code&gt; are effectively a map and maps are usually not treated as closed in Clojure. But 1&amp;gt; args that result in nothing happening still seem worth erroring on and 2&amp;gt; the user does not actually pass a map and may not realize that the implementation turns their arg keys and arg values into a map.&lt;/p&gt;
&lt;p&gt;(I came across this behavior when clj-kondo flagged a spec of mine as incorrect and I noticed a typo just like above where I had &lt;code&gt;req-un&lt;/code&gt; instead of :req-un. I fixed :req-un to be a keyword and this uncovered bugs in the spec but it had been a few months since I touched the code so I wished I had discovered it sooner :--)  )&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13200/should-s-keys-error-on-bad-arg-keys-not-req-req-un-etc</guid>
<pubDate>Sat, 26 Aug 2023 00:25:55 +0000</pubDate>
</item>
<item>
<title>Inconsistent results from s/valid? and s/explain when trying to spec list of values</title>
<link>https://ask.clojure.org/index.php/12957/inconsistent-results-from-valid-explain-when-trying-values</link>
<description>&lt;p&gt;When using spec-tools (&lt;code&gt;metosin/spec-tools {:mvn/version &quot;0.10.5&quot;}&lt;/code&gt;) to spec a list of values, result spec produces inconsistent results when validated against in s/valid? and s/explain.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Minimal example&lt;/strong&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns case
  (:require [spec-tools.data-spec :as ds]
            [clojure.spec.alpha :as s]))

(def invalid-data {:cards (list 66)})

(def valid-data {:cards [66]})

(def cards
  {:cards [int?]})

(defn do-stuff [data]
  (if (s/valid? (ds/spec ::sp cards) data)
    (println &quot;Conforms to spec according to 'valid?'&quot;)
    (println &quot;Does not conform to spec according to 'valid?'&quot;))
  (if (s/explain-data (ds/spec ::sp cards) data)
    (println &quot;Does not conform to spec according to 'explain-data'&quot;)
    (println &quot;Conforms to spec according to 'explain-data'&quot;)))

(comment
  (do-stuff invalid-data)
  (do-stuff valid-data))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For some reason s/valid? and s/explain produce different result when a list is used, but same when a vector is used. When I tried to reproduce this issue with spec created directly with clojure.spec.alpha, everything was fine, so it seems the problem is with spec-tools. Nevertheless, it should produce same results in all validation functions, I guess&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12957/inconsistent-results-from-valid-explain-when-trying-values</guid>
<pubDate>Wed, 24 May 2023 14:41:51 +0000</pubDate>
</item>
<item>
<title>Macro-expansion hooks similar to clojure spec</title>
<link>https://ask.clojure.org/index.php/12956/macro-expansion-hooks-similar-to-clojure-spec</link>
<description>&lt;p&gt;&lt;code&gt;clojure.spec.alpha&lt;/code&gt; currently holds a privileged position as a core library for Clojure core, being able to instrument macros as well as functions. &lt;code&gt;Compiler.java&lt;/code&gt; relies on &lt;code&gt;clojure.spec.alpha&lt;/code&gt; in &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/4090f405466ea90bbaf3addbe41f0a6acb164dbb/src/jvm/clojure/lang/Compiler.java#L6967&quot;&gt;this function&lt;/a&gt;, calling &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/spec.alpha/blob/70f26984ee171f816eee6b162b563d403a49bfe8/src/main/clojure/clojure/spec/alpha.clj#L702&quot;&gt;macroexpand-check&lt;/a&gt; specifically. This is a neat interaction and &lt;code&gt;macroexpand-check&lt;/code&gt; is simple and obvious.&lt;/p&gt;
&lt;p&gt;Other tools (such as Malli) can provide instrumentation for Clojure functions through &lt;code&gt;alter-var-root&lt;/code&gt; (as seen &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/metosin/malli/blob/1a735fe5550b7dccf0832433f2f6d68ce9355edc/src/malli/instrument.clj#L28&quot;&gt;in malli.instrument/-strument!&lt;/a&gt;), but they lack the ability to hook into macro-expansions.&lt;/p&gt;
&lt;p&gt;It would be nice if &lt;code&gt;Compiler.checkSpecs&lt;/code&gt; had some hook to allow for tools such as Malli to be used to instrument macros.&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12956/macro-expansion-hooks-similar-to-clojure-spec</guid>
<pubDate>Tue, 23 May 2023 14:18:10 +0000</pubDate>
</item>
<item>
<title>Full specs for clojure.core in core.specs.alpha</title>
<link>https://ask.clojure.org/index.php/12851/full-specs-for-clojure-core-in-core-specs-alpha</link>
<description>&lt;p&gt;Right now, core.specs.alpha only has specs for some basic special forms and macros.&lt;/p&gt;
&lt;p&gt;Can it be expanded to include all functions defined in clojure.core? Or, perhaps there can be another optional namespace that contains these specs? I imagine having these specs available for instrumentation could be really helpful for training new Clojure programmers and bug-catching.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12851/full-specs-for-clojure-core-in-core-specs-alpha</guid>
<pubDate>Fri, 14 Apr 2023 15:09:25 +0000</pubDate>
</item>
<item>
<title>clojure.alpha.spec/gen can't generate samples for schemas containing unregistered specs</title>
<link>https://ask.clojure.org/index.php/12731/clojure-generate-samples-schemas-containing-unregistered</link>
<description>&lt;p&gt;The &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/spec-alpha2/wiki/Schema-and-select#literal-schemas&quot;&gt;wiki&lt;/a&gt; explains that we could include a map of unqualified keys to specs&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Additionally, you may include a map of unqualified keys to the specs or spec names that should be used for them:&lt;br&gt;
[::a ::b {:c (s/spec int?)}]&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;But generators created for such specs fail trying to resolve the spec&lt;/p&gt;
&lt;p&gt;&lt;code&gt;
(require '[clojure.alpha.spec :as s])
(require '[clojure.alpha.spec.gen :as gen])
(s/def ::a string?)
(s/def ::b string?)
(gen/generate (s/gen (s/schema [::a ::b {:c (s/spec int?)}])))
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;returns the following error&lt;br&gt;
&amp;gt; Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1957$fn$G (protocols.clj:11).&lt;br&gt;
No implementation of method: :gen* of protocol: #'clojure.alpha.spec.protocols/Spec found for class: clojure.lang.PersistentList&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12731/clojure-generate-samples-schemas-containing-unregistered</guid>
<pubDate>Tue, 07 Mar 2023 15:35:25 +0000</pubDate>
</item>
<item>
<title>Why isn't a spec for :clojure.spec.alpha/problems?</title>
<link>https://ask.clojure.org/index.php/12680/why-isnt-a-spec-for-clojure-spec-alpha-problems</link>
<description>&lt;p&gt;I wrote a spec to represent a validation outcomes and for the invalid one I'm including the list of problems reported by &lt;code&gt;s/explain-data&lt;/code&gt;.&lt;br&gt;
I have something like this&lt;br&gt;
&lt;code&gt;(s/def ::invalid-item (s/keys :req [::path :clojure.spec.alpha/problems]))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;But this leads to some generative tests to fail since there is no spec defined for &lt;code&gt;clojure.spec.alpha/problems&lt;/code&gt;&lt;br&gt;
&amp;gt; Execution error at clojure.alpha.spec/reg-resolve! (spec.clj:90).&lt;br&gt;
Unable to resolve spec: :clojure.spec.alpha/problems&lt;/p&gt;
&lt;p&gt;Although I can perhaps infer the spec and define it in my own namespace I was expecting the spec to exist, is this a fair assumption?&lt;br&gt;
Is it a good practice to use :clojure.spec.alpha/problems in my own spec or should I translate it to my own spec?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12680/why-isnt-a-spec-for-clojure-spec-alpha-problems</guid>
<pubDate>Wed, 22 Feb 2023 12:55:46 +0000</pubDate>
</item>
<item>
<title>`(s/merge X Y)` disables conformation for `:*-un` keys in X</title>
<link>https://ask.clojure.org/index.php/12573/s-merge-x-y-disables-conformation-for-un-keys-in-x</link>
<description>&lt;p&gt;When merging map specs with unqualified keys, only the last spec retains its conformation behavior:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::int-or-string (s/or :int int? :str string?))
(s/def ::a ::int-or-string)
(s/def ::b ::int-or-string)
(s/def ::a-un (s/keys :opt-un [::a]))
(s/def ::b-un (s/keys :opt-un [::b]))
(s/def ::merge-un (s/merge ::a-un ::b-un))
(s/conform ::merge-un {:a 100 :b &quot;foo&quot;})
;;=&amp;gt; {:a 100, :b [:str &quot;foo&quot;]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Compare the behavior with qualified keys:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::a-qual (s/keys :opt [::a]))
(s/def ::b-qual (s/keys :opt [::b]))
(s/def ::merge-qual (s/merge ::a-qual ::b-qual))
(s/conform ::merge-qual {::a 100 ::b &quot;foo&quot;})
;;=&amp;gt; #:user{:a [:int 100], :b [:str &quot;foo&quot;]}
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12573/s-merge-x-y-disables-conformation-for-un-keys-in-x</guid>
<pubDate>Sat, 21 Jan 2023 22:21:34 +0000</pubDate>
</item>
<item>
<title>how clojure.spec.alpha validation correctness is reasoned about?</title>
<link>https://ask.clojure.org/index.php/12473/clojure-spec-alpha-validation-correctness-reasoned-about</link>
<description>&lt;p&gt;Hi!&lt;br&gt;
I am exploring an &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/spec.alpha/blob/master/src/main/clojure/clojure/spec/alpha.clj&quot;&gt;implementation&lt;/a&gt; of clojure.spec.alpha and found out a rather &quot;little&quot; (according to my naive expectations) &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/spec.alpha/blob/master/src/test/clojure/clojure/test_clojure/spec.clj&quot;&gt;set&lt;/a&gt; of tests :)&lt;/p&gt;
&lt;p&gt;Clojure spec defines a set of primitives being able to compose one with each other.&lt;br&gt;
Another words Clojure spec defines a language to describe structure + its predicative properties.&lt;/p&gt;
&lt;p&gt;Assuming that spec semantics is described informally in &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/about/spec&quot;&gt;Rationale&lt;/a&gt; and &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/guides/spec&quot;&gt;Guide&lt;/a&gt; how do you reason about the correspondence between declared semantics and spec implementation?&lt;/p&gt;
&lt;p&gt;I expected a huge set of tests covering basic scenarios and finite number of their compositions -- kinda induction base. And if induction base is true then other compositions are also ok.&lt;/p&gt;
&lt;p&gt;how do you reason about verification of the clojure.spec.alpha validation implementation correctness?&lt;/p&gt;
&lt;p&gt;UPD: narrowed the question context to &lt;code&gt;validation&lt;/code&gt; aspect according to Alex Miller first answer.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12473/clojure-spec-alpha-validation-correctness-reasoned-about</guid>
<pubDate>Wed, 14 Dec 2022 14:48:44 +0000</pubDate>
</item>
<item>
<title>Accessing :column, :line &amp; :file meta data of a Spec</title>
<link>https://ask.clojure.org/index.php/12199/accessing-column-line-file-meta-data-of-a-spec</link>
<description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;is it possible to somehow get at the :column, :line and :file&lt;br&gt;
information of where a Spec was defined?&lt;/p&gt;
&lt;p&gt;I believe this information could be collected from &lt;code&gt;(meta &amp;amp;form)&lt;/code&gt; in&lt;br&gt;
the &lt;code&gt;s/def&lt;/code&gt; macro but isn't stored anywhere right now.&lt;/p&gt;
&lt;p&gt;I would be great to have this information available to support REPL&lt;br&gt;
based tooling for features like &quot;jump to definition&quot;.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12199/accessing-column-line-file-meta-data-of-a-spec</guid>
<pubDate>Mon, 12 Sep 2022 08:56:40 +0000</pubDate>
</item>
<item>
<title>Spec alias behavior</title>
<link>https://ask.clojure.org/index.php/12043/spec-alias-behavior</link>
<description>&lt;p&gt;I'm using &lt;code&gt;spec.alpha&lt;/code&gt; from Clojure &lt;code&gt;1.11.1&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require '[clojure.spec.alpha :as s])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Add spec &lt;code&gt;::foo&lt;/code&gt; with three specs that represent ways to &quot;alias&quot; another spec (direct, s/spec, s/and).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::foo int?)

(s/def ::bar      ::foo)
(s/def ::bar-spec (s/spec ::foo))
(s/def ::bar-and  (s/and ::foo))
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Problems with documentation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Only &lt;code&gt;::bar-and&lt;/code&gt; spec preserves the information about aliasing.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/form ::bar)      ;=&amp;gt; clojure.core/int?
(s/form ::bar-spec) ;=&amp;gt; clojure.core/int?
(s/form ::bar-and)  ;=&amp;gt; (clojure.spec.alpha/and :user/foo)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I find that useful, e.g. it's trivial to &quot;find usages&quot; of the &lt;code&gt;::foo&lt;/code&gt; spec.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Problems with generator overriding&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Add spec &lt;code&gt;::m&lt;/code&gt; which aggregates specs from above.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::m (s/keys :req [::foo ::bar ::bar-spec ::bar-and]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Overriding data generator for underlying &lt;code&gt;::foo&lt;/code&gt; spec doesn't work for &lt;code&gt;::bar-spec&lt;/code&gt; alias.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(gen/generate (s/gen ::bar      {::foo (fn [] (gen/return 0))})) ;=&amp;gt; 0
(gen/generate (s/gen ::bar-spec {::foo (fn [] (gen/return 0))})) ;=&amp;gt; -2
(gen/generate (s/gen ::bar-and  {::foo (fn [] (gen/return 0))})) ;=&amp;gt; 0

(gen/generate (s/gen ::m {::foo (fn [] (gen/return 0))}))
;=&amp;gt; #:user{:foo 0, :bar 0, :bar-spec 31, :bar-and 0}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Overriding generator for alias spec doesn't work in the case of direct alias &lt;code&gt;::bar&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(gen/generate (s/gen ::bar      {::bar      (fn [] (gen/return 0))})) ;=&amp;gt; -492158
(gen/generate (s/gen ::bar-spec {::bar-spec (fn [] (gen/return 0))})) ;=&amp;gt; 0
(gen/generate (s/gen ::bar-and  {::bar-and  (fn [] (gen/return 0))})) ;=&amp;gt; 0

(gen/generate (s/gen ::m
                     {::bar      (fn [] (gen/return 0))
                      ::bar-spec (fn [] (gen/return 0))
                      ::bar-and  (fn [] (gen/return 0))}))
;=&amp;gt; #:user{:foo 183249700, :bar -1, :bar-spec 0, :bar-and 0}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only way to override the generator for &lt;code&gt;::bar&lt;/code&gt; is to override it for &lt;code&gt;::foo&lt;/code&gt;. But that also affects all aliases of &lt;code&gt;::foo&lt;/code&gt;, which is not always desirable.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;I like the behavior of &lt;code&gt;s/and&lt;/code&gt; the most but its definition is arguably the most confusing for other users, because it sounds like &lt;code&gt;s/and&lt;/code&gt; should have multiple arguments.&lt;/p&gt;
&lt;p&gt;What is the rationale for behavior of other two approaches?&lt;br&gt;
Is that expected?&lt;br&gt;
Is there any alternative way to define aliases?&lt;br&gt;
Will this be changed in the future spec?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12043/spec-alias-behavior</guid>
<pubDate>Mon, 11 Jul 2022 15:01:18 +0000</pubDate>
</item>
<item>
<title>Has middle of sequence &amp;rest destructuring been considered?</title>
<link>https://ask.clojure.org/index.php/11985/has-middle-of-sequence-rest-destructuring-been-considered</link>
<description>&lt;p&gt;Currently, Clojure allows for destructuring patterns that contain &amp;amp;rest arguments:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (let [[a b &amp;amp; c] (range 5)] [a b c])
[0 1 (2 3 4)]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Python allows for the same, but also for placing the so-called catch-all in the front and middle position as well (&lt;a rel=&quot;nofollow&quot; href=&quot;https://peps.python.org/pep-3132/&quot;&gt;PEP description&lt;/a&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; a, *b, c = range(5)
&amp;gt;&amp;gt;&amp;gt; a
0
&amp;gt;&amp;gt;&amp;gt; c
4
&amp;gt;&amp;gt;&amp;gt; b
[1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Has similar been considered for Clojure? Could look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user=&amp;gt; (let [[a &amp;amp; b c] (range 5)] [a b c])
[0 (1 2 3) 4]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I feel like this would be doable with how the core functions destructure currently works, but it’s possible I don’t know some subtlety. &lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11985/has-middle-of-sequence-rest-destructuring-been-considered</guid>
<pubDate>Mon, 20 Jun 2022 13:23:18 +0000</pubDate>
</item>
<item>
<title>Add support for checking instrumented syms in clojure.spec.test.alpha</title>
<link>https://ask.clojure.org/index.php/11816/support-checking-instrumented-syms-clojure-spec-test-alpha</link>
<description>&lt;p&gt;The &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.github.io/spec.alpha/clojure.spec.test.alpha-api.html&quot;&gt;API&lt;/a&gt; for &lt;code&gt;clojure.spec.test.alpha&lt;/code&gt; doesn't currently support finding the list of instrumented syms. This is useful, for example, when you want to restore the previous state of instrumentation. The list of currently instrumented syms is stored &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/spec.alpha/blob/master/src/main/clojure/clojure/spec/test/alpha.clj#L161&quot;&gt;internally&lt;/a&gt; in an atom, so it might be reasonable that these syms could be exposed to the caller. Currently, the only way AFAIK to poll this information is as a side-effect of calling &lt;code&gt;stest/unstrument&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;It would be great if there was a function like &lt;code&gt;instrumented-syms&lt;/code&gt; which could be called to identify what has already been instrumented. :) &lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11816/support-checking-instrumented-syms-clojure-spec-test-alpha</guid>
<pubDate>Tue, 26 Apr 2022 15:48:48 +0000</pubDate>
</item>
<item>
<title>Common patterns for optional fields interact poorly with spec</title>
<link>https://ask.clojure.org/index.php/11584/common-patterns-optional-fields-interact-poorly-with-spec</link>
<description>&lt;p&gt;I do lots of work with spec, and a frequent problem that I run into is in constructing a spec'ed object that is a map with optional fields.&lt;/p&gt;
&lt;p&gt;A &lt;a rel=&quot;nofollow&quot; href=&quot;https://grep.app/search?q=assoc%28.%2A%5Cn%29%7B0%2C5%7D.%2A%3A.%2A%5C%28®exp=true&amp;amp;filter[lang][0]=Clojure&quot;&gt;common pattern&lt;/a&gt; looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(assoc {::required-field :value}
       ::optional-field (when some-condition
                          :optional-value))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is a significant issue with this pattern however: if &lt;code&gt;::optional-field&lt;/code&gt; is on a &lt;code&gt;spec/keys&lt;/code&gt; &lt;code&gt;:opt&lt;/code&gt; field that is non-nilable, then every object that excludes the optional field will be invalid according to that spec because it will have a &lt;code&gt;nil&lt;/code&gt; value instead of omitting the key.&lt;/p&gt;
&lt;p&gt;The alternative to this approach is to use something like &lt;code&gt;cond-&amp;gt;&lt;/code&gt; as seen below:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(cond-&amp;gt; {::required-field :value}
  some-condition (assoc ::optional-field :optional-value))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This pattern is far from the only usage of &lt;code&gt;cond-&amp;gt;&lt;/code&gt;, but it &lt;a rel=&quot;nofollow&quot; href=&quot;https://grep.app/search?q=%5C%28cond-%3E%28.%2A%5Cn%29%7B1%2C3%7D.%2A%5C%28assoc%5Cs®exp=true&amp;amp;filter[lang][0]=Clojure&quot;&gt;appears fairly frequently&lt;/a&gt;. I see this pattern as detrimental because it requires the condition to be surfaced all the way at the top here, and there cannot be a way for some function used to produce &lt;code&gt;:optional-value&lt;/code&gt; to abort via nil punning while still producing a valid value without requiring additional flow control and local bindings.&lt;/p&gt;
&lt;p&gt;A more flexible solution to this problem uses &lt;code&gt;if-some&lt;/code&gt;, but this solution is verbose and becomes unwieldy when multiple optional fields need to be included at once, as can be seen below.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(let [m {::required-field :value}
      m (if-some [v (produces-optional-value)]
          (assoc m ::optional-field v)
          m)
      m (if-some [v (produces-other-value)]
          (assoc m ::other-field v)
          m)]
  m)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One solution to this problem is the function &lt;code&gt;assoc-some&lt;/code&gt;, which is like &lt;code&gt;assoc&lt;/code&gt;, but when given a &lt;code&gt;nil&lt;/code&gt; value it elides the key. This function is provided in &lt;a rel=&quot;nofollow&quot; href=&quot;http://weavejester.github.io/medley/medley.core.html#var-assoc-some&quot;&gt;medley&lt;/a&gt;, and &lt;a rel=&quot;nofollow&quot; href=&quot;https://grep.app/search?q=assoc-some&amp;amp;filter[lang][0]=Clojure&quot;&gt;sees some use&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The above unwieldy example would, with &lt;code&gt;assoc-some&lt;/code&gt;, look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(assoc-some {::required-field :value}
            ::optional-field (produces-optional-value)
            ::other-field (produces-other-value))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is likely not the only solution to this problem, but I think it is a problem worth considering.&lt;/p&gt;
&lt;p&gt;An example of another instance where the named solution &lt;em&gt;wouldn't&lt;/em&gt; work, but that appears to follow more or less the same pattern is &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/IGJoshua/farolero/blob/950d81e4a417770dd9d51b67358dbb2cc89b2cc2/src/cljc/farolero/core.cljc#L503-L516&quot;&gt;this code in farolero&lt;/a&gt;, which would be solved by a parallel function (that I have not yet found an implementation of in the wild besides my own) &lt;code&gt;update-some&lt;/code&gt; which will update a key with a new value, and if that value is &lt;code&gt;nil&lt;/code&gt; will remove the key.&lt;/p&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11584/common-patterns-optional-fields-interact-poorly-with-spec</guid>
<pubDate>Tue, 15 Feb 2022 21:48:34 +0000</pubDate>
</item>
<item>
<title>Unresolvable symbol safe for spec def?</title>
<link>https://ask.clojure.org/index.php/11543/unresolvable-symbol-safe-for-spec-def</link>
<description>&lt;p&gt;I  am able to register a spec with an unresolvable symbol even though the docs seem to indicate this should not be an acceptable value for the registry. Is this safe? Will it keep working?&lt;/p&gt;
&lt;p&gt;The &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html#clojure.spec.alpha/def&quot;&gt;documentation&lt;/a&gt; for &lt;code&gt;s/def&lt;/code&gt; says it takes as its first argument &quot;a namespace-qualified keyword or resolvable symbol.&quot; This is more expansive than the &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/guides/spec&quot;&gt;spec Guide&lt;/a&gt;, which says, &quot;Spec names are always fully-qualified keywords.&quot;&lt;/p&gt;
&lt;p&gt;But I recently switched a bunch of specs from keywords to symbols (fully qualified but unbound) and everything seems to work fine, from &lt;code&gt;s/valid?&lt;/code&gt; to &lt;code&gt;s/conform&lt;/code&gt; to &lt;code&gt;s/assert&lt;/code&gt;, referencing the definitions in &lt;code&gt;s/keys&lt;/code&gt;, etc.&lt;/p&gt;
&lt;p&gt;Here is a reduced example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user&amp;gt; (require '[clojure.spec.alpha :as s])
nil
user&amp;gt; (ns-resolve 'com.example 'foo)
Execution error at user/eval30383 (form-init8866507034262802929.clj:53).
No namespace: com.example found
user&amp;gt; (s/def com.example/foo string?)
com.example/foo
user&amp;gt; (s/valid? 'com.example/foo &quot;x&quot;)
true
user&amp;gt; (s/valid? 'com.example/foo 42)
false
user&amp;gt; (s/valid? (s/keys :req ['com.example/foo]) {'com.example/foo &quot;x&quot;})
true
user&amp;gt; (ns-resolve 'com.example 'foo)
Execution error at user/eval30389 (form-init8866507034262802929.clj:62).
No namespace: com.example found
user&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am guessing this is an instance where the implementation has raced a bit ahead of the docs, and even further ahead of the guide. Either or that or maybe I'm misunderstanding what &quot;resolvable symbol&quot; means. Thanks for any guidance or tips!&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11543/unresolvable-symbol-safe-for-spec-def</guid>
<pubDate>Thu, 03 Feb 2022 00:44:47 +0000</pubDate>
</item>
<item>
<title>The specs for `defn` do not roundtrip using `conform` / `unform`</title>
<link>https://ask.clojure.org/index.php/11241/the-specs-for-defn-do-not-roundtrip-using-conform-unform</link>
<description>&lt;p&gt;There are patches and userland fixes in &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJ-2021&quot;&gt;this JIRA ticket&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I am registering it here to enable upvoting!!&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11241/the-specs-for-defn-do-not-roundtrip-using-conform-unform</guid>
<pubDate>Wed, 03 Nov 2021 15:11:03 +0000</pubDate>
</item>
<item>
<title>Does fdef support multimethods?</title>
<link>https://ask.clojure.org/index.php/11101/does-fdef-support-multimethods</link>
<description>&lt;p&gt;Hello, As of 2016 spec &lt;code&gt;fdef&lt;/code&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;https://groups.google.com/g/clojure/c/BBWc3c40RDI&quot;&gt;did not support multimethods&lt;/a&gt;. Has this officially changed?&lt;/p&gt;
&lt;p&gt;Although it seemed to work at first -- I &lt;strong&gt;was able to instrument and test a multimethod&lt;/strong&gt; with &lt;code&gt;fdef&lt;/code&gt; --  I later hit an issue using &lt;code&gt;get-method&lt;/code&gt; on said multimethod after I instrumented it. &lt;/p&gt;
&lt;p&gt;I could make a test case for the get-method instrumentation issue,but I thought I'd first clear  up if this multimethod &lt;code&gt;fdef&lt;/code&gt; usage is even supposed to work  :-)&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11101/does-fdef-support-multimethods</guid>
<pubDate>Fri, 24 Sep 2021 20:09:27 +0000</pubDate>
</item>
<item>
<title>Spec `form` on `keys` missing namespace for `or`</title>
<link>https://ask.clojure.org/index.php/11087/spec-form-on-keys-missing-namespace-for-or</link>
<description>&lt;p&gt;&lt;code&gt;[org.clojure/clojure &quot;1.11.0-alpha2&quot;]&lt;/code&gt;&lt;br&gt;
&lt;code&gt;clojure.spec.alpha&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Repro (see &lt;code&gt;or&lt;/code&gt; inside &lt;code&gt;keys&lt;/code&gt;):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :x/foo int?)
(s/def :x/bar string?)

(s/form (s/keys :req-un [(or :x/foo :x/bar)]))
; =&amp;gt; (clojure.spec.alpha/keys :req-un [(or :x/foo :x/bar)])

(s/form (s/and (s/keys :req-un [(or :x/foo :x/bar)])))
;=&amp;gt; (clojure.spec.alpha/and (clojure.spec.alpha/keys :req-un [(clojure.core/or :x/foo :x/bar)]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is there a way to force &lt;code&gt;or&lt;/code&gt; to be namespaced in the first &lt;code&gt;form&lt;/code&gt;?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11087/spec-form-on-keys-missing-namespace-for-or</guid>
<pubDate>Thu, 23 Sep 2021 11:31:47 +0000</pubDate>
</item>
<item>
<title>Spec2: Use case for multi-spec as Schema provider</title>
<link>https://ask.clojure.org/index.php/10696/spec2-use-case-for-multi-spec-as-schema-provider</link>
<description>&lt;p&gt;I have a scenario where I'd like to define some attributes that are internal and common, and some other attributes that are external and dynamic, both related to the same &quot;things&quot;. Specifically, I am trying to model a trading library. There are many different exchanges/brokers/services, but all of them talk about similar/equivalent things.&lt;br&gt;
   Let's take the example of an order, you can have some attributes that can be common in all services (they don't necessarily appear on all services):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(spec/def :order/id string?)
(spec/def :order/quantity number?)
(spec/def :order/side #{:buy :sell})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, every external service can have specific attributes related to how that service handles orders:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(spec/def :service1-order/execution-instructions (spec/coll-of string?))
(spec/def :service2-order/iceberg-quantity (spec/coll-of string?))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I was thinking on having a multi-spec returning schemas, so each service can define what schema to use for an order, on a key like :service/name, but all of them can be &quot;selected&quot; from :order/order. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmulti order-schema :service/name)

(defmethod order-schema :service1
  [_]
  (spec/schema [:order/id :order/quantity :order/side :service1-order/execution-instructions]))

(defmethod order-schema :service2
  [_]
  (spec/schema [:order/id :order/quantity :order/side :service2-order/iceberg-quantity]))

(defmethod order-schema :default
  [_]
  (spec/schema [:order/id :order/quantity :order/side]))

(spec/def :order/order (spec/multi-spec order-schema :service/name))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can be useful also when nesting objects. Imagine I have an account that will contain orders. The account can also be specced as a multi-spec for each service:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(spec/def :account/id string?)
(spec/def :account/orders (spec/coll-of :order/order))

(spec/def :service1-account/type #{:spot :margin :futures})

(defmulti account-schema :service/id)

(defmethod account-schema :service1
  [_]
  (spec/schema [:account/id :account/orders :service1-account/type]))

(defmethod account-schema :default
  [_]
  (spec/schema [:account/id :account/orders]))

(spec/def :account/account (spec/multi-spec account-schema :service/id))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This would allow to select in the following manner: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(spec/def ::service1-account-info (spec/select :account/account [:service1-account/type :account/orders {:account/orders [:order/id :order/quantity :service1-order/execution-instructions]}]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way, external services can specify the information they handle about the common layer domain attributes and entities, as well as specify their specific attributes. The user is able to reuse a set of common functions that operate on the common attributes, and if different services provide these common attributes, they can be used interchangeably, while keeping a way to create functions and selections that are specific to specific services.&lt;/p&gt;
&lt;p&gt;Does this use case make sense? Is there a different way to model this that would be better?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10696/spec2-use-case-for-multi-spec-as-schema-provider</guid>
<pubDate>Tue, 15 Jun 2021 21:18:25 +0000</pubDate>
</item>
<item>
<title>Spec for any map entry whose key has a spec</title>
<link>https://ask.clojure.org/index.php/10684/spec-for-any-map-entry-whose-key-has-a-spec</link>
<description>&lt;p&gt;I need to spec map-entry-like vectors whose keys are themselves spec-ed. Each vector looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[:my.spec-ed.key/foo 42]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The specific keys used vary, and I am hoping to write a predicate that would dynamically spec any map-entry-like vector based on its key the way maps are spec-ed entry by entry via (s/keys).&lt;/p&gt;
&lt;p&gt; In this case let's say the key is spec-ed like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :my.spec-ed.key/foo int?)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The best I can come up with to spec the map-entry-like vector is this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :my.spec-ed.key/pair (s/and vector? #(s/valid? (first %) (second %))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works, but I don't like it because (s/explain) is not as good when something goes wrong. For example, &lt;code&gt;(s/explain :my.spec-ed.key/foo &quot;foo&quot;)&lt;/code&gt; is informative:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;foo&quot; - failed: int? spec: :my.spec-ed.key/foo
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Conversely, &lt;code&gt;(s/explain :my.spec-ed.key/pair [:my.spec-ed.key/foo &quot;bar&quot;])&lt;/code&gt; doesn't end up explaining what really went wrong (that I passed a string instead of an int), I have to go look up the original spec to find that out:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;[:my.spec-ed.key/foo &quot;bar&quot;] - failed: (valid? (first %) (second %)) spec: :my.spec-ed.key/pair
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Does anyone have any ideas for a better way to spec my map-entry-like vectors? For what it's worth, these vectors don't actually come from maps, and are not actually going to maps, so I can't use any of the spec functions designed to work on maps specifically.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10684/spec-for-any-map-entry-whose-key-has-a-spec</guid>
<pubDate>Sat, 12 Jun 2021 23:55:48 +0000</pubDate>
</item>
<item>
<title>Is the defn spec overly restrictive?</title>
<link>https://ask.clojure.org/index.php/10627/is-the-defn-spec-overly-restrictive</link>
<description>&lt;p&gt;It seems like it should be possible to use a macro for generating a attribute map for &lt;code&gt;defn&lt;/code&gt; but it appears as if the &lt;code&gt;defn&lt;/code&gt; spec requires a literal map. See example below, which makes the spec fail.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;   (defmacro example [f output input]
     `{:test (fn [] (is (= (~f ~input) ~output)))})

   (defn tails
     (example tails
              [1 2 3 4]
              '([1 2 3 4] (2 3 4) (3 4) (4)))
     [coll]
     (take-while some? (iterate next coll)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;-&amp;gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; Call to clojure.core/defn did not conform to spec.
       #:clojure.spec.alpha{:problems
                            ({:path [:fn-tail :arity-1 :params],
                              :pred clojure.core/vector?,
                              :val
                              (example
                               tails
                               [1 2 3 4]
                               '([1 2 3 4] (2 3 4) (3 4) (4))),
                              :via
                              [:clojure.core.specs.alpha/defn-args
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/param-list
                               :clojure.core.specs.alpha/param-list],
                              :in [1]}
                             {:path [:fn-tail :arity-n :bodies :params],
                              :pred clojure.core/vector?,
                              :val example,
                              :via
                              [:clojure.core.specs.alpha/defn-args
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/params+body
                               :clojure.core.specs.alpha/param-list
                               :clojure.core.specs.alpha/param-list],
                              :in [1 0]}),
                            :spec
                            #object[clojure.spec.alpha$regex_spec_impl$reify__2510 0x643aeb02 &quot;clojure.spec.alpha$regex_spec_impl$reify__2510@643aeb02&quot;],
                            :value
                            (tails
                             (example tails [1 2 3 4] '([1 2 3 4] (2 3 4) (3 4) (4)))
                             [coll]
                             (take-while some? (iterate next coll))),
                            :args
                            (tails
                             (example tails [1 2 3 4] '([1 2 3 4] (2 3 4) (3 4) (4)))
                             [coll]
                             (take-while some? (iterate next coll)))}
                     alpha.clj:  712  clojure.spec.alpha/macroexpand-check
                     alpha.clj:  704  clojure.spec.alpha/macroexpand-check
                      AFn.java:  156  clojure.lang.AFn/applyToHelper
                      AFn.java:  144  clojure.lang.AFn/applyTo
                      Var.java:  705  clojure.lang.Var/applyTo
                 Compiler.java: 6974  clojure.lang.Compiler/checkSpecs
                 Compiler.java: 6992  clojure.lang.Compiler/macroexpand1
                 Compiler.java: 7079  clojure.lang.Compiler/macroexpand
                 Compiler.java: 7165  clojure.lang.Compiler/eval
                 Compiler.java: 7136  clojure.lang.Compiler/eval
                      core.clj: 3202  clojure.core/eval
                      core.clj: 3198  clojure.core/eval
        interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn/fn
                      AFn.java:  152  clojure.lang.AFn/applyToHelper
                      AFn.java:  144  clojure.lang.AFn/applyTo
                      core.clj:  667  clojure.core/apply
                      core.clj: 1977  clojure.core/with-bindings*
                      core.clj: 1977  clojure.core/with-bindings*
                   RestFn.java:  425  clojure.lang.RestFn/invoke
        interruptible_eval.clj:   87  nrepl.middleware.interruptible-eval/evaluate/fn
                      main.clj:  437  clojure.main/repl/read-eval-print/fn
                      main.clj:  437  clojure.main/repl/read-eval-print
                      main.clj:  458  clojure.main/repl/fn
                      main.clj:  458  clojure.main/repl
                      main.clj:  368  clojure.main/repl
                   RestFn.java:  137  clojure.lang.RestFn/applyTo
                      core.clj:  667  clojure.core/apply
                      core.clj:  662  clojure.core/apply
                    regrow.clj:   20  refactor-nrepl.ns.slam.hound.regrow/wrap-clojure-repl/fn
                   RestFn.java: 1523  clojure.lang.RestFn/invoke
        interruptible_eval.clj:   84  nrepl.middleware.interruptible-eval/evaluate
        interruptible_eval.clj:   56  nrepl.middleware.interruptible-eval/evaluate
        interruptible_eval.clj:  152  nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn
                      AFn.java:   22  clojure.lang.AFn/run
                   session.clj:  202  nrepl.middleware.session/session-exec/main-loop/fn
                   session.clj:  201  nrepl.middleware.session/session-exec/main-loop
                      AFn.java:   22  clojure.lang.AFn/run
                   Thread.java:  829  java.lang.Thread/run
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10627/is-the-defn-spec-overly-restrictive</guid>
<pubDate>Tue, 18 May 2021 10:21:51 +0000</pubDate>
</item>
<item>
<title>should the docstring of `s/keys` give more information about :req-un?</title>
<link>https://ask.clojure.org/index.php/10589/should-the-docstring-of-keys-give-more-information-about-req</link>
<description>&lt;p&gt;Question came up on slack about how to validate optional keys. I was going to point them to the docstring for &lt;code&gt;s/keys&lt;/code&gt; but found it might need some clarification. The docstring in question:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;-------------------------
clojure.spec.alpha/keys
([&amp;amp; {:keys [req req-un opt opt-un gen]}])
Macro
  Creates and returns a map validating spec. :req and :opt are both
  vectors of namespaced-qualified keywords. The validator will ensure
  the :req keys are present. The :opt keys serve as documentation and
  may be used by the generator.

  The :req key vector supports 'and' and 'or' for key groups:

  (s/keys :req [::x ::y (or ::secret (and ::user ::pwd))] :opt [::z])

  There are also -un versions of :req and :opt. These allow
  you to connect unqualified keys to specs.  In each case, fully
  qualfied keywords are passed, which name the specs, but unqualified
  keys (with the same name component) are expected and checked at
  conform-time, and generated during gen:

  (s/keys :req-un [:my.ns/x :my.ns/y])

  The above says keys :x and :y are required, and will be validated
  and generated by specs (if they exist) named :my.ns/x :my.ns/y 
  respectively.

  In addition, the values of *all* namespace-qualified keys will be validated
  (and possibly destructured) by any registered specs. Note: there is
  no support for inline value specification, by design.

  Optionally takes :gen generator-fn, which must be a fn of no args that
  returns a test.check generator.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I had expected to see something to the effect that if the keys specified by &lt;code&gt;:opt&lt;/code&gt; or &lt;code&gt;:opt-un&lt;/code&gt; were present, they would be validated against the specs for those keys. Instead it only mentions that they serve as documentation.&lt;/p&gt;
&lt;p&gt;Seemed like this could perhaps use some clarification.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10589/should-the-docstring-of-keys-give-more-information-about-req</guid>
<pubDate>Wed, 12 May 2021 03:34:53 +0000</pubDate>
</item>
<item>
<title>spec fdef validating relationships between arguments</title>
<link>https://ask.clojure.org/index.php/10497/spec-fdef-validating-relationships-between-arguments</link>
<description>&lt;p&gt;I have the following data structure as the state of my mini restaurant app&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    {:orders {:e4d55743-c964-48e8-9cd1-cb1cf9075617 [#:helloworld.restaurant{:name &quot;chilly parotta&quot;, :quantity 3}  #:helloworld.restaurant{:name &quot;masal dosa&quot;, :quantity 2}]}, :menu {:kothu-parotta #:helloworld.restaurant{:name &quot;Kothu Parotta&quot;, :price 9.5, :quantity 50},  :butter-naan #:helloworld.restaurant{:name &quot;Butter Naan&quot;, :price 3, :quantity 50}, :paneer-butter-masala #:helloworld.restaurant{:name &quot;Paneer Butter Masala&quot;, :price 9.5, :quantity 50}}}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I have a  function to update the menu item corresponding to every  order item in the order to deduct the order quantity from the inventory and return the new menu. I want to validate&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The item in the order item, should exist in the menu&lt;/li&gt;
&lt;li&gt;The item's quantity in the inventory, should be greater than the ordered quantity&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I have the following specs (Note: I have not validated 2 for now). When I run this, function call to update-menu-item-for-order-item is not validating my predicate for 1. What am I doing wrong in the fdef spec?&lt;/p&gt;
&lt;pre&gt;
(s/def ::name string?)
(s/def ::price number?)
(s/def ::quantity number?)
(s/def ::order-item (s/keys :req [::name ::quantity]))
(s/def ::order-items (s/coll-of ::order-item))
(s/def ::menu-item (s/keys :req [::name ::price ::quantity]))
(s/def ::menu-item-id keyword?)
(s/def ::menu (s/map-of ::menu-item-id ::menu-item))

(s/fdef restaurant-save-menu :args (s/cat :menu-items (s/coll-of ::menu-item)))

(s/fdef update-menu-item-for-order-item
        :args (s/and (s/cat :menu ::menu :order-item ::order-item)
                     #(contains? (:menu %) (keyword (slugify (::name (:order-item %))))))
        :ret ::menu)

(stest/instrument `update-menu-item-for-order-item)
(stest/instrument `restaurant-save-menu)
&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10497/spec-fdef-validating-relationships-between-arguments</guid>
<pubDate>Mon, 19 Apr 2021 20:30:39 +0000</pubDate>
</item>
<item>
<title>Can I use spec as type hint?</title>
<link>https://ask.clojure.org/index.php/10464/can-i-use-spec-as-type-hint</link>
<description>&lt;p&gt;One cool feature of schema is that it works well with type hint,&lt;br&gt;
So I can use it for validation, compiler optimization and documentation directly on the function. I'd like to know if the same could be done using specs and if possible, a real example. Thanks!&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10464/can-i-use-spec-as-type-hint</guid>
<pubDate>Sun, 11 Apr 2021 07:30:38 +0000</pubDate>
</item>
<item>
<title>Unexpected spec failure on namespaced keys not included in :req</title>
<link>https://ask.clojure.org/index.php/10356/unexpected-spec-failure-on-namespaced-keys-not-included-req</link>
<description>&lt;p&gt;Hi there, I am here to report what I think is my second quirk in a week with spec instrumentation.&lt;/p&gt;
&lt;p&gt;I apologize if it has been already reported as bug but basically I cannot understand/expected the last call to trigger an error - the &lt;code&gt;::id-map&lt;/code&gt; spec does not require the check on &lt;code&gt;::other&lt;/code&gt; after all.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns repro-spec.test
  (:require [clojure.spec.alpha :as s]
            [clojure.spec.test.alpha :as st]))

(s/def ::impl any?)
(s/def ::id string?)
(s/def ::other number?)

(s/def ::id-map (s/keys :req [::id]))

(s/fdef delete-impl
  :args (s/cat :impl ::impl
               :id-map ::id-map))

(defn delete-impl [_ id-map]
  (println (str &quot;This deletes &quot; (::id id-map)))
  id-map)

(st/instrument)

(delete-impl {} {::id &quot;test&quot;})
;; prints &quot;This deletes test&quot;

(delete-impl {} {::id &quot;test&quot; ::other &quot;ignore me&quot;})
;; Execution error - invalid arguments to repro-spec.test/delete-impl at (REPL:27).
;; &quot;ignore me&quot; - failed: number? at: [:id-map :repro-spec.test/other] spec: :repro-spec.test/other
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am doing something wrong?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10356/unexpected-spec-failure-on-namespaced-keys-not-included-req</guid>
<pubDate>Thu, 18 Mar 2021 00:19:18 +0000</pubDate>
</item>
<item>
<title>Spec instrumentation does not work with extend-with-metadata</title>
<link>https://ask.clojure.org/index.php/10347/spec-instrumentation-does-not-work-with-extend-with-metadata</link>
<description>&lt;p&gt;Hi there, our code base have embraced &lt;code&gt;extend-with-metadata&lt;/code&gt; protocols since its introduction - we like to have map components rather than records.&lt;/p&gt;
&lt;p&gt;However, the code base and team are big clojure spec users and we were expecting the last form of the following to throw:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns defproto-spec.test
  (:require [clojure.spec.alpha :as s]
            [clojure.spec.test.alpha :as st]))

(defprotocol SpecTest
  :extend-via-metadata true
  (delete [impl id]))

(s/fdef delete-impl
  :args (s/cat :impl any? :id string?))

(defn delete-impl [_ id]
  (println (str &quot;This deletes &quot; id))
  id)

(def impl (with-meta {} {`delete delete-impl}))

(st/instrument)

(delete impl 1) ;; should throw but it doesn't
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why were we expecting it to work? &lt;/p&gt;
&lt;p&gt;Maybe because the feature is newer (1.10) and because of the following:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;defproto-spec.test&amp;gt; (st/instrument)
[defproto-spec.test/delete-me defproto-spec.test/delete-impl]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unfortunately that's not that case. I would be willing to work on a patch/document this if is not already in  someone else's pipeline.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;br&gt;
Andrea.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10347/spec-instrumentation-does-not-work-with-extend-with-metadata</guid>
<pubDate>Mon, 15 Mar 2021 17:22:34 +0000</pubDate>
</item>
<item>
<title>Generating a function invokes the function</title>
<link>https://ask.clojure.org/index.php/10293/generating-a-function-invokes-the-function</link>
<description>&lt;p&gt;I was experimenting with custom function generators in order to support the scenario describe in spec guide &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/guides/spec#_combining_check_and_instrument&quot;&gt;https://clojure.org/guides/spec#_combining_check_and_instrument&lt;/a&gt; but with the goal of making the generated values depend on the concrete arguments and not only on the :ret spec.&lt;/p&gt;
&lt;p&gt;While working on that, I noticed a weird behavior that I fail to understand where a generated function is being called 21 times before actually being returned.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require '[clojure.spec.alpha :as s]
         '[clojure.spec.gen.alpha :as gen])

(s/fdef foo
  :args (s/cat :x int?)
  :gen #(gen/return
         (fn [&amp;amp; argv]
           (prn argv)
           (gen/generate (s/gen string?)))))

(gen/generate (s/gen `foo))
;; Prints:
;; (-1)
;; (-1)
;; (-2)
;; (-1)
;; (3)
;; (-2)
;; (-2)
;; (-1)
;; (-35)
;; (14)
;; (-16)
;; (26)
;; (-17)
;; (150)
;; (0)
;; (-1)
;; (25)
;; (5638)
;; (543)
;; (57)
;; (257)
;; Returns: #function[fspec-gen-bug.core/fn--6577/fn--6578]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This behavior seems specific to function generation for example &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::bar
  (s/spec (s/coll-of int?)
          :gen #(gen/return
                 (doto [(gen/generate (s/gen int?))]
                   prn))))

(gen/generate (s/gen ::bar))
;; Prints:
;; [438803]
;; Returns: [438803]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is there a good reason why generated functions are invoked eagerly ? or is it a bug ?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10293/generating-a-function-invokes-the-function</guid>
<pubDate>Sat, 06 Mar 2021 21:55:46 +0000</pubDate>
</item>
<item>
<title>`cljs.spec.test.alpha/check` does not support passing lexically scoped values</title>
<link>https://ask.clojure.org/index.php/9976/cljs-spec-alpha-check-support-passing-lexically-scoped-values</link>
<description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;while trying to port some Clojure code to Clojurescript, I encountered a problem in my test suite when trying to check a list of function specs symbols using &lt;code&gt;cljs.spec.test.alpha/check&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here is a small scenario to reproduce the issue:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;br&gt;
(ns spec.check.problem&lt;br&gt;
  (:require&lt;br&gt;
   [clojure.spec.alpha :as s]&lt;br&gt;
   [clojure.spec.gen.alpha :as gen]&lt;br&gt;
   [clojure.spec.test.alpha :as stest]&lt;br&gt;
   [clojure.test.check]&lt;br&gt;
   [clojure.test.check.properties]))&lt;/p&gt;
&lt;p&gt;(defn foo&lt;br&gt;
  [x]&lt;br&gt;
  (inc x))&lt;/p&gt;
&lt;p&gt;(s/fdef foo&lt;br&gt;
  :args (s/cat :x int?)&lt;br&gt;
  :ret int?)&lt;/p&gt;
&lt;p&gt;(def list-of-specs&lt;br&gt;
  `[foo])&lt;/p&gt;
&lt;p&gt;(stest/check `[foo])        ; =&amp;gt; OK&lt;/p&gt;
&lt;p&gt;(stest/check list-of-specs) ; =&amp;gt; Unable to resolve symbol: list-of-specs in this context&lt;br&gt;
&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The above example work with Clojure  but not with ClojureScript. Is that intended or is this a bug ?&lt;/p&gt;
&lt;p&gt;I have used Clojure 1.10.1 and Clojurescript 1.10.758 for my tests.&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9976/cljs-spec-alpha-check-support-passing-lexically-scoped-values</guid>
<pubDate>Sun, 27 Dec 2020 11:30:23 +0000</pubDate>
</item>
<item>
<title>Using (s/or) without destructuring</title>
<link>https://ask.clojure.org/index.php/9844/using-s-or-without-destructuring</link>
<description>&lt;p&gt;I have a sequence of characters, and am trying to use (s/cat ...) with (s/or) in order to conform the sequence.&lt;/p&gt;
&lt;p&gt;The input could look something like &lt;code&gt;[0 1 2 3]&lt;/code&gt; or &lt;code&gt;[\a \b \c \d]&lt;/code&gt; and I am trying to use this spec.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/conform (s/cat :top (s/or :nums (s/+ #{0 1 2 3 4})
                             :letters (s/+ #{\a \b \c \d})
                  ... more stuff))
           [0 1 2 3])
=&amp;gt; :clojure.spec.alpha/invalid
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Not using the &lt;code&gt;(s/or)&lt;/code&gt; works if I choose one of &lt;code&gt;:nums&lt;/code&gt; or &lt;code&gt;:letters&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/conform (s/cat :first-set (s/+ #{0 1 2 3 4}))
           [0 1 2 3])
=&amp;gt; {:first-set [0 1 2 3]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The initial example works if the entire sequence is wrapped in another vec, but this isn't what I want.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/conform (s/cat :first-set (s/or :nums (s/+ #{0 1 2 3 4})
                                   :letters (s/+ #{\a \b \c \d})))
           [[0 1 2 3]])
=&amp;gt; {:first-set [:nums [0 1 2 3]]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using &lt;code&gt;(or ...)&lt;/code&gt; also seems to only work inside &lt;code&gt;(s/keys)&lt;/code&gt;. I'm guessing it's because the &lt;code&gt;(or&lt;/code&gt; always takes the first spec because it's not falsy.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/conform (s/cat :first-set (or (s/+ #{0 1 2 3 4})
                                 (s/+ #{\a \b \c \d})))
           [0 1 2 3])
=&amp;gt; {:first-set [0 1 2 3]}

(s/conform (s/cat :first-set (or (s/+ #{0 1 2 3 4})
                                 (s/+ #{\a \b \c \d})))
           [\a \b \c])
=&amp;gt; :clojure.spec.alpha/invalid
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is what I want to happen.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/conform (s/cat :first-set (s/something-here (s/+ #{0 1 2 3 4})
                                               (s/+ #{\a \b \c \d})))
           [0 1 2 3])
=&amp;gt; {:first-set [0 1 2 3]}

(s/conform (s/cat :first-set (s/something-here (s/+ #{0 1 2 3 4})
                                               (s/+ #{\a \b \c \d})))
           [\a \b \c])
=&amp;gt; {:first-set [\a \b \c]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I'm wondering what to do here.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9844/using-s-or-without-destructuring</guid>
<pubDate>Sun, 22 Nov 2020 19:20:02 +0000</pubDate>
</item>
<item>
<title>signal an error on mulimethod without a dispatch function?</title>
<link>https://ask.clojure.org/index.php/9737/signal-an-error-on-mulimethod-without-a-dispatch-function</link>
<description>&lt;pre&gt;&lt;code&gt;user=&amp;gt; (defmulti thing)
#'user/thing
user=&amp;gt; (defmethod thing :default [x] :hi)
#object[clojure.lang.MultiFn 0x7b60c3e &quot;clojure.lang.MultiFn@7b60c3e&quot;]
user=&amp;gt; (thing :hello)
Execution error (NullPointerException) at user/eval144 (REPL:1).
null
user=&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Someone was surprised by this behavior and missed the error that there was no dispatch function defined. Notable that there is some validation for &lt;code&gt;defmulti&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; (defmulti thing [x] (fn [x] x))
Syntax error macroexpanding defmulti at (REPL:1:1).
The syntax for defmulti has changed. Example: (defmulti name dispatch-fn :default dispatch-value)
user=&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Errors</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9737/signal-an-error-on-mulimethod-without-a-dispatch-function</guid>
<pubDate>Wed, 28 Oct 2020 20:09:35 +0000</pubDate>
</item>
<item>
<title>function spec challenge</title>
<link>https://ask.clojure.org/index.php/9690/function-spec-challenge</link>
<description>&lt;p&gt;What is the simplest way to spec this function? Specifically, the &lt;code&gt;:fn&lt;/code&gt; portion&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::edges #{0.149 0.150 0.050})
(s/def ::result #{:correct :incorrect})

(s/fdef foo
        :args (s/cat :student-response ::edges :authoritative-answer ::edges)
        :ret ::result
:fn ? )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Basically, if the &lt;code&gt;student-response&lt;/code&gt; rounded to the tenths place matches the &lt;code&gt;authoritative-answer&lt;/code&gt; rounded to the tenths place, the function returns &lt;code&gt;:correct&lt;/code&gt;, otherwise &lt;code&gt;:incorrect&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I selected edges that would hit the rounding boundaries rather than hoping the generative process for numbers would stumble on those edges.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9690/function-spec-challenge</guid>
<pubDate>Wed, 07 Oct 2020 03:38:56 +0000</pubDate>
</item>
<item>
<title>Has the destructuring spec changed since 1.8.0?</title>
<link>https://ask.clojure.org/index.php/9643/has-the-destructuring-spec-changed-since-1-8-0</link>
<description>&lt;p&gt;1.8.0:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;clj-rcon-test.core=&amp;gt; (defn- hi [as-codec &amp;amp; {:keys [offset] :or {:offset 0}}]
                #_=&amp;gt;   (println &quot;hi&quot;))
#'clj-rcon-test.core/hi
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;1.10.0:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;clj-rcon-test.core=&amp;gt; (defn- hi [as-codec &amp;amp; {:keys [offset] :or {:offset 0}}]
                #_=&amp;gt;   (println &quot;hi&quot;))
Syntax error macroexpanding clojure.core/defn- at (form-init17556793069765460591.clj:1:1).
:offset - failed: simple-symbol? at: [:fn-tail :arity-1 :params :var-params :var-form :map-destructure :or 0] spec: :clojure.core.specs.alpha/or
{:keys [offset], :or {:offset 0}} - failed: simple-symbol? at: [:fn-tail :arity-1 :params :var-params :var-form :local-symbol] spec: :clojure.core.specs.alpha/local-name
{:keys [offset], :or {:offset 0}} - failed: vector? at: [:fn-tail :arity-1 :params :var-params :var-form :seq-destructure] spec: :clojure.core.specs.alpha/seq-binding-form
as-codec - failed: vector? at: [:fn-tail :arity-n :bodies :params] spec: :clojure.core.specs.alpha/param-list

clj-rcon-test.core=&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For context this is what I'm trying to troubleshoot: &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/gpittarelli/clj-rcon/blob/master/src/clj_rcon/codecs.clj#L6&quot;&gt;https://github.com/gpittarelli/clj-rcon/blob/master/src/clj_rcon/codecs.clj#L6&lt;/a&gt;&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9643/has-the-destructuring-spec-changed-since-1-8-0</guid>
<pubDate>Fri, 25 Sep 2020 15:24:01 +0000</pubDate>
</item>
<item>
<title>Missing error on silly destructuring mistake</title>
<link>https://ask.clojure.org/index.php/9590/missing-error-on-silly-destructuring-mistake</link>
<description>&lt;p&gt;I happened to define a function like this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn foo [{:keys [bar :as lol]}] lol)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Obviously the destructuring is wrong, it should have been&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn foo [{:keys [bar] :as lol}] lol)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My question is then, should this destructuring error be caught by the specs for destructuring?&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9590/missing-error-on-silly-destructuring-mistake</guid>
<pubDate>Wed, 09 Sep 2020 07:32:08 +0000</pubDate>
</item>
<item>
<title>What is a good place to start for implementing a new Clojure backend?</title>
<link>https://ask.clojure.org/index.php/9586/what-good-place-start-for-implementing-new-clojure-backend</link>
<description>&lt;p&gt;I've done some digging around for a spec, test suite, or some scraps of documentation to inform the development of a Clojure backend. Most of my searching has only turned up other implementations that are mostly doing their own thing - even ClojureScript doesn't seem to have any way to verify conformance with what Clojure &lt;em&gt;should&lt;/em&gt; be. And from what I understand, ClojureCLR works largely because it was a (mostly) one-to-one clone of the Java source code in C#.&lt;/p&gt;
&lt;p&gt;Is there a preferred, or maintainer recommended, way to go about developing a new backend for Clojure? Or is that considered 'wild' territory?&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9586/what-good-place-start-for-implementing-new-clojure-backend</guid>
<pubDate>Fri, 04 Sep 2020 19:29:34 +0000</pubDate>
</item>
<item>
<title>Is there a way to find there was a name collision for keywords?</title>
<link>https://ask.clojure.org/index.php/9488/is-there-a-way-to-find-there-was-a-name-collision-for-keywords</link>
<description>&lt;p&gt;Recently, I found a bug in a project from the company I work at.&lt;/p&gt;
&lt;p&gt;Basically, what was happening was that inside a test's namespace we required two different namespaces, let's call them first-ns and second-ns here.&lt;/p&gt;
&lt;p&gt;Both first-ns and second-ns  had the definition of a spec with the same name, like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns first-ns)
(s/def :same/name
    int?)

(ns second-ns)
(s/def :same/name
    string?)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So my question is, is there a way to make the Clojure compiler complain about a name clash for cases like this? In large codebases with hundreds of specs, this can occasionally happen and cause a heisenbug. Is the only solution to make the specs namespaced to the same ns of the file in which they were defined?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9488/is-there-a-way-to-find-there-was-a-name-collision-for-keywords</guid>
<pubDate>Tue, 21 Jul 2020 16:01:35 +0000</pubDate>
</item>
<item>
<title>How could I add metadata to specs?</title>
<link>https://ask.clojure.org/index.php/9414/how-could-i-add-metadata-to-specs</link>
<description>&lt;p&gt;I'm trying to implement traceability into my specs.&lt;br&gt;
(With &quot;specs&quot; I specifically mean &lt;code&gt;s/def&lt;/code&gt; and &lt;code&gt;s/fdef&lt;/code&gt; blocks.)&lt;/p&gt;
&lt;p&gt;Originally, I came up with the idea to use metadata for this. However, that's unfortunately not possible: the code below gives me a syntax error... (&lt;code&gt;Metadata can only be applied to IMetas.&lt;/code&gt;)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require
    '[clojure.spec.alpha :as s])
(s/def
      ^{:rule &quot;205.3i&quot;
        :version &quot;2020.06.01&quot;}
      ::basic-land-type #{::forest ::island ::mountain ::plains ::swamp})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only other way I can think of is to somehow attach the metadata as a &quot;guardian&quot; element within the file (let's say, a &lt;code&gt;def&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;However, it seems quite ugly...&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def
  ^{:rule &quot;205.3i&quot;
    :version &quot;2020.06.01&quot;}
  tracing-info &quot;(see metadata)&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Would anyone know some way I could use to achieve this?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9414/how-could-i-add-metadata-to-specs</guid>
<pubDate>Sun, 05 Jul 2020 02:10:12 +0000</pubDate>
</item>
<item>
<title>How to get the req-un/opt-un of a keys spec?</title>
<link>https://ask.clojure.org/index.php/9393/how-to-get-the-req-un-opt-un-of-a-keys-spec</link>
<description>&lt;p&gt;I want to create a spec that is like spec/keys, but that enforces that only keys in req-un and opt-un are allowed.&lt;/p&gt;
&lt;p&gt;I was told that there is no easy way to do that with clojure.spec.alpha, and that I need to add an s/and with a function that will check that the given keys exist in req-un and opt-un.&lt;/p&gt;
&lt;p&gt;My plan is to simply create another spec using the spec already defined with spec/keys, since it seems to me that it's impossible to define that the spec is exhaustive in one single spec. So what I want is something like this:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(spec/def ::person (spec/keys :req-un [::name ::age]))&lt;/code&gt;&lt;br&gt;
&lt;code&gt;(spec/def ::person-exhaustive (s/and ::person #(only-allowed-keys %))&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;However, it is not clear to me how to do that properly. I noticed there is a spec/form function that returns the spec as data, but since it is a Cons it seems clunky to use it. Is there a better way to get that data from a spec?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9393/how-to-get-the-req-un-opt-un-of-a-keys-spec</guid>
<pubDate>Mon, 15 Jun 2020 22:33:18 +0000</pubDate>
</item>
<item>
<title>Exhaustive spec checking like spec/keys, how?</title>
<link>https://ask.clojure.org/index.php/9329/exhaustive-spec-checking-like-spec-keys-how</link>
<description>&lt;p&gt;My understanding is that spec/keys works for validating specific keys in a hash-map. However, I need it to also reject a hash-map that contains keys that are not in :req and :opt.&lt;/p&gt;
&lt;p&gt;For a contrived example, imagine I have a definition of a person as an edn:&lt;/p&gt;
&lt;p&gt;{:name &quot;John&quot;&lt;br&gt;
 :age 25}&lt;/p&gt;
&lt;p&gt;The spec could be:&lt;/p&gt;
&lt;p&gt;(spec/def ::name string?)&lt;br&gt;
(spec/def ::age int?)&lt;br&gt;
(spec/def ::person (spec/keys :req-un [::name ::age]))&lt;/p&gt;
&lt;p&gt;The above definition would conform to this ::person spec.&lt;/p&gt;
&lt;p&gt;However, let's say that, due to a confusion by a developer, another key unrelated to a Person's definition was added, and the hash-map is now:&lt;/p&gt;
&lt;p&gt;{:name &quot;John&quot;&lt;br&gt;
 :age 25&lt;br&gt;
 :voltage 220}&lt;/p&gt;
&lt;p&gt;That would still conform to the spec. But I want it to fail!&lt;/p&gt;
&lt;p&gt;Is there a simple way to make spec/keys reject a hash-map that has keys that are neither req nor opt?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9329/exhaustive-spec-checking-like-spec-keys-how</guid>
<pubDate>Tue, 26 May 2020 17:59:15 +0000</pubDate>
</item>
<item>
<title>Clojure tradeoffs and how do we mitigate them?</title>
<link>https://ask.clojure.org/index.php/9264/clojure-tradeoffs-and-how-do-we-mitigate-them</link>
<description>&lt;p&gt;Earlier this week I conversed with a person who had decided to stop using Clojure, I have summarized their points. Before we grab our pitchforks to go after any party here, I am more curious about what we can do to mitigate these tradeoffs if you agree with them. What have you tried? What's your experience? What's your advice?&lt;/p&gt;
&lt;p&gt;I feel it's important to state my goal is not to be critical of anyone's work here. I LOVE Clojure and I find this person's insight motivates me to learn about Clojure's trade-offs and explore how to address them using the plethora of tools already at our disposal.&lt;/p&gt;
&lt;p&gt;Below is a summary of that person's issues followed by my own thoughts.&lt;/p&gt;
&lt;h2&gt; REPL driven development&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Felt like a work-around to the slow startup problem&lt;/li&gt;
&lt;li&gt;REPL dev was not productive. Often encountered broken REPL states requiring a restart.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Clojure&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;No static types. &lt;/li&gt;
&lt;li&gt;How do you know what keys and values are within a map?&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Spec&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Too verbose. It seems to contradict the conciseness that Clojure typically embraces.&lt;/li&gt;
&lt;li&gt;A lot of legwork required to get value from spec requiring you to write many predicates, s/ forms, checkers, etc...&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;My thoughts:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Startup time of CLJS or CLJ hasn't felt like much of a barrier to me in development but GRAAL could be a good solution.&lt;/li&gt;
&lt;li&gt;I haven't encountered as much broken REPL state with Clojure but it definitely happens frequently with CLJS and Shadow. Is this something we can mitigate using libraries like Integrant or Component? What often leads to an inconsistent state?&lt;/li&gt;
&lt;li&gt;As for static typing, I can see why it's a goto solution for knowing what a map contains but aside from spec how can we address it in our code? &lt;/li&gt;
&lt;li&gt;Since speaking to this person I have observed there are many times where I'm executing in the REPL just to find out what data I have. &lt;/li&gt;
&lt;li&gt;Should we be adding more docs to our functions to specify which keys we need? Could we make better use of destructuring to help emphasize expected keys? Are there better tooling mechanisms to help with this?&lt;/li&gt;
&lt;li&gt;I don't have any issues with spec myself but I'm not the most experienced with it. &lt;/li&gt;
&lt;li&gt;What I like is that you are constructing a DSL to specify the relationships in your domain logic which seems advantageous to me over static typing which brings all problems down to a comp-sci academic level.&lt;/li&gt;
&lt;li&gt;Do I really need to know if this lead I'm trying to send to a vendor in my marketplace is a Record Type?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What are your thoughts?&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9264/clojure-tradeoffs-and-how-do-we-mitigate-them</guid>
<pubDate>Sat, 25 Apr 2020 23:49:49 +0000</pubDate>
</item>
<item>
<title>Exercising selection of union of specs throws error</title>
<link>https://ask.clojure.org/index.php/9091/exercising-selection-of-union-of-specs-throws-error</link>
<description>&lt;p&gt;I was trying to exercise a spec which is a union of two others with a selection&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns example
(:require [clojure.alpha.spec :as s]))
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::val1 int?)
(s/def ::foo (s/schema [::val1]))

(s/def ::val2 int?)
(s/def ::bar (s/schema [::val2]))
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::foobar (s/union ::foo ::bar))

(s/exercise (s/select ::foobar [::val1]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But this throws an error. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;No implementation of method: :keyspecs* of protocol:&lt;br&gt;
   #'clojure.alpha.spec.protocols/Schema found for class:&lt;br&gt;
   clojure.alpha.spec.impl$union_impl$reify__7091&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Should select work for this? Or am I doing something wrong?&lt;/p&gt;
&lt;p&gt;I am using &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;org.clojure/alpha.spec {:git/url &quot;https://github.com/clojure/spec-alpha2.git&quot;
                             :sha &quot;495e5ac3238be002b4de72d1c48479f6bec06bb3&quot;}
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9091/exercising-selection-of-union-of-specs-throws-error</guid>
<pubDate>Tue, 11 Feb 2020 20:40:50 +0000</pubDate>
</item>
<item>
<title>Sharing specs between modules</title>
<link>https://ask.clojure.org/index.php/9036/sharing-specs-between-modules</link>
<description>&lt;p&gt;I'm relatively new to Clojure/Spec and having read the documentation it's not immediatly obvious to me whether it is best practice is to (re)declare specs within the module they validate to or whether it is better to declare common specs seperatly for reuse across multiple modules.&lt;/p&gt;
&lt;p&gt;For example, a game might have many different objects that all need a position consisting of x, y coordinates. In &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/unclebob/spacewar&quot;&gt;this repo&lt;/a&gt; for example the ::x and ::y spec defs are redeclared in each &quot;entity&quot; module (clouds, explosions etc). Is this preferred to having a single spec for position and importing accross modules?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9036/sharing-specs-between-modules</guid>
<pubDate>Wed, 15 Jan 2020 11:23:04 +0000</pubDate>
</item>
<item>
<title>Select one spec from multi-spec</title>
<link>https://ask.clojure.org/index.php/8837/select-one-spec-from-multi-spec</link>
<description>&lt;p&gt;What would be the preferred way to select one spec from multi-spec? E.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmulti entity :entity)

(s/def ::e1
  (s/keys :req [:e1/attr1
                :e1/attr2
                :e1/attrN]))

(defmethod entity :e1 [_] ::e1)

(s/def ::e2
  (s/merge
   ::e1
   (s/keys :req [:e2/attr1
                 :e2/attr2
                 :e2/attrN])))

(defmethod entity :e2 [_] ::e2)

(s/def ::entity (s/multi-spec entity :entity))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How to spec a function that accepts only &lt;code&gt;::e2&lt;/code&gt; entity as an argument? ... or how to generate only &lt;code&gt;::e2&lt;/code&gt; entity values?&lt;/p&gt;
&lt;p&gt;Can't do &lt;code&gt;(s/cat :e2 ::e2)&lt;/code&gt; or &lt;code&gt;(s/gen ::e2)&lt;/code&gt; because &lt;code&gt;::e2&lt;/code&gt; doesn't have the required &lt;code&gt;:entity&lt;/code&gt; tag. Also, can't add &lt;code&gt;:req-un [:e1/entity]&lt;/code&gt; and  &lt;code&gt;:req-un [:e2/entity]&lt;/code&gt; to &lt;code&gt;::e1&lt;/code&gt; and &lt;code&gt;::e2&lt;/code&gt; because it would clash in the &lt;code&gt;s/merge&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The simplest way I've found is to use &lt;code&gt;s/and&lt;/code&gt;, e.g:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(gen/generate (s/gen (s/and ::entity ::e2)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But it is still hard to generate data for functions that accept homogeneous collection of any entity:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/and (s/coll-of ::entity :min-count 1)
       #(-&amp;gt;&amp;gt; % (map :entity) (apply =)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It would be easier if you could pass the entity tag to select the specific entity spec or it's generator, e.g. generate random entity and based on it's entity tag generate other elements.&lt;/p&gt;
&lt;p&gt;In other words, I am looking for a way to get &lt;code&gt;::e2&lt;/code&gt; entity spec via &lt;code&gt;:e2&lt;/code&gt; tag somehow, without maintaining a global helper map...&lt;/p&gt;
&lt;p&gt;Or is there any other solution?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8837/select-one-spec-from-multi-spec</guid>
<pubDate>Wed, 06 Nov 2019 18:00:39 +0000</pubDate>
</item>
<item>
<title>Spec with nested &quot;or&quot; fails unexceptedly</title>
<link>https://ask.clojure.org/index.php/8759/spec-with-nested-or-fails-unexceptedly</link>
<description>&lt;p&gt;I am trying to write a spec for existing data.  The moment I try to put one spec that contains an or into other it stops working.  We are using &lt;code&gt;confrom&lt;/code&gt; quite extensively so being able to get the &lt;code&gt;or&lt;/code&gt; labels is important.&lt;br&gt;
&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :a/b int?)
(s/def ::inner (s/or
                 :ident (s/tuple #{:a/b} :a/b)
                 :temp string?))
(s/def ::one int?)
(s/def ::two int?)
(s/def ::outer (s/and
            (s/keys :req [::inner])
                 (s/or
                  :one (s/keys :req [::one])
                  :two (s/keys :req [::two]))))
(s/valid? ::outer {::inner [:a/b 1]
               ::one 1})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;`&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;In the examle above if &lt;code&gt;::inner&lt;/code&gt; is just the spec under the &lt;code&gt;:ident&lt;/code&gt; branch of the &lt;code&gt;or&lt;/code&gt; then spec sees it as valid, but the moment there are two &lt;code&gt;or&lt;/code&gt; specs involved it breaks.&lt;/p&gt;
&lt;p&gt;Is there another way I'm supposed to construct specs such as these?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8759/spec-with-nested-or-fails-unexceptedly</guid>
<pubDate>Wed, 23 Oct 2019 12:52:41 +0000</pubDate>
</item>
</channel>
</rss>