1

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?

1 Answer 1

1

You could try:

(s/def ::gig-id
   (s/and keyword?
          (fn [x] (->> x name (re-matches #"gig-\d+")))))
Sign up to request clarification or add additional context in comments.

3 Comments

Would the full spec then be? (s/def ::id string?) (s/def ::title string?) (s/def ::artist string?) (s/def ::desc string?) (s/def ::img string?) (s/def ::price int?) (s/def ::sold-out boolean?) (s/def ::gig (s/keys :req-un [::id ::title ::artist ::desc ::img ::price ::sold-out])) (s/def ::gig-id (s/and keyword? (fn [x] (->> x name (re-matches #"gig-\d+"))))) (s/def ::gigs (s/and (s/map-of ::gig-id ::gig))) (s/def ::db (s/keys :req-un [::gigs])) @akond
You don't need ::db, you can use ::gigs, but yes, something like that.
And I feel like you've meant (s/def ::id ::gig-id)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.