How to spec a map in any keys?

Previous conversations on Twitter:

(map-spec :req {"zhCN" string? "enUS" string?} :closed? true)

I think this is cool but turned out it’s not a complete solution. Anyone tried that before?

Have you looked at what is coming in Spec 2? https://github.com/clojure/spec-alpha2/wiki/Schema-and-select

2 Likes

Not yet. Looks nice, https://github.com/clojure/spec-alpha2/wiki/Schema-and-select#unqualified-keys

(s/def ::order 
  (s/schema {:purchaser string? 
             :due-date inst?
             :line-items (s/coll-of (s/schema {:item-id pos-int? 
                                               :quantity nat-int?})
                           :kind vector?
                           :min-count 1
                           :gen-max 3)}))

(gen/sample (s/gen ::order) 5)
;; ({:due-date #inst "1970-01-01T00:00:00.000-00:00"} 
;;  {} 
;;  {:purchaser "", :due-date #inst "1969-12-31T23:59:59.999-00:00", 
;;   :line-items [{:item-id 2, :quantity 0}]} 
;;  {:purchaser "0"}
;;  {:due-date #inst "1969-12-31T23:59:59.999-00:00", 
;;   :line-items [{:quantity 1} {} {:item-id 2, :quantity 3}]})

is it available for use now?

There are snapshot builds on Sonatype. If you’re using clj/deps.edn you can add this to your deps.edn file to try it out:

:mvn/repos {"sonatype" {:url "https://oss.sonatype.org/content/repositories/snapshots/"}}
:deps {org.clojure/spec-alpha2 {:mvn/version "0.2.177-SNAPSHOT"}}

or you could use it directly from GitHub with a :git/url dependency.

If you’re using lein/boot, you can depend on that SNAPSHOT version above but you’ll need to add the Sonatype snapshots repo to your config (I don’t remember how to do that – it’s been a while since I last used either of those tools).

It’s definitely not “production-ready” yet, but it’s solid enough to play with. We have a branch at work of our 85K+ line code base that runs on Spec 2 (so we’re ready to switch over once the Clojure core team say it is solid enough to start using).

2 Likes

shadow-cljs actually. So “Sonatype” is another maven server so I should config the repo in shadow-cljs?

I don’t know anything about shadow-cljs but I thought it still used a project.clj file under the hood? In which case, yes, that’s where you’d configure the additional repo.

Got me wondering why it’s not in the official repository? Isn’t spec-alpha2 an official one?

That is an official repository. But the default, with all Clojure tooling, is not to include the snapshots repo, only the releases repo. You have to explicitly opt-in if you want to depend on a snapshot release.

Most of my projects do opt-in so that I can run tests against Clojure’s master branch (which builds every night to the Sonatype snapshots repo).

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.