metosin / schema-tools

Clojure(Script) tools for Plumatic Schema
http://metosin.github.io/schema-tools/
Eclipse Public License 2.0
107 stars 16 forks source link

declarative schemas #11

Closed ikitommi closed 6 years ago

ikitommi commented 9 years ago

For web apps, Schemas are used to both validate & document the apis. Would be nice if one could create the schemas more declaratively. There could be a schema function in schema-tools that would read declarations (using multimethod dispatch) and create the needed schema structures for those.

Something like:

(require '[schma.core :as s])
(require '[schema-tools.core :as st])

(s/defschema Model 
  "Model description"
  {:size (st/schema s/Int {:min 10, :max 20, :description "number 10-20"})
   :gender (st/schema (s/enum :male :female) {:description "gender"})})
mangolas commented 8 years ago

How about describing schema in hiccup style

(s/defschema Model
  (st/schema 
    [:type/map {:description "Model description"} 
      [:size :type/int {:optional true 
                              :default 0 :min 10 :max 20
                              :description "number 10-20"}]
      [:gender :type/enum {:values [:male :female]
                                        :default :male
                                        :description "gender"}]]))
Deraen commented 8 years ago

Could be alternative, but it would need to be converted to "real" Schema representation by some function. And we still need the Schemas for min/max etc.

mangolas commented 8 years ago

Sure, I meant it as a way to describe schema by end user, like me, who just want's to model api ins and outs. Internally it can be the "real" Schema or whatever.

ikitommi commented 6 years ago

32