jwesonga
jwesonga

Reputation: 4383

Using clojure.spec for a map

I have the following map:

(def gigs {:gig-01 {:id :gig-01
                    :title "Macaron"
                    :artist "Baher Khairy"
                    :desc "Sweet meringue-based rhythms with smooth and sweet injections of soul"
                    :img "https://res.cloudinary.com/schae/image/upload/f_auto,q_auto/v1519552695/giggin/baher-khairy-97645.jpg"
                    :price 1000
                    :sold-out false}
           :gig-02 {:id :gig-02
                    :title "Stairs"
                    :artist "Brentr De Ranter"
                    :desc "Stairs to the highets peaks of music."
                    :img "https://res.cloudinary.com/schae/image/upload/f_auto,q_auto/v1519552695/giggin/brent-de-ranter-426248.jpg"
                    :price 2000
                    :sold-out false}})

I'd like to create a spec for it, but I'm not sure how to define the key e.g. ":gig-01" any pointers?

Upvotes: 1

Views: 74

Answers (1)

akond
akond

Reputation: 16035

You could try:

(s/def ::gig-id
   (s/and keyword?
          (fn [x] (->> x name (re-matches #"gig-\d+")))))

Upvotes: 1

Related Questions