Fork me on GitHub
#clojure-spec
<
2017-05-06
>
reefersleep19:05:52

When using spec/explain on a spec which contains spec/coll-of in cljs and passing in an invalid datastructure, I don't seem to be getting any information about the offending element of the coll, only that something is wrong within the coll. Example:

(spec/explain (spec/coll-of string? []) ["hey" "yo" 9])
val: ["hey" "yo" 9] fails predicate: (coll-checker string?)
For contrast, here is the ouput for a Schema validation similar to the spec explain:
(schema/validate [schema/Str] ["hej" "yo" 9])
#error {:message "Value does not match schema: [nil nil (not (cljs$core$string? 9))]", :data {:type :schema.core/error, :schema [<#C1923ED97|schema>.core.Predicate{:p? #object[cljs$core$string_QMARK_ "function cljs$core$string_QMARK_(x){
Can I do something to make Spec give me more detailed output, similar to that of Schema? In my actual use case, the spec/coll-of contains more complex maps with nested specs, and if one thing goes wrong somewhere in the coll-of, I'm left in the dark, fumbling for the specifics.

mobileink20:05:43

reefersleep: it tells you precisely that your val contains a non-string.

mobileink20:05:37

reefersleep: do you have an example of being left in the dark?

reefersleep14:05:35

@mobileink: here's a more elaborate example of what I mean in regards to Schema providing more information than Spec, leaving me desiring more from Spec. Spec:

(spec/def ::id int?)

(spec/def ::name string?)

(spec/def ::person (s/keys :req-un [::id
                                    ::name]))

(spec/explain (spec/coll-of ::person []) [{:id 1 :name "john"} {:id 2 :name :heather}])
val: [{:id 1, :name "john"} {:id 2, :name :heather}] fails predicate: (coll-checker :sleepydo.db/person)

;; No information about _which_ element of the coll failed to validate, nor details about how it failed to validate
Schema:
(def Id schema/Int)

(def Name schema/Str)

(def Person {:id Id
             :name Name})

(schema/validate [Person] [{:id 1 :name "john"} {:id 2 :name :heather}])
#error {:message "Value does not match schema: [nil {:name (not (cljs$core$string? :heather))}]", :data {:type :schema.core/error, :schema [{:id <#C1923ED97|schema>.core.Predicate{:p? #object[cljs$core$integer_QMARK_ "function cljs$core$integer_QMARK_(n){
...
;; Information about which element of the coll failed to validate, as well as the offending value and its predicate function.

xiongtx20:05:13

Does s/with-gen take a thunk that returns a generator for laziness? Is that way it doesn’t just take a generator directly?

reefersleep14:05:35
replied to a thread:When using `spec/explain` on a spec which contains `spec/coll-of` in cljs and passing in an invalid datastructure, I don't seem to be getting any information about the offending element of the coll, only that _something_ is wrong within the coll. Example: (spec/explain (spec/coll-of string? []) ["hey" "yo" 9]) val: ["hey" "yo" 9] fails predicate: (coll-checker string?) For contrast, here is the ouput for a Schema validation similar to the spec explain: (schema/validate [schema/Str] ["hej" "yo" 9]) #error {:message "Value does not match schema: [nil nil (not (cljs$core$string? 9))]", :data {:type :schema.core/error, :schema [<#C1923ED97|schema>.core.Predicate{:p? #object[cljs$core$string_QMARK_ "function cljs$core$string_QMARK_(x){ Can I do something to make Spec give me more detailed output, similar to that of Schema? In my actual use case, the `spec/coll-of` contains more complex maps with nested specs, and if one thing goes wrong somewhere in the `coll-of`, I'm left in the dark, fumbling for the specifics.

@mobileink: here's a more elaborate example of what I mean in regards to Schema providing more information than Spec, leaving me desiring more from Spec. Spec:

(spec/def ::id int?)

(spec/def ::name string?)

(spec/def ::person (s/keys :req-un [::id
                                    ::name]))

(spec/explain (spec/coll-of ::person []) [{:id 1 :name "john"} {:id 2 :name :heather}])
val: [{:id 1, :name "john"} {:id 2, :name :heather}] fails predicate: (coll-checker :sleepydo.db/person)

;; No information about _which_ element of the coll failed to validate, nor details about how it failed to validate
Schema:
(def Id schema/Int)

(def Name schema/Str)

(def Person {:id Id
             :name Name})

(schema/validate [Person] [{:id 1 :name "john"} {:id 2 :name :heather}])
#error {:message "Value does not match schema: [nil {:name (not (cljs$core$string? :heather))}]", :data {:type :schema.core/error, :schema [{:id <#C1923ED97|schema>.core.Predicate{:p? #object[cljs$core$integer_QMARK_ "function cljs$core$integer_QMARK_(n){
...
;; Information about which element of the coll failed to validate, as well as the offending value and its predicate function.