Skip to content

No generic collection type? #384

@weissjeffm

Description

@weissjeffm

How can I define a collection spec that works on all collections (not just vectors or sets)?

To illustrate the issue I'm having:

cljs.user> (s/validate [s/Int] #{1 2})
#error {:message "Value does not match schema: (not (sequential? #{1 2}))", :data {:type :schema.core/error, :schema [#schema.core.Predicate{:p? #object[cljs$core$integer_QMARK_ "function cljs$core$integer_QMARK_(n){
return (typeof n === 'number') && (!(isNaN(n))) && (!((n === Infinity))) && ((parseFloat(n) === parseInt(n,(10))));
}"], :pred-name cljs$core$integer?}], :value #{1 2}, :error (not (sequential? #{1 2}))}}
	
cljs.user> (s/validate #{s/Int} [1 2])
#error {:message "Value does not match schema: (not (set? [1 2]))", :data {:type :schema.core/error, :schema #{#schema.core.Predicate{:p? #object[cljs$core$integer_QMARK_ "function cljs$core$integer_QMARK_(n){
return (typeof n === 'number') && (!(isNaN(n))) && (!((n === Infinity))) && ((parseFloat(n) === parseInt(n,(10))));
}"], :pred-name cljs$core$integer?}}, :value [1 2], :error (not (set? [1 2]))}}

It seems like one of these should work, or if they don't, there should be a third option where the schema is "I don't care what type of collection it is, as long as each item in it is an Int".

I can get what I want, sort of, with a little function I define myself, but seems like there ought to be a better way:

cljs.user> (defn coll [schema] (s/cond-pre [schema] #{schema}))
#'cljs.user/coll
cljs.user> (s/validate (coll s/Int) [1 2])
[1 2]
cljs.user> (s/validate (coll s/Int) #{1 2})
#{1 2}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions