Say I have the following specs:
(s/def :person/age number?)
(s/def :person/name string?)
(s/def ::person (s/keys :req [:person/name :person/age]))
Then I fetch an entity from Datomic:
(def person-entity (d/entity (d/db conn) [:person/name "Mr entity"]))
If I try to check conformance with the spec it fails because entities are not maps:
(s/explain ::person person-entity)
val: #:db{:id 17592186069950} fails spec: :some-ns/person predicate: map?
My app has functions that take entities as arguments and would like to avoid having to reify entities to maps everywhere just to get spec instrumentation to work in development.
How should I go about validating entities through spec?
Although it is true that your namespaces are a bit off (you should be using :person/name instead of ::name), there is also a restriction in spec that could be lifted. Currently s/keys requires that all input collections conform to the clojure.core/keys
predicate. Datomic entities are not maps, and so do not pass that check (and hence the error about the map?
predicate).
I've filed a bug report here: http://dev.clojure.org/jira/browse/CLJ-2041 Feel free to upvote/follow the ticket if you wish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With