Clojure Spec : map? failing because spec thinks my data-structure is a vector?

258 Views Asked by At

I'm trying to validate a large clojure data-structure in Spec.

I'm getting a failure on this part :

{:pageName "HelloWorld" :pageType ::workPage :cells ["Hello World" "How are you?"]}

using this :

(s/def ::WorkPage (s/keys :req-un [::pageName ::pageType ::cells]))

Which fails with this message (from expound)

{:pageName "HelloWorld", :pageType :assemblage.db/workPage, :cells ["Hello World" "How are you?"]} db.js:52:1
false db.js:54:1
-- Spec failed --------------------

  [:pageName "HelloWorld"]
  ^^^^^^^^^^^^^^^^^^^^^^^^

should satisfy

  map?

If I run the above spec by itself in the repl it works.

But in context (in a clojurescript file) it looks almost as if the data-structure is being converted into a vector somewhere before it gets tested.

Has anyone seen anything like this before?

Could it be the result of doing this in ClojureScript? Is this some weird javascript coercion leaking up and biting me?

Or is it likely to be something completely different?

1

There are 1 best solutions below

0
On

I believe that Spec definition of this map:

{:pageName "HelloWorld" :pageType ::workPage :cells ["Hello World" "How are you?"]}

should look something like:

(s/def ::my-map (s/keys :req-un [::pageName ::pageType ::cells]))

can be used like:

(s/valid? ::my-map {:pageName "HelloWorld" :pageType ::workPage :cells ["Hello World" "How are you?"]})

In your post - you have got the key of the definition inside the map itself and it causes problems probably.

Spec fails (at least on JS side) often with:

should satisfy map?

like when you register more spec keys under the same name etc.