Clojure spec - naming entity keywords

412 Views Asked by At

Is it considered bad practice to use namespace-qualified keywords with nonexistent namespaces, for defining specs? I'd like to have entity maps defined in common domain namespace... so to avoid loosing data when merging specs, I've used convention :entity/attribute instead of ::entity-attribute for attributes and standard ::entity for entities. It aligns nicer to database tables and columns. Each entity in a separate namespace reminds me of Java classes, doesn't sound like a good idea.

(s/def :country/id   ::nilable-nat-int)
(s/def :country/name ::non-empty-string)

(s/def ::country
  (s/keys :req [:country/id
                :country/name]))

;; ----------------------------------------

(s/def :location/id      ::nilable-nat-int)
(s/def :location/name    ::non-empty-string)
(s/def :location/zipcode ::nilable-non-empty-string)

(s/def ::location
  (s/merge
   (s/keys :req [:location/id
                 :location/name
                 :location/zipcode])
   (s/or :country ::country
         :country-id
         (s/keys :req [:country/id]))))
1

There are 1 best solutions below

0
On BEST ANSWER

As @glts commented, here is the right answer: mailing list.

I've decided to make keywords more specific, added this to the domain namespace:

(doseq [ns ["entity-1" ,,, "entity-n"]]
  (->> (str "project.domain." ns)
       (symbol)
       (create-ns)
       (alias (symbol ns))))

And then ::entity-n/attribute evaluates to :project.domain.entity-n/attribute.

Only one additional : is needed for the attributes from the question-example:

(s/def ::location/id ::nilable-nat-int)