<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions in Spec</title>
<link>https://ask.clojure.org/index.php/questions/clojure/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>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>`(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>Surprising behaviour</title>
<link>https://ask.clojure.org/index.php/13390/surprising-behaviour</link>
<description>&lt;p&gt;I am trying to learn spec, and have come across the following behaviour.&lt;/p&gt;
&lt;p&gt;My code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::foo (s/cat :first symbol?
                :rest (s/alt :simple vector?
                             :multi (s/cat :parts (s/* symbol?) :final vector?))))
(s/explain ::foo '(foo [blah] x))
(s/conform ::foo '(foo [blah] x))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What I find is that s/explain prints &quot;Success!&quot; while s/conform returns :s/invalid.&lt;br&gt;
Have I stumbled on a bug, or have I not understood the functions?&lt;br&gt;
Or is my spec invalid in the first place?&lt;br&gt;
(I am using Clojure 1.11.1 &amp;amp; spec 0.3.218)&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13390/surprising-behaviour</guid>
<pubDate>Mon, 23 Oct 2023 14:04:51 +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>Should the actual function be called when an fspec is being validated?</title>
<link>https://ask.clojure.org/index.php/12966/should-the-actual-function-called-when-fspec-being-validated</link>
<description>&lt;p&gt;TL;DR&lt;br&gt;
When instrumentation is turned on, spec validates a fspec that appears in the :args of another fspec by calling the actual function with generated arguments, which if it performs side effects could lead to unexpected and incorrect results.&lt;/p&gt;
&lt;p&gt;The problem&lt;br&gt;
If we have the following functions and specs&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn create-generator []
  (let [counter (atom [])]
    (fn [&amp;amp; _]
      (swap! counter inc))))

(s/def ::my-generator (s/fspec :args (s/cat :identifier-arguments (s/* string?))
                               :ret integer?))

(defn my-fn [generator]
  (generator))

(s/fdef my-fn
        :args (s/cat :my-context ::my-generator)
        :ret string?)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Calling (my-fn (create-generator)) will return different results depending on instrumentation being enabled or not.&lt;br&gt;
With instrumentation enabled, our function is being called 21 times more than the single invocation we'll be expecting (hint: this numbers comes from clojure.alpha.spec/&lt;em&gt;fspec-iterations&lt;/em&gt;).&lt;br&gt;
This happens because my-fn's spec has args, and in such case clojure.alpha.spec.test/instrument does the following:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;If a var has an :args fn-spec, sets the var's root binding to a fn&lt;br&gt;
that checks arg conformance (throwing an exception on failure) before&lt;br&gt;
delegating to the original fn.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This means it must check if the function received as argument conforms the ::my-generator spec, which is itself an fspec. For that it calls the fspec reified conform* and that implementation runs a quick-check style test, generating random arguments based on the :args spec and calling the actual function. This can be observed by creating an exception in the function returned by create-generator, the stacktrace looks like this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;'[[clojure.test_clojure.fspec_bug_test$create_generator$fn__5468 doInvoke &quot;fspec_bug_test.clj&quot; 4]
  [clojure.lang.RestFn applyTo &quot;RestFn.java&quot; 137]
  [clojure.core$apply invokeStatic &quot;core.clj&quot; 665]
  [clojure.core$apply invoke &quot;core.clj&quot; 660]
  [clojure.alpha.spec.impl$call_valid_QMARK_ invokeStatic &quot;impl.clj&quot; 1616]
  [clojure.alpha.spec.impl$call_valid_QMARK_ invoke &quot;impl.clj&quot; 1612]
  [clojure.alpha.spec.impl$validate_fn$fn__4117 invoke &quot;impl.clj&quot; 1627]
  [clojure.lang.AFn applyToHelper &quot;AFn.java&quot; 154]
  [clojure.lang.AFn applyTo &quot;AFn.java&quot; 144]
  [clojure.core$apply invokeStatic &quot;core.clj&quot; 665]
  [clojure.core$apply invoke &quot;core.clj&quot; 660]
  [clojure.test.check.properties$apply_gen$fn__5403$fn__5404 invoke &quot;properties.cljc&quot; 31]
  [clojure.test.check.properties$apply_gen$fn__5403 invoke &quot;properties.cljc&quot; 30]
  [clojure.test.check.rose_tree$fmap invokeStatic &quot;rose_tree.cljc&quot; 77]
  [clojure.test.check.rose_tree$fmap invoke &quot;rose_tree.cljc&quot; 73]
  [clojure.test.check.generators$fmap$fn__4871 invoke &quot;generators.cljc&quot; 104]
  [clojure.test.check.generators$gen_fmap$fn__4845 invoke &quot;generators.cljc&quot; 59]
  [clojure.test.check.generators$call_gen invokeStatic &quot;generators.cljc&quot; 43]
  [clojure.test.check.generators$call_gen invoke &quot;generators.cljc&quot; 39]
  [clojure.test.check$quick_check invokeStatic &quot;check.cljc&quot; 211]
  [clojure.test.check$quick_check doInvoke &quot;check.cljc&quot; 59]
  [clojure.lang.RestFn invoke &quot;RestFn.java&quot; 425]
  [clojure.lang.AFn applyToHelper &quot;AFn.java&quot; 156]
  [clojure.lang.RestFn applyTo &quot;RestFn.java&quot; 132]
  [clojure.core$apply invokeStatic &quot;core.clj&quot; 665]
  [clojure.core$apply invoke &quot;core.clj&quot; 660]
  [clojure.alpha.spec.gen$quick_check invokeStatic &quot;gen.clj&quot; 32]
  [clojure.alpha.spec.gen$quick_check doInvoke &quot;gen.clj&quot; 30]
  [clojure.lang.RestFn invoke &quot;RestFn.java&quot; 421]
  [clojure.alpha.spec.impl$validate_fn invokeStatic &quot;impl.clj&quot; 1628]
  [clojure.alpha.spec.impl$validate_fn invoke &quot;impl.clj&quot; 1623]
  [clojure.alpha.spec.impl$fspec_impl$reify__4124 conform_STAR_ &quot;impl.clj&quot; 1646]
  [clojure.alpha.spec$conform invokeStatic &quot;spec.clj&quot; 245]
  [clojure.alpha.spec$conform invoke &quot;spec.clj&quot; 237]
  [clojure.alpha.spec$conform invokeStatic &quot;spec.clj&quot; 241]
  [clojure.alpha.spec$conform invoke &quot;spec.clj&quot; 237]
  [clojure.alpha.spec.impl$dt invokeStatic &quot;impl.clj&quot; 219]
  [clojure.alpha.spec.impl$dt invoke &quot;impl.clj&quot; 214]
  [clojure.alpha.spec.impl$dt invokeStatic &quot;impl.clj&quot; 215]
  [clojure.alpha.spec.impl$dt invoke &quot;impl.clj&quot; 214]
  [clojure.alpha.spec.impl$deriv invokeStatic &quot;impl.clj&quot; 1426]
  [clojure.alpha.spec.impl$deriv invoke &quot;impl.clj&quot; 1420]
  [clojure.alpha.spec.impl$deriv invokeStatic &quot;impl.clj&quot; 1434]
  [clojure.alpha.spec.impl$deriv invoke &quot;impl.clj&quot; 1420]
  [clojure.alpha.spec.impl$re_conform invokeStatic &quot;impl.clj&quot; 1564]
  [clojure.alpha.spec.impl$re_conform invoke &quot;impl.clj&quot; 1555]
  [clojure.alpha.spec.impl$as_regex_spec$fn__3849 invoke &quot;impl.clj&quot; 1238]
  [clojure.alpha.spec.protocols$eval1958$fn__2056$G__1939__2067 invoke &quot;protocols.clj&quot; 11]
  [clojure.alpha.spec$conform invokeStatic &quot;spec.clj&quot; 245]
  [clojure.alpha.spec$conform invoke &quot;spec.clj&quot; 237]
  [clojure.alpha.spec$conform invokeStatic &quot;spec.clj&quot; 241]
  [clojure.alpha.spec$conform invoke &quot;spec.clj&quot; 237]
  [clojure.alpha.spec.test$spec_checking_fn$conform_BANG___4398 invoke &quot;test.clj&quot; 131]
  [clojure.alpha.spec.test$spec_checking_fn$fn__4400 doInvoke &quot;test.clj&quot; 150]
  [clojure.lang.RestFn invoke &quot;RestFn.java&quot; 408]
  [clojure.test_clojure.fspec_bug_test$eval5473 invokeStatic &quot;fspec_bug_test.clj&quot; 2]
  [clojure.test_clojure.fspec_bug_test$eval5473 invoke &quot;fspec_bug_test.clj&quot; 44]
  ...]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, the problem can be reduced to the fspec implementation of conform* since by just calling s/valid? will show the behavior of the function being called, but this is uncommon, while having functions annotated and turning on instrumentation during tests is a common practice, that's why the example in this PR shows a concrete problematic scenario with the current implementation of fspec.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;; This already shows the actual function being called, but is an unrealistic example
(s/valid? ::my-generator (fn [&amp;amp; args]
                           (println args)
                           1))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12966/should-the-actual-function-called-when-fspec-being-validated</guid>
<pubDate>Mon, 29 May 2023 17:11:04 +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>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>(s/valid? ::my-spec) succeeds, but (s/valid? (s/and ::my-spec ::my-spec)) fails</title>
<link>https://ask.clojure.org/index.php/12743/s-valid-my-spec-succeeds-but-s-valid-s-and-my-spec-my-spec-fails</link>
<description>&lt;p&gt;It seems like if &lt;code&gt;::my-spec&lt;/code&gt; validates, then &lt;code&gt;(s/and ::my-spec ::my-spec)&lt;/code&gt; should also validate.&lt;/p&gt;
&lt;p&gt;This is a simplified version of my actual code, but I'm running into problems validating some data that is spec'd using a combination s/and, s/keys, and s/or. This is the smallest reproduction I could come up with.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :a/coordinate
  (s/or :double double?
        :int int?))
(s/valid?
 (s/keys)
 {:a/coordinate 1.0})
;; true

(s/valid?
 (s/and (s/keys))
 {:a/coordinate 1.0})
;; true

(s/valid?
 (s/and (s/keys)
        (s/keys))
 {:a/coordinate 1.0})
;; false

(s/explain-str
 (s/and (s/keys)
        (s/keys))
 {:a/coordinate 1.0})
;; &quot;[:double 1.0] - failed: double? in: [:a/coordinate] at: [:a/coordinate :double] spec: :a/coordinate\n[:double 1.0] - failed: int? in: [:a/coordinate] at: [:a/coordinate :int] spec: :a/coordinate\n&quot;
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12743/s-valid-my-spec-succeeds-but-s-valid-s-and-my-spec-my-spec-fails</guid>
<pubDate>Wed, 08 Mar 2023 20:09:35 +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>Is it useful to extend fspec to enable the expression of constraints on a function such as commutativity.</title>
<link>https://ask.clojure.org/index.php/12406/extend-enable-expression-constraints-function-commutativity</link>
<description>&lt;p&gt;Does it seem useful to add a new kwarg to fspec to allow a function spec to provide one or more constraints on or invariants of the function across distinct invocations in order to provide for constraints such as commutativity. I am working on this, but thought good to check if anyone else thinks is useful.&lt;/p&gt;
&lt;p&gt;The new kwarg, possibly :alg given that this allows expression of the algebraic properties of the function, would take a map, with key being a naming keyword and value being a predicate (typically needing a let).&lt;/p&gt;
&lt;p&gt;Looking somewhat like this for a few binary operators:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/fdef add
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;  :args (s/cat :x int? :y int?)&lt;br&gt;
  :ret int?&lt;br&gt;
   :alg { :comm &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;        (let [f (:f %)
                x  (-&amp;gt; % args :x)
                y  (-&amp;gt; % args :x)]
               (= ((f x y) (f y x))

        :assoc 
        (let [f (:f %)
                x  (-&amp;gt; % args :x)
                y  (-&amp;gt; % args :x)
                z  (-&amp;gt; % args :x)]
                (= (f (f x y) z) (f x (f y z)))

        :ident   
        (let [f (:f %)
                x (-&amp;gt; % args :x)]
               (= (f x 0) x))})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(s/fdef mul&lt;br&gt;
  :args (s/cat :x int? :y int?)&lt;br&gt;
  :ret int?&lt;br&gt;
  :alg { :comm &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;        (let [f (:f %)
                x  (-&amp;gt; % args :x)
                y  (-&amp;gt; % args :x)]
               (= ((f x y) (f y x))

        :assoc 
        (let [f (:f %)
                x  (-&amp;gt; % args :x)
                y  (-&amp;gt; % args :x)
                z  (-&amp;gt; % args :x)]
                (= (f (f x y) z) (f x (f y z)))

        :ident   
        (let [f (:f %)
                x (-&amp;gt; % args :x)]
               (= (f x 0) x))

        :super
        (let [f (:f %)
                x (-&amp;gt; % args :x)]
               (= (f x 0) 0))})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(s/fdef max&lt;br&gt;
  :args (s/cat :x int? :y int?)&lt;br&gt;
  :ret int?&lt;br&gt;
   :alg { :comm &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;        (let [f (:f %)
                x  (-&amp;gt; % args :x)
                y  (-&amp;gt; % args :x)]
               (= ((f x y) (f y x))

        :assoc 
        (let [f (:f %)
                x  (-&amp;gt; % args :x)
                y  (-&amp;gt; % args :x)
                z  (-&amp;gt; % args :x)]
                (= (f (f x y) z) (f x (f y z)))

        :idem
        (let [f (:f %)
                x (-&amp;gt; % args :x)]
                (= (f x x) x)})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;:ident, :super and :idem above could obvs be done with :fn, but seems handy to put together once one gets :comm and :assoc in place.&lt;/p&gt;
&lt;p&gt;This may give rise to question of how to similarly handle constraints on or invariants of sets of related functions, although probably less commonly useful. However that may suggest that actually this would be better done as separate from the fspec in a separate spec - ?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12406/extend-enable-expression-constraints-function-commutativity</guid>
<pubDate>Sun, 27 Nov 2022 19:36:21 +0000</pubDate>
</item>
<item>
<title>How hard is it to learn Clojure?</title>
<link>https://ask.clojure.org/index.php/12340/how-hard-is-it-to-learn-clojure</link>
<description>&lt;p&gt;It had been a couple of weeks since I've started learning Clojure.&lt;/p&gt;
&lt;p&gt;I am a newbie at programming and I passed some basic courses &lt;a rel=&quot;nofollow&quot; href=&quot;https://skillcombo.com/topic/java/&quot;&gt;here&lt;/a&gt; about Java. I just wanted to know how long it will take for me to learn Clojure and if is it difficult for beginners. Also, you can recommend your favourite resources and roadmaps.&lt;/p&gt;
&lt;p&gt;Thanks in advance!&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12340/how-hard-is-it-to-learn-clojure</guid>
<pubDate>Fri, 28 Oct 2022 23:28:38 +0000</pubDate>
</item>
<item>
<title>Typo in clojure.spec.alpha’s keys docstring</title>
<link>https://ask.clojure.org/index.php/12275/typo-in-clojure-spec-alphas-keys-docstring</link>
<description>&lt;p&gt;Line 428 reads “fully &lt;em&gt;qualfied&lt;/em&gt; keywords”—it should be “qualified”.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12275/typo-in-clojure-spec-alphas-keys-docstring</guid>
<pubDate>Mon, 03 Oct 2022 12:04:28 +0000</pubDate>
</item>
<item>
<title>Spec2 - Bug on nested schema specs</title>
<link>https://ask.clojure.org/index.php/12228/spec2-bug-on-nested-schema-specs</link>
<description>&lt;p&gt;I would like to extend issue &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJ-2648&quot;&gt;https://clojure.atlassian.net/browse/CLJ-2648&lt;/a&gt; by stating that generate/valid?/exercise all dont work for any schema spec with a nested schema spec on it.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :another-entity/id uuid?)
(s/def :another-entity/some-attr int?)
(s/def ::another-entity (s/schema [:another-entity/id :another-entity/some-attr]))

(s/def :entity/id uuid?)
(s/def :entity/another-attr string?)
(s/def :entity/ref ::another-entity)
(s/def ::complex-entity (s/schema [:entity/id :entity/another-attr :entity/ref]))

(s/valid? ::complex-entity {:entity/id (random-uuid)
                            :entity/another-attr &quot;oi&quot;})
;;=&amp;gt; true
(s/valid? ::complex-entity {:entity/id (random-uuid)
                            :entity/another-attr &quot;oi&quot;
                            :entity/ref #:another-entity{:id (random-uuid)}})
;;=&amp;gt; Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1960$fn$G (protocols.clj:11).
;    No implementation of method: :conform* of protocol: #'clojure.alpha.spec.protocols/Spec found for class: clojure.lang.Keyword

(generators/generate (s/gen ::complex-entity))
;;=&amp;gt; Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1960$fn$G (protocols.clj:11).
;    No implementation of method: :conform* of protocol: #'clojure.alpha.spec.protocols/Spec found for class: clojure.lang.Keyword
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thanks :)&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12228/spec2-bug-on-nested-schema-specs</guid>
<pubDate>Wed, 21 Sep 2022 18:07:48 +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 2: Closed Generators</title>
<link>https://ask.clojure.org/index.php/12198/spec-2-closed-generators</link>
<description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;is it possible to get a &quot;closed&quot; generator from a `s/select` form&lt;br&gt;
and/or make it the default in Spec 2?&lt;/p&gt;
&lt;p&gt;From the example in the Wiki:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns user
  (:require [clojure.alpha.spec :as s]))

(s/def ::id int?)
(s/def ::first string?)
(s/def ::last string?)
(s/def ::user (s/schema [::id ::first ::last ::addr]))

(s/def ::my-user (s/select ::user [::id]))

(s/exercise ::my-user)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This generates users that have more keys than what I selected:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;([#:user{:id 0} #:user{:id 0}]
 [#:user{:addr #:user{:city &quot;1&quot;}, :first &quot;O&quot;, :id 0}
  #:user{:addr #:user{:city &quot;1&quot;}, :first &quot;O&quot;, :id 0}])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I believe it is the same data which the `s/schema` for the `::user`&lt;br&gt;
spec generates. My guess is this just hasn't been implemented yet.&lt;/p&gt;
&lt;p&gt;Would it be possible to support this?  &lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12198/spec-2-closed-generators</guid>
<pubDate>Mon, 12 Sep 2022 08:36:26 +0000</pubDate>
</item>
<item>
<title>Clojure Spec 2. Unexpected s/with-gen behaviour</title>
<link>https://ask.clojure.org/index.php/12185/clojure-spec-2-unexpected-s-with-gen-behaviour</link>
<description>&lt;p&gt;When using regular expression operators (&lt;code&gt;s/alt&lt;/code&gt; &lt;code&gt;s/cat&lt;/code&gt; etc ) to describe sequences, &lt;code&gt;s/with-gen&lt;/code&gt; doesn't work as I would have expected. It completely ignores the generator passed to the definition. Eg:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require '[clojure.alpha.spec :as s])
(require '[clojure.alpha.spec.gen :as gen])
(s/def ::some-spec 
 (s/with-gen string? (constantly (gen/return &quot;olar&quot;))))

(gen/sample (s/gen ::some-spec))
 ;;=&amp;gt; (&quot;olar&quot; &quot;olar&quot; &quot;olar&quot; &quot;olar&quot; &quot;olar&quot; &quot;olar&quot; &quot;olar&quot; &quot;olar&quot; &quot;olar&quot; &quot;olar&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; The above generator works as I would expected, now if we try to create a similar one for s/alt we get unexpected results: &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::some-other-spec 
 (s/with-gen
  (s/alt :greetings #{:olar :oizito :oi :hello :hey :yo :hallo}
             :farewell #{:bye :bye-bye :hasta-la-vista :tchussy :tchau-kakao})
  (constantly (gen/return [:tchau-kakao]))))

(gen/sample (s/gen ::some-other-spec))
;;=&amp;gt; =&amp;gt; ([:olar] [:hasta-la-vista] [:hallo] [:hallo] [:tchussy] [:oi] [:bye-bye] [:tchussy] [:tchau-kakao] [:yo])
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is this a bug or is it expected somehow?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12185/clojure-spec-2-unexpected-s-with-gen-behaviour</guid>
<pubDate>Fri, 09 Sep 2022 10:49:50 +0000</pubDate>
</item>
<item>
<title>Is Clojure is the only Lisp designed with Concurrency?</title>
<link>https://ask.clojure.org/index.php/12130/is-clojure-is-the-only-lisp-designed-with-concurrency</link>
<description>&lt;p&gt;I believe that Clojure is not the only Lisp build in with concurrency. From what I understand, LispWorks and perhaps Allegro Lisp is also concurrent in design. As for whether the Eclipse Lisp plug-in is concurrent, I am not sure as it relies on SBCL or CLISP for the environment. And Racket is also another Lisp dialect which Prof Matthias Felleisen has assured me that it is also a AI language, but as for concurrency I am not too sure. I believe the way forward other than create a new fork with Clojure would be better to engage the Lisp steering committee to change/add concurrency into the Common Lisp language. &lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12130/is-clojure-is-the-only-lisp-designed-with-concurrency</guid>
<pubDate>Fri, 26 Aug 2022 05:21:19 +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>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>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>Why does s/conform transform data?</title>
<link>https://ask.clojure.org/index.php/11230/why-does-s-conform-transform-data</link>
<description>&lt;p&gt;I'm tinkering away with spec and I don't understand part of the design. There doesn't seem to be a good mechanism to express &quot;this value satisfies a series of specs simultaniously&quot;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;and&lt;/code&gt; from clojure.core isn't useful, because then &lt;code&gt;s/explain&lt;/code&gt; will not dig in to figure out which specific spec a data structure failed to meet.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;s/and&lt;/code&gt; is actually a series of transforms and the logical value depends on the order of the arguments. I don't want to deal with that risk. I include a silly example a bit like what I was bitten by&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Start with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :example/s1 vector?)
(s/def :example/s2 vector?)
(s/def :example/spec (s/and :example/s1 :example/s2))
(s/valid? :example/spec [1 2 3]) ; =&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;then change &lt;code&gt;:example/s1&lt;/code&gt; to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def :example/s1 (s/cat :a integer? :b integer? :c integer?))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;now:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/valid? :example/spec [1 2 3]) ; =&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This put me in a position of trying to debug a spec which was a horrible experience; I don't want to be in that situation again. It isn't obvious to me how I would even start if the spec was more baroque and so I'd rather avoid functions that can transform/conform the data mid way through completely unless that is clearly required.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;s/every&lt;/code&gt; seems fundamentally unsafe for general use - despite the name it doesn't check that a spec is true for every entry. I have functions that extend long vectors with new entries and a spec based on &lt;code&gt;s/every&lt;/code&gt; will specifically not check the values that are most likely to contain bugs.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I'm in a position where I feel pressure to re-implement &lt;code&gt;s/every&lt;/code&gt; (to be comprehensive) or &lt;code&gt;s/and&lt;/code&gt; (to be validating but non-conforming) - but this is a bit confusing to me because I had just assumed what I wanted would be the common use case. Have I missed something? Why didn't a non-conforming variant of &lt;code&gt;s/and&lt;/code&gt; get included in spec?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11230/why-does-s-conform-transform-data</guid>
<pubDate>Sat, 30 Oct 2021 09:05:37 +0000</pubDate>
</item>
<item>
<title>s/conform of s/map-of does not conform keys, is that expected?</title>
<link>https://ask.clojure.org/index.php/11217/s-conform-of-s-map-of-does-not-conform-keys-is-that-expected</link>
<description>&lt;p&gt;If I run&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/conform
 (s/map-of (s/or :keyword keyword? :string string?) any?)
 {:a 1})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I get&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{:a 1}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;instead of&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{[:keyword :a] 1}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is that expected?&lt;/p&gt;
&lt;p&gt;One consequence of that is that if I try to &lt;code&gt;s/unform&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/unform
 (s/map-of (s/or :keyword keyword? :string string?) any?)
 {:a 1})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I get an exception, because &lt;code&gt;:a&lt;/code&gt; is not a valid conformed value for &lt;code&gt;s/or&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I bumped into this issue using spec-tools. Its implementation of explain-data calls &lt;code&gt;s/unform&lt;/code&gt; (&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/metosin/spec-tools/blob/master/src/spec_tools/core.cljc#L446&quot;&gt;https://github.com/metosin/spec-tools/blob/master/src/spec_tools/core.cljc#L446&lt;/a&gt;).  I'm trying to figure out whether this is a spec or spec-tools issue.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11217/s-conform-of-s-map-of-does-not-conform-keys-is-that-expected</guid>
<pubDate>Thu, 28 Oct 2021 14:51:23 +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>(s/form (s/multi-spec ...)) returns a list with non-symbolic objects</title>
<link>https://ask.clojure.org/index.php/10969/s-form-s-multi-spec-returns-a-list-with-non-symbolic-objects</link>
<description>&lt;p&gt;My understanding is that &lt;code&gt;s/form&lt;/code&gt; is expected to return symbolic lists that can be passed to &lt;code&gt;eval&lt;/code&gt;. Forms of multi-spec specs don't behave that way, returning retag as evaluated value (e.g. a function instance).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn retag [x v]
  (assoc (vec x) 0 v))

(defmulti command first)

(s/form (s/multi-spec command retag))
=&amp;gt; (clojure.spec.alpha/multi-spec 
    current.ns/command
    #object[current.ns$retag 0x708d0436 current.ns$retag@708d0436])

;; should be something like
=&amp;gt; (clojure.spec.alpha/multi-spec 
    current.ns/command 
    current.ns/retag)
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10969/s-form-s-multi-spec-returns-a-list-with-non-symbolic-objects</guid>
<pubDate>Tue, 24 Aug 2021 11:08:11 +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>fdef's on internal/private functions</title>
<link>https://ask.clojure.org/index.php/10525/fdefs-on-internal-private-functions</link>
<description>&lt;p&gt;Is it expected that &lt;code&gt;st/instrument&lt;/code&gt; can be enabled for all fdef'ed functions (e.g., &lt;code&gt;(st/instrument)&lt;/code&gt;)? For example, say a library has a public API calling some internal functions that are spec'ed with fdef's. If the fdefs are included in the published library, is it expected that calls to those internal functions calls should pass their fdef'ed specs even though they are internal? Take the below example code. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns my-lib.impl
  (:require
    [clojure.spec.alpha :as s]))

(defn- do-internal-thing
  [x]
  x)

(s/fdef do-internal-thing
  :args (s/cat :x number?))

(ns my-lib.api
  (:require
    [clojure.spec.alpha :as s]
    [my-lib.impl :as impl]))

(defn do-thing
  [y]
  (impl/do-internal-thing &quot;oops&quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If I call the api function do-thing, I will get an instrumentation exception on the call to &lt;code&gt;impl/do-internal-thing&lt;/code&gt;. Should users of Spec's instrumentation be expected to filter out some set of fdef'ed functions and pass that filtered list to &lt;code&gt;st/instrument&lt;/code&gt; or is this something the library should be expected to maintain? Maybe libraries should not include fdefs on internal functions? &lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10525/fdefs-on-internal-private-functions</guid>
<pubDate>Mon, 26 Apr 2021 17:03:25 +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>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>Problems with s/union</title>
<link>https://ask.clojure.org/index.php/10345/problems-with-s-union</link>
<description>&lt;p&gt;While playing around with spec2, I'm seeing the following behavior which seems like a bug:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::s1 (s/schema {:a string?}))
=&amp;gt; :dev.fp.livingdocs.specs/s1
(s/def ::s2 (s/schema {:b string?}))
=&amp;gt; :dev.fp.livingdocs.specs/s2
(s/def ::s3 (s/schema {:c string?}))
=&amp;gt; :dev.fp.livingdocs.specs/s3

;; The following is unexpected (neseted unions)
(s/def ::u (s/union ::s1 (s/union ::s2 ::s3)))
Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1665$fn$G     (protocols.clj:20).
No implementation of method: :keyspecs* of protocol: #'clojure.alpha.spec.protocols/Schema found for class: clojure.lang.PersistentList
(s/def ::u (s/union (s/union ::s1 ::s2) ::s3))
Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1665$fn$G (protocols.clj:20).
No implementation of method: :keyspecs* of protocol: #'clojure.alpha.spec.protocols/Schema found for class: clojure.lang.PersistentList

;; if I try to pull out the nested union into a separate keyword, it starts working 
(s/def ::u' (s/union ::s1 ::s2))
=&amp;gt; :dev.fp.livingdocs.specs/u'
(s/def ::u (s/union ::u' ::s3))
=&amp;gt; :dev.fp.livingdocs.specs/u
(s/explain (s/select ::u [:a :c]) {:a &quot;bdfd&quot;})
{:a &quot;bdfd&quot;} - failed: (fn [m] (contains? m :c))

;; but also this
(s/form ::ld/typed)
=&amp;gt;
(clojure.alpha.spec/schema
 [{:name clojure.core/string?, :type clojure.core/string?}])
(s/form ::ldd/editable)
=&amp;gt;
(clojure.alpha.spec/schema
 [{:optional clojure.core/boolean?,
   :maxLength clojure.core/integer?,
   :plainText clojure.core/boolean?,
   :excluexcludeFromTextCount clojure.core/boolean?}])
 
(s/def ::service
  (s/or :single (s/select (s/union ::ld/typed ::ldd/editable) [:type :name :optional])
         :multiple (s/select (s/union :ld/typed ::ldd/editable) [:type :name :maxLength])))
Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1665$fn$G (protocols.clj:20).
No implementation of method: :keyspecs* of protocol: #'clojure.alpha.spec.protocols/Schema found for class: clojure.lang.PersistentList
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In both cases &lt;code&gt;s/union&lt;/code&gt; shows up in a nested position inside a form passed to a spec macro. &lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10345/problems-with-s-union</guid>
<pubDate>Mon, 15 Mar 2021 09:49:01 +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>s/unform using spec containing (s/? (s/cat ,,,)) returns an invalid value (spec 1)</title>
<link>https://ask.clojure.org/index.php/9866/unform-using-spec-containing-cat-returns-invalid-value-spec</link>
<description>&lt;p&gt;While speccing a library I'm working on I tripped on a bheavior of &lt;code&gt;s/unform&lt;/code&gt; in combination with &lt;code&gt;s/?&lt;/code&gt; which I find odd:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require '[clojure.core.specs.alpha :as cs])

(def binding-form '[a b :as c])

(def x (s/conform ::cs/seq-binding-form binding-form))
;; x =&amp;gt; {:forms [[:local-symbol a] [:local-symbol b]], :as-form {:as :as, :as-sym c}}

(s/unform ::cs/seq-binding-form x) 
;; =&amp;gt; (a b (:as c))

(s/valid? ::cs/seq-binding-form (s/unform ::cs/seq-binding-form x))
;; =&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is this intended behavior? On intuition, I was expecting first unforming would produce the original form and also that the result of unform would validate (using the same spec).&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9866/unform-using-spec-containing-cat-returns-invalid-value-spec</guid>
<pubDate>Tue, 24 Nov 2020 22:06:42 +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>spec2 does not allow fn as retag in multi-spec</title>
<link>https://ask.clojure.org/index.php/9776/spec2-does-not-allow-fn-as-retag-in-multi-spec</link>
<description>&lt;p&gt;NB this is specifically a problem with &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/spec-alpha2&quot;&gt;spec-alpha2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We'll use the example from the multi-spec example from the tutorial:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(spec2/def :event/type keyword?)
(spec2/def :event/timestamp int?)
(spec2/def :search/url string?)
(spec2/def :error/message string?)
(spec2/def :error/code int?)

(defmulti event-type :event/type)
(defmethod event-type :event/search [_]
  (spec2/keys :req [:event/type :event/timestamp :search/url]))
(defmethod event-type :event/error [_]
  (spec2/keys :req [:event/type :event/timestamp :error/message :error/code]))

(spec2/def :event/event (spec2/multi-spec event-type :event/type))
;;  ⇒ works as expected

(spec2/def :event/event (spec2/multi-spec event-type (fn [val tag] (assoc val :event/type tag))))
;; throws no impl of conform* for PersistentList

;; test case
(gen/sample (spec2/gen :event/event))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Essentially looks like spec2 can't handle retag functions yet, which makes it impossible to spec anything beyond simple maps, sadly.&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9776/spec2-does-not-allow-fn-as-retag-in-multi-spec</guid>
<pubDate>Tue, 10 Nov 2020 09:46:25 +0000</pubDate>
</item>
<item>
<title>Problem with (s/nest) and (s/describe) in spec-alpha2</title>
<link>https://ask.clojure.org/index.php/9730/problem-with-s-nest-and-s-describe-in-spec-alpha2</link>
<description>&lt;p&gt;It looks like calling &lt;code&gt;(s/describe)&lt;/code&gt; for &lt;code&gt;(s/nest)&lt;/code&gt; specs fails with an exception.&lt;/p&gt;
&lt;p&gt;The following call:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/describe (s/nest int?))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;fails with this exception:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;IllegalStateException Attempting to call unbound fn: #'clojure.core/unquote
    clojure.lang.Var$Unbound.throwArity (Var.java:45)
    clojure.alpha.spec.impl/nest-impl/reify--1676 (impl.clj:647)
    clojure.alpha.spec/form (spec.clj:258)
    clojure.alpha.spec/form (spec.clj:254)
    clojure.alpha.spec/describe (spec.clj:282)
    clojure.alpha.spec/describe (spec.clj:279)
    stem.i.log/eval16342 (log.clj:91)
    stem.i.log/eval16342 (log.clj:91)
    clojure.lang.Compiler.eval (Compiler.java:7177)
    clojure.lang.Compiler.eval (Compiler.java:7132)
    clojure.core/eval (core.clj:3214)
    clojure.core.server/prepl/fn--8941 (server.clj:232)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;in spec-alpha2 with the latest commit from master&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{:git/url &quot;https://github.com/clojure/spec-alpha2.git&quot; :sha &quot;d514b06b25c41a676b95afcc9bfac8ca34c5741e&quot;}
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9730/problem-with-s-nest-and-s-describe-in-spec-alpha2</guid>
<pubDate>Tue, 20 Oct 2020 16:55:08 +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>Is it possible to attach spec to the local variable (via meta maybe?)</title>
<link>https://ask.clojure.org/index.php/9548/is-it-possible-attach-spec-the-local-variable-via-meta-maybe</link>
<description>&lt;p&gt;Sometimes it may help a lot if you can quickly check the shape of the local  data, after series of transformations for example.&lt;/p&gt;
&lt;p&gt;Is it possible? &lt;/p&gt;
&lt;p&gt;And is it possible to spec (some) steps of thread-first/thread-last for instance?&lt;/p&gt;
&lt;p&gt;As a random idea, something like this?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(-&amp;gt;&amp;gt; xs
     ^:xs (map ...) ;; &amp;lt;- attaching spec for output via tag
      (filter ...)
      (remove ...)
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9548/is-it-possible-attach-spec-the-local-variable-via-meta-maybe</guid>
<pubDate>Sat, 29 Aug 2020 07:03:24 +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>
</channel>
</rss>