Spec.alpha bug with sets and regular expression operators?

Hi,
I’m trying to validate the contents of a set for output options.
It may contain :csv and/or :xlsx.
When I try

> (require '[clojure.spec.alpha :as spec])
> (spec/explain (spec/+ #{:csv :xlsx}) #{:any})
#{:any} - failed: (or (nil? %) (sequential? %))

it fails since a set (#{:any} here) in not sequential.

>(spec/explain (spec/+ #{:csv :xlsx}) (seq #{:any}))
:any - failed: #{:csv :xlsx} in: [0]

works since we made it sequential.

Is this expected behaviour, or a bug in spec?

Since I want to combine this spec with others,
the fix above does not work any more :frowning:

(spec/and (spec/coll-of keyword? :kind set?)
                 (spec/+ #{:csv :xlsx}))

Are there better ways to validate set elements?
Thanks
Lothar

P.S.: spec/* and spec/? show the same “problem”

I think what you want is: (spec/coll-of #{:csv :xslx} :kind set?)

user=> (spec/explain (spec/coll-of #{:csv :xslx} :kind set?) #{:any})
:any - failed: #{:csv :xslx} in: [0]
nil
user=> (spec/explain (spec/coll-of #{:csv :xslx} :kind set?) #{:csv})
Success!
nil
user=> (spec/explain (spec/coll-of #{:csv :xslx} :kind set?) #{:csv :xslx})
Success!
nil
user=> 

The regex operators are strictly for sequential things so what you are seeing is not a bug, it is by design.

Thanks a lot – exactly what I wanted.
(Note to self: a set is a predicate…)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.