There's this line in spec-alpha2's wiki page on "Schema and select" (emphasis mine):
> General form: (s/select schema selection)
> β’ schema (required) - can be a registered schema name, schema form (like s/union), or literal schema
But I can't get the "schema form" to work.
(in-ns 'user)
=> #object[clojure.lang.Namespace 0x54029c23 "user"]
(s/def ::a int?)
=> :user/a
(s/def ::b keyword?)
=> :user/b
;; Literal schema: OK
(gen/sample
(s/gen
(s/select
[::a ::b]
[*]))
2)
=>
(#:user{:a -1, :b :B/d}
#:user{:a -1, :b :Q/r})
;; Schema name: OK
(s/register ::ab (s/schema [::a ::b]))
=> :user/ab
(gen/sample
(s/gen
(s/select
::ab
[*]))
2)
=>
(#:user{:a -1, :b :B/_}
{})
;; Union name: OK
(s/register ::ba (s/union [::a] [::b]))
=> :user/ba
(gen/sample
(s/gen
(s/select
::ba
[*]))
2)
=>
(#:user{:b :./l?}
#:user{:a -3})
;; s/schema form: Err
(gen/sample
(s/gen
(s/select
(s/schema [::a ::b])
[*])))
Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1814$fn$G (protocols.clj:20).
No implementation of method: :keyspecs* of protocol: #'clojure.alpha.spec.protocols/Schema found for class: clojure.lang.PersistentList
;; s/union form: Err
(gen/sample
(s/gen
(s/select
(s/union [::a] [::b])
[*])))
Execution error (IllegalArgumentException) at clojure.alpha.spec.protocols/eval1814$fn$G (protocols.clj:20).
No implementation of method: :keyspecs* of protocol: #'clojure.alpha.spec.protocols/Schema found for class: clojure.lang.PersistentList
Looks like I've messed something up wrt "symbolic" vs "object"?No, just bugs I think
Spec 2 is not ready for use
Yeah, sorry to make you feel like you have to repeat it again and again. I'm just toying around π
the whole schema/select impl is due for a rewrite, just don't have time to work on it right now
Hi all, I'm playing around with the lastest spec alpha 2. I'm not sure if I'm doing something wrong but:
(spec/def ::tag (spec/and string? (spec/conformer clojure.edn/read-string str)))
(spec/def ::value string?)
(spec/def ::kv (spec/cat :tag ::tag :value ::value))
(spec/def ::fix-message (spec/and string?
(spec/conformer (fn [x] (map #(clojure.string/split % #"=") (clojure.string/split x #"\01"))))
(spec/+ ::kv)))
Should work with a FIX input string (a bunch of k=v pairs joined by ASCII code 01) but currently fails with
["8" "FIX.4.4"] - failed: string? in: [0] at: [:tag] spec: :cme-fix.spec/tag
However, adding an additional predicate into ::kv
makes it work:
(spec/def ::tag (spec/and string? (spec/conformer clojure.edn/read-string str)))
(spec/def ::value string?)
(spec/def ::kv (spec/and (constantly true) (spec/cat :tag ::tag :value ::value)))
(spec/def ::fix-message (spec/and string?
(spec/conformer (fn [x] (map #(clojure.string/split % #"=") (clojure.string/split x #"\01"))))
(spec/+ ::kv)))
Am I doing something obviously wrong?I see, thank you π
Hey guys π , I'm tring to get started on using clojure.spec and I hit an unexpected/frustrating roadblock...
user=> (s/exercise false?)
Execution error (FileNotFoundException) at user/eval1428 (form-init5827540222428447763.clj:1).
Could not locate clojure/test/check/generators__init.class, clojure/test/check/generators.clj or clojure/test/check/generators.cljc on classpath.
This error is thrown for any spec for which I call s/exercise
Any help with be appreciated
You need to add test.check
library as a dependency to your classpath
cool... I'll go ahead and do that
spec uses it for generation
this is covered in the spec guide btw https://clojure.org/guides/spec
@jaihindhreddy That worked π :thumbsup:
oh... cool.. I didn't actually read that.. I'm following some tutorials... will go through that..
Is there a spec collection macro that works with eduction?
e.g., something that returns true here:
(s/valid? (s/coll-of int?) (eduction (map inc) (range 10)))
=> false
I donβt know if there is a spec that you can use but you could alternatively call seq
on your eduction to turn it into a LazySeq which would then pass the spec
@kenny I'm not sure, but I don't think eduction returns a collection, it returns a reducible :). That doesn't help but it does indicate that coll-of probably isn't the right tool.
Right. I'm curious what folks use to validate against it. Hmm, I could do that @shewitt!
eductions are suspended computations so there is no data to validate without forcing it
This use case is for in a test so I think @shewitt's idea solves it. In general, it probably wouldn't make sense to call valid? on an eduction for that reason, right? Even if checked with s/every, it'd still need to recompute *coll-check-limit*
when you finally use the eduction elsewhere.
Right