Spec for an enum param

19 views
Skip to first unread message

Sandeep Chayapathi

unread,
Nov 23, 2019, 8:04:14 PM11/23/19
to Luminus
(cross posting this from r/clojure)

I'm a clojure beginner. I recently began a API project (based on Luminus) to help me learn Clojure. 


I love the fact that there is support for Swagger out of the box. I have a question regarding how to represent (in swagger) a POST request body which takes in a limited set of values for one property.


Eg: A user can send to the API end point these keys

  • name - a String value , required.
  • admin - a boolean value, defaults to false.
  • group - a string value which can either be one of these values Student or Teacher


So far I have this code and struggling with how to define the group property, would appreciate any help here.


(:require [schema.core :as s])

(s/defschema Member {
               :name string?,
               :admin boolean?
               :group string?})

["/add-member"
       {:post {:summary  "add a new member"
        :parameters {:body Member}
         :tags ["term"]
        :response   {200 {:body map?}}
        :handler    (fn [{{{:keys [name admin group]} :body} :parameters}]
                      {:status 200
                       :body   {:success  1 ;; code talks to db here
                                  }}]

Sandeep Chayapathi

unread,
Nov 23, 2019, 9:50:54 PM11/23/19
to Luminus
I kinda solved it!

I used clojure.spec.alpha and defined a keyword for each property like so

(require 
   [clojure.spec.alpha :as sa]
   [schema.core :as s])

(sa/def ::group #{"Teacher" "Student" })
(sa/def ::name string?)
(sa/def ::admin boolean?)


and then I updated the Member to be so

(s/defschema Member (sa/keys :req-un [::group ::name ::admin]))

This solved my problem with swagger! 
Reply all
Reply to author
Forward
0 new messages